This repository was archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaygate.payweb3.php
More file actions
332 lines (284 loc) · 7.53 KB
/
Copy pathpaygate.payweb3.php
File metadata and controls
332 lines (284 loc) · 7.53 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* Class to do initiate and Query functions to PayGate for PayWeb 3
*
* @author PayGate
* @version 0.1
*
*/
class PayGate_PayWeb3{
/**
* @var string the url of the PayGate PayWeb 3 initiate page
*/
static public $initiate_url = 'https://secure.paygate.co.za/payweb3/initiate.trans';
/**
* @var string the url of the PayGate PayWeb 3 process page
*/
static public $process_url = 'https://secure.paygate.co.za/payweb3/process.trans';
/**
* @var string the url of the PayGate PayWeb 3 query page
*/
static public $query_url = 'https://secure.paygate.co.za/payweb3/query.trans';
/**
* @var array contains the data to be posted to PayGate PayWeb 3 initiate
*/
public $initiateRequest;
/**
* @var array contains the response data from the initiate
*/
public $initiateResponse;
/**
* @var array contains the data returned from the initiate, required for the redirect of the client
*/
public $processRequest;
/**
* @var array contains the data to be posted to PayGate PayWeb 3 query service
*/
public $queryRequest;
/**
* @var array contains the response data from the query
*/
public $queryResponse;
/**
* @var string
*
* Most common errors returned will be:
*
* DATA_CHK -> Checksum posted does not match the one calculated by PayGate, either due to an incorrect encryption key used or a field that has been excluded from the checksum calculation
* DATA_PW -> Mandatory fields have been excluded from the post to PayGate, refer to page 9 of the documentation as to what fields should be posted.
* DATA_CUR -> The currency that has been posted to PayGate is not supported.
* PGID_NOT_EN -> The PayGate ID being used to post data to PayGate has not yet been enabled, or there are no payment methods setup on it.
*
*/
public $lastError;
private $transactionStatusArray = array(
1 => 'Approved',
2 => 'Declined',
4 => 'Cancelled'
);
public $debug = false;
public $ssl = false;
/**
* @var string (as set up on the PayWeb 3 config page in the PayGate Back Office )
*/
private $encryptionKey;
function __construct(){
}
/**
* @return boolean
*/
public function isDebug(){
return $this->debug;
}
/**
* @param boolean $debug
*/
public function setDebug($debug){
$this->debug = $debug;
}
/**
* @return boolean
*/
public function isSsl(){
return $this->ssl;
}
/**
* @param boolean $ssl
*/
public function setSsl($ssl){
$this->ssl = $ssl;
}
/**
* @return array
*/
public function getInitiateRequest(){
return $this->initiateRequest;
}
/**
* @param array $postData
*/
public function setInitiateRequest($postData){
$this->initiateRequest = $postData;
}
/**
* @return array
*/
public function getQueryRequest(){
return $this->queryRequest;
}
/**
* @param array $queryRequest
*/
public function setQueryRequest($queryRequest){
$this->queryRequest = $queryRequest;
}
/**
* @return string
*/
public function getEncryptionKey(){
return $this->encryptionKey;
}
/**
* @param string $encryptionKey
*/
public function setEncryptionKey($encryptionKey){
$this->encryptionKey = $encryptionKey;
}
/**
* @return bool
*/
function _is_curl_installed(){
if(in_array('curl', get_loaded_extensions())){
return true;
} else {
return false;
}
}
/**
* returns a description of the transaction status number passed back from PayGate
*
* @param int $statusNumber
* @return string
*/
function getTransactionStatusDescription($statusNumber){
return $this->transactionStatusArray[$statusNumber];
}
/**
* Function to format date / time. php's DateTime object used to overcome limitation of standard date() function.
* DateTime available from PHP 5.2.0
*
* @param $format
* @return string
*/
public function getDateTime($format){
if (version_compare(PHP_VERSION, '5.2.0', '<')) {
$dateTime = date('Y-m-d H:i:s');
return $dateTime;
} else {
$dateTime = new DateTime();
return $dateTime->format($format);
}
}
/**
* Function to generate the checksum to be passed in the initiate call. Refer to examples on Page 15 of the PayWeb3 documentation
*
* @param array $postData
* @return string (md5 hash value)
*/
public function generateChecksum($postData){
$checksum = '';
foreach($postData as $key => $value){
if($value != ''){
$checksum .= $value;
}
}
$checksum .= $this->getEncryptionKey();
if($this->isDebug()){
error_log('Checksum Source: '.$checksum, 0);
}
return md5($checksum);
}
/**
* Function to handle response from initiate request and set error or processRequest as need be
*
* @return bool
*/
function handleInitiateResponse(){
if(array_key_exists('ERROR', $this->initiateResponse)){
$this->lastError = $this->initiateResponse['ERROR'];
unset($this->initiateResponse);
return false;
}
$this->processRequest = array(
'PAY_REQUEST_ID' => $this->initiateResponse['PAY_REQUEST_ID'],
'CHECKSUM' => $this->initiateResponse['CHECKSUM']
);
return true;
}
/**
* Function to handle response from Query request and set error as need be
*
* @return bool
*/
function handleQueryResponse(){
if(array_key_exists('ERROR', $this->queryResponse)){
$this->lastError = $this->queryResponse['ERROR'];
unset($this->queryResponse);
return false;
}
return true;
}
/**
* Function to do curl post to PayGate to initiate a PayWeb 3 transaction
*
* @return bool
*/
public function doInitiate(){
$this->initiateRequest['CHECKSUM'] = $this->generateChecksum($this->initiateRequest);
$result = $this->doCurlPost($this->initiateRequest, self::$initiate_url);
if($result !== false ){
parse_str($result, $this->initiateResponse);
$result = $this->handleInitiateResponse();
}
return $result;
}
/**
* Function to do curl post to PayGate to query a PayWeb 3 transaction
*
* @return bool
*/
public function doQuery(){
$this->queryRequest['CHECKSUM'] = $this->generateChecksum($this->queryRequest);
$result = $this->doCurlPost($this->queryRequest, self::$query_url);
if($result !== false ){
parse_str($result, $this->queryResponse);
$result = $this->handleQueryResponse();
}
return $result;
}
/**
* function to do actual curl post to PayGate
*
* @param array $postData data to be posted
* @param string $url to be posted to
* @return bool | string
*/
public function doCurlPost($postData, $url){
if($this->_is_curl_installed()){
$fields_string = '';
//url-ify the data for the POST
foreach($postData as $key => $value){
$fields_string .= $key . '=' . $value . '&';
}
//remove trailing '&'
$fields_string = rtrim($fields_string, '&');
if($this->isDebug()){
error_log('Post via Curl: ' . $fields_string, 0);
}
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
if(!$this->isSsl()){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
if($this->isDebug()){
error_log('Return from Curl: ' . $result, 0);
}
return $result;
} else {
$this->lastError = 'cURL is NOT installed on this server. http://php.net/manual/en/curl.setup.php';
return false;
}
}
}