Passed
Push — feature/paypal-express ( 8eac93...3cde97 )
by Volodymyr
05:19
created

RefundOrderTransaction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A doTransaction() 0 3 1
A __construct() 0 10 1
A getPaymentBraintreeTransactionStatusLogTransfer() 0 4 1
A getAmount() 0 16 3
A getTransactionCode() 0 3 1
A getShipmentExpenseTransfer() 0 9 3
A getTransactionType() 0 3 1
A refund() 0 11 2
A findTransaction() 0 3 1
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 RefundOrderTransaction 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
     * @return \Braintree\Result\Error|\Braintree\Result\Successful
82
     */
83
    protected function refund()
84
    {
85
        $transaction = $this->findTransaction();
86
87
        if ($transaction->status === ApiConstants::STATUS_CODE_CAPTURE_SUBMITTED) {
88
            return BraintreeTransaction::void($this->getTransactionIdentifier());
89
        }
90
91
        return BraintreeTransaction::refund(
92
            $this->getTransactionIdentifier(),
93
            $this->getAmount()
94
        );
95
    }
96
97
    /**
98
     * @return float|null
99
     */
100
    protected function getAmount()
101
    {
102
        $refundTransfer = $this->transactionMetaTransfer->requireRefund()->getRefund();
103
        $shipmentExpenseTransfer = $this->getShipmentExpenseTransfer();
104
105
        if ($refundTransfer->getAmount() === null) {
106
            return null;
107
        }
108
109
        $amount = $refundTransfer->getAmount();
110
111
        if ($shipmentExpenseTransfer) {
112
            $amount = $amount - $shipmentExpenseTransfer->getUnitPriceToPayAggregation();
113
        }
114
115
        return $this->moneyFacade->convertIntegerToDecimal($amount);
116
    }
117
118
    /**
119
     * @return \Braintree\Transaction
120
     */
121
    protected function findTransaction()
122
    {
123
        return BraintreeTransaction::find($this->getTransactionIdentifier());
124
    }
125
126
    /**
127
     * @return \Generated\Shared\Transfer\ExpenseTransfer|null
128
     */
129
    protected function getShipmentExpenseTransfer(): ?ExpenseTransfer
130
    {
131
        foreach ($this->transactionMetaTransfer->getRefund()->getExpenses() as $expenseTransfer) {
132
            if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
133
                return $expenseTransfer;
134
            }
135
        }
136
137
        return null;
138
    }
139
140
    /**
141
     * @return \Generated\Shared\Transfer\PaymentBraintreeTransactionStatusLogTransfer|null
142
     */
143
    protected function getPaymentBraintreeTransactionStatusLogTransfer(): ?PaymentBraintreeTransactionStatusLogTransfer
144
    {
145
        return $this->braintreeRepository
146
            ->findTransactionRequestLogByIdSalesOrderForShipment($this->transactionMetaTransfer->getIdSalesOrder());
147
    }
148
}
149