Completed
Pull Request — master (#64)
by
unknown
06:13 queued 10s
created

PayoneRefundMethodSender   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 37
c 1
b 0
f 0
dl 0
loc 108
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateApiLogAfterRefund() 0 10 1
A refundPayment() 0 28 1
A __construct() 0 15 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Payment\MethodSender;
9
10
use Generated\Shared\Transfer\PayoneRefundTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneRefundTransfer 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...
11
use Generated\Shared\Transfer\PayoneStandardParameterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...andardParameterTransfer 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\RefundResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RefundResponseTransfer 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\Payone\Persistence\SpyPaymentPayoneApiLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog 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 SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer;
16
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\RefundResponseMapper;
17
use SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface;
18
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface;
19
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
20
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReader;
21
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
22
23
class PayoneRefundMethodSender extends AbstractPayoneMethodSender implements PayoneRefundMethodSenderInterface
24
{
25
    /**
26
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
27
     */
28
    protected $executionAdapter;
29
30
    /**
31
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
32
     */
33
    protected $standardParameter;
34
35
    /**
36
     * @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface[]
37
     */
38
    protected $registeredMethodMappers;
39
40
    /**
41
     * @var \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface
42
     */
43
    protected $orderPriceDistributor;
44
45
    /**
46
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
47
     */
48
    protected $standartParameterMapper;
49
50
    /**
51
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface
52
     */
53
    protected $payoneRequestProductDataMapper;
54
55
    /**
56
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
57
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
58
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
59
     * @param \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface $orderPriceDistributor
60
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
61
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper
62
     */
63
    public function __construct(
64
        AdapterInterface $executionAdapter,
65
        PayoneQueryContainerInterface $queryContainer,
66
        PaymentMapperReader $paymentMapperReader,
67
        PayoneStandardParameterTransfer $standardParameter,
68
        OrderPriceDistributorInterface $orderPriceDistributor,
69
        StandartParameterMapperInterface $standartParameterMapper,
70
        PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper
71
    ) {
72
        parent::__construct($queryContainer, $paymentMapperReader);
73
        $this->executionAdapter = $executionAdapter;
74
        $this->standardParameter = $standardParameter;
75
        $this->orderPriceDistributor = $orderPriceDistributor;
76
        $this->standartParameterMapper = $standartParameterMapper;
77
        $this->payoneRequestProductDataMapper = $payoneRequestProductDataMapper;
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\PayoneRefundTransfer $refundTransfer
82
     *
83
     * @return \Generated\Shared\Transfer\RefundResponseTransfer
84
     */
85
    public function refundPayment(PayoneRefundTransfer $refundTransfer): RefundResponseTransfer
86
    {
87
        $distributedPriceOrderTransfer = $this->orderPriceDistributor->distributeOrderPrice(
88
            $refundTransfer->getOrder()
89
        );
90
        $refundTransfer->setOrder($distributedPriceOrderTransfer);
91
92
        $payonePaymentTransfer = $refundTransfer->getPayment();
93
94
        $paymentEntity = $this->getPaymentEntity($payonePaymentTransfer->getFkSalesOrder());
95
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
96
        $requestContainer = $paymentMethodMapper->mapPaymentToRefund($paymentEntity);
97
        $requestContainer->setAmount(0 - $paymentEntity->getSpyPaymentPayoneDetail()->getAmount());
98
        $requestContainer = $this->payoneRequestProductDataMapper->mapData($refundTransfer->getOrder(), $requestContainer);
99
100
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
101
102
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
103
104
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
105
        $responseContainer = new RefundResponseContainer($rawResponse);
106
107
        $this->updateApiLogAfterRefund($apiLogEntity, $responseContainer);
108
109
        $responseMapper = new RefundResponseMapper();
110
        $responseTransfer = $responseMapper->getRefundResponseTransfer($responseContainer);
111
112
        return $responseTransfer;
113
    }
114
115
    /**
116
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
117
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer $responseContainer
118
     *
119
     * @return void
120
     */
121
    protected function updateApiLogAfterRefund(SpyPaymentPayoneApiLog $apiLogEntity, RefundResponseContainer $responseContainer)
122
    {
123
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
124
        $apiLogEntity->setStatus($responseContainer->getStatus());
125
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
126
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
127
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
128
129
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
130
        $apiLogEntity->save();
131
    }
132
}
133