Tabby Payment Gateway Integration With Laravel


In this article, I explain how to integrate the Laravel app with tabby payment in simple steps.
step one:
you must have test keys .
public key test
secret key test
Or have live keys:
public key live
secret key live
contact Tabby to get it.
step two:
create a class called TabbyService.php on the App/Services path, I will use HTTP Client to send requests.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TabbyService
{
public $base_url = "https://api.tabby.ai/api/v2/";
public $pk_test = "pk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXX";
public $sk_test = "sk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXXX";
}
step three:
add the createSession function to make a payment and get a redirect URL.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TabbyService
{
public $base_url = "https://api.tabby.ai/api/v2/";
public $pk_test = "pk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXX";
public $sk_test = "sk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXXX";
public function createSession($data)
{
$body = $this->getConfig($data);
$http = Http::withToken($this->pk_test)->baseUrl($this->base_url);
$response = $http->post('checkout',$body);
return $response->object();
}
}
step four:
add the getSession function to check payment is successful, and use the secret key to check payment.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TabbyService
{
public $base_url = "https://api.tabby.ai/api/v2/";
public $pk_test = "pk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXX";
public $sk_test = "sk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXXX";
public function getSession($payment_id)
{
$http = Http::withToken($this->sk_test)->baseUrl($this->base_url);
$url = 'checkout/'.$payment_id;
$response = $http->get($url);
return $response->object();
}
}
step five:
add the config function.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TabbyService
{
public $base_url = "https://api.tabby.ai/api/v2/";
public $pk_test = "pk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXX";
public $sk_test = "sk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXXX";
public function getConfig($data)
{
$body= [];
$body = [
"payment" => [
"amount" => $data['amount'],
"currency" => $data['currency'],
"description" => $data['description'],
"buyer" => [
"phone" => $data['buyer_phone'],
"email" => $data['buyer_email'],
"name" => $data['full_name']
],
"shipping_address" => [
"city" => $data['city'],
"address" => $data['address'],
"zip" => $data['zip'],
],
"order" => [
"tax_amount" => "0.00",
"shipping_amount" => "0.00",
"discount_amount" => "0.00",
"updated_at" => now(),
"reference_id" => $data['order_id'],
"items" =>
$data['items']
,
],
"buyer_history" => [
"registered_since"=> $data['registered_since'],
"loyalty_level"=> $data['loyalty_level'],
],
],
"lang" => app()->getLocale(),
"merchant_code" => "your merchant_code",
"merchant_urls" => [
"success" => $data['success-url'],
"cancel" => $data['cancel-url'],
"failure" => $data['failure-url'],
]
];
return $body;
}
}
Step Six:
now you can see the full CLASS: TabbyService.php
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
class TabbyService
{
public $base_url = "https://api.tabby.ai/api/v2/";
public $pk_test = "pk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXX";
public $sk_test = "sk_test_XXXXXX-XXX-XXXX-XXX-XXXXXXXXXXXXXXX";
public function createSession($data)
{
$body = $this->getConfig($data);
$http = Http::withToken($this->pk_test)->baseUrl($this->base_url);
$response = $http->post('checkout',$body);
return $response->object();
}
public function getSession($payment_id)
{
$http = Http::withToken($this->sk_test)->baseUrl($this->base_url);
$url = 'checkout/'.$payment_id;
$response = $http->get($url);
return $response->object();
}
public function getConfig($data)
{
$body= [];
$body = [
"payment" => [
"amount" => $data['amount'],
"currency" => $data['currency'],
"description" => $data['description'],
"buyer" => [
"phone" => $data['buyer_phone'],
"email" => $data['buyer_email'],
"name" => $data['full_name']
],
"shipping_address" => [
"city" => $data['city'],
"address" => $data['address'],
"zip" => $data['zip'],
],
"order" => [
"tax_amount" => "0.00",
"shipping_amount" => "0.00",
"discount_amount" => "0.00",
"updated_at" => now(),
"reference_id" => $data['order_id'],
"items" =>
$data['items']
,
],
"buyer_history" => [
"registered_since"=> $data['registered_since'],
"loyalty_level"=> $data['loyalty_level'],
],
],
"lang" => app()->getLocale(),
"merchant_code" => "your merchant_code",
"merchant_urls" => [
"success" => $data['success-url'],
"cancel" => $data['cancel-url'],
"failure" => $data['failure-url'],
]
];
return $body;
}
}
usage:
make payment:
$items = collect([]); // array to save your products
// add first product
$items->push([
'title' => 'title',
'quantity' => 2,
'unit_price' => 20,
'category' => 'Clothes',
]);
$order_data = [
'amount'=> 199,
'currency' => 'SAR',
'description'=> 'description',
'full_name'=> 'ALi Omer',
'buyer_phone'=> '966500000001',
'buyer_email' => 'card.success@tabby.ai',
'address'=> 'Saudi Riyadh',
'city' => 'Riyadh',
'zip'=> '1234',
'order_id'=> '1234',
'registered_since' => $customer->created_at,
'loyalty_level'=> 0,
'success-url'=> route('success-url'),
'cancel-url' => route('cancel-url'),
'failure-url' => route('failure-url'),
'items' => $items,
];
$tabby = new TabbyService();
$payment = $tabby->createSession($order_data);
$id = $payment->id;
$redirect_url = $payment->configuration->available_products->installments[0]->web_url;
return redirect($redirect_url);
Here's a payment page:
Check Payment Status with ID.
$tabby = new TabbyService();
$order = Order::find(1);
$tabby_payemnt = $tabby->getSession($order->payment_id);
if (isset($tabby_payemnt ->payment) && $tabby_payemnt ->payment->status == "CLOSED") {
$order->status == 'PAID';
$order->paid == true;
$order->save();
}
Test Cards.
E-mail: card.success@tabby.ai
Rejected-Email: otp.rejected@tabby.ai
Phone 1: +966-50-0000001
Phone 2: +971-50-0000001
OTP: 8888
card number: 4111111111111111
expiration date: any future date
cvv: any number
Important Points:
make sure you add Webhooks, click here to see the doc
you must add a promo product to the product page details and cart page.
you can get all details in your email when contact Tabby.
for more information visit API Docs Of Tabby.
Subscribe to my newsletter
Read articles from Suhail Osman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
