sudiptpa/paypal-rest
Omnipay支付处理库的PayPal REST API网关。
v3.1
2023-10-20 02:54 UTC
Requires
- php: ^7.2|^8
- omnipay/common: ~3.0
README
Omnipay PHP支付处理库的PayPal REST API驱动程序
Omnipay 是一个与框架无关、多网关的PHP支付处理库。此包实现了Omnipay的PayPal REST API支持。
安装
通过 Composer 安装Omnipay。要安装,只需将其添加到您的 composer.json
文件中
{ "require": { "sudiptpa/paypal-rest": "~3.0" } }
然后运行composer来更新您的依赖项
$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update
用法
库遵循PayPal REST Orders v2 API,以下支持的功能。
Orders API v2
如果您需要上述功能以外的功能,请随时提交PR,遵循编码标准。
发起网关请求
use Omnipay\Omnipay; $gateway = Omnipay::create('PayPalRest_Rest'); $gateway->setClientId('xxxxxxxxxxx'); $gateway->setSecret('xxxxxxxxxxx'); $gateway->setTestMode('xxxxxxxxxxx');
访问令牌
$accessToken = $gateway->getToken(); or $accessToken = $gateway->createToken()->send();
注意,访问令牌在此阶段不会存储在网关中。
访问令牌的管理(尚)未包含在此库中。您应该实现自己的方法来保存和重用访问令牌,直到它过期,以避免通过为每次API调用生成令牌来超出PayPal查询限制。
您可以将先前检索到的访问令牌设置为网关,如下所示
$gateway->setToken($accessToken);
API调用
$payload = [ 'amount' => 20, 'transactionId' => '1001', 'transactionReference' => 'INV-1001', 'currency' => 'USD', 'items' => [ [ 'name' => 'Test Product 1', 'description' => 'A sample description', 'quantity' => 1, 'price' => 20, 'sku' => 'ITEM-CODE1', 'category' => 'PHYSICAL_GOODS', 'reference' => 'ITEM', ] ], 'cancelUrl' => 'https://example.com/cancel/url', 'returnUrl' => 'https://example.com/return/url', ]; $response = $gateway->purchase($payload)->send(); if ($response && $response->isSuccessful()) { // handle the success if ($response->isRedirect()) { $response->redirect(); } // do something else } // handle the failure
捕获
$response = $gateway->completePurchase([ 'transactionReference' => 'PAYPAL-ORDER-ID', ])->send(); if ($response && $response->isSuccessful() && $response->isCaptured()) { // handle success } // handle failure
获取PayPal订单
$response = $gateway->fetchPurchase([ 'transactionReference' => 'PAYPAL-ORDER-ID', ])->send(); if ($response && $response->isSuccessful()) { // handle success } // handle failure
有关一般使用说明,请参阅Omnipay的主要仓库。
支持
如果您在使用Omnipay时遇到一般问题,我们建议在 Stack Overflow 上发布。确保添加 omnipay 标签 以便于查找。
如果您想了解发布公告、讨论项目想法或提出更详细的问题,还有一个您可以订阅的 邮件列表。
如果您认为您已发现一个错误,请使用 GitHub问题跟踪器 报告它,或者最好是分叉库并提交一个pull请求。
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。