1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\Dandomain\Pay\Helper; |
4
|
|
|
|
5
|
|
|
use Loevgaard\Dandomain\Pay\Model\Payment; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The checksum helper will assist in creating checksums and also verifying checksums based on a payment |
9
|
|
|
*/ |
10
|
|
|
class ChecksumHelper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $sharedKey1; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $sharedKey2; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $checksum1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $checksum2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Payment |
34
|
|
|
*/ |
35
|
|
|
protected $payment; |
36
|
|
|
|
37
|
6 |
|
public function __construct(Payment $payment, string $sharedKey1, string $sharedKey2) |
38
|
|
|
{ |
39
|
6 |
|
$this->payment = $payment; |
40
|
6 |
|
$this->sharedKey1 = $sharedKey1; |
41
|
6 |
|
$this->sharedKey2 = $sharedKey2; |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns true if the checksum given from Dandomain matches the checksum we can compute. |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
3 |
|
public function checksumMatches(): bool |
50
|
|
|
{ |
51
|
3 |
|
return $this->payment->getApiKey() === $this->getChecksum1(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function getChecksum1() |
58
|
|
|
{ |
59
|
3 |
|
if (!$this->checksum1) { |
60
|
3 |
|
$this->checksum1 = static::generateChecksum1( |
61
|
3 |
|
$this->payment->getOrderId(), |
62
|
3 |
|
$this->payment->getTotalAmount(), |
63
|
3 |
|
$this->sharedKey1, |
64
|
3 |
|
$this->payment->getPaymentGatewayCurrencyCode() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return $this->checksum1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
3 |
|
public function getChecksum2() |
75
|
|
|
{ |
76
|
3 |
|
if (!$this->checksum2) { |
77
|
3 |
|
$this->checksum2 = static::generateChecksum2( |
78
|
3 |
|
$this->payment->getOrderId(), |
79
|
3 |
|
$this->sharedKey2, |
80
|
3 |
|
$this->payment->getPaymentGatewayCurrencyCode() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return $this->checksum2; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $orderId |
89
|
|
|
* @param float $amount |
90
|
|
|
* @param string $sharedKey |
91
|
|
|
* @param int $currency |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
6 |
|
public static function generateChecksum1(int $orderId, float $amount, string $sharedKey, int $currency): string |
96
|
|
|
{ |
97
|
|
|
// the amount needs to be formatted as a danish number, so we convert the float |
98
|
6 |
|
$amount = number_format($amount, 2, ',', ''); |
99
|
|
|
|
100
|
6 |
|
return strtolower(md5($orderId.'+'.$amount.'+'.$sharedKey.'+'.$currency)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Dandomain has a bug in their payment implementation where they don't |
105
|
|
|
* include amount in checksum on their complete/success page. |
106
|
|
|
* That is why we have a second method for computing that checksum. |
107
|
|
|
* |
108
|
|
|
* @param int $orderId |
109
|
|
|
* @param string $sharedKey |
110
|
|
|
* @param int $currency |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
3 |
|
public static function generateChecksum2(int $orderId, string $sharedKey, int $currency): string |
115
|
|
|
{ |
116
|
3 |
|
return strtolower(md5($orderId.'+'.$sharedKey.'+'.$currency)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|