PurchaseRequest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 191
ccs 63
cts 63
cp 1
rs 10
c 0
b 0
f 0
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessToken() 0 3 1
A setMerchantId() 0 3 1
A getHttpMethod() 0 3 1
A setAccessToken() 0 3 1
A getPassword() 0 3 1
A setPassword() 0 3 1
A getMerchantId() 0 3 1
A getEndpoint() 0 3 2
A setCustomerId() 0 3 1
A getCustomerId() 0 3 1
A sendData() 0 5 1
A getData() 0 21 1
B sendRequest() 0 31 2
1
<?php
2
3
namespace Omnipay\OCBC\Message;
4
5
use Guzzle\Http\Message\Response as HttpResponse;
6
use Omnipay\Common\Message\AbstractRequest;
7
8
/**
9
 * OCBC Purchase Request
10
 */
11
class PurchaseRequest extends AbstractRequest
12
{
13
    /**
14
     * @var string  API endpoint base to connect to in production mode
15
     */
16
    protected $liveEndpoint = 'https://api.ocbc.com/transactional/merchantcardpayments/1.0';
17
    /**
18
     * @var string  API endpoint base to connect to in test mode
19
     */
20
    protected $testEndpoint = 'https://api.ocbc.com:8243/transactional/merchantcardpayments/1.0';
21
22
    /**
23
     * The HTTP method used to send data to the API endpoint
24
     *
25
     * @return string
26
     */
27 9
    public function getHttpMethod()
28
    {
29 9
        return 'POST';
30
    }
31
32
    /**
33
     * Get the stored access token
34
     *
35
     * @return string
36
     */
37 11
    public function getAccessToken()
38
    {
39 11
        return $this->getParameter('accessToken');
40
    }
41
42
    /**
43
     * Set the stored access token
44
     *
45
     * @param string $value  Access token to store
46
     */
47 3
    public function setAccessToken($value)
48
    {
49 3
        return $this->setParameter('accessToken', $value);
50
    }
51
52
    /**
53
     * Get the stored merchant ID
54
     *
55
     * @return string
56
     */
57 12
    public function getMerchantId()
58
    {
59 12
        return $this->getParameter('merchantId');
60
    }
61
62
    /**
63
     * Set the stored merchant ID
64
     *
65
     * @param string $value  Merchant ID to store
66
     */
67 14
    public function setMerchantId($value)
68
    {
69 14
        return $this->setParameter('merchantId', $value);
70
    }
71
72
    /**
73
     * Get the stored merchant password
74
     *
75
     * @return string
76
     */
77 12
    public function getPassword()
78
    {
79 12
        return $this->getParameter('password');
80
    }
81
82
    /**
83
     * Set the stored merchant password
84
     *
85
     * @param string $value  Merchant password to store
86
     */
87 14
    public function setPassword($value)
88
    {
89 14
        return $this->setParameter('password', $value);
90
    }
91
92
    /**
93
     * Get the stored customer ID
94
     *
95
     * @return string
96
     */
97 10
    public function getCustomerId()
98
    {
99 10
        return $this->getParameter('customerId');
100
    }
101
102
    /**
103
     * Set the stored customer ID
104
     *
105
     * @param string $value  Customer ID to store
106
     */
107 11
    public function setCustomerId($value)
108
    {
109 11
        return $this->setParameter('customerId', $value);
110
    }
111
112
    /**
113
     * Set up the base data for a purchase request
114
     *
115
     * Note: the gateway reference field 'transactionId' is omitted as it does not exist yet
116
     *
117
     * @return mixed[]
118
     */
119 10
    public function getData()
120
    {
121 10
        $this->validate('amount', 'card');
122
123 10
        $card = $this->getCard();
124
125
        $data = array(
126 10
            'merchantAccountNo' => $this->getMerchantId(),
127 10
            'merchantPassword' => $this->getPassword(),
128 10
            'cardHolderName' => $card->getName(),
129 10
            'cardNo' => $card->getNumber(),
130 10
            'cardCvc' => $card->getCvv(),
131 10
            'cardExpMm' => $card->getExpiryDate('m'),
132 10
            'cardExpYy' => $card->getExpiryDate('y'),
133 10
            'amount' => $this->getAmount(),
134 10
            'merchantTranId' => $this->getTransactionId(),
135 10
            'txnDesc' => $this->getDescription(),
136 10
            'customerId' => $this->getCustomerId(),
137 10
        );
138
139 10
        return $data;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 10
    public function getEndpoint()
146
    {
147 10
        return ($this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint).'/sale';
148
    }
149
150
    /**
151
     * Make the actual request to OCBC
152
     *
153
     * @param mixed $data  The data to encode and send to the API endpoint
154
     *
155
     * @return HttpResponse  HTTP response object
156
     */
157 9
    public function sendRequest($data)
158
    {
159 9
        $config = $this->httpClient->getConfig();
160 9
        $curlOptions = $config->get('curl.options');
161 9
        $curlOptions[CURLOPT_SSLVERSION] = 6;
162 9
        $config->set('curl.options', $curlOptions);
163 9
        $this->httpClient->setConfig($config);
164
165
        // don't throw exceptions for 4xx errors
166 9
        $this->httpClient->getEventDispatcher()->addListener(
167 9
            'request.error',
168 9
            function ($event) {
169 2
                if ($event['response']->isClientError()) {
170 2
                    $event->stopPropagation();
171 2
                }
172 2
            }
173 9
        );
174
175 9
        $httpRequest = $this->httpClient->createRequest(
176 9
            $this->getHttpMethod(),
177 9
            $this->getEndpoint(),
178 9
            null,
179 9
            json_encode($data)
180 9
        );
181
182
        $httpResponse = $httpRequest
183 9
            ->setHeader('Authorization', 'Bearer '.$this->getAccessToken())
184 9
            ->setHeader('Content-type', 'application/json')
185 9
            ->send();
186
187 9
        return $httpResponse;
188
    }
189
    
190
    /**
191
     * Send the request to the API then build the response object
192
     *
193
     * @param mixed $data  The data to encode and send to the API endpoint
194
     *
195
     * @return PurchaseResponse
196
     */
197 9
    public function sendData($data)
198
    {
199 9
        $httpResponse = $this->sendRequest($data);
200
201 9
        return $this->response = new PurchaseResponse($this, $httpResponse);
202
    }
203
}
204