Skip to content

Commit 06a1a38

Browse files
https://github.com/mage2pro/checkout.com/issues/8
1 parent 046185e commit 06a1a38

File tree

5 files changed

+49
-58
lines changed

5 files changed

+49
-58
lines changed

Handler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
namespace Dfe\CheckoutCom;
33
use Dfe\CheckoutCom\Handler\DefaultT;
44
use Exception as E;
5-
abstract class Handler extends \Df\Core\OLegacy {
5+
/**
6+
* @see \Dfe\CheckoutCom\Handler\Charge
7+
* @see \Dfe\CheckoutCom\Handler\CustomerReturn
8+
* @see \Dfe\CheckoutCom\Handler\DefaultT
9+
*/
10+
abstract class Handler extends \Df\Core\O {
611
/**
712
* 2016-03-25
813
* @used-by \Dfe\CheckoutCom\Handler::p()

Handler/Charge.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Magento\Sales\Model\Order\Payment;
1111
/**
1212
* 2016-05-10
13-
* @see \Dfe\CheckoutCom\Handler\Charge\Captured::invoice()
14-
* @see \Dfe\CheckoutCom\Handler\Charge\Captured::process()
15-
* @see \Dfe\CheckoutCom\Handler\Charge\Refunded::process()
13+
* @see \Dfe\CheckoutCom\Handler\Charge\Captured
14+
* @see \Dfe\CheckoutCom\Handler\Charge\Refunded
15+
* @see \Dfe\CheckoutCom\Handler\Charge\Voided
1616
*/
1717
abstract class Charge extends Handler {
1818
/**
@@ -59,18 +59,15 @@ final protected function o() {return df_order($this->op());}
5959
* @used-by \Dfe\CheckoutCom\Handler\Charge\Voided::process()
6060
* @return Payment|DfPayment|null
6161
*/
62-
final protected function op() {return dfc($this, function() {return
63-
$this->paymentByTxnId($this->parentId())
64-
;});}
62+
final protected function op() {return dfc($this, function() {return $this->paymentByTxnId($this->parentId());});}
6563

6664
/**
6765
* 2016-03-26
6866
* @param string|null $id
6967
* @return Payment|DfPayment|null
7068
*/
7169
final protected function paymentByTxnId($id) {return dfc($this, function($id) {
72-
/** @var Payment|null $result */
73-
$result = null;
70+
$result = null; /** @var Payment|null $result */
7471
if ($id) {
7572
/** @var int|null $paymentId */
7673
$paymentId = df_fetch_one('sales_payment_transaction', 'payment_id', ['txn_id' => $id]);
@@ -95,31 +92,31 @@ final protected function paymentByTxnId($id) {return dfc($this, function($id) {
9592
* идентификатор транзакции, потому что метод
9693
* @see \Magento\Sales\Model\Order\Payment\Operations\CaptureOperation::capture()
9794
* перетрёт наш идентификатор кодом:
98-
$payment->setTransactionId(
99-
$this->transactionManager->generateTransactionId(
100-
$payment,
101-
Transaction::TYPE_CAPTURE,
102-
$payment->getAuthorizationTransaction()
103-
)
104-
);
95+
* $payment->setTransactionId(
96+
* $this->transactionManager->generateTransactionId(
97+
* $payment,
98+
* Transaction::TYPE_CAPTURE,
99+
* $payment->getAuthorizationTransaction()
100+
* )
101+
* );
105102
* https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Sales/Model/Order/Payment/Operations/CaptureOperation.php#L40-L46
106103
* Однако мне следовало посмотреть глубже, в реализацию метода
107104
* @see \Magento\Sales\Model\Order\Payment\Transaction\Manager::generateTransactionId()
108105
* чтобы понять, что когда нестандартный идентификатор транзакции уже установлен,
109106
* то метод его не перетирает:
110-
if (!$payment->getParentTransactionId()
111-
&& !$payment->getTransactionId() && $transactionBasedOn
112-
) {
113-
$payment->setParentTransactionId($transactionBasedOn->getTxnId());
114-
}
115-
// generate transaction id for an offline action or payment method that didn't set it
116-
if (
117-
($parentTxnId = $payment->getParentTransactionId())
118-
&& !$payment->getTransactionId()
119-
) {
120-
return "{$parentTxnId}-{$type}";
121-
}
122-
return $payment->getTransactionId();
107+
* if (!$payment->getParentTransactionId()
108+
* && !$payment->getTransactionId() && $transactionBasedOn
109+
* ) {
110+
* $payment->setParentTransactionId($transactionBasedOn->getTxnId());
111+
* }
112+
* // generate transaction id for an offline action or payment method that didn't set it
113+
* if (
114+
* ($parentTxnId = $payment->getParentTransactionId())
115+
* && !$payment->getTransactionId()
116+
* ) {
117+
* return "{$parentTxnId}-{$type}";
118+
* }
119+
* return $payment->getTransactionId();
123120
* https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Sales/Model/Order/Payment/Transaction/Manager.php#L73-L80
124121
* Поэтому никакие обходные манёвры нам не нужны,
125122
* и смело устанвливаем транзакции наш нестандартный идентификатор прямо здесь.

Handler/Charge/Captured.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Magento\Sales\Model\Order as O;
88
use Magento\Sales\Model\Order\Invoice;
99
use Magento\Sales\Model\Service\InvoiceService;
10-
// 2016-05-10
11-
// charge.captured
12-
// http://docs.checkout.com/getting-started/webhooks
10+
# 2016-05-10 charge.captured http://docs.checkout.com/getting-started/webhooks
1311
final class Captured extends Charge {
1412
/**
1513
* 2016-03-25
@@ -23,19 +21,14 @@ final class Captured extends Charge {
2321
* @throws LE
2422
*/
2523
protected function process() {
26-
/** @var string|null $result */
27-
$result = null;
28-
/** @var O|DfOrder $o */
29-
$o = $this->o();
30-
// 2016-05-11
31-
// The payment is «Flagged».
32-
// The «Accept» operation should be performed.
24+
$r = null; /** @var string|null $r */
25+
$o = $this->o(); /** @var O|DfOrder $o */
26+
# 2016-05-11 The payment is «Flagged». The «Accept» operation should be performed.
3327
if ($o->isPaymentReview()) {
3428
$this->op()->accept();
3529
$o->save();
3630
}
37-
// 2016-05-11
38-
// The payment is in the «Authorized» state.
31+
# 2016-05-11 The payment is in the «Authorized» state.
3932
else {
4033
// 2016-12-30
4134
// Мы не должны считать исключительной ситуацией повторное получение
@@ -46,7 +39,7 @@ protected function process() {
4639
// by making your event processing idempotent.»
4740
// https://stripe.com/docs/webhooks#best-practices
4841
if (!$o->canInvoice()) {
49-
$result = __('The order does not allow an invoice to be created.');
42+
$r = __('The order does not allow an invoice to be created.');
5043
}
5144
else {
5245
$o->setIsInProcess(true);
@@ -55,7 +48,7 @@ protected function process() {
5548
df_mail_invoice($this->invoice());
5649
}
5750
}
58-
return $result;
51+
return $r;
5952
}
6053

6154
/**
@@ -65,17 +58,15 @@ protected function process() {
6558
*/
6659
private function invoice() {
6760
if (!isset($this->{__METHOD__})) {
68-
/** @var InvoiceService $invoiceService */
69-
$invoiceService = df_o(InvoiceService::class);
70-
/** @var Invoice|DfInvoice $result */
71-
$result = $invoiceService->prepareInvoice($this->o());
72-
if (!$result) {
61+
$invoiceService = df_o(InvoiceService::class); /** @var InvoiceService $invoiceService */
62+
$r = $invoiceService->prepareInvoice($this->o()); /** @var Invoice|DfInvoice $r */
63+
if (!$r) {
7364
throw new LE(__('We can\'t save the invoice right now.'));
7465
}
75-
if (!$result->getTotalQty()) {
66+
if (!$r->getTotalQty()) {
7667
throw new LE(__('You can\'t create an invoice without products.'));
7768
}
78-
df_register('current_invoice', $result);
69+
df_register('current_invoice', $r);
7970
/**
8071
* 2016-03-26
8172
* @used-by \Magento\Sales\Model\Order\Invoice::register()
@@ -84,9 +75,9 @@ private function invoice() {
8475
* and not \Magento\Sales\Model\Order\Invoice::CAPTURE_OFFINE,
8576
* that was created by the transaction capture.
8677
*/
87-
$result->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
88-
$result->register();
89-
$this->{__METHOD__} = $result;
78+
$r->setRequestedCaptureCase(Invoice::CAPTURE_ONLINE);
79+
$r->register();
80+
$this->{__METHOD__} = $r;
9081
}
9182
return $this->{__METHOD__};
9283
}

Handler/Charge/Voided.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
namespace Dfe\CheckoutCom\Handler\Charge;
33
use Dfe\CheckoutCom\Handler\Charge;
44
use Magento\Framework\DataObject as _DO;
5-
// 2016-05-10
6-
// charge.voided
7-
// http://docs.checkout.com/getting-started/webhooks
5+
# 2016-05-10 charge.voided http://docs.checkout.com/getting-started/webhooks
86
final class Voided extends Charge {
97
/**
108
* 2016-05-10
@@ -16,7 +14,7 @@ final class Voided extends Charge {
1614
* @used-by \Dfe\CheckoutCom\Handler::p()
1715
*/
1816
protected function process() {
19-
// 2016-05-11 isPaymentReview() means that the transaction is «Flagged». We need to void it.
17+
# 2016-05-11 isPaymentReview() means that the transaction is «Flagged». We need to void it.
2018
$this->o()->isPaymentReview() ? $this->op()->deny() : $this->op()->void(new _DO);
2119
$this->o()->save();
2220
}

Handler/DefaultT.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Dfe\CheckoutCom\Handler;
33
use Dfe\CheckoutCom\Handler;
4-
class DefaultT extends Handler {
4+
final class DefaultT extends Handler {
55
/**
66
* 2016-05-11
77
* Override the method in order to return «Not implemented.» instead of «The event is not for our store.»

0 commit comments

Comments
 (0)