1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Braintree\Business\Payment\Transaction; |
9
|
|
|
|
10
|
|
|
use Braintree\Transaction; |
11
|
|
|
use Braintree\Transaction as BraintreeTransaction; |
12
|
|
|
use Generated\Shared\Transfer\ExpenseTransfer; |
|
|
|
|
13
|
|
|
use Spryker\Shared\Shipment\ShipmentConstants; |
14
|
|
|
use SprykerEco\Zed\Braintree\BraintreeConfig; |
15
|
|
|
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants; |
16
|
|
|
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface; |
17
|
|
|
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface; |
18
|
|
|
use SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface; |
19
|
|
|
|
20
|
|
|
class RefundOrderTransaction extends AbstractTransaction |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface |
24
|
|
|
*/ |
25
|
|
|
protected $moneyFacade; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $shipmentRefundTransactionHandler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $braintreeRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \SprykerEco\Zed\Braintree\BraintreeConfig $brainTreeConfig |
39
|
|
|
* @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade |
40
|
|
|
* @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler |
41
|
|
|
* @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
BraintreeConfig $brainTreeConfig, |
45
|
|
|
BraintreeToMoneyFacadeInterface $moneyFacade, |
46
|
|
|
ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler, |
47
|
|
|
BraintreeRepositoryInterface $braintreeRepository |
48
|
|
|
) { |
49
|
|
|
parent::__construct($brainTreeConfig); |
50
|
|
|
$this->moneyFacade = $moneyFacade; |
51
|
|
|
$this->shipmentRefundTransactionHandler = $shipmentRefundTransactionHandler; |
52
|
|
|
$this->braintreeRepository = $braintreeRepository; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function getTransactionType(): string |
59
|
|
|
{ |
60
|
|
|
return ApiConstants::CREDIT; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function getTransactionCode(): string |
67
|
|
|
{ |
68
|
|
|
return ApiConstants::TRANSACTION_CODE_REFUND; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \Braintree\Result\Error|\Braintree\Result\Successful |
73
|
|
|
*/ |
74
|
|
|
public function doTransaction() |
75
|
|
|
{ |
76
|
|
|
return $this->refund(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Braintree\Result\Error|\Braintree\Result\Successful |
81
|
|
|
*/ |
82
|
|
|
protected function refund() |
83
|
|
|
{ |
84
|
|
|
$transaction = $this->findTransaction(); |
85
|
|
|
|
86
|
|
|
if ($transaction->status === ApiConstants::STATUS_CODE_CAPTURE_SUBMITTED) { |
87
|
|
|
return BraintreeTransaction::void($this->getTransactionIdentifier()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return BraintreeTransaction::refund( |
91
|
|
|
$this->getTransactionIdentifier(), |
92
|
|
|
$this->findAmount() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return float|null |
98
|
|
|
*/ |
99
|
|
|
protected function findAmount(): ?float |
100
|
|
|
{ |
101
|
|
|
$refundTransfer = $this->transactionMetaTransfer->requireRefund()->getRefund(); |
102
|
|
|
$amount = $refundTransfer->getAmount(); |
103
|
|
|
|
104
|
|
|
if (!$amount) { |
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$shipmentExpenseTransfer = $this->findShipmentExpenseTransfer(); |
109
|
|
|
|
110
|
|
|
if ($shipmentExpenseTransfer) { |
111
|
|
|
$amount = $amount - $shipmentExpenseTransfer->getUnitPriceToPayAggregation(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->moneyFacade->convertIntegerToDecimal($amount); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \Braintree\Transaction |
119
|
|
|
*/ |
120
|
|
|
protected function findTransaction(): Transaction |
121
|
|
|
{ |
122
|
|
|
return BraintreeTransaction::find($this->getTransactionIdentifier()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \Generated\Shared\Transfer\ExpenseTransfer|null |
127
|
|
|
*/ |
128
|
|
|
protected function findShipmentExpenseTransfer(): ?ExpenseTransfer |
129
|
|
|
{ |
130
|
|
|
foreach ($this->transactionMetaTransfer->getRefund()->getExpenses() as $expenseTransfer) { |
131
|
|
|
if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) { |
132
|
|
|
return $expenseTransfer; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths