AbstractTransaction::getSuccessResponseTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 15
rs 9.8666
c 0
b 0
f 0
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\Configuration;
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\TransactionMetaTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\TransactionMetaTransfer 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 Orm\Zed\Braintree\Persistence\SpyPaymentBraintreeTransactionRequestLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Braintree\Persis...eeTransactionRequestLog 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 Orm\Zed\Braintree\Persistence\SpyPaymentBraintreeTransactionStatusLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Braintree\Persis...reeTransactionStatusLog 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 SprykerEco\Zed\Braintree\BraintreeConfig;
16
17
abstract class AbstractTransaction implements TransactionInterface
18
{
19
    /**
20
     * @var \Generated\Shared\Transfer\TransactionMetaTransfer
21
     */
22
    protected $transactionMetaTransfer;
23
24
    /**
25
     * @var \SprykerEco\Zed\Braintree\BraintreeConfig
26
     */
27
    protected $config;
28
29
    /**
30
     * @param \SprykerEco\Zed\Braintree\BraintreeConfig $config
31
     */
32
    public function __construct(BraintreeConfig $config)
33
    {
34
        $this->config = $config;
35
    }
36
37
    /**
38
     * @param \Generated\Shared\Transfer\TransactionMetaTransfer $transactionMetaTransfer
39
     *
40
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
41
     */
42
    public function executeTransaction(TransactionMetaTransfer $transactionMetaTransfer)
43
    {
44
        $this->initializeBraintree();
45
46
        $this->transactionMetaTransfer = $transactionMetaTransfer;
47
48
        $this->beforeTransaction();
49
        $response = $this->doTransaction();
50
        $transactionResponse = $this->afterTransaction($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Braintree\Transaction; however, parameter $response of SprykerEco\Zed\Braintree...ion::afterTransaction() does only seem to accept Braintree\Result\Error|Braintree\Result\Successful, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $transactionResponse = $this->afterTransaction(/** @scrutinizer ignore-type */ $response);
Loading history...
51
52
        return $transactionResponse;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    protected function getTransactionIdentifier()
59
    {
60
        return $this->transactionMetaTransfer->requireTransactionIdentifier()->getTransactionIdentifier();
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    protected function getIdPayment()
67
    {
68
        return $this->transactionMetaTransfer->requireIdPayment()->getIdPayment();
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    protected function beforeTransaction()
75
    {
76
        $this->logApiRequest(
77
            $this->getTransactionIdentifier(),
78
            $this->getTransactionType(),
79
            $this->getTransactionCode(),
80
            $this->getIdPayment()
0 ignored issues
show
Bug introduced by
$this->getIdPayment() of type string is incompatible with the type integer expected by parameter $idPayment of SprykerEco\Zed\Braintree...action::logApiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            /** @scrutinizer ignore-type */ $this->getIdPayment()
Loading history...
81
        );
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    abstract protected function getTransactionType();
88
89
    /**
90
     * @return string
91
     */
92
    abstract protected function getTransactionCode();
93
94
    /**
95
     * @return \Braintree\Result\Successful|\Braintree\Result\Error|\Braintree\Transaction
96
     */
97
    abstract protected function doTransaction();
98
99
    /**
100
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
101
     *
102
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
103
     */
104
    protected function afterTransaction($response)
105
    {
106
        if ($this->isTransactionSuccessful($response)) {
107
            $braintreeTransactionResponseTransfer = $this->getSuccessResponseTransfer($response);
108
            $this->logApiResponse($braintreeTransactionResponseTransfer, $this->getIdPayment(), $response->transaction->statusHistory);
0 ignored issues
show
Bug introduced by
$this->getIdPayment() of type string is incompatible with the type integer expected by parameter $idPayment of SprykerEco\Zed\Braintree...ction::logApiResponse(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            $this->logApiResponse($braintreeTransactionResponseTransfer, /** @scrutinizer ignore-type */ $this->getIdPayment(), $response->transaction->statusHistory);
Loading history...
109
110
            return $braintreeTransactionResponseTransfer;
111
        }
112
113
        $braintreeTransactionResponseTransfer = $this->getErrorResponseTransfer($response);
114
        $this->logApiResponse($braintreeTransactionResponseTransfer, $this->getIdPayment());
115
116
        return $braintreeTransactionResponseTransfer;
117
    }
118
119
    /**
120
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
121
     *
122
     * @return bool
123
     */
124
    protected function isTransactionSuccessful($response)
125
    {
126
        return $response->success;
127
    }
128
129
    /**
130
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
131
     *
132
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
133
     */
134
    protected function getSuccessResponseTransfer($response)
135
    {
136
        $transaction = $response->transaction;
137
        $braintreeTransactionResponseTransfer = $this->getResponseTransfer()
138
            ->setIsSuccess(true)
139
            ->setTransactionId($transaction->id)
140
            ->setCode($transaction->processorResponseCode)
141
            ->setMessage($transaction->processorResponseText)
142
            ->setProcessingTimestamp($transaction->createdAt->getTimestamp())
143
            ->setTransactionStatus($transaction->status)
144
            ->setTransactionType($transaction->type)
145
            ->setTransactionAmount($transaction->amount)
146
            ->setMerchantAccount($transaction->merchantAccountId);
147
148
        return $braintreeTransactionResponseTransfer;
149
    }
150
151
    /**
152
     * @param \Braintree\Result\Successful|\Braintree\Result\Error $response
153
     *
154
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
155
     */
156
    protected function getErrorResponseTransfer($response)
157
    {
158
        $braintreeTransactionResponseTransfer = $this->getResponseTransfer()
159
            ->setIsSuccess(false)
160
            ->setMessage($response->message);
161
162
        return $braintreeTransactionResponseTransfer;
163
    }
164
165
    /**
166
     * @return \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer
167
     */
168
    protected function getResponseTransfer()
169
    {
170
        $braintreeTransactionResponseTransfer = new BraintreeTransactionResponseTransfer();
171
        $braintreeTransactionResponseTransfer
172
            ->setTransactionId($this->getTransactionIdentifier())
173
            ->setTransactionCode($this->getTransactionCode());
174
175
        return $braintreeTransactionResponseTransfer;
176
    }
177
178
    /**
179
     * @param string $transactionId
180
     * @param string $transactionType
181
     * @param string $transactionCode
182
     * @param int $idPayment
183
     *
184
     * @return \Orm\Zed\Braintree\Persistence\SpyPaymentBraintreeTransactionRequestLog
185
     */
186
    protected function logApiRequest($transactionId, $transactionType, $transactionCode, $idPayment)
187
    {
188
        $logEntity = new SpyPaymentBraintreeTransactionRequestLog();
189
        $logEntity
190
            ->setTransactionId($transactionId)
191
            ->setTransactionType($transactionType)
192
            ->setTransactionCode($transactionCode)
193
            ->setFkPaymentBraintree($idPayment);
194
        $logEntity->save();
195
196
        return $logEntity;
197
    }
198
199
    /**
200
     * @param \Generated\Shared\Transfer\BraintreeTransactionResponseTransfer $responseTransfer
201
     * @param int $idPayment
202
     * @param array $logs
203
     *
204
     * @return void
205
     */
206
    protected function logApiResponse(BraintreeTransactionResponseTransfer $responseTransfer, $idPayment, array $logs = [])
207
    {
208
        if (count($logs) > 0) {
209
            $log = array_pop($logs);
210
            $responseTransfer
211
                ->setTransactionStatus($log->status)
212
                ->setTransactionAmount($log->amount)
213
                ->setProcessingTimestamp($log->timestamp->getTimestamp());
214
        }
215
216
        $logEntity = new SpyPaymentBraintreeTransactionStatusLog();
217
        $logEntity->fromArray($responseTransfer->toArray());
218
        $logEntity->setFkPaymentBraintree($idPayment);
219
        $logEntity->save();
220
    }
221
222
    /**
223
     * @return void
224
     */
225
    protected function initializeBraintree()
226
    {
227
        Configuration::environment($this->config->getEnvironment());
228
        Configuration::merchantId($this->config->getMerchantId());
229
        Configuration::publicKey($this->config->getPublicKey());
230
        Configuration::privateKey($this->config->getPrivateKey());
231
    }
232
}
233