Skip to content

Commit

Permalink
Merge pull request #2 from yabacon/dev
Browse files Browse the repository at this point in the history
Webhook support, tying invoice_id to transaction reference
  • Loading branch information
ibrahimlawal authored May 18, 2017
2 parents 7b9355e + d4c1086 commit 2a94d2a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
54 changes: 51 additions & 3 deletions modules/gateways/callback/paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/ ********************************************************************* \
* *
* Paystack Payment Gateway *
* Version: 1.0.0 *
* Build Date: 15 May 2016 *
* Version: 1.0.1 *
* Build Date: 18 May 2017 *
* *
************************************************************************
* *
Expand Down Expand Up @@ -32,6 +32,8 @@

// 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') {
Expand Down Expand Up @@ -77,6 +79,7 @@
"amount"=>$amountinkobo,
"email"=>$email,
"phone"=>$phone,
"reference" => $txnref,
"callback_url"=>$callback_url
)
)
Expand Down Expand Up @@ -119,11 +122,47 @@
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;
$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
Expand Down Expand Up @@ -152,6 +191,8 @@
}

if ($success) {
// print_r($txStatus);
// die();
/**
* Validate Callback Invoice ID.
*
Expand Down Expand Up @@ -254,3 +295,10 @@ function verifyTransaction($trxref, $secretKey)

return $txStatus;
}







22 changes: 19 additions & 3 deletions modules/gateways/paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
** ***************************************************************** **\
* *
* Paystack Payment Gateway *
* Version: 1.0.0 *
* Build Date: 15 May 2016 *
* Version: 1.0.1 *
* Build Date: 18 May 2017 *
* *
************************************************************************
* *
Expand All @@ -24,12 +24,23 @@
* @return array
*/
function paystack_config()
{
{
$isSSL = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
$callbackUrl = 'http' . ($isSSL ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['SERVER_NAME'], 0, strrpos($_SERVER['SERVER_NAME'], '/')) .
'/modules/gateways/callback/paystack.php';

return array(
'FriendlyName' => array(
'Type' => 'System',
'Value' => 'Paystack (Debit/Credit Cards)'
),
'webhook' => array(
'FriendlyName' => 'Webhook URL',
'Type' => 'yesno',
'Description' => 'Copy and paste this URL on your Webhook URL settings <code>'.$callbackUrl.'</code>',
'Default' => "'".$callbackUrl."'",
),
'gatewayLogs' => array(
'FriendlyName' => 'Gateway logs',
'Type' => 'yesno',
Expand Down Expand Up @@ -101,6 +112,9 @@ function paystack_link($params)
$invoiceId = $params['invoiceid'];
$amountinkobo = intval(floatval($params['amount'])*100);
$currency = $params['currency'];
///Transaction_reference
$txnref = $invoiceId . '_' .time();


if (!(strtoupper($currency) == 'NGN')) {
return ("Paystack only accepts NGN payments for now.");
Expand All @@ -114,6 +128,7 @@ function paystack_link($params)
'invoiceid'=>$invoiceId,
'email'=>$email,
'phone'=>$phone,
'reference' => $txnref,
'amountinkobo'=>$amountinkobo,
'go'=>'standard'
));
Expand Down Expand Up @@ -156,6 +171,7 @@ function paystack_link($params)
email: \''.addslashes(trim($email)).'\',
phone: \''.addslashes(trim($phone)).'\',
amount: '.$amountinkobo.',
ref:\''.$txnref.'\',
callback: function(response){
window.location.href = \''.addslashes($callbackUrl).'&trxref=\' + response.trxref;
},
Expand Down

0 comments on commit 2a94d2a

Please sign in to comment.