Completed
Pull Request — master (#7)
by
unknown
11:29
created

RestAuthorizeRequest::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Omnipay\ZipPay\Message;
3
4
use Omnipay\ZipPay\ItemInterface;
5
use Omnipay\ZipPay\ItemTypeInterface;
6
use Omnipay\ZipPay\Message\RestAuthorizeResponse;
7
8
/**
9
 * Authorize Request
10
 *
11
 * @method Response send()
12
 */
13
class RestAuthorizeRequest extends AbstractRequest
14 6
{
15
    public function getEndpoint()
16 6
    {
17
        return parent::getEndpoint() . '/checkouts';
18
    }
19 6
20
    public function getHttpMethod()
21 6
    {
22
        return 'POST';
23
    }
24 6
25
    public function getFirstName()
26 6
    {
27
        return $this->getCard()->getFirstName();
28
    }
29
30
    public function setFirstName($value)
31
    {
32
        return $this->getCard()->setFirstName($value);
33
    }
34 6
35
    public function getLastName()
36 6
    {
37
        return $this->getCard()->getLastName();
38
    }
39
40
    public function setLastName($value)
41
    {
42
        return $this->getCard()->setLastName($value);
43
    }
44 6
45
    public function getEmail()
46 6
    {
47
        return $this->getCard()->getEmail();
48
    }
49
50
    public function setEmail($value)
51
    {
52
        return $this->getCard()->setEmail($value);
53
    }
54 6
55
    public function getReference()
56 6
    {
57
        return $this->getParameter('reference');
58
    }
59
60
    public function setReference($value)
61
    {
62
        return $this->setParameter('reference', $value);
63
    }
64 6
65
    public function getMeta()
66 6
    {
67
        return $this->getParameter('meta');
68
    }
69
70
    public function setMeta($value)
71
    {
72
        return $this->setParameter('meta', $value);
73
    }
74 6
75
    public function hasMetaData()
76 6
    {
77
        return !empty($this->getMeta());
78
    }
79 6
80
    public function hasShippingAddress()
81 6
    {
82 6
        return !empty($this->getShippingAddressLine1());
83 6
    }
84 4
85 2
    public function getData()
86
    {
87 6
        $this->validate(
88
            'amount',
89 6
            'currency',
90 6
            'returnUrl'
91 6
        );
92 6
93 6
        $data = $this->getBaseData();
94 6
95
        $data['shopper']['first_name'] = $this->getFirstName();
96 6
        $data['shopper']['last_name'] = $this->getLastName();
97
        $data['shopper']['email'] = $this->getEmail();
98
        $data['shopper']['billing_address'] = $this->getBillingAddress();
99
        $data['order'] = $this->getOrder();
100 6
        $data['config'] = $this->getConfig();
101
102
        if ($this->hasMetaData()) {
103 6
            $data['metadata'] = $this->getMetaData();
104
        }
105
106 6
        return $data;
107 6
    }
108 6
109 6
    public function getBillingAddress()
110 6
    {
111 6
        return [
112 6
            'line1' => $this->getBillingAddressLine1(),
113 2
            'city' => $this->getBillingAddressCity(),
114
            'state' => $this->getBillingAddressState(),
115
            'postal_code' => $this->getBillingAddressPostalCode(),
116 6
            'country' => $this->getBillingAddressCountry(),
117
            'first_name' => $this->getBillingAddressFirstName(),
118
            'last_name' => $this->getBillingAddressLastName(),
119 6
        ];
120 6
    }
121 6
122 6
    public function getOrder()
123 6
    {
124 2
        return [
125
            'reference' => $this->getReference(),
126
            'amount' => $this->getAmount(),
127 6
            'currency' => $this->getCurrency(),
128
            'shipping' => $this->getOrderShippingDetails(),
129 6
            'items' => $this->getOrderItems(),
130 6
        ];
131
    }
132 6
133
    public function getOrderItems()
134
    {
135
        $data = [];
136
        $items = $this->getItems();
137
138 6
        if ($items) {
139
            foreach ($items as $item) {
140
                $data[] = $this->convertItemToItemData($item);
141
            }
142
        }
143
144
        return $data;
145
    }
146
147
    protected function convertItemToItemData(ItemInterface $item)
148
    {
149
        return [
150
            'name' => $item->getName(),
151
            'amount' => $this->formatCurrency($item->getPrice()),
152
            'quantity' => $item->getQuantity(),
153 6
            'type' => $item instanceof ItemTypeInterface ? $item->getType() : 'sku',
154
            'reference' => $item->getReference(),
155
            'image_uri' => $item->getImageUri(),
156 6
        ];
157 2
    }
158
159
    public function getOrderShippingDetails()
160 6
    {
161
        $shippingDetails = [
162
            'pickup' => true,
163 6
        ];
164 2
        // Check for a shipping address and set pickup to `false` and add the address
165
        // details if one has been supplied.
166
        if ($this->hasShippingAddress()) {
167
            $shippingDetails = [
168
                'pickup' => false,
169
                'address' => [
170
                    'line1' => $this->getShippingAddressLine1(),
171
                    'line2' => $this->getShippingAddressLine2(),
172 6
                    'city' => $this->getShippingAddressCity(),
173
                    'state' => $this->getShippingAddressState(),
174 6
                    'postal_code' => $this->getShippingAddressPostalCode(),
175
                    'country' => $this->getShippingAddressCountry(),
176
                ],
177 6
            ];
178
        }
179 6
        return $shippingDetails;
180
    }
181
182 6
    public function getConfig()
183
    {
184 6
        return [
185
            'redirect_uri' => $this->getReturnUrl(),
186
        ];
187 6
    }
188
189 6
    public function getMetaData()
190
    {
191
        return $this->getMeta();
192 6
    }
193
194 6
    public function getBillingAddressLine1()
195
    {
196
        return $this->getCard()->getBillingAddress1();
197 6
    }
198
199 6
    public function getBillingAddressCity()
200
    {
201
        return $this->getCard()->getBillingCity();
202 6
    }
203
204 6
    public function getBillingAddressState()
205
    {
206
        return $this->getCard()->getBillingState();
207 6
    }
208
209 6
    public function getBillingAddressPostalCode()
210
    {
211
        return $this->getCard()->getBillingPostcode();
212
    }
213
214
    public function getBillingAddressCountry()
215
    {
216
        return $this->getCard()->getBillingCountry();
217
    }
218
219
    public function getBillingAddressFirstName()
220
    {
221
        return $this->getCard()->getBillingFirstName();
222
    }
223
224
    public function getBillingAddressLastName()
225
    {
226
        return $this->getCard()->getBillingLastName();
227
    }
228
229
    public function getShippingAddressLine1()
230
    {
231
        return $this->getCard()->getShippingAddress1();
232
    }
233
234
    public function getShippingAddressLine2()
235
    {
236
        return $this->getCard()->getShippingAddress2();
237
    }
238
239
    public function getShippingAddressCity()
240
    {
241
        return $this->getCard()->getShippingCity();
242
    }
243
244
    public function getShippingAddressState()
245
    {
246
        return $this->getCard()->getShippingState();
247
    }
248
249
    public function getShippingAddressPostalCode()
250
    {
251
        return $this->getCard()->getShippingPostcode();
252
    }
253
254
    public function getShippingAddressCountry()
255
    {
256
        return $this->getCard()->getShippingCountry();
257
    }
258
259
    protected function createResponse($data, $headers = [], $status = 404)
260
    {
261
        return $this->response = new RestAuthorizeResponse($this, $data, $headers, $status);
262
    }
263
}
264