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/ */
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'];
$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