Last active
July 5, 2021 19:43
-
-
Save takumade/df9a1e11a6ab254468dbc9dbecff9edf to your computer and use it in GitHub Desktop.
How to implement Paynow in Laravel
This file contains hidden or 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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
use \Paynow\Payments\Paynow; | |
use Illuminate\Support\Facades\Validator; | |
use Illuminate\Support\Facades\Log; | |
class PaynowPaymentController extends Controller | |
{ | |
// | |
public function makeExpressPayment(Request $request) | |
{ | |
$items = [ | |
[ | |
"name" => "Bread", | |
"price" 1.00], | |
[ | |
"name" => "Beans", | |
"price" 2.00 | |
] | |
]; | |
$paynow_rate = 110; | |
// You can grab email from user like so: Auth::user()->email | |
$user_email = "[email protected]"; | |
// You can grab phone number from request like so: $request->phone_number | |
$phone_number = "0778112671"; | |
$wallet_name = "ecocash"; | |
$paynow = new \Paynow\Payments\Paynow( | |
"Integration ID here", | |
"Integration Key Here", | |
"Update URL", | |
"Return URL", | |
); | |
// Create Payments | |
$invoice_name = "Invoice " . time(); | |
$payment = $paynow->createPayment($invoice_name, $user_email); | |
// Add some products | |
foreach ($items as $item) { | |
$payment->add($item["name"], floatval($item["price"]) * $paynow_rate); | |
} | |
// Detect platform and send payment | |
if (strpos($phone_number, '071') === 0) { | |
$wallet_name = "onemoney"; | |
} | |
if (strpos($phone_number, '073') === 0) { | |
$wallet_name = "telecash"; | |
} | |
$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 to a payment successful page | |
} | |
$count++; | |
if ($count > $timeout) { | |
// Timeout reached | |
// Notify user here | |
} | |
} | |
} | |
// Payment failed | |
// Route to payment failed page | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment