Skip to content
Home ยป Blog ยป Paynow Integration with PHP

Paynow Integration with PHP

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

PHP
// 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'
);

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:

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

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:

PHP
$payment->add('Sadza and Beans', 1.25);

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

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

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

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

PHP
$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);
}

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

PHP
$response = $paynow->sendMobile($payment, '077777777', 'ecocash');

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

PHP
$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);

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