PHP code example of ialtoobi / thawani

1. Go to this page and download the library: Download ialtoobi/thawani 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/ */

    

ialtoobi / thawani example snippets


THAWANI_MODE=test
THAWANI_LIVE_SECRET_KEY=your_live_secret_key
THAWANI_LIVE_PUBLISHABLE_KEY=your_live_publishable_key

use Ialtoobi\Thawani\ThawaniManager;
use Illuminate\Support\Facades\Session;

$thawani = new ThawaniManager();

$data = [ 
    "client_reference_id" => "123412",
    "mode" => "payment",
    "products" => [
        [
            "name" => "product 1",
            "quantity" => 1,
            "unit_amount" => 100,
        ],
    ],
    "success_url" => "https://company.com/success",
    "cancel_url" => "https://company.com/cancel",
    "metadata" => [
        "Customer name" => "ialtoobi",
        "order id" => 0,
    ],
];

$create_checkout_session_result = $thawani->createCheckoutSession($data);

if ($create_checkout_session_result) {

    $session_id = $create_checkout_session_result['session_id'];
    $redirect_url = $create_checkout_session_result['redirect_url'];

    // Save session_id for later use, such as confirming payment status
    Session::put('session_id', $session_id);

    return redirect($redirect_url);
}else {
    // Handle errors or unsuccessful attempts here, such as logging the error or notifying the user
}

// Assume $session_id is retrieved from a previous step or stored session
$session_id = 'your_session_id_here';

$retrieve_checkout_session_result  = $thawani->retrieveCheckoutSession($session_id);

$payment_status = $retrieve_checkout_session_result['data']['status'];

//Check payment status
if ($payment_status === 'paid') {
    // Handle successful payment
    // This could 

// Use a unique string to reference your customer 
$customer_data = [
    "client_customer_id": "[email protected]"
];

$create_customer_result = $thawani->createCustomer($customer_data);

//Save `customer_id` in the database to use for payment intent later
$customer_id = $create_customer_result['data']['id'];

$customer_id = 'your_customer_id_here';

$retrieve_customer_result = $thawani->retrieveCustomer($customer_id);

$data = $retrieve_customer_result['data'];

$customer_id = 'your_customer_id_here';

$delete_customer_result = $thawani->deleteCustomer($customer_id);

$description = $delete_customer_result['description'];

$customer_id = 'your_customer_id_here';

$customer_payment_method_result = $thawani->customerPaymentMethod($customer_id);

//Example: select first `payment_method_id` to create payment intent later
$payment_method_id = $customer_payment_method_result['data'][0]['id'];

$card_id = 'card_id';

$payment_method_result = $thawani->deletePaymentMethod($card_id);

$description = $payment_method_result['description'];

$paymentData = [
    "payment_method_id" => $payment_method_id, 
    "amount" => 100,
    "client_reference_id" => 1234,
    "return_url" => "https://company.com/success",
    "metadata" => [
        "customer_name" => 'Mohammed Al-Toubi',
        "order_id" => 1
    ]
];

$payment_intent_id = $thawani->createPaymentIntent($paymentData); 

$payment_intent_id = 'your_payment_intent_id_here';

$confirm_payment_intent_result = $thawani->confirmPaymentIntent($payment_intent_id);

if ($confirm_payment_intent_result) {
    
    $payment_id = $confirm_payment_intent_result['payment_id'];
    $redirect_url = $confirm_payment_intent_result['redirect_url'];

    // Save payment_id for later use, such as confirming payment status
    Session::put('payment_id', $payment_id);

    return redirect($redirect_url);
}else {
    // Handle errors or unsuccessful attempts here, such as logging the error or notifying the user
}

// Assume $payment_id is retrieved from a previous step or stored session
$payment_id = 'your_payment_id_here';

$retrieve_payment_intent_result  = $thawani->retrievePaymentIntent($payment_id);

$payment_status = $retrieve_payment_intent_result['data']['status'];

//Check payment status
if ($payment_status === 'succeeded') {
    // Handle successful payment
    // This could 

$payment_intent_id = 'your_payment_intent_id_here';

$cancel_payment_intent_result = $thawani->cancelPaymentIntent($payment_intent_id);

$description = $cancel_payment_intent_result['description'];
bash
php artisan vendor:publish --tag=config