PHP code example of aghfatehi / laravel-tamara
1. Go to this page and download the library: Download aghfatehi/laravel-tamara library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aghfatehi / laravel-tamara example snippets
'providers' => [
Aghfatehi\Tamara\TamaraServiceProvider::class,
],
'aliases' => [
'Tamara' => Aghfatehi\Tamara\Facades\Tamara::class,
],
use Aghfatehi\Tamara\Facades\Tamara;
// Get available payment types
$types = Tamara::getPaymentTypes('SA', 'SAR', 500);
// Build checkout request body with example values
$requestbody = [
'total_amount' => [ // Order total amount
'amount' => 500,
'currency' => 'SAR',
],
'shipping_amount' => [ // Shipping cost
'amount' => 0,
'currency' => 'SAR',
],
'tax_amount' => [ // Tax amount
'amount' => 0,
'currency' => 'SAR',
],
'order_reference_id' => uniqid('tamara_'), // Unique order reference
'order_number' => 'ORD-' . time(), // Merchant order number
'items' => [ // Order items (max 50)
[
'name' => 'Order Payment',
'type' => 'Digital', // Digital or Physical
'reference_id' => '1', // Item ID in your system
'sku' => 'PAYMENT-001',
'quantity' => 1,
'unit_price' => [ // Price per unit
'amount' => 500,
'currency' => 'SAR',
],
'total_amount' => [ // quantity * unit_price
'amount' => 500,
'currency' => 'SAR',
],
],
],
'consumer' => [ // Customer details
'email' => '[email protected] ',
'first_name' => 'Ahmed',
'last_name' => 'Ali',
'phone_number' => '500000000', // Without country code prefix
],
'country_code' => 'SA', // SA, AE, KW, BH, QA, OM
'description' => 'Payment for order',
'merchant_url' => [ // Callback URLs (with optional reference_id)
'success' => route('tamara.callback', ['reference_id' => 'ORD-12345']),
'failure' => route('tamara.failure', ['reference_id' => 'ORD-12345']),
'cancel' => route('tamara.cancel', ['reference_id' => 'ORD-12345']),
'notification' => route('tamara.webhook'),
],
'payment_type' => 'PAY_BY_INSTALMENTS', // PAY_BY_INSTALMENTS or PAY_NEXT_MONTH
'instalments' => 3, // 3,4 or 6
'billing_address' => [ // Billing address
'city' => 'Riyadh',
'country_code' => 'SA',
'first_name' => 'Ahmed',
'last_name' => 'Ali',
'line1' => 'Default Address',
'phone_number' => '500000000',
],
'shipping_address' => [ // Shipping address
'city' => 'Riyadh',
'country_code' => 'SA',
'first_name' => 'Ahmed',
'last_name' => 'Ali',
'line1' => 'Default Address',
'phone_number' => '500000000',
],
'platform' => 'Laravel', // Platform name
'is_mobile' => false, // true if mobile app
'locale' => 'en_US', // en_US or ar_SA
];
// Create checkout session
$response = Tamara::createCheckout($requestbody);
// Redirect customer to Tamara checkout
if (isset($response['checkout_url'])) {
return redirect()->away($response['checkout_url']);
}
// Handle errors
if (isset($response['errors'])) {
$errorMessage = $response['errors'][0]['message'] ?? 'Payment failed';
return back()->with('error', $errorMessage);
}
// Payment Types
$types = Tamara::getPaymentTypes('SA', 'SAR', 500, '500000000');
// Checkout
$checkout = Tamara::createCheckout($data);
// Order Management
$order = Tamara::getOrder('order-id-here');
$order = Tamara::getOrderByReferenceId('ref-id-here');
// Authorise / Capture / Cancel / Refund
$authorised = Tamara::authoriseOrder('order-id');
$captured = Tamara::captureOrder('order-id', $data);
$cancelled = Tamara::cancelOrder('order-id', $data);
$refunded = Tamara::refundOrder('order-id', 500, 'SAR', 'Refund comment');
// Webhook Management
$webhook = Tamara::webhookRegister('https://example.com/webhook', [
'order_approved',
'order_declined',
'order_authorised',
'order_captured',
'order_refunded',
]);
$list = Tamara::webhookList();
$detail = Tamara::webhookGet('webhook-id');
Tamara::webhookDelete('webhook-id');
Tamara::webhookUpdate('webhook-id', 'https://example.com/webhook', [...]);
// Example: POST /tamara/pay with reference_id
$response = Http::post(url('/tamara/pay'), [
'amount' => 500,
'order_reference_id' => 'ORD-12345',
'reference_id' => 'combined_order_42',
'order_number' => 'INV-2025-001',
'item_reference_id' => 'product_1',
]);
// config/tamara.php
'routes' => [
'prefix' => 'payment/tamara', // Custom prefix
'middleware' => ['web', 'auth'], // Custom middleware
],
// Example webhook payload handling
public function webhook(Request $request)
{
$event = $request->input('event_type');
$orderId = $request->input('order_id');
$status = $request->input('status');
switch ($event) {
case 'order_approved':
// Mark order as approved
break;
case 'order_captured':
// Fulfill the order
break;
case 'order_refunded':
// Process refund
break;
}
}
bash
php artisan vendor:publish --tag=tamara-config
bash
php artisan vendor:publish --tag=tamara-migrations
php artisan migrate