將支付解決方案整合到您的 PHP 應用程式中變得更加容易。我們很高興地宣布發布 Pesapal PHP SDK,這是一個強大且用戶友好的庫,旨在簡化您與 Pesapal 支付網關的交互。
Pesapal 是非洲領先的支付平台,為各種規模的企業提供安全可靠的支付處理。然而,直接整合 Pesapal 的 API 可能既耗時又複雜。我們的 SDK 抽象化了 API 的複雜性,為開發人員提供了乾淨直覺的介面。
透過 Composer 安裝 SDK:
composer require katorymnd/pesapal-php-sdk
使用您的 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 旨在節省您的時間和精力,讓您專注於建立出色的應用程序,而無需擔心支付整合的複雜性。我們很高興看到您用它建造了什麼!
以上是Pesapal PHP SDK 簡介:簡化您的支付集成的詳細內容。更多資訊請關注PHP中文網其他相關文章!