1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\Dandomain\Pay; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The Handler handles the request from Dandomain, typically a POST request with the order specific parameters |
9
|
|
|
* and turns that request into a PaymentRequest object. |
10
|
|
|
* |
11
|
|
|
* Also it provides helper methods for checking checksums |
12
|
|
|
*/ |
13
|
|
|
class Handler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $sharedKey1; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $sharedKey2; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $checksum1; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $checksum2; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PaymentRequest |
37
|
|
|
*/ |
38
|
|
|
protected $paymentRequest; |
39
|
|
|
|
40
|
12 |
|
public function __construct(ServerRequestInterface $request, string $sharedKey1, string $sharedKey2) |
41
|
|
|
{ |
42
|
12 |
|
$paymentRequest = new PaymentRequest(); |
43
|
12 |
|
$paymentRequest->populateFromRequest($request); |
44
|
|
|
|
45
|
12 |
|
$this->paymentRequest = $paymentRequest; |
46
|
12 |
|
$this->sharedKey1 = $sharedKey1; |
47
|
12 |
|
$this->sharedKey2 = $sharedKey2; |
48
|
12 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns true if the checksum given from Dandomain matches the checksum we can compute. |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
3 |
|
public function checksumMatches(): bool |
56
|
|
|
{ |
57
|
3 |
|
return $this->paymentRequest->getApiKey() === $this->getChecksum1(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
3 |
|
public function getChecksum1() |
64
|
|
|
{ |
65
|
3 |
|
if (!$this->checksum1) { |
66
|
3 |
|
$this->checksum1 = static::generateChecksum1( |
67
|
3 |
|
$this->paymentRequest->getOrderId(), |
68
|
3 |
|
$this->paymentRequest->getTotalAmount(), |
69
|
3 |
|
$this->sharedKey1, |
70
|
3 |
|
$this->paymentRequest->getPaymentGatewayCurrencyCode() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $this->checksum1; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getChecksum2() |
81
|
|
|
{ |
82
|
3 |
|
if (!$this->checksum2) { |
83
|
3 |
|
$this->checksum2 = static::generateChecksum2( |
84
|
3 |
|
$this->paymentRequest->getOrderId(), |
85
|
3 |
|
$this->sharedKey2, |
86
|
3 |
|
$this->paymentRequest->getPaymentGatewayCurrencyCode() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return $this->checksum2; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int $orderId |
95
|
|
|
* @param float $amount |
96
|
|
|
* @param string $sharedKey |
97
|
|
|
* @param int $currency |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
6 |
|
public static function generateChecksum1(int $orderId, float $amount, string $sharedKey, int $currency): string |
102
|
|
|
{ |
103
|
|
|
// the amount needs to be formatted as a danish number, so we convert the float |
104
|
6 |
|
$amount = number_format($amount, 2, ',', ''); |
105
|
|
|
|
106
|
6 |
|
return strtolower(md5($orderId.'+'.$amount.'+'.$sharedKey.'+'.$currency)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Dandomain has a bug in their payment implementation where they don't |
111
|
|
|
* include amount in checksum on their complete/success page. |
112
|
|
|
* That is why we have a second method for computing that checksum. |
113
|
|
|
* |
114
|
|
|
* @param int $orderId |
115
|
|
|
* @param string $sharedKey |
116
|
|
|
* @param int $currency |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
3 |
|
public static function generateChecksum2(int $orderId, string $sharedKey, int $currency): string |
121
|
|
|
{ |
122
|
3 |
|
return strtolower(md5($orderId.'+'.$sharedKey.'+'.$currency)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return PaymentRequest |
127
|
|
|
*/ |
128
|
6 |
|
public function getPaymentRequest(): PaymentRequest |
129
|
|
|
{ |
130
|
6 |
|
return $this->paymentRequest; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param PaymentRequest $paymentRequest |
135
|
|
|
* |
136
|
|
|
* @return Handler |
137
|
|
|
*/ |
138
|
9 |
|
public function setPaymentRequest(PaymentRequest $paymentRequest): self |
139
|
|
|
{ |
140
|
9 |
|
$this->paymentRequest = $paymentRequest; |
141
|
|
|
|
142
|
9 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|