将支付解决方案集成到您的 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中文网其他相关文章!