ialtoobi / thawani
一个用于集成Thawani支付网关的Laravel包。
Requires
- php: ^7.3|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Thawani Manager是一个Laravel包,旨在简化与Thawani支付网关API的交互,使您能够轻松地将Thawani支付解决方案集成到您的Laravel应用程序中。
特性
- 创建和管理结账会话
- 处理客户信息和支付方式
- 生成支付URL以将用户重定向到Thawani支付页面
- 通过Thawani的API处理支付
安装
1. 需求包
使用Composer将包添加到您的Laravel项目中
composer require ialtoobi/thawani
或者使用GIT克隆包
git clone https://github.com/ialtoobi/Thawani-Manager.git 或通过点击这里下载归档。
2. 发布配置
发布包配置以自定义设置(模式、密钥、可发布密钥)
php artisan vendor:publish --tag=config
3. 配置环境变量
要配置您的应用程序以使用Thawani Pay,您需要在您的.env文件中设置以下变量。这些设置将指定您的应用程序是在测试模式还是生产模式下运行,并提供生产环境所需的API密钥。
-
THAWANI_MODE: 根据您的环境将其设置为'test'或'live'。这将确定ThawaniManager将使用哪些API密钥和端点集。
-
THAWANI_LIVE_SECRET_KEY: 这是Thawani Pay提供给您的生活密钥。它用于对Thawani的API请求进行服务器端身份验证。
-
THAWANI_LIVE_PUBLISHABLE_KEY: 这是Thawani Pay提供给您的生活可发布密钥。它用于对客户端请求进行身份验证。
THAWANI_MODE=test THAWANI_LIVE_SECRET_KEY=your_live_secret_key THAWANI_LIVE_PUBLISHABLE_KEY=your_live_publishable_key
使用
创建结账会话以生成支付URL
要创建新的结账会话并将用户重定向到Thawani支付页面
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 }
检索结账会话
要检索现有结账会话的详细信息,请使用session_id
// 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 include updating order status in your database }
管理客户
创建客户以保存卡片进行后续支付
请确保在创建结账会话时提供创建的customer_id。
// Use a unique string to reference your customer $customer_data = [ "client_customer_id": "customer_email@mail.com" ]; $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
$customer_id = 'your_customer_id_here'; $retrieve_customer_result = $thawani->retrieveCustomer($customer_id); $data = $retrieve_customer_result['data'];
要删除客户,请使用customer_id
$customer_id = 'your_customer_id_here'; $delete_customer_result = $thawani->deleteCustomer($customer_id); $description = $delete_customer_result['description'];
处理支付
要获取与客户相关的已保存支付方式的列表,请使用customer_id
$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 = '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 }
要检索支付意向的详细信息,请使用payment_intent_id
// 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 include updating order status in your database }
要取消支付意向,请使用payment_intent_id
$payment_intent_id = 'your_payment_intent_id_here'; $cancel_payment_intent_result = $thawani->cancelPaymentIntent($payment_intent_id); $description = $cancel_payment_intent_result['description'];
支持
有关问题、问题或贡献,请在此GitHub存储库中打开问题: 点击这里
关注我于X @ialtoobi
许可
Thawani Manager是一个开源包,许可协议为MIT。