Saver::getPaymentPriceToPay()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 1
dl 0
loc 15
rs 9.2222
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\AfterPay\Business\Order;
9
10
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer 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\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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\SaveOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SaveOrderTransfer 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\AfterPay\Persistence\SpyPaymentAfterPay;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay 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\AfterPay\Persistence\SpyPaymentAfterPayOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\AfterPay\Persist...aymentAfterPayOrderItem 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\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
16
use SprykerEco\Shared\AfterPay\AfterPayConfig as SharedAfterPayConfig;
17
use SprykerEco\Zed\AfterPay\AfterPayConfig;
18
19
class Saver implements SaverInterface
20
{
21
    use TransactionTrait;
22
23
    /**
24
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
30
     */
31
    public function __construct(AfterPayConfig $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
38
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
39
     *
40
     * @return void
41
     */
42
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer): void
43
    {
44
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== SharedAfterPayConfig::PROVIDER_NAME) {
45
            return;
46
        }
47
48
        $this->getTransactionHandler()->handleTransaction(function () use ($quoteTransfer, $saveOrderTransfer) {
49
            $this->executeSavePaymentForOrderAndItemsTransaction($quoteTransfer, $saveOrderTransfer);
50
        });
51
    }
52
53
    /**
54
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
55
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
56
     *
57
     * @return void
58
     */
59
    protected function executeSavePaymentForOrderAndItemsTransaction(
60
        QuoteTransfer $quoteTransfer,
61
        SaveOrderTransfer $saveOrderTransfer
62
    ): void {
63
        $paymentEntity = $this->buildPaymentEntity($quoteTransfer, $saveOrderTransfer);
64
        $paymentEntity->save();
65
66
        $idPayment = $paymentEntity->getIdPaymentAfterPay();
67
68
        foreach ($saveOrderTransfer->getOrderItems() as $orderItem) {
69
            $this->savePaymentForOrderItem($orderItem, $idPayment);
70
        }
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\ItemTransfer $orderItemTransfer
75
     * @param int $idPayment
76
     *
77
     * @return void
78
     */
79
    protected function savePaymentForOrderItem(ItemTransfer $orderItemTransfer, int $idPayment): void
80
    {
81
        $paymentOrderItemEntity = new SpyPaymentAfterPayOrderItem();
82
        $paymentOrderItemEntity
83
            ->setFkPaymentAfterPay($idPayment)
84
            ->setFkSalesOrderItem($orderItemTransfer->getIdSalesOrderItem());
85
86
        $paymentOrderItemEntity->save();
87
    }
88
89
    /**
90
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
91
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
92
     *
93
     * @return \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay
94
     */
95
    protected function buildPaymentEntity(
96
        QuoteTransfer $quoteTransfer,
97
        SaveOrderTransfer $saveOrderTransfer
98
    ): SpyPaymentAfterPay {
99
        $paymentEntity = new SpyPaymentAfterPay();
100
101
        $paymentTransfer = $quoteTransfer->getPayment();
102
103
        $paymentEntity
104
            ->setFkSalesPayment($paymentTransfer->getIdSalesPayment())
105
            ->setPaymentMethod($paymentTransfer->getPaymentMethod())
106
            ->setFkSalesOrder($saveOrderTransfer->getIdSalesOrder())
107
            ->setIdCheckout($this->getIdCheckout($quoteTransfer))
108
            ->setIdChannel($this->getIdChannel($paymentTransfer->getPaymentMethod()))
109
            ->setInfoscoreCustomerNumber($paymentTransfer->getAfterPayCustomerNumber())
110
            ->setExpenseTotal($quoteTransfer->getTotals()->getExpenseTotal())
111
            ->setGrandTotal($this->getPaymentPriceToPay($quoteTransfer));
112
113
        $quoteTransfer->setOrderReference($saveOrderTransfer->getOrderReference());
114
        $quoteTransfer->getPayment()->setIdSalesOrder($saveOrderTransfer->getIdSalesOrder());
115
116
        return $paymentEntity;
117
    }
118
119
    /**
120
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
121
     *
122
     * @return int
123
     */
124
    protected function getPaymentPriceToPay(QuoteTransfer $quoteTransfer): int
125
    {
126
        if ($quoteTransfer->getPayment() && $quoteTransfer->getPayment()->getAmount()) {
127
            return $quoteTransfer->getPayment()->getAmount();
128
        }
129
130
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
131
            if ($paymentTransfer->getPaymentMethod() !== SharedAfterPayConfig::PROVIDER_NAME || !$paymentTransfer->getAmount()) {
132
                continue;
133
            }
134
135
            return $paymentTransfer->getAmount();
136
        }
137
138
        return $quoteTransfer->getTotals()->getGrandTotal();
139
    }
140
141
    /**
142
     * @param string $paymentMethod
143
     *
144
     * @return string
145
     */
146
    protected function getIdChannel(string $paymentMethod): string
147
    {
148
        return $this->config->getPaymentChannelId($paymentMethod);
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
153
     *
154
     * @return string|null
155
     */
156
    protected function getIdCheckout(QuoteTransfer $quoteTransfer): ?string
157
    {
158
        if ($quoteTransfer->getAfterPayAvailablePaymentMethods()) {
159
            return $quoteTransfer->getAfterPayAvailablePaymentMethods()->getCheckoutId();
160
        }
161
162
        return null;
163
    }
164
}
165