Completed
Push — master ( e2eed5...73dfaa )
by Oleksandr
13s queued 10s
created

RefundItemsTransaction::getTransactionCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 as BraintreeTransaction;
11
use Braintree\Transaction;
12
use Generated\Shared\Transfer\BraintreeTransactionResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...sactionResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\PaymentBraintreeTransactionStatusLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...actionStatusLogTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Spryker\Shared\Shipment\ShipmentConstants;
16
use SprykerEco\Zed\Braintree\BraintreeConfig;
17
use SprykerEco\Zed\Braintree\Business\Payment\Method\ApiConstants;
18
use SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface;
19
use SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface;
20
use SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface;
21
22
class RefundItemsTransaction extends AbstractTransaction
23
{
24
    /**
25
     * @var \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface
26
     */
27
    protected $moneyFacade;
28
29
    /**
30
     * @var \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface
31
     */
32
    protected $shipmentRefundTransactionHandler;
33
34
    /**
35
     * @var \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface
36
     */
37
    protected $braintreeRepository;
38
39
    /**
40
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $brainTreeConfig
41
     * @param \SprykerEco\Zed\Braintree\Dependency\Facade\BraintreeToMoneyFacadeInterface $moneyFacade
42
     * @param \SprykerEco\Zed\Braintree\Business\Payment\Transaction\Handler\ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler
43
     * @param \SprykerEco\Zed\Braintree\Persistence\BraintreeRepositoryInterface $braintreeRepository
44
     */
45
    public function __construct(
46
        BraintreeConfig $brainTreeConfig,
47
        BraintreeToMoneyFacadeInterface $moneyFacade,
48
        ShipmentRefundTransactionHandlerInterface $shipmentRefundTransactionHandler,
49
        BraintreeRepositoryInterface $braintreeRepository
50
    ) {
51
        parent::__construct($brainTreeConfig);
52
        $this->moneyFacade = $moneyFacade;
53
        $this->shipmentRefundTransactionHandler = $shipmentRefundTransactionHandler;
54
        $this->braintreeRepository = $braintreeRepository;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    protected function getTransactionType(): string
61
    {
62
        return ApiConstants::CREDIT;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    protected function getTransactionCode(): string
69
    {
70
        return ApiConstants::TRANSACTION_CODE_REFUND;
71
    }
72
73
    /**
74
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
75
     */
76
    public function doTransaction()
77
    {
78
        return $this->refund();
79
    }
80
81
    /**
82
     * @param \Braintree\Result\Error|\Braintree\Result\Successful $response
83
     *
84
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
85
     */
86
    public function afterTransaction($response): BraintreeTransactionResponseTransfer
87
    {
88
        $shipmentExpenseTransfer = $this->findShipmentExpenseTransfer();
89
90
        if ($shipmentExpenseTransfer) {
91
            $this->refundShipmentExpense();
92
        }
93
94
        return parent::afterTransaction($response);
95
    }
96
97
    /**
98
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
99
     */
100
    protected function refund()
101
    {
102
        $transaction = $this->findTransaction();
103
104
        if ($transaction->status === ApiConstants::STATUS_CODE_CAPTURE_SUBMITTED) {
105
            return BraintreeTransaction::void($this->getTransactionIdentifier());
106
        }
107
108
        return BraintreeTransaction::refund(
109
            $this->getTransactionIdentifier(),
110
            $this->getAmount()
111
        );
112
    }
113
114
    /**
115
     * @return float
116
     */
117
    protected function getAmount(): float
118
    {
119
        return $this->moneyFacade->convertIntegerToDecimal($this->transactionMetaTransfer->getRefundAmount());
120
    }
121
122
    /**
123
     * @return \Braintree\Transaction
124
     */
125
    protected function findTransaction(): Transaction
126
    {
127
        return BraintreeTransaction::find($this->getTransactionIdentifier());
128
    }
129
130
    /**
131
     * @return \Generated\Shared\Transfer\ExpenseTransfer|null
132
     */
133
    protected function findShipmentExpenseTransfer(): ?ExpenseTransfer
134
    {
135
        foreach ($this->transactionMetaTransfer->getRefund()->getExpenses() as $expenseTransfer) {
136
            if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
137
                return $expenseTransfer;
138
            }
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @return \Generated\Shared\Transfer\PaymentBraintreeTransactionStatusLogTransfer|null
146
     */
147
    protected function findPaymentBraintreeTransactionStatusLogTransfer(): ?PaymentBraintreeTransactionStatusLogTransfer
148
    {
149
        return $this->braintreeRepository
150
            ->findTransactionRequestLogByIdSalesOrderForShipment($this->transactionMetaTransfer->getIdSalesOrder());
151
    }
152
153
    /**
154
     * @return void
155
     */
156
    protected function refundShipmentExpense(): void
157
    {
158
        $paymentBraintreeTransactionStatusLogTransfer = $this->findPaymentBraintreeTransactionStatusLogTransfer();
159
160
        if ($paymentBraintreeTransactionStatusLogTransfer) {
161
            $shipmentRefundTransitionMetaTransfer = clone $this->transactionMetaTransfer;
162
            $shipmentRefundTransitionMetaTransfer->setShipmentRefundTransactionId($paymentBraintreeTransactionStatusLogTransfer->getTransactionId());
163
            $shipmentRefundTransitionMetaTransfer->setRefundAmount($paymentBraintreeTransactionStatusLogTransfer->getTransactionAmount());
164
165
            $this->shipmentRefundTransactionHandler->refundShipment($shipmentRefundTransitionMetaTransfer);
166
        }
167
    }
168
}
169