Passed
Push — feature/paypal-express ( aa383f...32ec15 )
by Volodymyr
05:03
created

RefundTransaction   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

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