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() === static::generateChecksum1( |
46
|
|
|
$this->paymentRequest->getOrderId(), |
47
|
|
|
$this->paymentRequest->getTotalAmount(), |
48
|
|
|
$this->sharedKey1, |
49
|
|
|
$this->paymentRequest->getCurrencySymbol() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $orderId |
55
|
|
|
* @param float $amount |
56
|
|
|
* @param string $sharedKey |
57
|
|
|
* @param string $currency |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public static function generateChecksum1(int $orderId, float $amount, string $sharedKey, string $currency) : string |
61
|
|
|
{ |
62
|
|
|
// the amount needs to be formatted as a danish number, so we convert the float |
63
|
|
|
$amount = number_format($amount, 2, ',', ''); |
64
|
|
|
return strtolower(md5($orderId.'+'.$amount.'+'.$sharedKey.'+'.$currency)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Dandomain has a bug in their payment implementation where they don't |
69
|
|
|
* include amount in checksum on their complete/success page |
70
|
|
|
* |
71
|
|
|
* @param int $orderId |
72
|
|
|
* @param string $sharedKey |
73
|
|
|
* @param string $currency |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public static function generateChecksum2(int $orderId, string $sharedKey, string $currency) : string |
77
|
|
|
{ |
78
|
|
|
return strtolower(md5($orderId.'+'.$sharedKey.'+'.$currency)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return PaymentRequest |
83
|
|
|
*/ |
84
|
|
|
public function getPaymentRequest() : PaymentRequest |
85
|
|
|
{ |
86
|
|
|
return $this->paymentRequest; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param PaymentRequest $paymentRequest |
91
|
|
|
* @return Handler |
92
|
|
|
*/ |
93
|
|
|
public function setPaymentRequest(PaymentRequest $paymentRequest) : self |
94
|
|
|
{ |
95
|
|
|
$this->paymentRequest = $paymentRequest; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|