Completed
Push — master ( 558fc2...aa58a2 )
by Vuong
01:31
created

PurchaseRequest::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @link https://github.com/phpviet/omnipay-momo
4
 * @copyright (c) PHP Viet
5
 * @license [MIT](http://www.opensource.org/licenses/MIT)
6
 */
7
8
namespace Omnipay\MoMo\Message\POS;
9
10
use Omnipay\MoMo\Message\AbstractHashRequest;
11
12
/**
13
 * @author Vuong Minh <[email protected]>
14
 * @since 1.0.0
15
 */
16
class PurchaseRequest extends AbstractHashRequest
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function initialize(array $parameters = [])
22
    {
23
        parent::initialize($parameters);
24
        $this->setParameter('payType', 3);
25
        $this->setParameter('version', 2);
26
27
        return $this;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     * @throws \Omnipay\Common\Exception\InvalidRequestException
33
     */
34
    public function getData(): array
35
    {
36
        $this->validate('paymentCode');
37
        $parameters = parent::getData();
38
        unset($parameters['paymentCode']);
39
40
        return $parameters;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function sendData($data): PurchaseResponse
47
    {
48
        $response = $this->httpClient->request('POST', $this->getEndpoint().'/pay/pos', [
49
            'Content-Type' => 'application/json; charset=utf-8',
50
        ], json_encode($data));
51
        $responseData = $response->getBody()->getContents();
52
53
        return $this->response = new PurchaseResponse($this, json_decode($responseData, true) ?? []);
54
    }
55
56
    /**
57
     * Trả về mã cửa hàng.
58
     *
59
     * @return null|string
60
     */
61
    public function getStoreId(): ?string
62
    {
63
        return $this->getParameter('storeId');
64
    }
65
66
    /**
67
     * Thiết lập mã cửa hàng.
68
     *
69
     * @param  string  $id
70
     * @return $this
71
     */
72
    public function setStoreId(string $id)
73
    {
74
        return $this->setParameter('storeId', $id);
75
    }
76
77
    /**
78
     * Trả về tên cửa hàng.
79
     *
80
     * @return null|string
81
     */
82
    public function getStoreName(): ?string
83
    {
84
        return $this->getParameter('storeName');
85
    }
86
87
    /**
88
     * Thiết lập tên cửa hàng.
89
     *
90
     * @param  string  $name
91
     * @return $this
92
     */
93
    public function setStoreName(string $name)
94
    {
95
        return $this->setParameter('storeName', $name);
96
    }
97
98
    /**
99
     * Trả về mã đơn hàng.
100
     *
101
     * @return null|string
102
     */
103
    public function getPartnerRefId(): ?string
104
    {
105
        return $this->getParameter('partnerRefId');
106
    }
107
108
    /**
109
     * Thiết lập mã đơn hàng.
110
     *
111
     * @param  string  $id
112
     * @return $this
113
     */
114
    public function setPartnerRefId(string $id)
115
    {
116
        return $this->setParameter('partnerRefId', $id);
117
    }
118
119
    /**
120
     * Trả về mã thanh toán.
121
     *
122
     * @return null|string
123
     */
124
    public function getPaymentCode(): ?string
125
    {
126
        return $this->getParameter('paymentCode');
127
    }
128
129
    /**
130
     * Thiết lập mã khách thanh toán.
131
     *
132
     * @param  string  $code
133
     * @return $this
134
     */
135
    public function setPaymentCode(string $code)
136
    {
137
        return $this->setParameter('paymentCode', $code);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    protected function getHashParameters(): array
144
    {
145
        $parameters = [
146
            'partnerCode', 'partnerRefId', 'amount', 'paymentCode',
147
        ];
148
149
        if ($this->getParameter('storeId')) {
150
            $parameters[] = 'storeId';
151
        }
152
153
        if ($this->getParameter('storeName')) {
154
            $parameters[] = 'storeName';
155
        }
156
157
        return $parameters;
158
    }
159
}
160