-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpaystack.php
More file actions
397 lines (318 loc) · 12.4 KB
/
paystack.php
File metadata and controls
397 lines (318 loc) · 12.4 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php register_shutdown_function( 'paystackshutdownFunction');
/**
/ ********************************************************************* \
* *
* Paystack Payment Gateway *
* Version: 2.3.0 *
* Build Date: April 25, 2024 *
* *
************************************************************************
* *
* Email: support@paystack.com *
* Website: https://www.paystack.com *
* *
\ ********************************************************************* /
**/
class whmcs_paystack_plugin_tracker {
var $public_key;
var $plugin_name;
function __construct($plugin, $pk){
//configure plugin name
//configure public key
$this->plugin_name = $plugin;
$this->public_key = $pk;
}
function log_transaction_success($trx_ref){
//send reference to logger along with plugin name and public key
$url = "https://plugin-tracker.paystackintegrations.com/log/charge_success";
$fields = [
'plugin_name' => $this->plugin_name,
'transaction_reference' => $trx_ref,
'public_key' => $this->public_key
];
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
// echo $result;
}
}
// Require libraries needed for gateway module functions.
require_once __DIR__ . '/../../../init.php';
require_once __DIR__ . '/../../../includes/gatewayfunctions.php';
require_once __DIR__ . '/../../../includes/invoicefunctions.php';
// Detect module name from filename.
$gatewayModuleName = basename(__FILE__, '.php');
// Fetch gateway configuration parameters.
$gatewayParams = getGatewayVariables($gatewayModuleName);
// Die if module is not active.
if (!$gatewayParams['type']) {
die("Module Not Activated");
}
// Retrieve data returned in payment gateway callback
$invoiceId = filter_input(INPUT_GET, "invoiceid");
$txnref = $invoiceId . '_' .time();
$trxref = filter_input(INPUT_GET, "trxref");
if ($gatewayParams['testMode'] == 'on') {
$secretKey = $gatewayParams['testSecretKey'];
} else {
$secretKey = $gatewayParams['liveSecretKey'];
}
if(strtolower(filter_input(INPUT_GET, 'go'))==='standard'){
// falling back to standard
$ch = curl_init();
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$amountinkobo = filter_input(INPUT_GET, 'amountinkobo');
$email = filter_input(INPUT_GET, 'email');
$phone = filter_input(INPUT_GET, 'phone');
$callback_url = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
$_SERVER['SCRIPT_NAME'] . '?invoiceid=' . rawurlencode($invoiceId);
$txStatus = new stdClass();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.paystack.co/transaction/initialize/");
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Authorization: Bearer '. trim($secretKey),
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
json_encode(
array(
"amount"=>$amountinkobo,
"email"=>$email,
"phone"=>$phone,
"reference" => $txnref,
"callback_url"=>$callback_url
)
)
);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
// exec the cURL
$response = curl_exec($ch);
// should be 0
if (curl_errno($ch)) {
// curl ended with an error
$txStatus->error = "cURL said:" . curl_error($ch);
curl_close($ch);
} else {
//close connection
curl_close($ch);
// Then, after your curl_exec call:
$body = json_decode($response);
if (!$body->status) {
// paystack has an error message for us
$txStatus->error = "Paystack API said: " . $body->message;
} else {
// get body returned by Paystack API
$txStatus = $body->data;
}
}
if(!$txStatus->error){
header('Location: ' . $txStatus->authorization_url);
die('<meta http-equiv="refresh" content="0;url='.$txStatus->authorization_url.'" />
Redirecting to <a href=\''.$txStatus->authorization_url.'\'>'.$txStatus->authorization_url.'</a>...');
} else {
if ($gatewayParams['gatewayLogs'] == 'on') {
$output = "Transaction Initialize failed"
. "\r\nReason: {$txStatus->error}";
logTransaction($gatewayModuleName, $output, "Unsuccessful");
}
die($txStatus->error);
}
}
// if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
// exit();
// }
$input = @file_get_contents("php://input");
$event = json_decode($input);
if (isset($event->event)) {
// echo "<pre>";
if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, $secretKey))){
exit();
}
switch($event->event){
case 'subscription.create':
break;
case 'subscription.disable':
break;
case 'charge.success':
$trxref = $event->data->reference;
//PSTK Logger
if ($gatewayParams['testMode'] == 'on') {
$pk = $gatewayParams['testPublicKey'];
} else {
$pk = $gatewayParams['livePublicKey'];
}
$pstk_logger = new whmcs_paystack_plugin_tracker('whmcs',$pk );
$pstk_logger->log_transaction_success($trxref);
//-------------------------------------
$order_details = explode( '_', $trxref);
$invoiceId = (int) $order_details[0];
break;
case 'invoice.create':
// Recurring payments
case 'invoice.update':
// Recurring payments
break;
}
http_response_code(200);
// exit();
}
/**
* Verify Paystack transaction.
*/
$txStatus = verifyTransaction($trxref, $secretKey);
if ($txStatus->error) {
if ($gatewayParams['gatewayLogs'] == 'on') {
$output = "Transaction ref: " . $trxref
. "\r\nInvoice ID: " . $invoiceId
. "\r\nStatus: failed"
. "\r\nReason: {$txStatus->error}";
logTransaction($gatewayModuleName, $output, "Unsuccessful");
}
$success = false;
} elseif ($txStatus->status == 'success') {
if ($gatewayParams['gatewayLogs'] == 'on') {
$output = "Transaction ref: " . $trxref
. "\r\nInvoice ID: " . $invoiceId
. "\r\nStatus: succeeded";
logTransaction($gatewayModuleName, $output, "Successful");
//PSTK Logger
if ($gatewayParams['testMode'] == 'on') {
$pk = $gatewayParams['testPublicKey'];
} else {
$pk = $gatewayParams['livePublicKey'];
}
$pstk_logger_ = new whmcs_paystack_plugin_tracker('whmcs',$pk );
$pstk_logger_->log_transaction_success($trxref);
//-------------------------------------
}
$success = true;
} else {
if ($gatewayParams['gatewayLogs'] == 'on') {
$output = "Transaction ref: " . $trxref
. "\r\nInvoice ID: " . $invoiceId
. "\r\nStatus: {$txStatus->status}";
logTransaction($gatewayModuleName, $output, "Unsuccessful");
}
$success = false;
}
function paystackshutdownFunction(){
$invoiceId = filter_input(INPUT_GET, "invoiceid");
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$invoice_url = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')) .
'/../../../viewinvoice.php?id='.
rawurlencode($invoiceId);
header('Location: '.$invoice_url);
}
if ($success) {
// print_r($txStatus);
// die();
/**
* Validate Callback Invoice ID.
*
* Checks invoice ID is a valid invoice number.
*
* Performs a die upon encountering an invalid Invoice ID.
*
* Returns a normalised invoice ID.
*/
$invoiceId = checkCbInvoiceID($invoiceId, $gatewayModuleName);
/**
* Check Callback Transaction ID.
*
* Performs a check for any existing transactions with the same given
* transaction number.
*
* Performs a die upon encountering a duplicate.
*/
checkCbTransID($trxref);
$amount = floatval($txStatus->amount)/100;
$requested_amount = floatval($txStatus->requested_amount)/100;
if (isset($requested_amount) && $requested_amount > 0)
{
$amount = $requested_amount;
}
$fees = floatval($txStatus->fees)/100;
if ($gatewayParams['convertto']) {
$result = select_query("tblclients", "tblinvoices.invoicenum,tblclients.currency,tblcurrencies.code", array("tblinvoices.id" => $invoiceId), "", "", "", "tblinvoices ON tblinvoices.userid=tblclients.id INNER JOIN tblcurrencies ON tblcurrencies.id=tblclients.currency");
$data = mysql_fetch_array($result);
$invoice_currency_id = $data['currency'];
$converto_amount = convertCurrency($amount, $gatewayParams['convertto'], $invoice_currency_id);
$converto_fees = convertCurrency($fees, $gatewayParams['convertto'], $invoice_currency_id);
$amount = format_as_currency($converto_amount);
$fees = format_as_currency($converto_fees);
}
/**
* Add Invoice Payment.
*
* Applies a payment transaction entry to the given invoice ID.
*
* @param int $invoiceId Invoice ID
* @param string $transactionId Transaction ID
* @param float $paymentAmount Amount paid (defaults to full balance)
* @param float $paymentFee Payment fee (optional)
* @param string $gatewayModule Gateway module name
*/
addInvoicePayment($invoiceId, $trxref, $amount, $fees, $gatewayModuleName);
// load invoice
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$invoice_url = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')) .
'/../../../viewinvoice.php?id='.
rawurlencode($invoiceId);
header('Location: '.$invoice_url);
} else {
die($txStatus->error . ' ; ' . $txStatus->status);
}
function verifyTransaction($trxref, $secretKey)
{
$ch = curl_init();
$txStatus = new stdClass();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.paystack.co/transaction/verify/" . rawurlencode($trxref));
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Authorization: Bearer '. trim($secretKey)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
// exec the cURL
$response = curl_exec($ch);
// should be 0
if (curl_errno($ch)) {
// curl ended with an error
$txStatus->error = "cURL said:" . curl_error($ch);
curl_close($ch);
} else {
//close connection
curl_close($ch);
// Then, after your curl_exec call:
$body = json_decode($response);
if (!$body->status) {
// paystack has an error message for us
$txStatus->error = "Paystack API said: " . $body->message;
} else {
// get body returned by Paystack API
$txStatus = $body->data;
}
}
return $txStatus;
}