Passed
Push — master ( df9d34...ce947e )
by
unknown
10:03
created

CompletePurchaseResponse::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Omnipay\SmartPay\Message;
4
5
use Omnipay\Common\Exception\InvalidResponseException;
6
use Omnipay\Common\Http\Exception;
7
use Omnipay\Common\Message\AbstractResponse;
8
use Omnipay\Common\Message\RequestInterface;
9
10
/**
11
 * Bank Muscat Smart Pay Complete Purchase Response
12
 */
13
class CompletePurchaseResponse extends AbstractResponse
14
{
15
    /**
16
     * Constructor
17
     *
18
     * @param RequestInterface $request the initiating request.
19
     * @param mixed $data
20
     *
21
     * @throws InvalidResponseException If merchant data or order number is missing, or signature does not match
22
     */
23
24
    protected $rawData;
25
26
    public function __construct(RequestInterface $request, $data)
27
    {
28
        parent::__construct($request, $data);
29
30
        foreach (['encResp', 'orderNo'] as $key) {
31
            if (empty($data[$key])) {
32
                throw new InvalidResponseException('Invalid response from payment gateway ['.$key.'] missing');
33
            }
34
        }
35
36
        try {
37
            $crypto = new Crypto();
38
39
            $decryptedString = $crypto->decrypt($this->data['encResp'], $this->getRequest()->getWorkingKey());
0 ignored issues
show
Bug introduced by
The method getWorkingKey() does not exist on Omnipay\Common\Message\RequestInterface. It seems like you code against a sub-type of Omnipay\Common\Message\RequestInterface such as Omnipay\SmartPay\Message\PurchaseRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $decryptedString = $crypto->decrypt($this->data['encResp'], $this->getRequest()->/** @scrutinizer ignore-call */ getWorkingKey());
Loading history...
40
        } catch (Exception $exception) {
41
            throw new InvalidResponseException('Invalid response from payment gateway');
42
        }
43
44
        if ( empty($decryptedString) ) {
45
            throw new InvalidResponseException('Invalid response from payment gateway');
46
        }
47
48
        $orderData = [];
49
50
        foreach (explode('&', $decryptedString) as $item) {
51
            [$key, $value] = explode('=', $item);
52
            $orderData[$key] = $value;
53
        }
54
55
        foreach (['order_id', 'tracking_id', 'status_message', 'order_status'] as $key) {
56
            if (empty($orderData[$key])) {
57
                throw new InvalidResponseException('Invalid response from payment gateway ['.$key.'] missing');
58
            }
59
        }
60
61
        $this->rawData = $data;
62
        $this->data = $orderData;
63
    }
64
65
    /**
66
     * Is the response successful?
67
     *
68
     * @return boolean
69
     */
70
    public function isSuccessful()
71
    {
72
        return $this->getOrderStatus() === 'Success';
73
    }
74
75
    /**
76
     * Get the order status if available.
77
     *
78
     * @return null|string
79
     */
80
    public function getOrderStatus()
81
    {
82
        return $this->getKey('order_status');
83
    }
84
85
86
    public function getMessage()
87
    {
88
        return $this->getKey('status_message');
89
    }
90
91
    /**
92
     * Get the transaction identifier if available.
93
     *
94
     * @return null|string
95
     */
96
    public function getTransactionReference()
97
    {
98
        return $this->getKey('tracking_id');
99
    }
100
101
    /**
102
     * Get the merchant-supplied transaction identifier if available.
103
     *
104
     * @return null|string
105
     */
106
    public function getTransactionId()
107
    {
108
        return $this->getKey('order_id');
109
    }
110
111
    /**
112
     * Get the card number if available.
113
     *
114
     * @return null|string
115
     */
116
    public function getCardNumber()
117
    {
118
        return $this->getKey('merchant_param6');
119
    }
120
121
    /**
122
     * Get the card type if available.
123
     *
124
     * @return null|string
125
     */
126
    public function getCardType()
127
    {
128
        return $this->getKey('card_type') ?? $this->getKey('card_name');
129
    }
130
131
    /**
132
     * Get the card expiry if available.
133
     *
134
     * @return null|string
135
     */
136
    public function getCardExpiry()
137
    {
138
        return $this->getKey('merchant_param7');
139
    }
140
141
142
    /**
143
     * Helper method to get a specific response parameter if available.
144
     *
145
     * @param string $key The key to look up
146
     *
147
     * @return null|mixed
148
     */
149
    protected function getKey($key)
150
    {
151
        return $this->data[$key] ?? null;
152
    }
153
}
154