1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
#****************************************************************************** |
4
|
|
|
#* Name : PxPay_Curl.inc.php |
5
|
|
|
#* Description : Classes used interact with the PxPay interface using PHP with the cURL extension installed |
6
|
|
|
#* Copyright : Payment Express 2017(c) |
7
|
|
|
#* Date : 2017-04-10 |
8
|
|
|
#*@version : 2.0 |
9
|
|
|
#* Author : Payment Express DevSupport |
10
|
|
|
#****************************************************************************** |
11
|
|
|
# Use this class to parse an XML document |
12
|
|
|
class MifMessage |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
public $xml_; |
15
|
|
|
public $xml_index_; |
16
|
|
|
public $xml_value_; |
17
|
|
|
|
18
|
|
|
# Constructor: |
19
|
|
|
# Create a MifMessage with the specified XML text. |
20
|
|
|
# The constructor returns a null object if there is a parsing error. |
21
|
|
|
public function __construct($xml) |
22
|
|
|
{ |
23
|
|
|
$p = xml_parser_create(); |
24
|
|
|
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); |
25
|
|
|
$ok = xml_parse_into_struct($p, $xml, $value, $index); |
26
|
|
|
xml_parser_free($p); |
27
|
|
|
if ($ok) { |
28
|
|
|
$this->xml_ = $xml; |
29
|
|
|
$this->xml_value_ = $value; |
30
|
|
|
$this->xml_index_ = $index; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
# Return the value of the specified top-level attribute. |
35
|
|
|
# This method can only return attributes of the root element. |
36
|
|
|
# If the attribute is not found, return "". |
37
|
|
|
public function get_attribute($attribute) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$attributes = $this->xml_value_[0]["attributes"]; |
40
|
|
|
return $attributes[$attribute]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
# Return the text of the specified element. |
44
|
|
|
# The element is given as a simplified XPath-like name. |
45
|
|
|
# For example, "Link/ServerOk" refers to the ServerOk element |
46
|
|
|
# nested in the Link element (nested in the root element). |
47
|
|
|
# If the element is not found, return "". |
48
|
|
|
public function get_element_text($element) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$index = $this->get_element_index($element, 0); |
51
|
|
|
if ($index == 0) { |
52
|
|
|
return ""; |
53
|
|
|
} else { |
54
|
|
|
#When element existent but empty |
55
|
|
|
$elementObj = $this->xml_value_[$index]; |
56
|
|
|
if (! array_key_exists("value", $elementObj)) { |
57
|
|
|
return ""; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->xml_value_[$index]["value"]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
# (internal method) |
65
|
|
|
# Return the index of the specified element, |
66
|
|
|
# relative to some given root element index. |
67
|
|
|
# |
68
|
|
|
public function get_element_index($element, $rootindex = 0) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
#$element = strtoupper($element); |
|
|
|
|
71
|
|
|
$pos = strpos($element, "/"); |
72
|
|
|
if ($pos !== false) { |
73
|
|
|
# element contains '/': find first part |
74
|
|
|
$start_path = substr($element, 0, $pos); |
75
|
|
|
$remain_path = substr($element, $pos+1); |
76
|
|
|
$index = $this->get_element_index($start_path, $rootindex); |
77
|
|
|
if ($index == 0) { |
78
|
|
|
# couldn't find first part give up. |
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
# recursively find rest |
82
|
|
|
return $this->get_element_index($remain_path, $index); |
83
|
|
|
} else { |
84
|
|
|
# search from the parent across all its children |
85
|
|
|
# i.e. until we get the parent's close tag. |
86
|
|
|
$level = $this->xml_value_[$rootindex]["level"]; |
87
|
|
|
if ($this->xml_value_[$rootindex]["type"] == "complete") { |
88
|
|
|
return 0; # no children |
89
|
|
|
} |
90
|
|
|
$index = $rootindex+1; |
91
|
|
|
while ($index<count($this->xml_value_) && |
92
|
|
|
!($this->xml_value_[$index]["level"]==$level && |
93
|
|
|
$this->xml_value_[$index]["type"]=="close")) { |
94
|
|
|
# if one below parent and tag matches, bingo |
95
|
|
|
if ($this->xml_value_[$index]["level"] == $level+1 && |
96
|
|
|
$this->xml_value_[$index]["tag"] == $element) { |
97
|
|
|
return $index; |
98
|
|
|
} |
99
|
|
|
$index++; |
100
|
|
|
} |
101
|
|
|
return 0; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
class PxPay_Curl |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
public $PxPay_Key; |
109
|
|
|
public $PxPay_Url; |
110
|
|
|
public $PxPay_Userid; |
111
|
|
|
public function __construct($Url, $UserId, $Key) |
112
|
|
|
{ |
113
|
|
|
error_reporting(E_ERROR); |
114
|
|
|
$this->PxPay_Key = $Key; |
115
|
|
|
$this->PxPay_Url = $Url; |
116
|
|
|
$this->PxPay_Userid = $UserId; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
#****************************************************************************** |
120
|
|
|
# Create a request for the PxPay interface |
121
|
|
|
#****************************************************************************** |
122
|
|
|
public function makeRequest($request) |
123
|
|
|
{ |
124
|
|
|
#Validate the Request |
125
|
|
|
if ($request->validData() == false) { |
126
|
|
|
return "" ; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$request->setUserId($this->PxPay_Userid); |
130
|
|
|
$request->setKey($this->PxPay_Key); |
131
|
|
|
|
132
|
|
|
$xml = $request->toXml(); |
133
|
|
|
|
134
|
|
|
$result = $this->submitXml($xml); |
135
|
|
|
|
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
#****************************************************************************** |
140
|
|
|
# Return the transaction outcome details |
141
|
|
|
#****************************************************************************** |
142
|
|
|
public function getResponse($result) |
143
|
|
|
{ |
144
|
|
|
$inputXml = "<ProcessResponse><PxPayUserId>".$this->PxPay_Userid."</PxPayUserId><PxPayKey>".$this->PxPay_Key. |
145
|
|
|
"</PxPayKey><Response>".$result."</Response></ProcessResponse>"; |
146
|
|
|
|
147
|
|
|
$outputXml = $this->submitXml($inputXml); |
148
|
|
|
|
149
|
|
|
$pxresp = new PxPayResponse($outputXml); |
150
|
|
|
return $pxresp; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
#****************************************************************************** |
154
|
|
|
# Actual submission of XML using cURL. Returns output XML |
155
|
|
|
#****************************************************************************** |
156
|
|
|
public function submitXml($inputXml) |
157
|
|
|
{ |
158
|
|
|
$ch = curl_init(); |
159
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->PxPay_Url); |
160
|
|
|
|
161
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
162
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $inputXml); |
163
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
164
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
165
|
|
|
|
166
|
|
|
#set up proxy, this may change depending on ISP, please contact your ISP to get the correct cURL settings |
167
|
|
|
#curl_setopt($ch,CURLOPT_PROXY , "proxy:8080"); |
|
|
|
|
168
|
|
|
#curl_setopt($ch,CURLOPT_PROXYUSERPWD,"username:password"); |
|
|
|
|
169
|
|
|
|
170
|
|
|
$outputXml = curl_exec($ch); |
171
|
|
|
|
172
|
|
|
curl_close($ch); |
173
|
|
|
|
174
|
|
|
return $outputXml; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
#****************************************************************************** |
179
|
|
|
# Class for PxPay request messages. |
180
|
|
|
#****************************************************************************** |
181
|
|
|
class PxPayRequest extends PxPayMessage |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
public $UrlFail; |
184
|
|
|
public $UrlSuccess; |
185
|
|
|
public $AmountInput; |
186
|
|
|
public $EnableAddBillCard; |
187
|
|
|
public $PxPayUserId; |
188
|
|
|
public $PxPayKey; |
189
|
|
|
public $Opt; |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
#Constructor |
193
|
|
|
public function __construct() |
194
|
|
|
{ |
195
|
|
|
parent::__construct(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function setEnableAddBillCard($EnableBillAddCard) |
199
|
|
|
{ |
200
|
|
|
$this->EnableAddBillCard = $EnableBillAddCard; |
201
|
|
|
} |
202
|
|
|
public function setUrlFail($UrlFail) |
203
|
|
|
{ |
204
|
|
|
$this->UrlFail = $UrlFail; |
205
|
|
|
} |
206
|
|
|
public function setUrlSuccess($UrlSuccess) |
207
|
|
|
{ |
208
|
|
|
$this->UrlSuccess = $UrlSuccess; |
209
|
|
|
} |
210
|
|
|
public function setAmountInput($AmountInput) |
211
|
|
|
{ |
212
|
|
|
$this->AmountInput = sprintf("%9.2f", $AmountInput); |
213
|
|
|
} |
214
|
|
|
public function setUserId($UserId) |
215
|
|
|
{ |
216
|
|
|
$this->PxPayUserId = $UserId; |
217
|
|
|
} |
218
|
|
|
public function setKey($Key) |
219
|
|
|
{ |
220
|
|
|
$this->PxPayKey = $Key; |
221
|
|
|
} |
222
|
|
|
public function setOpt($Opt) |
223
|
|
|
{ |
224
|
|
|
$this->Opt = $Opt; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
#****************************************************************** |
229
|
|
|
#Data validation |
230
|
|
|
#****************************************************************** |
231
|
|
|
public function validData() |
232
|
|
|
{ |
233
|
|
|
$msg = ""; |
234
|
|
|
if ($this->TxnType != "Purchase") { |
235
|
|
|
if ($this->TxnType != "Auth") { |
236
|
|
|
$msg = "Invalid TxnType[$this->TxnType]<br>"; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if (strlen($this->MerchantReference) > 64) { |
241
|
|
|
$msg = "Invalid MerchantReference [$this->MerchantReference]<br>"; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (strlen($this->TxnId) > 16) { |
245
|
|
|
$msg = "Invalid TxnId [$this->TxnId]<br>"; |
246
|
|
|
} |
247
|
|
|
if (strlen($this->TxnData1) > 255) { |
248
|
|
|
$msg = "Invalid TxnData1 [$this->TxnData1]<br>"; |
249
|
|
|
} |
250
|
|
|
if (strlen($this->TxnData2) > 255) { |
251
|
|
|
$msg = "Invalid TxnData2 [$this->TxnData2]<br>"; |
252
|
|
|
} |
253
|
|
|
if (strlen($this->TxnData3) > 255) { |
254
|
|
|
$msg = "Invalid TxnData3 [$this->TxnData3]<br>"; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (strlen($this->EmailAddress) > 255) { |
258
|
|
|
$msg = "Invalid EmailAddress [$this->EmailAddress]<br>"; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if (strlen($this->UrlFail) > 255) { |
262
|
|
|
$msg = "Invalid UrlFail [$this->UrlFail]<br>"; |
263
|
|
|
} |
264
|
|
|
if (strlen($this->UrlSuccess) > 255) { |
265
|
|
|
$msg = "Invalid UrlSuccess [$this->UrlSuccess]<br>"; |
266
|
|
|
} |
267
|
|
|
if (strlen($this->BillingId) > 32) { |
268
|
|
|
$msg = "Invalid BillingId [$this->BillingId]<br>"; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
if ($msg != "") { |
272
|
|
|
trigger_error($msg, E_USER_ERROR); |
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
#****************************************************************************** |
280
|
|
|
# Abstract base class for PxPay messages. |
281
|
|
|
# These are messages with certain defined elements, which can be serialized to XML. |
282
|
|
|
|
283
|
|
|
#****************************************************************************** |
284
|
|
|
class PxPayMessage |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
public $TxnType; |
287
|
|
|
public $CurrencyInput; |
288
|
|
|
public $TxnData1; |
289
|
|
|
public $TxnData2; |
290
|
|
|
public $TxnData3; |
291
|
|
|
public $MerchantReference; |
292
|
|
|
public $EmailAddress; |
293
|
|
|
public $BillingId; |
294
|
|
|
public $TxnId; |
295
|
|
|
|
296
|
|
|
public function __construct() |
297
|
|
|
{ |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function setBillingId($BillingId) |
301
|
|
|
{ |
302
|
|
|
$this->BillingId = $BillingId; |
303
|
|
|
} |
304
|
|
|
public function getBillingId() |
|
|
|
|
305
|
|
|
{ |
306
|
|
|
return $this->BillingId; |
307
|
|
|
} |
308
|
|
|
public function setTxnType($TxnType) |
309
|
|
|
{ |
310
|
|
|
$this->TxnType = $TxnType; |
311
|
|
|
} |
312
|
|
|
public function getTxnType() |
|
|
|
|
313
|
|
|
{ |
314
|
|
|
return $this->TxnType; |
315
|
|
|
} |
316
|
|
|
public function setCurrencyInput($CurrencyInput) |
317
|
|
|
{ |
318
|
|
|
$this->CurrencyInput = $CurrencyInput; |
319
|
|
|
} |
320
|
|
|
public function getCurrencyInput() |
|
|
|
|
321
|
|
|
{ |
322
|
|
|
return $this->CurrencyInput; |
323
|
|
|
} |
324
|
|
|
public function setMerchantReference($MerchantReference) |
325
|
|
|
{ |
326
|
|
|
$this->MerchantReference = $MerchantReference; |
327
|
|
|
} |
328
|
|
|
public function getMerchantReference() |
|
|
|
|
329
|
|
|
{ |
330
|
|
|
return $this->MerchantReference; |
331
|
|
|
} |
332
|
|
|
public function setEmailAddress($EmailAddress) |
333
|
|
|
{ |
334
|
|
|
$this->EmailAddress = $EmailAddress; |
335
|
|
|
} |
336
|
|
|
public function getEmailAddress() |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
return $this->EmailAddress; |
339
|
|
|
} |
340
|
|
|
public function setTxnData1($TxnData1) |
341
|
|
|
{ |
342
|
|
|
$this->TxnData1 = $TxnData1; |
343
|
|
|
} |
344
|
|
|
public function getTxnData1() |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
return $this->TxnData1; |
347
|
|
|
} |
348
|
|
|
public function setTxnData2($TxnData2) |
349
|
|
|
{ |
350
|
|
|
$this->TxnData2 = $TxnData2; |
351
|
|
|
} |
352
|
|
|
public function getTxnData2() |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
return $this->TxnData2; |
355
|
|
|
} |
356
|
|
|
public function getTxnData3() |
|
|
|
|
357
|
|
|
{ |
358
|
|
|
return $this->TxnData3; |
359
|
|
|
} |
360
|
|
|
public function setTxnData3($TxnData3) |
361
|
|
|
{ |
362
|
|
|
$this->TxnData3 = $TxnData3; |
363
|
|
|
} |
364
|
|
|
public function setTxnId($TxnId) |
365
|
|
|
{ |
366
|
|
|
$this->TxnId = $TxnId; |
367
|
|
|
} |
368
|
|
|
public function getTxnId() |
|
|
|
|
369
|
|
|
{ |
370
|
|
|
return $this->TxnId; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function toXml() |
374
|
|
|
{ |
375
|
|
|
$arr = get_object_vars($this); |
376
|
|
|
|
377
|
|
|
$xml = "<GenerateRequest>"; |
378
|
|
|
while (list($prop, $val) = each($arr)) { |
379
|
|
|
$xml .= "<$prop>$val</$prop>" ; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$xml .= "</GenerateRequest>"; |
383
|
|
|
return $xml; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
#****************************************************************************** |
388
|
|
|
# Class for PxPay response messages. |
389
|
|
|
#****************************************************************************** |
390
|
|
|
|
391
|
|
|
class PxPayResponse extends PxPayMessage |
|
|
|
|
392
|
|
|
{ |
393
|
|
|
public $Success; |
394
|
|
|
public $AuthCode; |
395
|
|
|
public $CardName; |
396
|
|
|
public $CardHolderName; |
397
|
|
|
public $CardNumber; |
398
|
|
|
public $DateExpiry; |
399
|
|
|
public $ClientInfo; |
400
|
|
|
public $DpsTxnRef; |
401
|
|
|
public $DpsBillingId; |
402
|
|
|
public $AmountSettlement; |
403
|
|
|
public $CurrencySettlement; |
404
|
|
|
public $TxnMac; |
405
|
|
|
public $ResponseText; |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
public function __construct($xml) |
409
|
|
|
{ |
410
|
|
|
$msg = new MifMessage($xml); |
411
|
|
|
parent::__construct(); |
412
|
|
|
|
413
|
|
|
$this->Success = $msg->get_element_text("Success"); |
414
|
|
|
$this->setTxnType($msg->get_element_text("TxnType")); |
415
|
|
|
$this->CurrencyInput = $msg->get_element_text("CurrencyInput"); |
416
|
|
|
$this->setMerchantReference($msg->get_element_text("MerchantReference")); |
417
|
|
|
$this->setTxnData1($msg->get_element_text("TxnData1")); |
418
|
|
|
$this->setTxnData2($msg->get_element_text("TxnData2")); |
419
|
|
|
$this->setTxnData3($msg->get_element_text("TxnData3")); |
420
|
|
|
$this->AuthCode = $msg->get_element_text("AuthCode"); |
421
|
|
|
$this->CardName = $msg->get_element_text("CardName"); |
422
|
|
|
$this->CardHolderName = $msg->get_element_text("CardHolderName"); |
423
|
|
|
$this->CardNumber = $msg->get_element_text("CardNumber"); |
424
|
|
|
$this->DateExpiry = $msg->get_element_text("DateExpiry"); |
425
|
|
|
$this->ClientInfo = $msg->get_element_text("ClientInfo"); |
426
|
|
|
$this->TxnId = $msg->get_element_text("TxnId"); |
427
|
|
|
$this->setEmailAddress($msg->get_element_text("EmailAddress")); |
428
|
|
|
$this->DpsTxnRef = $msg->get_element_text("DpsTxnRef"); |
429
|
|
|
$this->BillingId = $msg->get_element_text("BillingId"); |
430
|
|
|
$this->DpsBillingId = $msg->get_element_text("DpsBillingId"); |
431
|
|
|
$this->AmountSettlement = $msg->get_element_text("AmountSettlement"); |
432
|
|
|
$this->CurrencySettlement = $msg->get_element_text("CurrencySettlement"); |
433
|
|
|
$this->TxnMac = $msg->get_element_text("TxnMac"); |
434
|
|
|
$this->ResponseText = $msg->get_element_text("ResponseText"); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
public function getSuccess() |
|
|
|
|
439
|
|
|
{ |
440
|
|
|
return $this->Success; |
441
|
|
|
} |
442
|
|
|
public function getAuthCode() |
|
|
|
|
443
|
|
|
{ |
444
|
|
|
return $this->AuthCode; |
445
|
|
|
} |
446
|
|
|
public function getCardName() |
|
|
|
|
447
|
|
|
{ |
448
|
|
|
return $this->CardName; |
449
|
|
|
} |
450
|
|
|
public function getCardHolderName() |
|
|
|
|
451
|
|
|
{ |
452
|
|
|
return $this->CardHolderName; |
453
|
|
|
} |
454
|
|
|
public function getCardNumber() |
|
|
|
|
455
|
|
|
{ |
456
|
|
|
return $this->CardNumber; |
457
|
|
|
} |
458
|
|
|
public function getDateExpiry() |
|
|
|
|
459
|
|
|
{ |
460
|
|
|
return $this->DateExpiry; |
461
|
|
|
} |
462
|
|
|
public function getClientInfo() |
|
|
|
|
463
|
|
|
{ |
464
|
|
|
return $this->ClientInfo; |
465
|
|
|
} |
466
|
|
|
public function getDpsTxnRef() |
|
|
|
|
467
|
|
|
{ |
468
|
|
|
return $this->DpsTxnRef; |
469
|
|
|
} |
470
|
|
|
public function getDpsBillingId() |
|
|
|
|
471
|
|
|
{ |
472
|
|
|
return $this->DpsBillingId; |
473
|
|
|
} |
474
|
|
|
public function getAmountSettlement() |
|
|
|
|
475
|
|
|
{ |
476
|
|
|
return $this->AmountSettlement; |
477
|
|
|
} |
478
|
|
|
public function getCurrencySettlement() |
479
|
|
|
{ |
480
|
|
|
$this->CurrencySettlement; |
481
|
|
|
} |
482
|
|
|
public function getTxnMac() |
|
|
|
|
483
|
|
|
{ |
484
|
|
|
return $this->TxnMac; |
485
|
|
|
} |
486
|
|
|
public function getResponseText() |
|
|
|
|
487
|
|
|
{ |
488
|
|
|
return $this->ResponseText; |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.