CompletePurchaseResponse::__construct()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 22
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 32
ccs 24
cts 24
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace Omnipay\Redsys\Message;
4
5
use Omnipay\Common\Message\AbstractResponse;
6
use Omnipay\Common\Message\RequestInterface;
7
use Omnipay\Common\Exception\InvalidResponseException;
8
9
/**
10
 * Redsys Complete Purchase Response
11
 */
12
class CompletePurchaseResponse extends AbstractResponse
13
{
14
    /** @var array */
15
    protected $merchantParameters;
16
    /** @var string */
17
    protected $returnSignature;
18
    /** @var boolean */
19
    protected $usingUpcaseParameters = false;
20
    /** @var boolean */
21
    protected $usingUpcaseResponse = false;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param RequestInterface $request the initiating request.
27
     * @param mixed $data
28
     *
29
     * @throws InvalidResponseException If merchant data or order number is missing, or signature does not match
30
     */
31 10
    public function __construct(RequestInterface $request, $data)
32
    {
33 10
        parent::__construct($request, $data);
34
35 10
        $security = new Security;
36
37 10
        if (!empty($data['Ds_MerchantParameters'])) {
38 8
            $this->merchantParameters = $security->decodeMerchantParameters($data['Ds_MerchantParameters']);
39 10
        } elseif (!empty($data['DS_MERCHANTPARAMETERS'])) {
40 1
            $this->merchantParameters = $security->decodeMerchantParameters($data['DS_MERCHANTPARAMETERS']);
41 1
            $this->usingUpcaseResponse = true;
42 1
        } else {
43 1
            throw new InvalidResponseException('Invalid response from payment gateway (no data)');
44
        }
45
46 9
        if (!empty($this->merchantParameters['Ds_Order'])) {
47 7
            $order = $this->merchantParameters['Ds_Order'];
48 9
        } elseif (!empty($this->merchantParameters['DS_ORDER'])) {
49 1
            $order = $this->merchantParameters['DS_ORDER'];
50 1
            $this->usingUpcaseParameters = true;
51 1
        } else {
52 1
            throw new InvalidResponseException();
53
        }
54
55 8
        $this->returnSignature = $security->createReturnSignature(
56 8
            $data[$this->usingUpcaseResponse ? 'DS_MERCHANTPARAMETERS' : 'Ds_MerchantParameters'],
57 8
            $order,
58 8
            $this->request->getHmacKey()
0 ignored issues
show
Bug introduced by
The method getHmacKey() 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\Redsys\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

58
            $this->request->/** @scrutinizer ignore-call */ 
59
                            getHmacKey()
Loading history...
59 8
        );
60
61 8
        if ($this->returnSignature != $data[$this->usingUpcaseResponse ? 'DS_SIGNATURE' : 'Ds_Signature']) {
62 1
            throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)');
63
        }
64 7
    }
65
66
    /**
67
     * Is the response successful?
68
     *
69
     * @return boolean
70
     */
71 7
    public function isSuccessful()
72
    {
73 7
        $key = $this->usingUpcaseParameters ? 'DS_RESPONSE' : 'Ds_Response';
74 7
        return isset($this->merchantParameters[$key])
75 7
            && is_numeric($this->merchantParameters[$key])
76 7
            && 0 <= $this->merchantParameters[$key]
77 7
            && 100 > $this->merchantParameters[$key];
78
    }
79
80
    /**
81
     * Get the response data, included the decoded merchant parameters if available.
82
     *
83
     * @return mixed
84
     */
85 4
    public function getData()
86
    {
87 4
        $data = parent::getData();
88 4
        return is_array($data) && is_array($this->merchantParameters)
89 4
            ? array_merge($data, $this->merchantParameters)
90 4
            : $data;
91
    }
92
93
    /**
94
     * Helper method to get a specific merchant parameter if available.
95
     *
96
     * @param string $key The key to look up
97
     *
98
     * @return null|mixed
99
     */
100 7
    protected function getKey($key)
101
    {
102 7
        if ($this->usingUpcaseParameters) {
103 1
            $key = strtoupper($key);
104 1
        }
105 7
        return isset($this->merchantParameters[$key]) ? $this->merchantParameters[$key] : null;
106
    }
107
108
    /**
109
     * Get the authorisation code if available.
110
     *
111
     * @return null|string
112
     */
113 3
    public function getTransactionReference()
114
    {
115 3
        return $this->getKey('Ds_AuthorisationCode');
116
    }
117
118
    /**
119
     * Get the merchant response message if available.
120
     *
121
     * @return null|string
122
     */
123 7
    public function getMessage()
124
    {
125 7
        return $this->getKey('Ds_Response');
126
    }
127
128
    /**
129
     * Get the card type if available.
130
     *
131
     * @return null|string
132
     */
133 2
    public function getCardType()
134
    {
135 2
        return $this->getKey('Ds_Card_Type');
136
    }
137
}
138