1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31:
32: class Mage_Paypal_Model_Method_Agreement extends Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract
33: implements Mage_Payment_Model_Billing_Agreement_MethodInterface
34: {
35: 36: 37: 38: 39:
40: protected $_code = Mage_Paypal_Model_Config::METHOD_BILLING_AGREEMENT;
41:
42: 43: 44: 45:
46: protected $_canAuthorize = true;
47: protected $_canCapture = true;
48: protected $_canCapturePartial = true;
49: protected $_canRefund = true;
50: protected $_canRefundInvoicePartial = true;
51: protected $_canVoid = true;
52: protected $_canUseCheckout = false;
53: protected $_canUseInternal = false;
54: protected $_canFetchTransactionInfo = true;
55: protected $_canReviewPayment = true;
56:
57: 58: 59: 60: 61:
62: protected $_pro = null;
63:
64: 65: 66: 67: 68:
69: public function __construct($params = array())
70: {
71: $proInstance = array_shift($params);
72: if ($proInstance && ($proInstance instanceof Mage_Paypal_Model_Pro)) {
73: $this->_pro = $proInstance;
74: } else {
75: $this->_pro = Mage::getModel('paypal/pro');
76: }
77: $this->_pro->setMethod($this->_code);
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function setStore($store)
87: {
88: $this->setData('store', $store);
89: if (null === $store) {
90: $store = Mage::app()->getStore()->getId();
91: }
92: $this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function initBillingAgreementToken(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
103: {
104: $api = $this->_pro->getApi()
105: ->setReturnUrl($agreement->getReturnUrl())
106: ->setCancelUrl($agreement->getCancelUrl())
107: ->setBillingType($this->_pro->getApi()->getBillingAgreementType());
108:
109: $api->callSetCustomerBillingAgreement();
110: $agreement->setRedirectUrl(
111: $this->_pro->getConfig()->getStartBillingAgreementUrl($api->getToken())
112: );
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121:
122: public function getBillingAgreementTokenInfo(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
123: {
124: $api = $this->_pro->getApi()
125: ->setToken($agreement->getToken());
126: $api->callGetBillingAgreementCustomerDetails();
127: $responseData = array(
128: 'token' => $api->getData('token'),
129: 'email' => $api->getData('email'),
130: 'payer_id' => $api->getData('payer_id'),
131: 'payer_status' => $api->getData('payer_status')
132: );
133: $agreement->addData($responseData);
134: return $responseData;
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function placeBillingAgreement(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
144: {
145: $api = $this->_pro->getApi()
146: ->setToken($agreement->getToken());
147: $api->callCreateBillingAgreement();
148: $agreement->setBillingAgreementId($api->getData('billing_agreement_id'));
149: return $this;
150: }
151:
152: 153: 154: 155: 156: 157:
158: public function updateBillingAgreementStatus(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
159: {
160: $targetStatus = $agreement->getStatus();
161: $api = $this->_pro->getApi()
162: ->setReferenceId($agreement->getReferenceId())
163: ->setBillingAgreementStatus($targetStatus);
164: try {
165: $api->callUpdateBillingAgreement();
166: } catch (Mage_Core_Exception $e) {
167:
168: if (!(Mage_Sales_Model_Billing_Agreement::STATUS_CANCELED == $targetStatus
169: && $api->getIsBillingAgreementAlreadyCancelled())) {
170: throw $e;
171: }
172: }
173: return $this;
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public function authorize(Varien_Object $payment, $amount)
184: {
185: return $this->_placeOrder($payment, $amount);
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function void(Varien_Object $payment)
195: {
196: $this->_pro->void($payment);
197: return $this;
198: }
199:
200: 201: 202: 203: 204: 205: 206:
207: public function capture(Varien_Object $payment, $amount)
208: {
209: if (false === $this->_pro->capture($payment, $amount)) {
210: $this->_placeOrder($payment, $amount);
211: }
212: return $this;
213: }
214:
215: 216: 217: 218: 219: 220: 221:
222: public function refund(Varien_Object $payment, $amount)
223: {
224: $this->_pro->refund($payment, $amount);
225: return $this;
226: }
227:
228: 229: 230: 231: 232: 233:
234: public function cancel(Varien_Object $payment)
235: {
236: $this->_pro->cancel($payment);
237: return $this;
238: }
239:
240: 241: 242: 243: 244: 245:
246: public function canReviewPayment(Mage_Payment_Model_Info $payment)
247: {
248: return parent::canReviewPayment($payment) && $this->_pro->canReviewPayment($payment);
249: }
250:
251: 252: 253: 254: 255: 256:
257: public function acceptPayment(Mage_Payment_Model_Info $payment)
258: {
259: parent::acceptPayment($payment);
260: return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_ACCEPT);
261: }
262:
263: 264: 265: 266: 267: 268:
269: public function denyPayment(Mage_Payment_Model_Info $payment)
270: {
271: parent::denyPayment($payment);
272: return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_DENY);
273: }
274:
275: 276: 277: 278: 279: 280: 281:
282: public function fetchTransactionInfo(Mage_Payment_Model_Info $payment, $transactionId)
283: {
284: return $this->_pro->fetchTransactionInfo($payment, $transactionId);
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
295: {
296: $order = $payment->getOrder();
297: $billingAgreement = Mage::getModel('sales/billing_agreement')->load(
298: $payment->getAdditionalInformation(
299: Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract::TRANSPORT_BILLING_AGREEMENT_ID
300: )
301: );
302:
303: $api = $this->_pro->getApi()
304: ->setReferenceId($billingAgreement->getReferenceId())
305: ->setPaymentAction($this->_pro->getConfig()->paymentAction)
306: ->setAmount($amount)
307: ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
308: ->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
309: ->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled)
310: ->setInvNum($order->getIncrementId())
311: ;
312:
313:
314: $api->callDoReferenceTransaction();
315: $this->_pro->importPaymentInfo($api, $payment);
316: $api->callGetTransactionDetails();
317: $this->_pro->importPaymentInfo($api, $payment);
318:
319: $payment->setTransactionId($api->getTransactionId())
320: ->setIsTransactionClosed(0);
321:
322: if ($api->getBillingAgreementId()) {
323: $order->addRelatedObject($billingAgreement);
324: $billingAgreement->setIsObjectChanged(true);
325: $billingAgreement->addOrderRelation($order);
326: }
327:
328: return $this;
329: }
330:
331:
332: protected function _isAvailable($quote)
333: {
334: return $this->_pro->getConfig()->isMethodAvailable($this->_code);
335: }
336:
337: 338: 339: 340: 341: 342:
343: public function getConfigPaymentAction()
344: {
345: return $this->_pro->getConfig()->getPaymentAction();
346: }
347:
348: }
349: