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