This repository has been archived by the owner on Aug 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpayex.php
executable file
·310 lines (249 loc) · 8.51 KB
/
payex.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
<?php
class PayEx
{
//$respons is xml
static private $respons;
static private $orderRef;
static public $result;
static public $status;
static private $purchaseOperation = 'SALE'; // AUTHORIZATION or SALE
static private $price = 0; // Product price, in lowest monetary unit (=1 NOK)
static private $priceArgList = ''; // No CPA, VISA,
static private $orderID = '0'; // Local order id
static private $productNumber = '0'; // Local product number
static private $description = ''; // Product description
static private $clientIPAddress = '';
static private $clientIdentifier = '';
static private $additionalValues = '';
static private $externalID = '';
static private $agreementRef = '';
static private $view = 'CC';
static $config;
static private $isConstructed;
static private $configFile = "config.php";
function __construct(){ PayEx::construct(); }
static function construct(){
if(!self::$isConstructed){
self::$config = new stdClass();
$config = include(self::$configFile);
foreach($config as $name => $value){
if(!isset(self::$config->{$name})){
self::setConfig($name, $value);
}
}
self::$isConstructed = true;
}
}
static function setConfigFile($filepath){
self::$configFile = $filepath;
self::construct();
}
static function setConfig($name, $value){
self::$config->{$name} = $value;
}
static function getConfig($name){
if(isset(self::$config->{$name}))
return self::$config->{$name};
else return null;
}
static function getPrice(){
return self::$price;
}
static function setPrice($price){
if(!is_numeric($price)) return false;
self::$price = number_format($price, 2, "", "");
}
static function setOrderID($orderID){
self::$orderID = $orderID;
}
static function getOrderID(){
return self::$orderID ? self::$orderID : 0;
}
static function setProductNumber($productNumber){
self::$productNumber = $productNumber;
}
static function getProductNumber(){
return self::$productNumber ? self::$productNumber : 0;
}
static function setDescription($description){
self::$description = $description;
}
static function getDescription(){
return self::$description ? self::$description : "Undefined";
}
static function setExternalID($externalID){
self::$externalID = $externalID;
}
static function getExternalID(){
return self::$externalID ? self::$externalID : 0;
}
static function initialization()
{
self::construct();
//$_server won't work if run from console.
self::$clientIPAddress = $_SERVER['REMOTE_ADDR'];
self::$clientIdentifier = "USERAGENT=".$_SERVER['HTTP_USER_AGENT'];
$params = array(
'accountNumber' => self::getConfig('accountNumber'),
'purchaseOperation' => self::$purchaseOperation,
'price' => self::getPrice(),
'priceArgList' => self::$priceArgList,
'currency' => self::getConfig('currency'),
'vat' => self::getConfig('vat'),
'orderID' => self::getOrderID(),
'productNumber' => self::getProductNumber(),
'description' => self::getDescription(),
'clientIPAddress' => self::$clientIPAddress,
'clientIdentifier' => self::$clientIdentifier,
'additionalValues' => self::$additionalValues,
'externalID' => self::getExternalID(),
'returnUrl' => self::getConfig('returnURL'),
'view' => self::$view,
'agreementRef' => self::$agreementRef,
'cancelUrl' => self::getConfig('cancelURL'),
'clientLanguage' => self::getConfig('clientLanguage')
);
return $params;
}
static function transaction($orderID, $price, $productNumber=0, $description=""){
self::setPrice($price);
self::setOrderID($orderID);
self::setProductNumber($productNumber);
self::setDescription($description);
self::TwoPhaseTransaction();
return new Payex;
}
static function getStatus(){
return json_decode(json_encode(self::$status));
}
static function isOK(){
return self::getStatus()->code == "OK" ? true : false;
}
/* this function is used when selling physical merchandieses, books, cars... */
static function TwoPhaseTransaction()
{
$params = self::initialization();
self::$result = self::initialize7($params);
self::$status = self::checkStatus(self::$result);
}
static function Redirect()
{
// if code & description & errorCode is OK, redirect the user
if(self::$status['code'] == "OK" && self::$status['errorCode'] == "OK" && self::$status['description'] == "OK")
{
header('Location: '.self::$status['redirectUrl']); exit;
}else {
foreach(self::$status as $error => $value)
{
echo "$error, $value"."\n";
}
}
}
static function CompleteIt($orderRef)
{
$orderRef = stripcslashes( $orderRef );
$params = array
(
'accountNumber' => self::getConfig('accountNumber'),
'orderRef' => $orderRef
);
$completeResponse = self::CompleteOrder($params);
$result = self::complete($completeResponse);
return $result;
}
static function createHash($params)
{
return md5($params.self::getConfig('encryptionKey'));
}
static function checkStatus($xml)
{
$returnXml = new SimpleXMLElement($xml);
$code = strtoupper($returnXml->status->code);
$errorCode = strtoupper($returnXml->status->errorCode);
$description = strtoupper($returnXml->status->description);
$orderRef = strtoupper($returnXml->orderRef);
$authenticationRequired = strtoupper($returnXml->authenticationRequired);
return $status = array(
'code'=>$code,
'errorCode'=>$errorCode,
'description'=>$description,
'redirectUrl'=>$returnXml->redirectUrl,
'orderRef'=>$orderRef,
'authenticationRequired'=>$authenticationRequired);
}
static function complete($params)
{
$returnXml = new SimpleXMLElement($params);
$code = strtoupper($returnXml->status->code);
$errorCode = strtoupper($returnXml->status->errorCode);
$description = strtoupper($returnXml->status->description);
$transactionStatus = strtoupper($returnXml->transactionStatus);
return $status = array(
'code'=>$code,
'errorCode'=>$errorCode,
'description'=>$description,
'transactionStatus'=>$transactionStatus);
}
static function initialize7($params)
{
$PayEx = new SoapClient(self::getConfig('PxOrderWSDL'),array("trace" => 1, "exceptions" => 0));
$hash = self::createHash(trim(implode("", $params)));
$params['hash'] = $hash;
try{
//defining which initialize version to run, this one is 7.
$respons = $PayEx->Initialize7($params);
/* NB: SHOULD BE EDITED TO NOT SHOW THE CUSTOMER THIS MESSAGE, BUT SHOW A GENERIC ERROR MESSAGE FOR THE USER, BUT YOU SHOULD BE INFORMED OF THE ERROR. "*/
}catch (SoapFault $error){
echo "Error: {$error->faultstring}";
}
return $respons->{'Initialize7Result'};
//print_r($respons->{'Initialize7Result'}."\n");
}
static function CompleteOrder($params)
{
$PayEx = new SoapClient(self::getConfig('PxOrderWSDL'),array("trace" => 1, "exceptions" => 0));
$hash = self::createHash(trim(implode("", $params)));
//append the hash to the parameters
$params['hash'] = $hash;
try{
//defining which complete
$respons = $PayEx->Complete($params);
/* NB: SHOULD BE EDITED TO NOT SHOW THE CUSTOMER THIS MESSAGE, BUT SHOW A GENERIC ERROR MESSAGE FOR THE USER, BUT YOU SHOULD BE INFORMED OF THE ERROR. "*/
}catch (SoapFault $error){
echo "Error: {$error->faultstring}";
}
return $respons->{'CompleteResult'};
}
static function saleCC3($params)
{
$PayEx = new SoapClient(self::getConfig('PxConfinedWSDL'),array("trace" => 1, "exceptions" => 0));
$hash = self::createHash(trim(implode("", $params)));
//append the hash to the parameters
$params['hash'] = $hash;
try{
//defining which initialize version to run, this one is 6.
$respons = $PayEx->SaleCC3($params);
/* NB: SHOULD BE EDITED TO NOT SHOW THE CUSTOMER THIS MESSAGE, BUT SHOW A GENERIC ERROR MESSAGE FOR THE USER, BUT YOU SHOULD BE INFORMED OF THE ERROR. "*/
}catch (SoapFault $error){
echo "Error: {$error->faultstring}";
}
return $respons->{'SaleCC3Result'};
}
static function prepareSaleCC2($params)
{
$PayEx = new SoapClient(self::getConfig('PxConfinedWSDL'),array("trace" => 1, "exceptions" => 0));
unset($params['transactionType']);
//create the hash
$hash = self::createHash(trim(implode("", $params)));
$params['hash'] = $hash;
try{
//defining which initialize version to run, this one is 6.
$respons = $PayEx->PrepareSaleCC2($params);
/* NB: SHOULD BE EDITED TO NOT SHOW THE CUSTOMER THIS MESSAGE, BUT SHOW A GENERIC ERROR MESSAGE FOR THE USER, BUT YOU SHOULD BE INFORMED OF THE ERROR. "*/
}catch (SoapFault $error){
echo "Error: {$error->faultstring}";
}
return $respons->{'PrepareSaleCC2Result'};
}
}
?>