forked from bookin/yii2-wallet-one
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTWalletOne.php
178 lines (153 loc) · 5.11 KB
/
TWalletOne.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Created by PhpStorm.
* User: Bookin
* Date: 08.12.2015
* Time: 23:36
*/
namespace bookin\walletone;
use yii\base\ErrorException;
use yii\helpers\Url;
trait TWalletOne
{
public $apiUrl = 'https://wl.walletone.com/checkout/checkout/Index';
public $secretKey = '';
public $buttonLabel = '';
public $walletOptions = [];
/**
* @var string Default sha1
*/
public $signatureMethod;
/**
* @throws ErrorException
*/
protected function checkOptions($options=[]){
$walletOptions = $options ?: $this->walletOptions;
$required = [
'WMI_MERCHANT_ID',
'WMI_PAYMENT_AMOUNT',
'WMI_CURRENCY_ID',
'WMI_DESCRIPTION',
'WMI_SUCCESS_URL',
'WMI_FAIL_URL'
];
if($walletOptions){
foreach($required as $key=>$val){
if(array_key_exists($val,$walletOptions)){
unset($required[$key]);
}
}
}
if($required){
throw new ErrorException('Error configuration WalletOne, need set - '.implode(', ',$required));
}
}
/**
* @param array $options this parameter will be merge with walletOptions
* @return $this
*/
public function addWalletOptions($options=[]){
$this->walletOptions = self::array_merge_recursive_distinct($options, $this->walletOptions);
return $this;
}
/**
* @param array $options
* @return array
* @throws ErrorException
*/
public function getFields($options=[]){
$fields = self::array_merge_recursive_distinct($options, $this->walletOptions);
$this->checkOptions($fields);
if(isset($fields['WMI_DESCRIPTION'])){
$fields['WMI_DESCRIPTION'] = 'BASE64:' . base64_encode($fields['WMI_DESCRIPTION']);
}
if(isset($fields['WMI_SUCCESS_URL']) && is_array($fields['WMI_SUCCESS_URL'])){
$fields['WMI_SUCCESS_URL'] = Url::toRoute($fields['WMI_SUCCESS_URL'], true);
}
if(isset($fields['WMI_FAIL_URL']) && is_array($fields['WMI_FAIL_URL'])){
$fields['WMI_FAIL_URL'] = Url::toRoute($fields['WMI_FAIL_URL'], true);
}
$fields['WMI_PAYMENT_AMOUNT'] = number_format($fields['WMI_PAYMENT_AMOUNT'], 2, '.', '');
if(isset($this->secretKey) && isset($this->signatureMethod)){
$signature = self::getSignature($this->secretKey, $fields, $this->signatureMethod);
$fields["WMI_SIGNATURE"] = $signature;
}
return $fields;
}
/**
* @param $secretKey
* @param array $data
* @param string $signatureMethod
* @return string
*/
protected static function getSignature($secretKey, $data, $signatureMethod = 'sha1'){
unset($data['WMI_SIGNATURE']);
foreach ($data as $name => $val) {
if (is_array($val)) {
usort($val, "strcasecmp");
$data[$name] = $val;
}
}
uksort($data, "strcasecmp");
$fieldValues = "";
foreach ($data as $value) {
if (is_array($value)) {
foreach ($value as $v) {
$v = iconv("utf-8", "windows-1251", $v);
$fieldValues .= urldecode($v);
}
} else {
$value = iconv("utf-8", "windows-1251", $value);
$fieldValues .= urldecode($value);
}
}
$signature = '';
if ($signatureMethod == 'md5') {
$signature = base64_encode(pack("H*", md5($fieldValues . $secretKey)));
}
if ($signatureMethod == 'sha1') {
$signature = base64_encode(pack("H*", sha1($fieldValues . $secretKey)));
}
return $signature;
}
/**
* @param $secretKey
* @param $data
* @param string $signatureMethod
* @return bool
* @throws ErrorException
*/
public static function checkSignature($secretKey, $data, $signatureMethod = 'sha1'){
if($secretKey && !$signatureMethod){
throw new WalletOneException('Need set signature method');
}
if(is_null($secretKey) || is_null($signatureMethod)){
return true;
}else if(in_array($signatureMethod,['sha1','md5'])){
if (!is_array($data) or !isset($data) or is_null($data)) {
return false;
}
$signature = self::getSignature($secretKey, $data, $signatureMethod);
if($signature == $data["WMI_SIGNATURE"]){
return true;
}
}
return false;
}
public static function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
$merged = $array1;
foreach ( $array2 as $key => &$value )
{
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
{
$merged [$key] = self::array_merge_recursive_distinct ( $merged [$key], $value );
}
else
{
$merged [$key] = $value;
}
}
return $merged;
}
}