-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCardlinkFormGenerator.php
More file actions
135 lines (120 loc) · 3.49 KB
/
CardlinkFormGenerator.php
File metadata and controls
135 lines (120 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
class CardlinkFormGenerator
{
private static $scheme = [
//NAME => isRequired
//GENERAL:
'version' => true,
'mid' => true,
'lang' => false,
'deviceCategory' => false,
'orderid' => true,
'orderDesc' => true,
'orderAmount' => true,
'currency' => true,
//Payer
'payerEmail' => false,
'payerPhone' => false,
//Bill
'billCountry' => true,
'billState' => true,
'billZip' => true,
'billCity' => true,
'billAddress' => true,
//Optional Description
'weight' => false,
'dimensions' => false,
'shipCountry' => false,
'shipState' => false,
'shipZip' => false,
'shipCity' => false,
'shipAddress' => false,
'addFraudScore' => false,
'maxPayRetries' => false,
'reject3dsU' => false,
'payMethod' => false,
'trType' => false,
'extInstallmentoffset' => false,
'extInstallmentperiod' => false,
'extRecurringfrequency' => false,
'extRecurringenddate' => false,
'blockScore' => false,
'cssUrl' => false,
//Callback
'confirmUrl' => true,
'cancelUrl' => true,
//Bonus
'var1' => false,
'var2' => false,
'var3' => false,
'var4' => false,
'var5' => false,
'var6' => false,
'var7' => false,
'var8' => false,
'var9' => false,
//Safety
'digest' => true
];
private $secret;
private $confirmUrl;
private $cancelUrl;
private $version;
private $mid;
/**
* CardlinkFormGenerator constructor.
* @param string $confirmUrl
* @param string $cancelUrl
* @param string $mid
* @param string $secret
*/
public function __construct(string $confirmUrl, string $cancelUrl, string $mid, string $version, string $secret = 'Cardlink1')
{
$this->confirmUrl = $confirmUrl;
$this->cancelUrl = $cancelUrl;
$this->secret = $secret;
$this->version = $version;
$this->mid = $mid;
}
/**
* @param array $userForm
* @return array
* @throws Exception
*/
public function generate(array $userForm) : array
{
$result = [];
foreach (self::$scheme as $key => $isRequired) {
$givenValue = $userForm[$key] ?? null;
switch ($key) {
case 'digest':
$result[$key] = $this->calculateDigest($result);
break;
case 'version':
case 'mid':
case 'confirmUrl':
case 'cancelUrl':
$result[$key] = $this->$key;
break;
default:
if ($givenValue == null && $isRequired) {
throw new Exception('Field ' . $key . ' is required!');
} elseif ($givenValue !== null) {
$result[$key] = $givenValue;
}
}
}
return $result;
}
/**
* @param array $data
* @return string
*/
private function calculateDigest(array $data) : string
{
$dataString = implode('', $data);
$dataString .= $this->secret;
$dataString =!mb_detect_encoding($dataString,'utf-8',true)?utf8_encode($dataString):$dataString;
return base64_encode(hash("sha256", utf8_encode($dataString), true));
}
}