首頁  >  文章  >  後端開發  >  Pesapal PHP SDK 簡介:簡化您的支付集成

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

DDD
DDD原創
2024-11-24 00:19:10132瀏覽

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