PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Payment/Model/Method/Abstract.php

https://bitbucket.org/jit_bec/shopifine
PHP | 717 lines | 310 code | 62 blank | 345 comment | 22 complexity | 9352b10af707b07a5ff9b689890813fe MD5 | raw file
Possible License(s): LGPL-3.0
  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_Payment
  23. * @copyright Copyright (c) 2012 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. * Payment method abstract model
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. abstract class Mage_Payment_Model_Method_Abstract extends Varien_Object
  32. {
  33. const ACTION_ORDER = 'order';
  34. const ACTION_AUTHORIZE = 'authorize';
  35. const ACTION_AUTHORIZE_CAPTURE = 'authorize_capture';
  36. const STATUS_UNKNOWN = 'UNKNOWN';
  37. const STATUS_APPROVED = 'APPROVED';
  38. const STATUS_ERROR = 'ERROR';
  39. const STATUS_DECLINED = 'DECLINED';
  40. const STATUS_VOID = 'VOID';
  41. const STATUS_SUCCESS = 'SUCCESS';
  42. protected $_code;
  43. protected $_formBlockType = 'payment/form';
  44. protected $_infoBlockType = 'payment/info';
  45. /**
  46. * Payment Method features
  47. * @var bool
  48. */
  49. protected $_isGateway = false;
  50. protected $_canOrder = false;
  51. protected $_canAuthorize = false;
  52. protected $_canCapture = false;
  53. protected $_canCapturePartial = false;
  54. protected $_canRefund = false;
  55. protected $_canRefundInvoicePartial = false;
  56. protected $_canVoid = false;
  57. protected $_canUseInternal = true;
  58. protected $_canUseCheckout = true;
  59. protected $_canUseForMultishipping = true;
  60. protected $_isInitializeNeeded = false;
  61. protected $_canFetchTransactionInfo = false;
  62. protected $_canReviewPayment = false;
  63. protected $_canCreateBillingAgreement = false;
  64. protected $_canManageRecurringProfiles = true;
  65. /**
  66. * TODO: whether a captured transaction may be voided by this gateway
  67. * This may happen when amount is captured, but not settled
  68. * @var bool
  69. */
  70. protected $_canCancelInvoice = false;
  71. /**
  72. * Fields that should be replaced in debug with '***'
  73. *
  74. * @var array
  75. */
  76. protected $_debugReplacePrivateDataKeys = array();
  77. public function __construct()
  78. {
  79. }
  80. /**
  81. * Check order availability
  82. *
  83. * @return bool
  84. */
  85. public function canOrder()
  86. {
  87. return $this->_canOrder;
  88. }
  89. /**
  90. * Check authorise availability
  91. *
  92. * @return bool
  93. */
  94. public function canAuthorize()
  95. {
  96. return $this->_canAuthorize;
  97. }
  98. /**
  99. * Check capture availability
  100. *
  101. * @return bool
  102. */
  103. public function canCapture()
  104. {
  105. return $this->_canCapture;
  106. }
  107. /**
  108. * Check partial capture availability
  109. *
  110. * @return bool
  111. */
  112. public function canCapturePartial()
  113. {
  114. return $this->_canCapturePartial;
  115. }
  116. /**
  117. * Check refund availability
  118. *
  119. * @return bool
  120. */
  121. public function canRefund()
  122. {
  123. return $this->_canRefund;
  124. }
  125. /**
  126. * Check partial refund availability for invoice
  127. *
  128. * @return bool
  129. */
  130. public function canRefundPartialPerInvoice()
  131. {
  132. return $this->_canRefundInvoicePartial;
  133. }
  134. /**
  135. * Check void availability
  136. *
  137. * @param Varien_Object $payment
  138. * @return bool
  139. */
  140. public function canVoid(Varien_Object $payment)
  141. {
  142. return $this->_canVoid;
  143. }
  144. /**
  145. * Using internal pages for input payment data
  146. * Can be used in admin
  147. *
  148. * @return bool
  149. */
  150. public function canUseInternal()
  151. {
  152. return $this->_canUseInternal;
  153. }
  154. /**
  155. * Can be used in regular checkout
  156. *
  157. * @return bool
  158. */
  159. public function canUseCheckout()
  160. {
  161. return $this->_canUseCheckout;
  162. }
  163. /**
  164. * Using for multiple shipping address
  165. *
  166. * @return bool
  167. */
  168. public function canUseForMultishipping()
  169. {
  170. return $this->_canUseForMultishipping;
  171. }
  172. /**
  173. * Can be edit order (renew order)
  174. *
  175. * @return bool
  176. */
  177. public function canEdit()
  178. {
  179. return true;
  180. }
  181. /**
  182. * Check fetch transaction info availability
  183. *
  184. * @return bool
  185. */
  186. public function canFetchTransactionInfo()
  187. {
  188. return $this->_canFetchTransactionInfo;
  189. }
  190. /**
  191. * Check whether payment method instance can create billing agreements
  192. *
  193. * @return bool
  194. */
  195. public function canCreateBillingAgreement()
  196. {
  197. return $this->_canCreateBillingAgreement;
  198. }
  199. /**
  200. * Fetch transaction info
  201. *
  202. * @param Mage_Payment_Model_Info $payment
  203. * @param string $transactionId
  204. * @return array
  205. */
  206. public function fetchTransactionInfo(Mage_Payment_Model_Info $payment, $transactionId)
  207. {
  208. return array();
  209. }
  210. /**
  211. * Retrieve payment system relation flag
  212. *
  213. * @return bool
  214. */
  215. public function isGateway()
  216. {
  217. return $this->_isGateway;
  218. }
  219. /**
  220. * flag if we need to run payment initialize while order place
  221. *
  222. * @return bool
  223. */
  224. public function isInitializeNeeded()
  225. {
  226. return $this->_isInitializeNeeded;
  227. }
  228. /**
  229. * To check billing country is allowed for the payment method
  230. *
  231. * @return bool
  232. */
  233. public function canUseForCountry($country)
  234. {
  235. /*
  236. for specific country, the flag will set up as 1
  237. */
  238. if($this->getConfigData('allowspecific')==1){
  239. $availableCountries = explode(',', $this->getConfigData('specificcountry'));
  240. if(!in_array($country, $availableCountries)){
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. /**
  247. * Check method for processing with base currency
  248. *
  249. * @param string $currencyCode
  250. * @return boolean
  251. */
  252. public function canUseForCurrency($currencyCode)
  253. {
  254. return true;
  255. }
  256. /**
  257. * Check manage billing agreements availability
  258. *
  259. * @return bool
  260. */
  261. public function canManageBillingAgreements()
  262. {
  263. return ($this instanceof Mage_Payment_Model_Billing_Agreement_MethodInterface);
  264. }
  265. /**
  266. * Whether can manage recurring profiles
  267. *
  268. * @return bool
  269. */
  270. public function canManageRecurringProfiles()
  271. {
  272. return $this->_canManageRecurringProfiles
  273. && ($this instanceof Mage_Payment_Model_Recurring_Profile_MethodInterface);
  274. }
  275. /**
  276. * Retrieve model helper
  277. *
  278. * @return Mage_Payment_Helper_Data
  279. */
  280. protected function _getHelper()
  281. {
  282. return Mage::helper('payment');
  283. }
  284. /**
  285. * Retrieve payment method code
  286. *
  287. * @return string
  288. */
  289. public function getCode()
  290. {
  291. if (empty($this->_code)) {
  292. Mage::throwException(Mage::helper('payment')->__('Cannot retrieve the payment method code.'));
  293. }
  294. return $this->_code;
  295. }
  296. /**
  297. * Retrieve block type for method form generation
  298. *
  299. * @return string
  300. */
  301. public function getFormBlockType()
  302. {
  303. return $this->_formBlockType;
  304. }
  305. /**
  306. * Retrieve block type for display method information
  307. *
  308. * @return string
  309. */
  310. public function getInfoBlockType()
  311. {
  312. return $this->_infoBlockType;
  313. }
  314. /**
  315. * Retrieve payment iformation model object
  316. *
  317. * @return Mage_Payment_Model_Info
  318. */
  319. public function getInfoInstance()
  320. {
  321. $instance = $this->getData('info_instance');
  322. if (!($instance instanceof Mage_Payment_Model_Info)) {
  323. Mage::throwException(Mage::helper('payment')->__('Cannot retrieve the payment information object instance.'));
  324. }
  325. return $instance;
  326. }
  327. /**
  328. * Validate payment method information object
  329. *
  330. * @return Mage_Payment_Model_Abstract
  331. */
  332. public function validate()
  333. {
  334. /**
  335. * to validate payment method is allowed for billing country or not
  336. */
  337. $paymentInfo = $this->getInfoInstance();
  338. if ($paymentInfo instanceof Mage_Sales_Model_Order_Payment) {
  339. $billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
  340. } else {
  341. $billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
  342. }
  343. if (!$this->canUseForCountry($billingCountry)) {
  344. Mage::throwException(Mage::helper('payment')->__('Selected payment type is not allowed for billing country.'));
  345. }
  346. return $this;
  347. }
  348. /**
  349. * Order payment abstract method
  350. *
  351. * @param Varien_Object $payment
  352. * @param float $amount
  353. *
  354. * @return Mage_Payment_Model_Abstract
  355. */
  356. public function order(Varien_Object $payment, $amount)
  357. {
  358. if (!$this->canOrder()) {
  359. Mage::throwException(Mage::helper('payment')->__('Order action is not available.'));
  360. }
  361. return $this;
  362. }
  363. /**
  364. * Authorize payment abstract method
  365. *
  366. * @param Varien_Object $payment
  367. * @param float $amount
  368. *
  369. * @return Mage_Payment_Model_Abstract
  370. */
  371. public function authorize(Varien_Object $payment, $amount)
  372. {
  373. if (!$this->canAuthorize()) {
  374. Mage::throwException(Mage::helper('payment')->__('Authorize action is not available.'));
  375. }
  376. return $this;
  377. }
  378. /**
  379. * Capture payment abstract method
  380. *
  381. * @param Varien_Object $payment
  382. * @param float $amount
  383. *
  384. * @return Mage_Payment_Model_Abstract
  385. */
  386. public function capture(Varien_Object $payment, $amount)
  387. {
  388. if (!$this->canCapture()) {
  389. Mage::throwException(Mage::helper('payment')->__('Capture action is not available.'));
  390. }
  391. return $this;
  392. }
  393. /**
  394. * Set capture transaction ID to invoice for informational purposes
  395. * @param Mage_Sales_Model_Order_Invoice $invoice
  396. * @param Mage_Sales_Model_Order_Payment $payment
  397. * @return Mage_Payment_Model_Method_Abstract
  398. */
  399. public function processInvoice($invoice, $payment)
  400. {
  401. $invoice->setTransactionId($payment->getLastTransId());
  402. return $this;
  403. }
  404. /**
  405. * Set refund transaction id to payment object for informational purposes
  406. * Candidate to be deprecated:
  407. * there can be multiple refunds per payment, thus payment.refund_transaction_id doesn't make big sense
  408. *
  409. * @param Mage_Sales_Model_Order_Invoice $invoice
  410. * @param Mage_Sales_Model_Order_Payment $payment
  411. * @return Mage_Payment_Model_Method_Abstract
  412. */
  413. public function processBeforeRefund($invoice, $payment)
  414. {
  415. $payment->setRefundTransactionId($invoice->getTransactionId());
  416. return $this;
  417. }
  418. /**
  419. * Refund specified amount for payment
  420. *
  421. * @param Varien_Object $payment
  422. * @param float $amount
  423. *
  424. * @return Mage_Payment_Model_Abstract
  425. */
  426. public function refund(Varien_Object $payment, $amount)
  427. {
  428. if (!$this->canRefund()) {
  429. Mage::throwException(Mage::helper('payment')->__('Refund action is not available.'));
  430. }
  431. return $this;
  432. }
  433. /**
  434. * Set transaction ID into creditmemo for informational purposes
  435. * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  436. * @param Mage_Sales_Model_Order_Payment $payment
  437. * @return Mage_Payment_Model_Method_Abstract
  438. */
  439. public function processCreditmemo($creditmemo, $payment)
  440. {
  441. $creditmemo->setTransactionId($payment->getLastTransId());
  442. return $this;
  443. }
  444. /**
  445. * Cancel payment abstract method
  446. *
  447. * @param Varien_Object $payment
  448. *
  449. * @return Mage_Payment_Model_Abstract
  450. */
  451. public function cancel(Varien_Object $payment)
  452. {
  453. return $this;
  454. }
  455. /**
  456. * @deprecated after 1.4.0.0-alpha3
  457. * this method doesn't make sense, because invoice must not void entire authorization
  458. * there should be method for invoice cancellation
  459. * @param Mage_Sales_Model_Order_Invoice $invoice
  460. * @param Mage_Sales_Model_Order_Payment $payment
  461. * @return Mage_Payment_Model_Method_Abstract
  462. */
  463. public function processBeforeVoid($invoice, $payment)
  464. {
  465. $payment->setVoidTransactionId($invoice->getTransactionId());
  466. return $this;
  467. }
  468. /**
  469. * Void payment abstract method
  470. *
  471. * @param Varien_Object $payment
  472. *
  473. * @return Mage_Payment_Model_Abstract
  474. */
  475. public function void(Varien_Object $payment)
  476. {
  477. if (!$this->canVoid($payment)) {
  478. Mage::throwException(Mage::helper('payment')->__('Void action is not available.'));
  479. }
  480. return $this;
  481. }
  482. /**
  483. * Whether this method can accept or deny payment
  484. *
  485. * @param Mage_Payment_Model_Info $payment
  486. *
  487. * @return bool
  488. */
  489. public function canReviewPayment(Mage_Payment_Model_Info $payment)
  490. {
  491. return $this->_canReviewPayment;
  492. }
  493. /**
  494. * Attempt to accept a payment that us under review
  495. *
  496. * @param Mage_Payment_Model_Info $payment
  497. * @return bool
  498. * @throws Mage_Core_Exception
  499. */
  500. public function acceptPayment(Mage_Payment_Model_Info $payment)
  501. {
  502. if (!$this->canReviewPayment($payment)) {
  503. Mage::throwException(Mage::helper('payment')->__('The payment review action is unavailable.'));
  504. }
  505. return false;
  506. }
  507. /**
  508. * Attempt to deny a payment that us under review
  509. *
  510. * @param Mage_Payment_Model_Info $payment
  511. * @return bool
  512. * @throws Mage_Core_Exception
  513. */
  514. public function denyPayment(Mage_Payment_Model_Info $payment)
  515. {
  516. if (!$this->canReviewPayment($payment)) {
  517. Mage::throwException(Mage::helper('payment')->__('The payment review action is unavailable.'));
  518. }
  519. return false;
  520. }
  521. /**
  522. * Retrieve payment method title
  523. *
  524. * @return string
  525. */
  526. public function getTitle()
  527. {
  528. return $this->getConfigData('title');
  529. }
  530. /**
  531. * Retrieve information from payment configuration
  532. *
  533. * @param string $field
  534. * @param int|string|null|Mage_Core_Model_Store $storeId
  535. *
  536. * @return mixed
  537. */
  538. public function getConfigData($field, $storeId = null)
  539. {
  540. if (null === $storeId) {
  541. $storeId = $this->getStore();
  542. }
  543. $path = 'payment/'.$this->getCode().'/'.$field;
  544. return Mage::getStoreConfig($path, $storeId);
  545. }
  546. /**
  547. * Assign data to info model instance
  548. *
  549. * @param mixed $data
  550. * @return Mage_Payment_Model_Info
  551. */
  552. public function assignData($data)
  553. {
  554. if (is_array($data)) {
  555. $this->getInfoInstance()->addData($data);
  556. }
  557. elseif ($data instanceof Varien_Object) {
  558. $this->getInfoInstance()->addData($data->getData());
  559. }
  560. return $this;
  561. }
  562. /**
  563. * Parepare info instance for save
  564. *
  565. * @return Mage_Payment_Model_Abstract
  566. */
  567. public function prepareSave()
  568. {
  569. return $this;
  570. }
  571. /**
  572. * Check whether payment method can be used
  573. *
  574. * TODO: payment method instance is not supposed to know about quote
  575. *
  576. * @param Mage_Sales_Model_Quote|null $quote
  577. *
  578. * @return bool
  579. */
  580. public function isAvailable($quote = null)
  581. {
  582. $checkResult = new StdClass;
  583. $isActive = (bool)(int)$this->getConfigData('active', $quote ? $quote->getStoreId() : null);
  584. $checkResult->isAvailable = $isActive;
  585. $checkResult->isDeniedInConfig = !$isActive; // for future use in observers
  586. Mage::dispatchEvent('payment_method_is_active', array(
  587. 'result' => $checkResult,
  588. 'method_instance' => $this,
  589. 'quote' => $quote,
  590. ));
  591. // disable method if it cannot implement recurring profiles management and there are recurring items in quote
  592. if ($checkResult->isAvailable) {
  593. $implementsRecurring = $this->canManageRecurringProfiles();
  594. // the $quote->hasRecurringItems() causes big performance impact, thus it has to be called last
  595. if ($quote && !$implementsRecurring && $quote->hasRecurringItems()) {
  596. $checkResult->isAvailable = false;
  597. }
  598. }
  599. return $checkResult->isAvailable;
  600. }
  601. /**
  602. * Method that will be executed instead of authorize or capture
  603. * if flag isInitializeNeeded set to true
  604. *
  605. * @param string $paymentAction
  606. * @param object $stateObject
  607. *
  608. * @return Mage_Payment_Model_Abstract
  609. */
  610. public function initialize($paymentAction, $stateObject)
  611. {
  612. return $this;
  613. }
  614. /**
  615. * Get config payment action url
  616. * Used to universalize payment actions when processing payment place
  617. *
  618. * @return string
  619. */
  620. public function getConfigPaymentAction()
  621. {
  622. return $this->getConfigData('payment_action');
  623. }
  624. /**
  625. * Log debug data to file
  626. *
  627. * @param mixed $debugData
  628. */
  629. protected function _debug($debugData)
  630. {
  631. if ($this->getDebugFlag()) {
  632. Mage::getModel('core/log_adapter', 'payment_' . $this->getCode() . '.log')
  633. ->setFilterDataKeys($this->_debugReplacePrivateDataKeys)
  634. ->log($debugData);
  635. }
  636. }
  637. /**
  638. * Define if debugging is enabled
  639. *
  640. * @return bool
  641. */
  642. public function getDebugFlag()
  643. {
  644. return $this->getConfigData('debug');
  645. }
  646. /**
  647. * Used to call debug method from not Payment Method context
  648. *
  649. * @param mixed $debugData
  650. */
  651. public function debugData($debugData)
  652. {
  653. $this->_debug($debugData);
  654. }
  655. }