Skip to content

Commit 2a94d2a

Browse files
authored
Merge pull request #2 from yabacon/dev
Webhook support, tying invoice_id to transaction reference
2 parents 7b9355e + d4c1086 commit 2a94d2a

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

modules/gateways/callback/paystack.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/ ********************************************************************* \
44
* *
55
* Paystack Payment Gateway *
6-
* Version: 1.0.0 *
7-
* Build Date: 15 May 2016 *
6+
* Version: 1.0.1 *
7+
* Build Date: 18 May 2017 *
88
* *
99
************************************************************************
1010
* *
@@ -32,6 +32,8 @@
3232

3333
// Retrieve data returned in payment gateway callback
3434
$invoiceId = filter_input(INPUT_GET, "invoiceid");
35+
$txnref = $invoiceId . '_' .time();
36+
3537
$trxref = filter_input(INPUT_GET, "trxref");
3638

3739
if ($gatewayParams['testMode'] == 'on') {
@@ -77,6 +79,7 @@
7779
"amount"=>$amountinkobo,
7880
"email"=>$email,
7981
"phone"=>$phone,
82+
"reference" => $txnref,
8083
"callback_url"=>$callback_url
8184
)
8285
)
@@ -119,11 +122,47 @@
119122
die($txStatus->error);
120123
}
121124
}
125+
// if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
126+
// exit();
127+
// }
128+
$input = @file_get_contents("php://input");
129+
$event = json_decode($input);
130+
if (isset($event->event)) {
131+
// echo "<pre>";
132+
if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, $secretKey))){
133+
exit();
134+
}
135+
136+
switch($event->event){
137+
case 'subscription.create':
138+
139+
break;
140+
case 'subscription.disable':
141+
break;
142+
case 'charge.success':
143+
$trxref = $event->data->reference;
144+
$order_details = explode( '_', $trxref);
145+
$invoiceId = (int) $order_details[0];
146+
147+
break;
148+
case 'invoice.create':
149+
// Recurring payments
150+
case 'invoice.update':
151+
// Recurring payments
152+
153+
break;
154+
}
155+
http_response_code(200);
156+
157+
158+
// exit();
159+
}
160+
122161
/**
123162
* Verify Paystack transaction.
124163
*/
125164
$txStatus = verifyTransaction($trxref, $secretKey);
126-
165+
127166
if ($txStatus->error) {
128167
if ($gatewayParams['gatewayLogs'] == 'on') {
129168
$output = "Transaction ref: " . $trxref
@@ -152,6 +191,8 @@
152191
}
153192

154193
if ($success) {
194+
// print_r($txStatus);
195+
// die();
155196
/**
156197
* Validate Callback Invoice ID.
157198
*
@@ -254,3 +295,10 @@ function verifyTransaction($trxref, $secretKey)
254295

255296
return $txStatus;
256297
}
298+
299+
300+
301+
302+
303+
304+

modules/gateways/paystack.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
** ***************************************************************** **\
44
* *
55
* Paystack Payment Gateway *
6-
* Version: 1.0.0 *
7-
* Build Date: 15 May 2016 *
6+
* Version: 1.0.1 *
7+
* Build Date: 18 May 2017 *
88
* *
99
************************************************************************
1010
* *
@@ -24,12 +24,23 @@
2424
* @return array
2525
*/
2626
function paystack_config()
27-
{
27+
{
28+
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
29+
$callbackUrl = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
30+
substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '/')) .
31+
'/modules/gateways/callback/paystack.php';
32+
2833
return array(
2934
'FriendlyName' => array(
3035
'Type' => 'System',
3136
'Value' => 'Paystack (Debit/Credit Cards)'
3237
),
38+
'webhook' => array(
39+
'FriendlyName' => 'Webhook URL',
40+
'Type' => 'yesno',
41+
'Description' => 'Copy and paste this URL on your Webhook URL settings <code>'.$callbackUrl.'</code>',
42+
'Default' => "'".$callbackUrl."'",
43+
),
3344
'gatewayLogs' => array(
3445
'FriendlyName' => 'Gateway logs',
3546
'Type' => 'yesno',
@@ -101,6 +112,9 @@ function paystack_link($params)
101112
$invoiceId = $params['invoiceid'];
102113
$amountinkobo = intval(floatval($params['amount'])*100);
103114
$currency = $params['currency'];
115+
///Transaction_reference
116+
$txnref = $invoiceId . '_' .time();
117+
104118

105119
if (!(strtoupper($currency) == 'NGN')) {
106120
return ("Paystack only accepts NGN payments for now.");
@@ -114,6 +128,7 @@ function paystack_link($params)
114128
'invoiceid'=>$invoiceId,
115129
'email'=>$email,
116130
'phone'=>$phone,
131+
'reference' => $txnref,
117132
'amountinkobo'=>$amountinkobo,
118133
'go'=>'standard'
119134
));
@@ -156,6 +171,7 @@ function paystack_link($params)
156171
email: \''.addslashes(trim($email)).'\',
157172
phone: \''.addslashes(trim($phone)).'\',
158173
amount: '.$amountinkobo.',
174+
ref:\''.$txnref.'\',
159175
callback: function(response){
160176
window.location.href = \''.addslashes($callbackUrl).'&trxref=\' + response.trxref;
161177
},

0 commit comments

Comments
 (0)