PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Paypal/Model/Method/Agreement.php

https://github.com/itfcfan/magento-mirror
PHP | 345 lines | 165 code | 29 blank | 151 comment | 10 complexity | 094064c60dc42445f75ffeeb2c032b2f MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Paypal
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Paypal Billing Agreement method
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Paypal_Model_Method_Agreement extends Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract
  32. implements Mage_Payment_Model_Billing_Agreement_MethodInterface
  33. {
  34. /**
  35. * Method code
  36. *
  37. * @var string
  38. */
  39. protected $_code = Mage_Paypal_Model_Config::METHOD_BILLING_AGREEMENT;
  40. /**
  41. * Method instance settings
  42. *
  43. */
  44. protected $_canAuthorize = true;
  45. protected $_canCapture = true;
  46. protected $_canCapturePartial = true;
  47. protected $_canRefund = true;
  48. protected $_canRefundInvoicePartial = true;
  49. protected $_canVoid = true;
  50. protected $_canUseCheckout = false;
  51. protected $_canUseInternal = false;
  52. protected $_canFetchTransactionInfo = true;
  53. protected $_canReviewPayment = true;
  54. /**
  55. * Website Payments Pro instance
  56. *
  57. * @var Mage_Paypal_Model_Pro
  58. */
  59. protected $_pro = null;
  60. /**
  61. * Initialize Mage_Paypal_Model_Pro model
  62. *
  63. * @param array $params
  64. */
  65. public function __construct($params = array())
  66. {
  67. $proInstance = array_shift($params);
  68. if ($proInstance && ($proInstance instanceof Mage_Paypal_Model_Pro)) {
  69. $this->_pro = $proInstance;
  70. } else {
  71. $this->_pro = Mage::getModel('paypal/pro');
  72. }
  73. $this->_pro->setMethod($this->_code);
  74. }
  75. /**
  76. * Store setter
  77. * Also updates store ID in config object
  78. *
  79. * @param Mage_Core_Model_Store|int $store
  80. */
  81. public function setStore($store)
  82. {
  83. $this->setData('store', $store);
  84. if (null === $store) {
  85. $store = Mage::app()->getStore()->getId();
  86. }
  87. $this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
  88. return $this;
  89. }
  90. /**
  91. * Init billing agreement
  92. *
  93. * @param Mage_Payment_Model_Billing_AgreementAbstract $agreement
  94. * @return Mage_Paypal_Model_Method_Agreement
  95. */
  96. public function initBillingAgreementToken(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
  97. {
  98. $api = $this->_pro->getApi()
  99. ->setReturnUrl($agreement->getReturnUrl())
  100. ->setCancelUrl($agreement->getCancelUrl())
  101. ->setBillingType($this->_pro->getApi()->getBillingAgreementType());
  102. $api->callSetCustomerBillingAgreement();
  103. $agreement->setRedirectUrl(
  104. $this->_pro->getConfig()->getStartBillingAgreementUrl($api->getToken())
  105. );
  106. return $this;
  107. }
  108. /**
  109. * Retrieve billing agreement customer details by token
  110. *
  111. * @param Mage_Payment_Model_Billing_AgreementAbstract $agreement
  112. * @return array
  113. */
  114. public function getBillingAgreementTokenInfo(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
  115. {
  116. $api = $this->_pro->getApi()
  117. ->setToken($agreement->getToken());
  118. $api->callGetBillingAgreementCustomerDetails();
  119. $responseData = array(
  120. 'token' => $api->getData('token'),
  121. 'email' => $api->getData('email'),
  122. 'payer_id' => $api->getData('payer_id'),
  123. 'payer_status' => $api->getData('payer_status')
  124. );
  125. $agreement->addData($responseData);
  126. return $responseData;
  127. }
  128. /**
  129. * Create billing agreement by token specified in request
  130. *
  131. * @param Mage_Payment_Model_Billing_AgreementAbstract $agreement
  132. * @return Mage_Paypal_Model_Method_Agreement
  133. */
  134. public function placeBillingAgreement(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
  135. {
  136. $api = $this->_pro->getApi()
  137. ->setToken($agreement->getToken());
  138. $api->callCreateBillingAgreement();
  139. $agreement->setBillingAgreementId($api->getData('billing_agreement_id'));
  140. return $this;
  141. }
  142. /**
  143. * Update billing agreement status
  144. *
  145. * @param Mage_Payment_Model_Billing_AgreementAbstract $agreement
  146. * @return Mage_Paypal_Model_Method_Agreement
  147. */
  148. public function updateBillingAgreementStatus(Mage_Payment_Model_Billing_AgreementAbstract $agreement)
  149. {
  150. $targetStatus = $agreement->getStatus();
  151. $api = $this->_pro->getApi()
  152. ->setReferenceId($agreement->getReferenceId())
  153. ->setBillingAgreementStatus($targetStatus);
  154. try {
  155. $api->callUpdateBillingAgreement();
  156. } catch (Mage_Core_Exception $e) {
  157. // when BA was already canceled, just pretend that the operation succeeded
  158. if (!(Mage_Sales_Model_Billing_Agreement::STATUS_CANCELED == $targetStatus
  159. && $api->getIsBillingAgreementAlreadyCancelled())) {
  160. throw $e;
  161. }
  162. }
  163. return $this;
  164. }
  165. /**
  166. * Authorize payment
  167. *
  168. * @param Varien_Object $payment
  169. * @param float $amount
  170. * @return Mage_Paypal_Model_Method_Agreement
  171. */
  172. public function authorize(Varien_Object $payment, $amount)
  173. {
  174. return $this->_placeOrder($payment, $amount);
  175. }
  176. /**
  177. * Void payment
  178. *
  179. * @param Mage_Sales_Model_Order_Payment $payment
  180. * @return Mage_Paypal_Model_Method_Agreement
  181. */
  182. public function void(Varien_Object $payment)
  183. {
  184. $this->_pro->void($payment);
  185. return $this;
  186. }
  187. /**
  188. * Capture payment
  189. *
  190. * @param Mage_Sales_Model_Order_Payment $payment
  191. * @param float $amount
  192. * @return Mage_Paypal_Model_Method_Agreement
  193. */
  194. public function capture(Varien_Object $payment, $amount)
  195. {
  196. if (false === $this->_pro->capture($payment, $amount)) {
  197. $this->_placeOrder($payment, $amount);
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Refund capture
  203. *
  204. * @param Mage_Sales_Model_Order_Payment $payment
  205. * @param float $amount
  206. * @return Mage_Paypal_Model_Method_Agreement
  207. */
  208. public function refund(Varien_Object $payment, $amount)
  209. {
  210. $this->_pro->refund($payment, $amount);
  211. return $this;
  212. }
  213. /**
  214. * Cancel payment
  215. *
  216. * @param Mage_Sales_Model_Order_Payment $payment
  217. * @return Mage_Paypal_Model_Method_Agreement
  218. */
  219. public function cancel(Varien_Object $payment)
  220. {
  221. $this->_pro->cancel($payment);
  222. return $this;
  223. }
  224. /**
  225. * Whether payment can be reviewed
  226. *
  227. * @param Mage_Sales_Model_Order_Payment $payment
  228. * @return bool
  229. */
  230. public function canReviewPayment(Mage_Payment_Model_Info $payment)
  231. {
  232. return parent::canReviewPayment($payment) && $this->_pro->canReviewPayment($payment);
  233. }
  234. /**
  235. * Attempt to accept a pending payment
  236. *
  237. * @param Mage_Sales_Model_Order_Payment $payment
  238. * @return bool
  239. */
  240. public function acceptPayment(Mage_Payment_Model_Info $payment)
  241. {
  242. parent::acceptPayment($payment);
  243. return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_ACCEPT);
  244. }
  245. /**
  246. * Attempt to deny a pending payment
  247. *
  248. * @param Mage_Sales_Model_Order_Payment $payment
  249. * @return bool
  250. */
  251. public function denyPayment(Mage_Payment_Model_Info $payment)
  252. {
  253. parent::denyPayment($payment);
  254. return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_DENY);
  255. }
  256. /**
  257. * Fetch transaction details info
  258. *
  259. * @param Mage_Payment_Model_Info $payment
  260. * @param string $transactionId
  261. * @return array
  262. */
  263. public function fetchTransactionInfo(Mage_Payment_Model_Info $payment, $transactionId)
  264. {
  265. return $this->_pro->fetchTransactionInfo($payment, $transactionId);
  266. }
  267. /**
  268. * Place an order with authorization or capture action
  269. *
  270. * @param Mage_Sales_Model_Order_Payment $payment
  271. * @param float $amount
  272. * @return Mage_Paypal_Model_Method_Agreement
  273. */
  274. protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
  275. {
  276. $order = $payment->getOrder();
  277. $billingAgreement = Mage::getModel('sales/billing_agreement')->load(
  278. $payment->getAdditionalInformation(Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract::TRANSPORT_BILLING_AGREEMENT_ID)
  279. );
  280. $api = $this->_pro->getApi()
  281. ->setReferenceId($billingAgreement->getReferenceId())
  282. ->setPaymentAction($this->_pro->getConfig()->paymentAction)
  283. ->setAmount($amount)
  284. ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
  285. ->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
  286. ->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled)
  287. ;
  288. // call api and import transaction and other payment information
  289. $api->callDoReferenceTransaction();
  290. $this->_pro->importPaymentInfo($api, $payment);
  291. $api->callGetTransactionDetails();
  292. $this->_pro->importPaymentInfo($api, $payment);
  293. $payment->setTransactionId($api->getTransactionId())
  294. ->setIsTransactionClosed(0);
  295. if ($api->getBillingAgreementId()) {
  296. $order->addRelatedObject($billingAgreement);
  297. $billingAgreement->setIsObjectChanged(true);
  298. $billingAgreement->addOrderRelation($order->getId());
  299. }
  300. return $this;
  301. }
  302. protected function _isAvailable($quote)
  303. {
  304. return $this->_pro->getConfig()->isMethodAvailable($this->_code);
  305. }
  306. /**
  307. * Payment action getter compatible with payment model
  308. *
  309. * @see Mage_Sales_Model_Payment::place()
  310. * @return string
  311. */
  312. public function getConfigPaymentAction()
  313. {
  314. return $this->_pro->getConfig()->getPaymentAction();
  315. }
  316. }