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