AbstractRequest::setItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Omnipay\AllPay\Message;
4
5
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
6
7
abstract class AbstractRequest extends BaseAbstractRequest
8
{
9
    const API_VERSION = 'VER000000005';
10
    const ACQ_ID = '99020344';
11
    const CHARSET = 'UTF-8';
12
13
    protected $liveEndpoint = 'https://api.allpayx.com/api';
14
    protected $testEndpoint = 'https://testapi.allpayx.com/api';
15
16
    protected $apiEndpoint = '';
17
18 24
    public function sendData($data)
19
    {
20 24
        $data['signature'] = $this->sign($data);
21
22 24
        $data = http_build_query($data, '', '&');
23 24
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
24
25 24
        $httpResponse = $this->httpClient->request('POST', $this->getEndpoint(), $headers, $data);
26
//        dd($httpResponse->getBody()->getContents());
27 24
        return $this->createResponse($httpResponse->getBody()->getContents());
28
    }
29
30 30
    protected function getBaseData()
31
    {
32 30
        $this->validate('merchantId', 'transTime');
33
34
        return [
35 30
            'version' => static::API_VERSION,
36 30
            'charSet' => static::CHARSET,
37 30
            'acqID' => static::ACQ_ID,
38 30
            'signType' => 'MD5',
39 30
            'transTime' => $this->getTransTime(),
40 30
            'merID' => $this->getMerchantId()
41
        ];
42
    }
43
44 24
    protected function getEndpoint()
45
    {
46 24
        return ($this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint) . '/' . $this->apiEndpoint;
47
    }
48
49 48
    public function setMerchantId($value)
50
    {
51 48
        return $this->setParameter('merchantId', $value);
52
    }
53
54 39
    public function getMerchantId()
55
    {
56 39
        return $this->getParameter('merchantId');
57
    }
58
59 45
    public function setSignature($value)
60
    {
61 45
        return $this->setParameter('signature', $value);
62
    }
63
64 36
    public function getSignature()
65
    {
66 36
        return $this->getParameter('signature');
67
    }
68
69 48
    public function setTransTime($value)
70
    {
71 48
        return $this->setParameter('transTime', $value);
72
    }
73
74 39
    public function getTransTime()
75
    {
76 39
        return $this->getParameter('transTime');
77
    }
78
79 18
    public function setItems($value)
80
    {
81 18
        return $this->setParameter('items', $value);
82
    }
83
84 18
    public function getItems()
85
    {
86 18
        return $this->getParameter('items');
87
    }
88
89 27
    public function sign($data)
90
    {
91 27
        ksort($data);
92
93 27
        $str = '';
94
95 27
        foreach ($data as $key => $value) {
96 27
            $str .= sprintf('%s=%s', $key, $value) . '&';
97
        }
98
99 27
        $data = substr($str, 0, -1) . $this->getSignature();
100
101 27
        return md5($data);
102
    }
103
}
104