YaKassaPaymentForm::getMerchantReceipt()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 21
ccs 0
cts 16
cp 0
crap 12
rs 9.9
1
<?php
2
3
/**
4
 * This file is part of Ya.Kassa package.
5
 *
6
 * © Appwilio (http://appwilio.com)
7
 * © JhaoDa (https://github.com/jhaoda)
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
declare(strict_types=1);
13
14
namespace Appwilio\YaKassa;
15
16
use Appwilio\YaKassa\Contracts\YaKassaOrder;
17
use Appwilio\YaKassa\Contracts\YaKassaOrder54FZ;
18
use Appwilio\YaKassa\Contracts\YaKassaOrderItem54FZ;
19
20
class YaKassaPaymentForm
21
{
22
    /** @var string */
23
    private $paymentUrl;
24
25
    /** @var YaKassaOrder */
26
    private $order;
27
28
    /** @var array */
29
    private $parameters = [];
30
31
    public function __construct(string $shopId, string $showcaseId, string $paymentUrl)
32
    {
33
        $this->setParameter('shopId', $shopId);
34
        $this->setParameter('scid', $showcaseId);
35
36
        $this->paymentUrl = $paymentUrl;
37
    }
38
39
    public function setOrder(YaKassaOrder $order): self
40
    {
41
        $this->order = $order;
42
43
        return $this;
44
    }
45
46
    public function setFailUrl(string $failUrl): self
47
    {
48
        return $this->setParameter('shopFailURL', $failUrl);
49
    }
50
51
    public function setSuccessUrl(string $successUrl): self
52
    {
53
        return $this->setParameter('shopSuccessURL', $successUrl);
54
    }
55
56
    public function setDefaultUrl(string $defaultUrl): self
57
    {
58
        return $this->setParameter('shopDefaultUrl', $defaultUrl);
59
    }
60
61
    public function setParameter($key, $value): self
62
    {
63
        $this->parameters[$key] = $value;
64
65
        return $this;
66
    }
67
68
    public function getPaymentUrl(): string
69
    {
70
        return $this->paymentUrl;
71
    }
72
73
    public function toArray(): array
74
    {
75
        $this->setParameter('sum', number_format($this->order->getOrderSum(), 2, '.', ''));
76
        $this->setParameter('orderNumber', $this->order->getOrderNumber());
77
        $this->setParameter('customerNumber', $this->order->getCustomerNumber());
78
79
        $this->setParameter('cps_email', $this->order->getCustomerEmail());
80
        $this->setParameter('cps_phone', $this->order->getCustomerPhone());
81
82
        if ($pm = $this->order->getPaymentType()) {
83
            $this->setParameter('paymentType', $pm);
84
        }
85
86
        if ($this->order instanceof YaKassaOrder54FZ) {
87
            $this->setParameter('ym_merchant_receipt', json_encode(
88
                $this->getMerchantReceipt($this->order),
89
                JSON_UNESCAPED_UNICODE
90
            ));
91
        }
92
93
        return array_filter($this->parameters);
94
    }
95
96
    private function getMerchantReceipt(YaKassaOrder54FZ $order): array
97
    {
98
        $orderItems = $order->getItems();
99
100
        if ($orderItems instanceof \Traversable) {
101
            $orderItems = iterator_to_array($orderItems);
102
        }
103
104
        $receiptItems = [];
105
106
        foreach ($orderItems as $item) {
107
            $receiptItems[] = $this->convertItemToArray($item);
108
        }
109
110
        $receipt = [
111
            'items' => $receiptItems,
112
            'taxSystem' => $order->getTaxSystem(),
113
            'customerContact' => $order->getCustomerContact()
114
        ];
115
116
        return array_filter($receipt);
117
    }
118
119
    private function convertItemToArray(YaKassaOrderItem54FZ $item): array
120
    {
121
        return array_filter([
122
            'price' => [
123
                'amount' => number_format($item->getAmount(), 2, '.', '')
124
            ],
125
            'text' => mb_substr($item->getTitle(), 0, 127, 'UTF-8'),
126
            'tax' => $item->getTaxRate(),
127
            'quantity' => $this->formatQuantity($item->getQuantity()),
128
            'currency' => $item->getCurrency(),
129
        ]);
130
    }
131
132
    private function formatQuantity($quantity)
133
    {
134
        return is_int($quantity) ? $quantity : (float) number_format($quantity, 3, '.', '');
135
    }
136
}
137