DEV Community

loading...
Cover image for PayNow Integration Part 4: Mobile Payments

PayNow Integration Part 4: Mobile Payments

Takunda Madechangu
πŸ“± Mobile App Developer 🌐 Web App Developer πŸ–₯ Desktop App Developer πŸƒ Flutter Enthusiast πŸ‘¨β€πŸ’» Proud Developer πŸ‡ΏπŸ‡Ό Proudly Zimbabwean 🏞 I am a nation
・2 min read

So we finished the boring parts and today we are going to do the following

1. Initialize Paynow

Before you can do anything you have to include PayNow and initialize it first

// include paynow here
<?php
$paynow = new Paynow\Payments\Paynow(
    'INTEGRATION_ID',
    'INTEGRATION_KEY',
    'http://example.com/gateways/paynow/update',
    'http://example.com/return?gateway=paynow'
);
Enter fullscreen mode Exit fullscreen mode

This creates a PayNow instance, your keys should be correct or it will raise Integration Exception on checkout. Errors can be annoying.

2. Creating a payment

The next stage involves adding a payment to the PayNow instance you have created:

$invoice_name = "Invoice " . time();
$user_email = filter_var($_POST['user_email'], FILTER_SANITIZE_EMAIL);
$payment = $paynow->createPayment($invoice_name, $user_email);
Enter fullscreen mode Exit fullscreen mode

This adds a payment to your instance you create in step 1. Once payment succeeds PayNow will send an email to the customer for transparence and reference purposes.

3. Add some products

If you have one product you can add it like this:

$payment->add('Sadza and Beans', 1.25);
Enter fullscreen mode Exit fullscreen mode

If you have more than one products then you can add them like this

$products = [Object1, Object2, Object3, ...., Object10 ];

foreach($products as $product){
     $payment->add($product->name, $product->price);
}
Enter fullscreen mode Exit fullscreen mode

If those products have quantities or amounts then it will be something like this

$products = [Object1, Object2, Object3, ...., Object10 ];
$total = 0;
$tax = 0.01;

foreach($products as $product){
     $total = $product->price * $product->qty;
     $tax_amount = $total * $tax;
     $total = $tax_amount + $total;
     $payment->add($product->name, $product->price);
}
Enter fullscreen mode Exit fullscreen mode

You could add things like coupons, discounts, promotions and what what but well I will leave that you.

Sending payment to PayNow

After adding payment the next stage is sending the payment to payment to PayNow

$response = $paynow->sendMobile($payment, '077777777', 'ecocash');
Enter fullscreen mode Exit fullscreen mode

Here were requesting for a mobile transaction to occur between our Paynow account and 0777777777 's Ecocash.

The number you get from user through forms or from database. The platform('ecocash') can be retrieved from user through forms or by auto detection

$phone = "07777777";
$platform = ""
if(str_starts_with($phone, '071')){
     $platform= "onemoney";
}else if(str_starts_with($phone, '073')){
    $platform = "telecash";
}else{
    $platform = "ecocash";
}

// Then 
$response = $paynow->sendMobile($payment, $phone, $platform);
Enter fullscreen mode Exit fullscreen mode

Your users will love this 😎😎😎

So this part alone should send a message to your customer mobile asking for payment:

Alt Text

Of course there will be some small differences.

Ok that's it for this one in the next tutorial we will talk about checking payments.

image

Discussion (0)