首页  >  文章  >  后端开发  >  Pesapal PHP SDK 简介:简化您的支付集成

Pesapal PHP SDK 简介:简化您的支付集成

DDD
DDD原创
2024-11-24 00:19:10113浏览

Introducing the Pesapal PHP SDK: Simplify Your Payment Integration

将支付解决方案集成到您的 PHP 应用程序中变得更加容易。我们很高兴地宣布发布 Pesapal PHP SDK,这是一个强大且用户友好的库,旨在简化您与 Pesapal 支付网关的交互。

?为什么选择 Pesapal PHP SDK?

Pesapal 是非洲领先的支付平台,为各种规模的企业提供安全可靠的支付处理。然而,直接集成 Pesapal 的 API 可能既耗时又复杂。我们的 SDK 抽象了 API 的复杂性,为开发人员提供了干净直观的界面。

主要特点

  • 轻松身份验证:使用 Pesapal 简化 OAuth 身份验证。
  • 无缝支付集成:快速发起付款并处理回调。
  • 交易管理:轻松查看交易状态并处理退款。
  • 符合 PSR-4:遵循 PHP-FIG 自动加载标准。
  • 广泛的文档:有详细记录的方法和示例,可帮助您入门。

?入门

安装

通过 Composer 安装 SDK:

composer require katorymnd/pesapal-php-sdk

要求

  • PHP 8.0 或更高版本
  • 作曲家
  • Pesapal 商家帐户

配置

使用您的 Pesapal 凭据初始化客户端:

require 'vendor/autoload.php';

use Katorymnd\PesapalPhpSdk\Api\PesapalClient;
use Katorymnd\PesapalPhpSdk\Config\PesapalConfig;

$consumerKey = 'YOUR_CONSUMER_KEY';
$consumerSecret = 'YOUR_CONSUMER_SECRET';

// Initialize PesapalConfig and PesapalClient
$configPath = __DIR__ . '/../pesapal_dynamic.json';
$config = new PesapalConfig($consumerKey, $consumerSecret, $configPath);
$environment = 'sandbox';
$sslVerify = false; // Enable SSL verification for production


$pesapal = new PesapalClient($config, $environment, $sslVerify);


?发起付款

以下是您如何使用最少的代码发起付款:

use Katorymnd\PesapalPhpSdk\Utils\PesapalHelpers;

$merchantReference = PesapalHelpers::generateMerchantReference();
$notificationId = 'adbd39cc-a48e-4789-b42b-79ad8deb32df';  // Replace with actual notification ID from IPN registration

// Define the order data as an associative array for the POST request
$paymentDetails= [
    "id" => $merchantReference,
    "currency" => "USD",
    "amount" => 100.00,
    "description" => "Payment for invoice " . $merchantReference,
    "callback_url" => "https://www.example.com/payment-callback",
    "notification_id" => $notificationId,
    "redirect_mode" => "PARENT_WINDOW",
    "cancellation_url" => "https://www.example.com/payment-cancel",
    "billing_address" => [
        "phone_number" => "0700000000",
        "email_address" => "john.doe@example.com",
        "country_code" => "UG",
        "first_name" => "John",
        "middle_name" => "",
        "last_name" => "Doe",
        "line_1" => "123 Example Street",
        "line_2" => "",
        "city" => "Kampala",
        "state" => "KMP",
        "postal_code" => 256
    ]
];

// Obtain a valid access token
    $accessToken = $clientApi->getAccessToken();
    if (!$accessToken) {
        throw new PesapalException('Failed to obtain access token');
    }

// Submit order request to Pesapal
    $response = $clientApi->submitOrderRequest($orderData);

if ($response['status'] === 200 && isset($response['response']['redirect_url'])) {
        $redirectUrl = $response['response']['redirect_url'];
        $orderTrackingId = $response['response']['order_tracking_id'];

} else {
    // Handle errors
    $response['response']['error']
}

?检查交易状态

检查交易状态:

use Katorymnd\PesapalPhpSdk\Exceptions\PesapalException;

// Obtain a valid access token
    $accessToken = $clientApi->getAccessToken();
    if (!$accessToken) {
        throw new PesapalException('Failed to obtain access token');
    }

 // Get the transaction status
    $response = $clientApi->getTransactionStatus($orderTrackingId);

if ($response['status'] === 200 && isset($response['response'])) {
        $transactionStatusData = $response['response'];
}


?处理退款

轻松退款:

// Prepare refund data with user-provided values
$refundData = [
    'confirmation_code' => '7323605385336397404011', // the code is received by  checking the transaction status
    'amount' => 50.00,
    'username' => 'John Doe',
    'remarks' => 'Customer Requested Refund'
];

try {
    // Request Refund
    $refundResponse = $clientApi->requestRefund($refundData);

    if ($refundResponse['status'] === 200 && isset($refundResponse['response'])) {
        $refundDataResponse = $refundResponse['response'];

        // Add refund response to the output
        $responseData['refund_response'] = $refundDataResponse;
    } else {
        $errorMessage = $refundResponse['response']['error']['message'] ?? 'Unknown error occurred while requesting a refund.';
        throw new PesapalException($errorMessage);
    }
} catch (PesapalException $e) {
    // Add the error to the response
    $responseData['refund_error'] = [
        'error' => $e->getMessage(),
        'details' => $e->getErrorDetails(),
    ];
}


?测试与开发

我们提供了一套全面的测试套件,以确保 SDK 的可靠性。运行测试:

vendor/bin/phpunit

?文档

有关所有可用方法和功能的详细信息,请参阅我们的 GitHub 存储库。


?贡献

我们欢迎社区的贡献!请随意提交问题、分叉存储库并提出拉取请求。


?包裹配送

该 SDK 在 Packagist 上可用,可以轻松通过 Composer 包含在您的项目中。


?️持续集成

我们已经使用 GitHub Actions 设置了持续集成,以确保每次更新都保持最高的质量标准。


?结论

Pesapal PHP SDK 旨在节省您的时间和精力,让您专注于构建出色的应用程序,而无需担心支付集成的复杂性。我们很高兴看到您用它构建了什么!


?保持联系

  • GitHub:github.com/katorymnd/pesapal-php-sdk
  • Packagist:packagist.org/packages/katorymnd/pesapal-php-sdk

以上是Pesapal PHP SDK 简介:简化您的支付集成的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn