jkbroot/thawani

一个用于与 Thawani 支付网关集成的 Laravel 扩展包。

1.1.1 2024-04-02 00:29 UTC

This package is auto-updated.

Last update: 2024-10-02 01:41:09 UTC


README

alt tag

jkbroot/thawani

此 Laravel 扩展包简化了与 Thawani 支付网关的集成,提供了一系列直观的功能,用于在 Laravel 应用程序中创建支付会话、管理客户和处理交易。

功能

  • 易于与 Laravel 应用程序集成
  • 支持创建和管理 Thawani 结算会话
  • 客户管理工具
  • 支付和退款处理
  • 支持支付意图

要求

  • PHP 7.3 或更高版本
  • Laravel 6.0 或更高版本

安装

composer require jkbroot/thawani

此包支持 Laravel 5.5 中引入的包自动发现。因此,在大多数情况下不需要手动注册 ServiceProvider。

如果您使用的是不支持自动发现的 Laravel 版本,或者已明确禁用它,则需要手动注册 ServiceProvider。

将 ServiceProvider 添加到 config/app.php 中的 providers 数组

'providers' => [
    // Other Service Providers

    Jkbroot\Thawani\ThawaniServiceProvider::class,
],

配置

运行以下 Artisan 命令以发布配置文件。这将发布 Thawani 配置文件到 config/thawani.php,您可以在其中自定义设置。

php artisan vendor:publish --tag=config

复制您提供的 Thawani Pay 配置片段并将其粘贴到您的 .env 文件中。但是,THAWANI_MODE 应该设置为 live 而不是 test 以用于生产环境。

#Thawani Pay Configuration for Production Environment
THAWANI_LIVE_SECRET_KEY=your_thawani_live_secret_key
THAWANI_LIVE_PUBLISHABLE_KEY=your_thawani_live_publishable_key
#Thawani mode change to (live) for production.
THAWANI_MODE=test

将 your_thawani_live_secret_key 和 your_thawani_live_publishable_key 替换为您的实际 Thawani live 密钥和可发布密钥。

实例化 ThawaniPay 类

要开始使用 Thawani 支付功能,您需要实例化 ThawaniPay 类。按照以下步骤操作

步骤 1:导入 ThawaniPay 类

在您创建 ThawaniPay 类的实例之前,请确保使用 use 语句将其导入到您的 PHP 文件中。

use Jkbroot\Thawani\ThawaniPay;

步骤 2:实例化 ThawaniPay 类

一旦类被导入,您可以使用以下代码来实例化它

$thawani = new ThawaniPay();

这创建了一个 ThawaniPay 类的实例,允许您使用其方法和属性在 Laravel 应用程序中处理 Thawani 支付。

附加说明

  • 在尝试使用之前,请确保已通过 Composer 安装了 jkbroot/thawani 包。
  • 请查阅包文档,了解任何特定配置、要求或包提供的附加功能。

如何使用

1 - 结算 & 会话

  • 创建会话
use Jkbroot\Thawani\ThawaniPay;

$thawani = new ThawaniPay();


$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" => "somename",
        "order id" => 0,
    ],
];

$session = $thawani->checkoutSessions->create($data);

创建会话响应

[
    "success" => true
    "code" => 2004
    "description" => "Session generated successfully"
    "data" => array:16 [
        "mode" => "payment"
        "session_id" => "checkout_0Cp4idrQaNFMws8YFJH020FJA3I4ITwkvmk2RxUc9Les5kY3ym"
        "client_reference_id" => "123412"
        "customer_id" => null
        "products" => [
          0 => [
            "name" => "product 1"
            "unit_amount" => 100
            "quantity" => 1
          ]
        ]
        "total_amount" => 100
        "currency" => "OMR"
        "success_url" => "https://company.com/success"
        "cancel_url" => "https://company.com/cancel"
        "return_url" => null
        "payment_status" => "unpaid"
        "invoice" => "20240228270008"
        "save_card_on_success" => false
        "metadata" => [
          "Customer name" => "somename"
          "order id" => "0"
        ]
        "created_at" => "2024-02-28T15:19:35.2937785Z"
        "expire_at" => "2024-02-29T15:19:35.2204113Z"
    ]
]
  • 检索会话
$session_id = "checkout_0Cp4idrQaNFMws8YFJH020FJA3I4ITwkvmk2RxUc9Les5kY3ym";
$session = $thawani->checkoutSessions->retrieve($session_id);

检索会话响应

[
  "success" => true
  "code" => 2000
  "description" => "session retrieved successfully"
  "data" =>  [
    "mode" => "payment"
    "session_id" => "checkout_0Cp4idrQaNFMws8YFJH020FJA3I4ITwkvmk2RxUc9Les5kY3ym"
    "client_reference_id" => "123412"
    "customer_id" => null
    "products" => [
      0 => [
        "name" => "product 1"
        "unit_amount" => 100
        "quantity" => 1
      ]
    ]
    "total_amount" => 100
    "currency" => "OMR"
    "success_url" => "https://company.com/success"
    "cancel_url" => "https://company.com/cancel"
    "return_url" => null
    "payment_status" => "unpaid"
    "invoice" => "20240228270008"
    "save_card_on_success" => false
    "metadata" =>[
      "Customer name" => "somename"
      "order id" => "0"
    ]
    "created_at" => "2024-02-28T15:19:35.2937785"
    "expire_at" => "2024-02-29T15:19:35.2204113"
  ]
]
  • 取消会话
$session_id = "checkout_0Cp4idrQaNFMws8YFJH020FJA3I4ITwkvmk2RxUc9Les5kY3ym";
$session = $thawani->checkoutSessions->cancel($session_id);

取消会话响应

[
  "success" => true
  "code" => 2105
  "description" => "Checkout session cancelled successfully"
]
  • 列出会话
$sessions = $thawani->checkoutSessions->list();

//for limit the lists and paginate between lists (limit,skip)
$sessions = $thawani->checkoutSessions->list(10,0);

列出会话响应

[
  "success" => true
  "code" => 2000
  "description" => "sessions retrieved successfully"
  "data" => [/*'array off Sessions'*/]
]
  • 创建结算 URL
$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" => "somename",
        "order id" => 0,
    ],
];

$checkoutUrl = $thawani->checkoutSessions->createCheckoutUrl($data);
  • 创建结算 URL 响应
[
  "session_id" => "checkout_5Q3IPYhiWaOIDotTqHJMjtaklHoHeWTjT6iTPaT0kQSTSZF3Za"
  "redirect_url" => "https://uatcheckout.thawani.om/pay/checkout_5Q3IPYhiWaOIDotTqHJMjtaklHoHeWTjT6iTPaT0kQSTSZF3Za?key=HGvTMLDssJghr9tlN9gr4DVYt0qyBy"
]
  • session_id:创建的结算会话的唯一标识符。
  • redirect_url:您应将客户重定向到的 URL,以完成支付过程。此 URL 将引导客户进入 Thawani 的支付网关,在那里客户可以安全地输入其支付详情。

2 - 客户

  • 创建客户
$data = [
        "client_customer_id" => "customer@example.com"
    ];
    
$customer = $thawani->customers->create($data);
  • 创建客户响应
[
  "success" => true
  "code" => 2001
  "description" => "Customer added successfully"
  "data" =>[
    "id" => "cus_pbW3HE7eG81zRvJX"
    "customer_client_id" => "customer@example.com"
  ]
]
  • 检索客户
$customerId = 'cus_pbW3HE7eG81zRvJX';
$customer = $thawani->customers->retrieve($customerId);
  • 检索客户响应
[
  "success" => true
  "code" => 2000
  "description" => "Customers retrieved successfully"
  "data" => [
    "id" => "cus_pbW3HE7eG81zRvJX"
    "customer_client_id" => "customer@example.com"
  ]
]
  • 删除客户
$customerId = 'cus_pbW3HE7eG81zRvJX';
$customer = $thawani->customers->delete($customerId);
  • 列出客户
$customers = $thawani->customers->list();

//for limit the lists and paginate between lists (limit,skip)
$customers = $thawani->customers->list(10,0);

3 - 支付意图

  • 列出支付意图
$paymentIntents = $thawani->paymentIntents->list();

//for limit the lists and paginate between lists (limit,skip)

$paymentIntents = $thawani->paymentIntents->list(10,0);
  • 列出支付意图响应
[
  "success" => true
  "code" => 2000
  "description" => "sessions retrieved successfully"
  "data" => [/*'array off Payment intents'*/]
]
  • 创建支付意图
$data = [
    "amount" => 100,
    "payment_method" => "card_zK5a7sd98wdwe78TbiSUyLUjann6xFx",
    "description" => "Payment for order 123412",
    "client_reference_id" => "123412",
    "return_url" => "https://thw.om/success",
    "metadata" => [
        "Customer name" => "somename",
    ],
];

$paymentIntent = $thawani->paymentIntents->create($data);
  • 创建支付意图响应
{
  "success": true,
  "code": 0,
  "description": "string",
  "data": {
    "id": "string",
    "client_reference_id": "string",
    "amount": 0,
    "currency": "string",
    "payment_method": "string",
    "next_action": {
      "url": "string",
      "return_url": "string"
    },
    "status": "requires_payment_method",
    "metadata": {},
    "created_at": "2019-08-24T14:15:22Z",
    "expire_at": "2019-08-24T14:15:22Z"
  }
}

以下是针对您Laravel包中集成Thawani支付网关所支持的类中每个方法的简洁说明

CheckoutSessions

  • create:初始化一个新的支付会话。
  • retrieve:获取特定会话的详细信息。
  • cancel:取消现有的会话。
  • list:列出所有会话,可选带有分页。
  • createCheckoutUrl:生成支付结算页面的URL。
  • retrieveByClientReference:使用客户端提供的参考ID检索会话。
  • retrieveByInvoice:使用发票号检索会话。

Customers

  • create:在支付系统中注册一个新的客户。
  • retrieve:获取特定客户的详细信息。
  • delete:从系统中删除客户。
  • list:列出所有客户,可选带有分页。

PaymentIntents

  • create:创建支付意向以启动交易。
  • retrieve:获取特定支付意向的详细信息。
  • cancel:取消未解决的支付意向。
  • list:列出支付意向,可选带有分页。
  • confirm:确认支付意向,尝试最终化交易。

PaymentMethods

  • list:列出与给定客户ID关联的所有支付方式。
  • delete:从客户的账户中删除指定的支付方式。

Refunds

  • create:为特定交易启动退款。
  • retrieve:获取特定退款的详细信息。
  • list:列出所有退款,可选带有分页。

本概览为您提供了Laravel包中集成Thawani支付服务可用功能性的快速指南。