8ctopus/paypal-rest-api

使用 PayPal REST API 处理支付和订阅

2.2.0 2024-05-24 07:19 UTC

This package is auto-updated.

Last update: 2024-09-01 13:57:22 UTC


README

packagist downloads min php version license tests code coverage badge lines of code

使用 PSR-7PSR-17PSR-18 实现 PayPal REST API 的 PHP 实现。

该包仍在开发中,欢迎贡献力量。目前,它涵盖了 Orders(一次性支付)、订阅(ProductsPlansSubscriptions)以及 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

参考