1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* eCoin driver for Omnipay PHP payment library |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/omnipay-ecoin |
7
|
|
|
* @package omnipay-ecoin |
8
|
|
|
* @license MIT |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Omnipay\eCoin\Message; |
13
|
|
|
|
14
|
|
|
use Omnipay\Common\Exception\InvalidResponseException; |
15
|
|
|
use Omnipay\Common\Message\AbstractResponse; |
16
|
|
|
use Omnipay\Common\Message\RequestInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* eCoin Complete Purchase Response. |
20
|
|
|
*/ |
21
|
|
|
class CompletePurchaseResponse extends AbstractResponse |
22
|
|
|
{ |
23
|
3 |
|
public function __construct(RequestInterface $request, $data) |
24
|
|
|
{ |
25
|
3 |
|
$this->request = $request; |
26
|
3 |
|
$this->data = $data; |
27
|
|
|
|
28
|
3 |
|
if ($this->getHash() !== $this->calculateHash()) { |
29
|
1 |
|
throw new InvalidResponseException('Invalid hash'); |
30
|
|
|
} |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Whether the payment is successful. |
35
|
|
|
* @return boolean |
36
|
|
|
*/ |
37
|
1 |
|
public function isSuccessful() |
38
|
|
|
{ |
39
|
1 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Whether the payment is test. |
44
|
|
|
* XXX TODO. |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
1 |
|
public function getTestMode() |
48
|
|
|
{ |
49
|
1 |
|
return (bool) $this->data['TEST_VAR_TO_BE_SET']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
1 |
|
public function getTransactionId() |
57
|
|
|
{ |
58
|
1 |
|
return $this->data['ECM_INV_NO']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public function getTransactionReference() |
66
|
|
|
{ |
67
|
1 |
|
return $this->data['ECM_TRANS_ID']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function getAmount() |
75
|
|
|
{ |
76
|
1 |
|
return $this->data['ECM_ITEM_COST']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the currency. |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
1 |
|
public function getCurrency() |
84
|
|
|
{ |
85
|
1 |
|
return 'USD'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the payer ID. |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
public function getPayer() |
93
|
|
|
{ |
94
|
1 |
|
return $this->data['ECM_PAYER_ID']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the payment date. |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public function getTime() |
102
|
|
|
{ |
103
|
1 |
|
return date('c', $this->data['ECM_TRANS_DATE']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get hash from request. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
3 |
|
public function getHash() |
112
|
|
|
{ |
113
|
3 |
|
return $this->data['ECM_HASH']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Calculate hash to validate incoming confirmation. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
3 |
|
public function calculateHash() |
122
|
|
|
{ |
123
|
3 |
|
$str = $this->data['ECM_TRANS_ID'] . |
124
|
3 |
|
$this->data['ECM_TRANS_DATE'] . |
125
|
3 |
|
$this->request->getPurse() . |
|
|
|
|
126
|
3 |
|
$this->data['ECM_PAYER_ID'] . |
127
|
3 |
|
$this->data['ECM_ITEM_COST'] . |
128
|
3 |
|
$this->data['ECM_QTY'] . |
129
|
3 |
|
$this->request->getSecret(); |
|
|
|
|
130
|
|
|
|
131
|
3 |
|
return md5($str); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: