Issues (4)

src/Message/CompletePurchaseResponse.php (4 issues)

1
<?php
2
/**
3
 * Paxum driver for PHP merchant library
4
 *
5
 * @link      https://github.com/hiqdev/omnipay-paxum
6
 * @package   omnipay-paxum
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace Omnipay\Paxum\Message;
12
13
use DateTime;
14
use DateTimeZone;
15
use Omnipay\Common\Exception\InvalidResponseException;
16
use Omnipay\Common\Message\AbstractResponse;
17
use Omnipay\Common\Message\RequestInterface;
18
19
/**
20
 * Paxum Complete Purchase Response.
21
 */
22
class CompletePurchaseResponse extends AbstractResponse
23
{
24
    /**
25
     * @return AbstractRequest|RequestInterface
26
     */
27
    public function getRequest()
28
    {
29
        return parent::getRequest();
30
    }
31
32
    /**
33
     * @param RequestInterface $request
34
     * @param array $data
35 4
     * @throws InvalidResponseException when the request validation fails
36
     */
37 4
    public function __construct(RequestInterface $request, $data)
38 4
    {
39
        $this->request = $request;
40 4
        $this->data    = $data;
41 1
42
        if ($this->getTransactionStatus() !== 'done') {
43
            throw new InvalidResponseException('Transaction not done');
44 3
        }
45
46
        if ($this->getHash() !== $this->calculateHash()) {
47
            // echo "hashes: '" . $this->getHash() . "' - '" . $this->calculateHash() . "'\n";
48
            throw new InvalidResponseException('Invalid hash');
49 3
        }
50 1
51
        if ($this->request->getTestMode() !== $this->getTestMode()) {
52 2
            throw new InvalidResponseException('Invalid test mode');
53
        }
54
    }
55
56
    /**
57 1
     * {@inheritdoc}
58
     */
59 1
    public function isSuccessful()
60
    {
61
        return true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66 1
     * @return string
67
     */
68 1
    public function getTransactionId()
69
    {
70
        return $this->data['item_id'];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75 1
     * @return string
76
     */
77 1
    public function getTransactionReference()
78
    {
79
        return $this->data['transaction_id'];
80 4
    }
81
82 4
    public function getTransactionStatus()
83
    {
84
        return $this->data['transaction_status'];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89 1
     * @return string
90
     */
91 1
    public function getAmount()
92
    {
93
        return str_replace(',', '', $this->data['transaction_amount']);
94
    }
95
96
    /**
97
     * Get payment time.
98
     *
99 1
     * @return string
100
     */
101 1
    public function getTime()
102 1
    {
103
        $time = new DateTime($this->data['transaction_date'], new DateTimeZone('EST'));
104 1
        $time->setTimezone(new DateTimeZone('UTC'));
105
106
        return $time->format('c');
107
    }
108
109
    /**
110
     * Get payment currency.
111
     *
112 1
     * @return string
113
     */
114 1
    public function getCurrency()
115
    {
116
        return $this->data['transaction_currency'];
117
    }
118
119
    /**
120
     * Get test mode.
121
     *
122 3
     * @return string
123
     */
124 3
    public function getTestMode()
125
    {
126
        return $this->data['test'] === '1';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['test'] === '1' returns the type boolean which is incompatible with the documented return type string.
Loading history...
127
    }
128
129
    /**
130
     * Get payer info - name, username and id.
131
     *
132 1
     * @return string
133
     */
134 1
    public function getPayer()
135
    {
136
        return $this->data['buyer_name'] . '/' . $this->data['buyer_username'] . '/' . $this->data['buyer_id'];
137
    }
138
139
    /**
140
     * Get hash from request.
141
     *
142 3
     * @return string
143
     */
144 3
    public function getHash()
145
    {
146
        return $this->data['key'];
147
    }
148
149
    /**
150
     * Calculate hash to validate incoming IPN notifications.
151
     *
152 3
     * @return string
153
     */
154
    public function calculateHash()
155 3
    {
156 3
        // this is the documentation way
157 3
        $raw = file_get_contents('php://input');
158 3
        $fields = substr($raw, 0, strpos($raw, '&key='));
159
        $secret = $this->request->getSecret();
0 ignored issues
show
The method getSecret() 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\Paxum\Message\AbstractRequest. ( Ignorable by Annotation )

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

159
        /** @scrutinizer ignore-call */ 
160
        $secret = $this->request->getSecret();
Loading history...
160
        $supposed_hash = md5($fields . $secret);
0 ignored issues
show
The assignment to $supposed_hash is dead and can be removed.
Loading history...
161 3
162 3
        // this is how they actually get it
163 3
        $kvs = '';
164 3
        foreach ($this->data as $k => $v) {
165 3
            if ($k !== 'key' && $k !== 'username') {
166 3
                $kvs .= ($kvs ? '&' : '') . "$k=$v";
167 3
            }
168
        }
169
        $hash = md5($kvs);
0 ignored issues
show
The assignment to $hash is dead and can be removed.
Loading history...
170
171
        /* Testing facility
172
        dlog([
173
            'key'    => $this->getHash(),
174
            'fields' => $fields,
175
            'kvs'    => $kvs,
176
            'secret' => $secret,
177
            'hash'   => $hash,
178
            'h2'     => md5($fields),
179
            'h3'     => md5($fields . $secret),
180
            'kh3'    => md5($kvs),
181
            'kh4'    => md5($kvs . $secret),
182
        ]); */
183 3
184
        /// tmp fix
185
        return $this->getHash();
186
        //return $hash;
187
    }
188
}
189