This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjazzcash.php
315 lines (277 loc) · 8.47 KB
/
jazzcash.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
class jazzcash {
/**
* @var credentials
* Credentials
*/
private $_cred;
/**
* @var prefix constant
*/
public $_prefix = "JC_";
/**
* @var hash
*/
public $_hash = "SHA256";
/**
* @var data
*/
public $_data = [];
/**
* @var version
*/
public $_ver = '1.1';
/**
* @var urls
*/
public $purl = '/ApplicationAPI/API/';
/**
* @var isToken
*/
public $_isToken = false;
/**
* @var sandbox
*/
public $sandbox = true;
/**
* @var Request Type
*/
public $reqType = 'Authorize';
/**
* @var hashf Fields to be hashed
*/
public $hashF = [];
/**
* Initialize controller
*/
public function __construct()
{
$this->initCredentials();
}
/**
* Init Credentials
*
* @return void
*/
public function initCredentials()
{
$this->_cred = new \stdClass();
if( !defined($this->_prefix . 'SALT') || !defined($this->_prefix . 'PASS') || !defined($this->_prefix . 'MERCHANT_ID')) {
throw new \Exception('JazzCash configuration missing');
return false;
}
$this->_cred->salt = constant($this->_prefix . 'SALT');
$this->_cred->pass = constant($this->_prefix . 'PASS');
$this->_cred->mch = constant($this->_prefix . 'MERCHANT_ID');
return $this;
}
/**
* API URL Version
*
* @return String
*/
public function ver()
{
if ( $this->_ver >= 2 ) {
return $this->purl . $this->_ver . '/';
}
return $this->purl;
}
/**
* Action Methods
*
* @return string
*/
public function actionURL()
{
$endpoint = '';
switch( strtoupper($this->reqType) ) {
case 'PAY':
$endpoint = 'Purchase/PAY';
$this->hashF = ['pp_CustomerCardNumber', 'pp_CustomerCardExpiry', 'pp_CustomerCardCvv','pp_Amount', 'pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_TxnCurrency','pp_Frequency','pp_InstrumentType'];
break;
case 'CHECK3DSENROLLMENT':
$endpoint = $this->ver() . 'Purchase/Check3DsEnrollment';
$this->hashF = ['pp_CustomerCardNumber', 'pp_CustomerCardExpiry', 'pp_CustomerCardCvv','pp_Amount', 'pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_TxnCurrency','pp_Frequency','pp_InstrumentType'];
break;
case 'AUTHORIZE':
$endpoint = $this->ver() . 'authorize/AuthorizePayment';
$this->hashF = ['pp_CustomerCardNumber', 'pp_CustomerCardExpiry', 'pp_CustomerCardCvv','pp_Amount', 'pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_TxnCurrency','pp_Frequency','pp_InstrumentType'];
break;
case 'CAPTURE':
$endpoint = $this->ver() . 'authorize/Capture';
$this->hashF = ['pp_CustomerCardNumber', 'pp_CustomerCardExpiry', 'pp_CustomerCardCvv','pp_Amount', 'pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_TxnCurrency','pp_Frequency','pp_InstrumentType'];
break;
case 'REFUND':
$endpoint = $this->ver() . 'authorize/Refund';
$this->hashF = ['pp_CustomerCardNumber', 'pp_CustomerCardExpiry', 'pp_CustomerCardCvv','pp_Amount', 'pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_TxnCurrency','pp_Frequency','pp_InstrumentType'];
break;
case 'VOID':
$endpoint = $this->ver() . 'authorize/Void';
$this->hashF = ['pp_TxnRefNo', 'pp_MerchantID', 'pp_Password'];
break;
case 'PAYMENTINQUIRY':
$endpoint = $this->ver() . 'PaymentInquiry/Inquire';
$this->hashF = ['pp_TxnRefNo', 'pp_MerchantID', 'pp_Password', 'pp_Version'];
break;
}
if( $this->_isToken ) {
return $endpoint . 'ViaToken';
}
return $endpoint;
}
/**
* is Test
*
* @return string
*/
public function url()
{
$url = constant($this->_prefix . ($this->sandbox === false ? "LIVE" : "SANDBOX") . "_URL");
return $url . $this->actionURL();
}
/**
* Set Data
*
* @param Array/String
*
* @return void
*/
public function set_data($attr, $val = '')
{
if(!is_array($attr)) {
$this->_data[$attr] = $val;
} else {
foreach($attr as $a => $v) {
$this->_data[$a] = $v;
}
}
return true;
}
/**
* Get Data
*
* @param attr // to get single attribute
*
* @return string/array
*/
public function get_data($attr = false)
{
if($attr) {
return isset($this->_data[$attr]) ? $this->_data[$attr] : null;
}
return $this->_data;
}
public function genString($i, $array, $res = '')
{
if( is_array($array) ) {
foreach($array as $k => $a) {
if( is_array($a) ) {
$res = $this->genString($i, $a, $res);
} else if( $k == $i && !empty($a) && !is_null($a) ) {
$res = $a .'&';
}
}
}
return $res;
}
/**
* Generate Secure Hash
*
* Required Attributes
* @param string secret key
* @param int amount
* @param string merchant id
* @param mixed order info
*
* @return string
*/
public function secureHash()
{
$this->actionURL();
sort($this->hashF);
$f = '';
foreach($this->hashF as $h) {
$f .= $this->genString($h, $this->get_data());
}
$a = $this->_cred->salt ."&";
$b = $a . substr($f, 0, -1);
$hash = hash_hmac($this->_hash, $b, $this->_cred->salt);
return $hash;
}
/**
* Set Default attributes
*
* @return void
*/
public function loadDefaultAttr()
{
$this->set_data([
"pp_TxnCurrency" => "PKR",
"pp_MerchantID" => $this->_cred->mch,
"pp_Password" => $this->_cred->pass,
]);
if( strtoupper($this->reqType) == 'PAY') {
$this->set_data([
"pp_TxnType" => "MPAY",
"pp_Version" => $this->_ver,
]);
}
$this->set_data('pp_SecureHash', $this->secureHash());
if( $this->_ver < 2 && !in_array(strtoupper($this->reqType), ["PAY", "PAYMENTINQUIRY"])) {
$a = [];
$a[$this->reqType .'Request'] = $this->get_data();
$this->_data = $a;
}
return $this;
}
/**
* validate payload
*
* @return boolean
*/
public function validatePayload()
{
if( is_null($this->get_data('pp_TxnRefNo')) ) {
throw new \Exception('Transaction reference number is required');
return false;
} else if ( is_null($this->get_data('pp_Amount'))) {
throw new \Exception('Amount is missing');
return false;
} else if ( is_null($this->get_data('InstrumentDTO')) || ( !isset($this->get_data('InstrumentDTO')['pp_CustomerCardNumber']) || !isset($this->get_data('InstrumentDTO')['pp_CustomerCardExpiry']) || !isset($this->get_data('InstrumentDTO')['pp_CustomerCardCvv']) )) {
throw new \Exception('Card details missing');
return false;
}
return true;
}
/**
* Send Request
*
* @return json
*/
public function send()
{
if( !function_exists('curl_version') ) {
throw new \Exception("Please enable curl");
return false;
}
$this->loadDefaultAttr();
$req = curl_init();
// initiating request
curl_setopt($req, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($req, CURLOPT_URL, $this->url() );
curl_setopt($req, CURLOPT_POST, 1);
curl_setopt($req, CURLOPT_POSTFIELDS, json_encode( $this->get_data() ) );
curl_setopt($req, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Receive server response ...
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($req);
if (curl_errno($req)) {
$res = ['curl_error' => curl_error($req)];
}
curl_close ($req);
return $res;
}
}
?>