8ctopus / paypal-rest-api
使用 PayPal REST API 处理支付和订阅
2.2.0
2024-05-24 07:19 UTC
Requires
- php: >=8.1
Requires (Dev)
- clue/commander: ^1.4
- friendsofphp/php-cs-fixer: ^3.8
- hassankhan/config: ^3.1
- nimbly/shuttle: ^1.0
- nunomaduro/collision: ^6.2
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0|^10.0
README
使用 PSR-7
、PSR-17
和 PSR-18
实现 PayPal REST API 的 PHP 实现。
该包仍在开发中,欢迎贡献力量。目前,它涵盖了 Orders
(一次性支付)、订阅(Products
、Plans
和 Subscriptions
)以及 web hooks
(在特定事件发生时从 PayPal 接收通知)。这就是创建商店所需的一切,无论是一次性支付还是基于订阅。
安装包
composer require 8ctopus/paypal-rest-api
开始之前
将 .env.example
复制到 .env
并填写您的 PayPal REST API 凭证。如果您还没有凭证,请遵循指南
https://developer.paypal.com/api/rest/
演示
以下是一个代码示例,展示了如何进行一次性支付。要查看所有可能,请运行 demo.php
。还有使用此包的演示商店,请查看 PayPal 沙盒。
use HttpSoft\Message\RequestFactory; use HttpSoft\Message\StreamFactory; use Nimbly\Shuttle\Shuttle; use Oct8pus\PayPal\Orders; use Oct8pus\PayPal\Orders\Intent; use Oct8pus\PayPal\OAuth; use Oct8pus\PayPal\HttpHandler; require_once __DIR__ . '/vendor/autoload.php'; $handler = new HttpHandler( // PSR-18 http client new Shuttle(), // PSR-17 request factory new RequestFactory(), // PSR-7 stream factory new StreamFactory() ); $sandbox = true; // get authorization token $auth = new OAuth($sandbox, $handler, 'rest.id', 'rest.pass'); $orders = new Orders($sandbox, $handler, $auth); // create order $response = $orders->create(Intent::Capture, 'USD', 10.0); // you must redirect the user to approve the payment before you can capture $redirectUrl = "https://www.sandbox.paypal.com/checkoutnow?token={$response['id']}"; ... // once the user has approved the payment, capture it $response = $orders->capture($args['id']); if ($response['status'] === 'COMPLETED') { echo 'payment processed!'; }
运行测试
composer test
参考
- PayPal REST API 官方文档:https://developer.paypal.com/api/rest/
- PayPal REST 存档 PHP SDK:https://github.com/paypal/PayPal-PHP-SDK/