Response::isPending()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace Omnipay\Pelecard\Message;
3
4
use Omnipay\Common\Message\AbstractResponse;
5
use Omnipay\Common\Message\RequestInterface;
6
use Guzzle\Http\ClientInterface;
7
use Guzzle\Http\Client as HttpClient;
8
9
/**
10
 * Response
11
 */
12
class Response extends AbstractResponse
13
{
14
15
    public function __construct(RequestInterface $request, $data)
16
    {
17
        $this->request = $request;
18
        $this->data = $data;
19
    }
20
21
    public function isSuccessful()
22
    {
23
        if (isset($this->data['StatusCode']) && $this->data['StatusCode'] === '000') {
24
            // do confirmation as suggested by the api documentation ValidateByUniqueKey.
25
            $url = 'https://gateway20.pelecard.biz/PaymentGW/ValidateByUniqueKey';
26
            //The parameter name in the confirmation JSON is UniqueKey because in case there is no UserKey (was not sent in the initial JSON) you can perform confirmation using TransactionId instead.
27
            $request = [
28
                "ConfirmationKey" => $this->data['ResultData']['ConfirmationKey'],
29
                "UniqueKey" => $this->request->getTransactionId()?$this->request->getTransactionId():$this->data['ResultData']['TransactionId'],
30
                "TotalX100" => $this->data['ResultData']['DebitTotal']
31
            ];
32
            $httpClient = new HttpClient('', array(
33
                'curl.options' => array(
34
                    CURLOPT_CONNECTTIMEOUT => 60
35
                )
36
            ));
37
            $httpRequest = $httpClient->post($url, [
38
                'Content-Type' => 'application/json; charset=utf-8',
39
                'Accept' => 'application/json',
40
                'json' => json_encode($request)
41
            ], json_encode($request));
42
            $httpResponse = $httpRequest->send();
43
            return $httpResponse->json() == 1;
44
        } else
45
            return false;
46
    }
47
48
    public function isCancelled()
49
    {
50
        if (isset($this->data['StatusCode']) && $this->data['StatusCode'] === '000') {
51
            return false;
52
        }
53
        return true;
54
    }
55
56
    public function isPending()
57
    {
58
        return false;
59
    }
60
61
    public function getTransactionReference()
62
    {
63
        if (isset($this->data['URL']) && ! empty($this->data['URL'])) {
64
            $url = parse_url($this->data['URL']);
65
            if (! empty($url['query'])) {
66
                parse_str($url['query'], $query);
67
                if (! empty($query['transactionId'])) {
68
                    return $query['transactionId'];
69
                }
70
            }
71
        }
72
        throw new \Exception('Unable to parse query to extract transaction reference.');
73
    }
74
75
    public function getRedirectUrl()
76
    {
77
        if (isset($this->data['URL'])) {
78
            return $this->data['URL'];
79
        }
80
    }
81
82
    public function isRedirect()
83
    {
84
        if (isset($this->data['URL']) && ! empty($this->data['URL'])) {
85
            return true;
86
        }
87
        return false;
88
    }
89
90
    public function getMessage()
91
    {
92
        return $this->data['Error']['ErrMsg'];
93
    }
94
}
95