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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Paypal\Model;
use Magento\Paypal\Model\Api\Nvp;
use Magento\Paypal\Model\Api\ProcessableException as ApiProcessableException;
use Magento\Paypal\Model\Express\Checkout as ExpressCheckout;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\ScopeInterface;
/**
* PayPal Express Module
* @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Express extends \Magento\Payment\Model\Method\AbstractMethod
{
/**
* @var string
*/
protected $_code = \Magento\Paypal\Model\Config::METHOD_WPP_EXPRESS;
/**
* @var string
*/
protected $_formBlockType = \Magento\Paypal\Block\Express\Form::class;
/**
* @var string
*/
protected $_infoBlockType = \Magento\Paypal\Block\Payment\Info::class;
/**
* Availability option
*
* @var bool
*/
protected $_isGateway = true;
/**
* Availability option
*
* @var bool
*/
protected $_canOrder = true;
/**
* Availability option
*
* @var bool
*/
protected $_canAuthorize = true;
/**
* Availability option
*
* @var bool
*/
protected $_canCapture = true;
/**
* Availability option
*
* @var bool
*/
protected $_canCapturePartial = true;
/**
* Availability option
*
* @var bool
*/
protected $_canRefund = true;
/**
* Availability option
*
* @var bool
*/
protected $_canRefundInvoicePartial = true;
/**
* Availability option
*
* @var bool
*/
protected $_canVoid = true;
/**
* Availability option
*
* @var bool
*/
protected $_canUseInternal = false;
/**
* Availability option
*
* @var bool
*/
protected $_canUseCheckout = true;
/**
* Availability option
*
* @var bool
*/
protected $_canFetchTransactionInfo = true;
/**
* Availability option
*
* @var bool
*/
protected $_canReviewPayment = true;
/**
* Website Payments Pro instance
*
* @var \Magento\Paypal\Model\Pro
*/
protected $_pro;
/**
* Payment additional information key for payment action
*
* @var string
*/
protected $_isOrderPaymentActionKey = 'is_order_action';
/**
* Payment additional information key for number of used authorizations
*
* @var string
*/
protected $_authorizationCountKey = 'authorization_count';
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_urlBuilder;
/**
* @var \Magento\Paypal\Model\CartFactory
*/
protected $_cartFactory;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;
/**
* @var \Magento\Framework\Exception\LocalizedExceptionFactory
*/
protected $_exception;
/**
* @var \Magento\Sales\Api\TransactionRepositoryInterface
*/
protected $transactionRepository;
/**
* @var Transaction\BuilderInterface
*/
protected $transactionBuilder;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param ProFactory $proFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param CartFactory $cartFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\Exception\LocalizedExceptionFactory $exception
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param Transaction\BuilderInterface $transactionBuilder
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
ProFactory $proFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Paypal\Model\CartFactory $cartFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Exception\LocalizedExceptionFactory $exception,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$paymentData,
$scopeConfig,
$logger,
$resource,
$resourceCollection,
$data
);
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
$this->_cartFactory = $cartFactory;
$this->_checkoutSession = $checkoutSession;
$this->_exception = $exception;
$this->transactionRepository = $transactionRepository;
$this->transactionBuilder = $transactionBuilder;
$proInstance = array_shift($data);
if ($proInstance && $proInstance instanceof \Magento\Paypal\Model\Pro) {
$this->_pro = $proInstance;
} else {
$this->_pro = $proFactory->create();
}
$this->_pro->setMethod($this->_code);
$this->_setApiProcessableErrors();
}
/**
* Set processable error codes to API model
*
* @return \Magento\Paypal\Model\Api\Nvp
*/
protected function _setApiProcessableErrors()
{
return $this->_pro->getApi()->setProcessableErrors(
[
ApiProcessableException::API_INTERNAL_ERROR,
ApiProcessableException::API_UNABLE_PROCESS_PAYMENT_ERROR_CODE,
ApiProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL,
ApiProcessableException::API_UNABLE_TRANSACTION_COMPLETE,
ApiProcessableException::API_TRANSACTION_EXPIRED,
ApiProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED,
ApiProcessableException::API_COUNTRY_FILTER_DECLINE,
ApiProcessableException::API_MAXIMUM_AMOUNT_FILTER_DECLINE,
ApiProcessableException::API_OTHER_FILTER_DECLINE,
ApiProcessableException::API_ADDRESS_MATCH_FAIL,
]
);
}
/**
* Store setter
*
* Also updates store ID in config object
*
* @param \Magento\Store\Model\Store|int $store
* @return $this
*/
public function setStore($store)
{
$this->setData('store', $store);
if (null === $store) {
$store = $this->_storeManager->getStore()->getId();
}
$this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
return $this;
}
/**
* Can be used in regular checkout
*
* @return bool
*/
public function canUseCheckout()
{
if ($this->_scopeConfig->isSetFlag(
'payment/hosted_pro/active',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) && !$this->_scopeConfig->isSetFlag(
'payment/hosted_pro/display_ec',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
) {
return false;
}
return parent::canUseCheckout();
}
/**
* Whether method is available for specified currency
*
* @param string $currencyCode
* @return bool
*/
public function canUseForCurrency($currencyCode)
{
return $this->_pro->getConfig()->isCurrencyCodeSupported($currencyCode);
}
/**
* Payment action getter compatible with payment model
*
* @see \Magento\Sales\Model\Payment::place()
* @return string
*/
public function getConfigPaymentAction()
{
return $this->_pro->getConfig()->getPaymentAction();
}
/**
* Check whether payment method can be used
*
* @param \Magento\Quote\Api\Data\CartInterface|Quote|null $quote
* @return bool
*/
public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
{
return parent::isAvailable($quote) && $this->_pro->getConfig()->isMethodAvailable();
}
/**
* Custom getter for payment configuration
*
* @param string $field
* @param int|null $storeId
* @return mixed
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getConfigData($field, $storeId = null)
{
if ('order_place_redirect_url' === $field) {
return $this->getOrderPlaceRedirectUrl();
}
return $this->_pro->getConfig()->getValue($field);
}
/**
* Order payment
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @param float $amount
* @return $this
*/
public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$paypalTransactionData = $this->_checkoutSession->getPaypalTransactionData();
if (!is_array($paypalTransactionData)) {
$this->_placeOrder($payment, $amount);
} else {
$this->_importToPayment($this->_pro->getApi()->setData($paypalTransactionData), $payment);
}
$payment->setAdditionalInformation($this->_isOrderPaymentActionKey, true);
if ($payment->getIsFraudDetected()) {
return $this;
}
$payment->getOrder()->setActionFlag(Order::ACTION_FLAG_INVOICE, false);
return $this;
}
/**
* Authorize payment
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @param float $amount
* @return $this
*/
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
return $this->_placeOrder($payment, $amount);
}
/**
* Void payment
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function void(\Magento\Payment\Model\InfoInterface $payment)
{
//Switching to order transaction if needed
if ($payment->getAdditionalInformation(
$this->_isOrderPaymentActionKey
) && !$payment->getVoidOnlyAuthorization()
) {
$orderTransaction = $this->getOrderTransaction($payment);
if ($orderTransaction) {
$payment->setParentTransactionId($orderTransaction->getTxnId());
$payment->setTransactionId($orderTransaction->getTxnId() . '-void');
}
}
$this->_pro->void($payment);
return $this;
}
/**
* Capture payment
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @param float $amount
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$authorizationTransaction = $payment->getAuthorizationTransaction();
$authorizationPeriod = abs((int)$this->getConfigData('authorization_honor_period'));
$maxAuthorizationNumber = abs((int)$this->getConfigData('child_authorization_number'));
$order = $payment->getOrder();
$isAuthorizationCreated = false;
if ($payment->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
$voided = false;
if (!$authorizationTransaction->getIsClosed() && $this->_isTransactionExpired(
$authorizationTransaction,
$authorizationPeriod
)
) {
//Save payment state and configure payment object for voiding
$isCaptureFinal = $payment->getShouldCloseParentTransaction();
$payment->setShouldCloseParentTransaction(false);
$payment->setParentTransactionId($authorizationTransaction->getTxnId());
$payment->unsTransactionId();
$payment->setVoidOnlyAuthorization(true);
$payment->void(new \Magento\Framework\DataObject());
//Revert payment state after voiding
$payment->unsAuthorizationTransaction();
$payment->unsTransactionId();
$payment->setShouldCloseParentTransaction($isCaptureFinal);
$voided = true;
}
if ($authorizationTransaction->getIsClosed() || $voided) {
if ($payment->getAdditionalInformation($this->_authorizationCountKey) > $maxAuthorizationNumber - 1) {
$this->_exception->create(
['phrase' => __('The maximum number of child authorizations is reached.')]
);
}
$api = $this->_callDoAuthorize($amount, $payment, $authorizationTransaction->getParentTxnId());
//Adding authorization transaction
$this->_pro->importPaymentInfo($api, $payment);
$payment->setTransactionId($api->getTransactionId());
$payment->setParentTransactionId($authorizationTransaction->getParentTxnId());
$payment->setIsTransactionClosed(false);
$formatedPrice = $order->getBaseCurrency()->formatTxt($amount);
if ($payment->getIsTransactionPending()) {
$message = __(
'We\'ll authorize the amount of %1 as soon as the payment gateway approves it.',
$formatedPrice
);
} else {
$message = __('The authorized amount is %1.', $formatedPrice);
}
$transaction = $this->transactionBuilder->setPayment($payment)
->setOrder($order)
->setTransactionId($payment->getTransactionId())
->setFailSafe(true)
->build(Transaction::TYPE_AUTH);
$payment->addTransactionCommentsToOrder($transaction, $message);
$payment->setParentTransactionId($api->getTransactionId());
$isAuthorizationCreated = true;
}
//close order transaction if needed
if ($payment->getShouldCloseParentTransaction()) {
$orderTransaction = $this->getOrderTransaction($payment);
if ($orderTransaction) {
$orderTransaction->setIsClosed(true);
$order->addRelatedObject($orderTransaction);
}
}
}
if (false === $this->_pro->capture($payment, $amount)) {
$this->_placeOrder($payment, $amount);
}
if ($isAuthorizationCreated && isset($transaction)) {
$transaction->setIsClosed(true);
}
return $this;
}
/**
* Refund capture
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$this->_pro->refund($payment, $amount);
return $this;
}
/**
* Cancel payment
*
* @param \Magento\Framework\DataObject|\Magento\Payment\Model\InfoInterface|Payment $payment
* @return $this
*/
public function cancel(\Magento\Payment\Model\InfoInterface $payment)
{
$this->void($payment);
return $this;
}
/**
* Whether payment can be reviewed
*
* @return bool
*/
public function canReviewPayment()
{
return parent::canReviewPayment() && $this->_pro->canReviewPayment($this->getInfoInstance());
}
/**
* Attempt to accept a pending payment
*
* @param \Magento\Payment\Model\Info|Payment $payment
* @return bool
*/
public function acceptPayment(\Magento\Payment\Model\InfoInterface $payment)
{
parent::acceptPayment($payment);
return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_ACCEPT);
}
/**
* Attempt to deny a pending payment
*
* @param \Magento\Payment\Model\InfoInterface|Payment $payment
* @return bool
*/
public function denyPayment(\Magento\Payment\Model\InfoInterface $payment)
{
parent::denyPayment($payment);
return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_DENY);
}
/**
* Checkout redirect URL getter for onepage checkout (hardcode)
*
* @see \Magento\Checkout\Controller\Onepage::savePaymentAction()
* @see Quote\Payment::getCheckoutRedirectUrl()
* @return string
*/
public function getCheckoutRedirectUrl()
{
return $this->_urlBuilder->getUrl('paypal/express/start');
}
/**
* Fetch transaction details info
*
* @param \Magento\Payment\Model\InfoInterface $payment
* @param string $transactionId
* @return array
*/
public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
{
return $this->_pro->fetchTransactionInfo($payment, $transactionId);
}
/**
* Returns api instance
*
* @return Api\Nvp
*/
public function getApi()
{
return $this->_pro->getApi();
}
/**
* Assign data to info model instance
*
* @param array|\Magento\Framework\DataObject $data
* @return \Magento\Payment\Model\Info
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function assignData(\Magento\Framework\DataObject $data)
{
parent::assignData($data);
$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
if (!is_array($additionalData)) {
return $this;
}
foreach ($additionalData as $key => $value) {
// Skip extension attributes
if ($key === \Magento\Framework\Api\ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY) {
continue;
}
$this->getInfoInstance()->setAdditionalInformation($key, $value);
}
return $this;
}
/**
* Place an order with authorization or capture action
*
* @param Payment $payment
* @param float $amount
* @return $this
*/
protected function _placeOrder(Payment $payment, $amount)
{
$order = $payment->getOrder();
// prepare api call
$token = $payment->getAdditionalInformation(ExpressCheckout::PAYMENT_INFO_TRANSPORT_TOKEN);
$cart = $this->_cartFactory->create(['salesModel' => $order]);
$api = $this->getApi()->setToken(
$token
)->setPayerId(
$payment->getAdditionalInformation(ExpressCheckout::PAYMENT_INFO_TRANSPORT_PAYER_ID)
)->setAmount(
$amount
)->setPaymentAction(
$this->_pro->getConfig()->getValue('paymentAction')
)->setNotifyUrl(
$this->_urlBuilder->getUrl('paypal/ipn/')
)->setInvNum(
$order->getIncrementId()
)->setCurrencyCode(
$order->getBaseCurrencyCode()
)->setPaypalCart(
$cart
)->setIsLineItemsEnabled(
$this->_pro->getConfig()->getValue('lineItemsEnabled')
);
if ($order->getIsVirtual()) {
$api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
} else {
$api->setAddress($order->getShippingAddress());
$api->setBillingAddress($order->getBillingAddress());
}
// call api and get details from it
$api->callDoExpressCheckoutPayment();
$this->_importToPayment($api, $payment);
return $this;
}
/**
* Import payment info to payment
*
* @param Nvp $api
* @param Payment $payment
* @return void
*/
protected function _importToPayment($api, $payment)
{
$payment->setTransactionId(
$api->getTransactionId()
)->setIsTransactionClosed(
0
)->setAdditionalInformation(
ExpressCheckout::PAYMENT_INFO_TRANSPORT_REDIRECT,
$api->getRedirectRequired()
);
if ($api->getBillingAgreementId()) {
$payment->setBillingAgreementData(
[
'billing_agreement_id' => $api->getBillingAgreementId(),
'method_code' => \Magento\Paypal\Model\Config::METHOD_BILLING_AGREEMENT,
]
);
}
$this->_pro->importPaymentInfo($api, $payment);
}
/**
* Check void availability
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* @internal param \Magento\Framework\DataObject $payment
*/
public function canVoid()
{
$info = $this->getInfoInstance();
if ($info->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
$orderTransaction = $this->getOrderTransaction($info);
if ($orderTransaction) {
$info->setParentTransactionId($orderTransaction->getTxnId());
}
}
return $this->_canVoid;
}
/**
* Check capture availability
*
* @return bool
*/
public function canCapture()
{
$payment = $this->getInfoInstance();
$this->_pro->getConfig()->setStoreId($payment->getOrder()->getStore()->getId());
if ($payment->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
$orderTransaction = $this->getOrderTransaction($payment);
if ($orderTransaction->getIsClosed()) {
return false;
}
$orderValidPeriod = abs((int)$this->getConfigData('order_valid_period'));
$dateCompass = new \DateTime($orderTransaction->getCreatedAt());
$dateCompass->modify('+' . $orderValidPeriod . ' days');
$currentDate = new \DateTime();
if ($currentDate > $dateCompass || $orderValidPeriod == 0) {
return false;
}
}
return $this->_canCapture;
}
/**
* Call DoAuthorize
*
* @param int $amount
* @param \Magento\Framework\DataObject $payment
* @param string $parentTransactionId
* @return \Magento\Paypal\Model\Api\AbstractApi
*/
protected function _callDoAuthorize($amount, $payment, $parentTransactionId)
{
$apiData = $this->_pro->getApi()->getData();
foreach ($apiData as $k => $v) {
if (is_object($v)) {
unset($apiData[$k]);
}
}
$this->_checkoutSession->setPaypalTransactionData($apiData);
$this->_pro->resetApi();
$api = $this->_setApiProcessableErrors()
->setAmount($amount)
->setCurrencyCode($payment->getOrder()->getBaseCurrencyCode())
->setTransactionId($parentTransactionId)
->callDoAuthorization();
$payment->setAdditionalInformation(
$this->_authorizationCountKey,
$payment->getAdditionalInformation($this->_authorizationCountKey) + 1
);
return $api;
}
/**
* Check transaction for expiration in PST
*
* @param Transaction $transaction
* @param int $period
* @return bool
*/
protected function _isTransactionExpired(Transaction $transaction, $period)
{
$period = (int)$period;
if (0 == $period) {
return true;
}
$transactionClosingDate = new \DateTime($transaction->getCreatedAt(), new \DateTimeZone('GMT'));
$transactionClosingDate->setTimezone(new \DateTimeZone('US/Pacific'));
/**
* 11:49:00 PayPal transactions closing time
*/
$transactionClosingDate->setTime(11, 49, 00);
$transactionClosingDate->modify('+' . $period . ' days');
$currentTime = new \DateTime(null, new \DateTimeZone('US/Pacific'));
if ($currentTime > $transactionClosingDate) {
return true;
}
return false;
}
/**
* Is active
*
* @param int|null $storeId
* @return bool
*/
public function isActive($storeId = null)
{
$pathStandardExpress = 'payment/' . Config::METHOD_WPS_EXPRESS . '/active';
$pathPaypalExpress = 'payment/' . Config::METHOD_WPP_EXPRESS . '/active';
return parent::isActive($storeId)
|| (bool)(int)$this->_scopeConfig->getValue($pathStandardExpress, ScopeInterface::SCOPE_STORE, $storeId)
|| (bool)(int)$this->_scopeConfig->getValue($pathPaypalExpress, ScopeInterface::SCOPE_STORE, $storeId);
}
/**
* Get transaction with type order
*
* @param OrderPaymentInterface $payment
* @return false|\Magento\Sales\Api\Data\TransactionInterface
*/
protected function getOrderTransaction($payment)
{
return $this->transactionRepository->getByTransactionType(
Transaction::TYPE_ORDER,
$payment->getId(),
$payment->getOrder()->getId()
);
}
}