Created
June 27, 2021 12:23
-
-
Save takumade/b9d83f750b728e29dfdf20890d601088 to your computer and use it in GitHub Desktop.
PHP Paynow Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// THere must be a folder called libraries which contains the folder paynow | |
// The paynow folder contains paynow sdk | |
require_once '../libraries/paynow/autoloader.php'; | |
function detectWalletName($phone_number){ | |
// Detect wallet name | |
$wallet_name = "ecocash"; | |
if (strpos($phone_number, '071') === 0) { | |
$wallet_name = "onemoney"; | |
} | |
if (strpos($phone_number, '073') === 0) { | |
$wallet_name = "telecash"; | |
} | |
return $wallet_name; | |
} | |
function paynowExpress($items){ | |
$phone_number = "0779123456"; // You can get it from request or database | |
$user_email = 'user@example.com'; // You can get it from request or database | |
// initialize paynow payment | |
$paynow = new Paynow\Payments\Paynow( | |
'INTEGRATION_ID', | |
'INTEGRATION_KEY', | |
'http://example.com/gateways/paynow/update', | |
// The return url can be set at later stages. You might want to do this if you want to pass data to the return url (like the reference of the transaction) | |
'http://example.com/return?gateway=paynow' | |
); | |
// Create payment | |
$payment = $paynow->createPayment('Invoice 35', $user_email); | |
// Add payment items | |
foreach ($items as $item){ | |
$payment->add($item['name'], $item['price']); | |
} | |
// Detect wallet name and send payment | |
$wallet_name = detectWalletName($phone_number); | |
$response = $paynow->sendMobile($payment, $phone_number, $wallet_name); | |
// Check transaction success | |
if ($response->success()) { | |
$timeout = 9; | |
$count = 0; | |
while (true) { | |
sleep(3); | |
// Get the status of the transaction | |
// Get transaction poll URL | |
$pollUrl = $response->pollUrl(); | |
$status = $paynow->pollTransaction($pollUrl); | |
//Check if paid | |
if ($status->paid()) { | |
// Yay! Transaction was paid for | |
// You can update transaction status here | |
// Then route/redirect to a payment successful page | |
} | |
$count++; | |
if ($count > $timeout) { | |
// Timeout reached | |
// Notify user here | |
} | |
} | |
} | |
// Payment failed | |
// Route/Redirect to payment failed page | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment