revoltify/sslcommerz-php-library

用于SSLCommerz支付网关集成的PHP库

1.0.0 2024-08-21 03:17 UTC

This package is auto-updated.

Last update: 2024-09-21 03:31:48 UTC


README

这是一个用于集成SSLCommerz支付网关的PHP库。它为您的PHP应用程序提供了简单易用的接口,以通过SSLCommerz处理支付。

功能

  • 简单集成SSLCommerz支付网关
  • 易于使用的API进行支付处理
  • 支持沙箱和实时环境
  • 安全交易处理
  • 退款处理
  • 详细文档

安装

要安装此库,请使用Composer

composer require revoltify/sslcommerz-php-library

使用方法

1. 初始化支付

您可以通过初始化SSLCommerz类并提供必要的支付详细信息来创建一个新的支付会话。

<?php

require 'vendor/autoload.php';

use SSLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\PaymentParams;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);

    // Set up payment parameters
    $params = (new PaymentParams())
        ->setAmount(1000)  // Amount in BDT
        ->setCurrency('BDT')
        ->setTransactionId(uniqid())  // Unique transaction ID
        ->setSuccessUrl('https://yourdomain.com/success.php')
        ->setFailUrl('https://yourdomain.com/fail.php')
        ->setCancelUrl('https://yourdomain.com/cancel.php')
        ->setCustomerInfo('Customer Name', 'customer@example.com', '01700000000', 'Customer Address', 'Dhaka', 'Bangladesh')
        ->setProductInfo('Product Name', 'Electronics', 'general')
        ->setCustomValues('Ref001', 'Ref002', 'Ref003', 'Ref004');

    // Initiate payment and get the response
    $response = $sslcommerz->initiatePayment($params);

    // Redirect to the payment gateway
    header("location:" . $response['GatewayPageURL']);

} catch (SSLCommerzException $e) {
    // Handle any errors that occur during the payment initiation
    echo $e->getMessage();
}

2. 验证支付

支付处理完成后,SSLCommerz会向您的服务器发送通知。您可以使用以下代码验证支付:

<?php

require 'vendor/autoload.php';

use SSLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);
    
    // Validate the payment
    $response = $sslcommerz->validatePayment();
    
    // Output the validation response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors during payment validation
    echo $e->getMessage();
}

3. 处理IPN(即时支付通知)

支付完成后,SSLCommerz会向您的服务器发送IPN(即时支付通知)。您可以使用以下代码处理此通知并验证支付:

<?php

require 'vendor/autoload.php';

use SSLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);
    
    // Validate the payment received via IPN
    $response = $sslcommerz->validatePayment();
    
    // Output the IPN validation response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors during IPN validation
    echo $e->getMessage();
}

4. 退款支付

您可以使用refundPayment方法处理支付交易的退款。以下是一个示例:

<?php

require 'vendor/autoload.php';

use SSLCommerz\Exceptions\SSLCommerzException;
use SSLCommerz\SSLCommerz;

try {
    // Initialize SSLCommerz with your store ID, store password, and sandbox mode
    $sslcommerz = new SSLCommerz('your_store_id', 'your_store_password', true);

    // Refund a payment
    $bank_tran_id = 'TRANSACTION_ID';  // Replace with the actual bank_tran_id
    $refund_amount = 500;  // Amount to refund in BDT
    $refund_remarks = 'Customer request for refund';  // Optional refund remarks

    $response = $sslcommerz->refundPayment($bank_tran_id, $refund_amount, $refund_remarks);
    
    // Output the refund response
    echo "<pre>";
    print_r($response);

} catch (SSLCommerzException $e) {
    // Handle any errors that occur during the refund process
    echo $e->getMessage();
}

贡献

欢迎贡献!如果您发现任何问题或有任何功能请求,请打开一个问题或提交一个pull请求。

许可

此库是开源软件,许可协议为MIT。