qbo-tech/paypal-checkout-sdk

PayPal Checkout REST API 的 PHP SDK

v1.0.2 2024-06-25 23:42 UTC

This package is not auto-updated.

Last update: 2024-09-18 23:11:43 UTC


README

此 SDK 已弃用;您仍可以使用它,但不会接受新的功能或支持请求。建议使用直接的 REST API 集成。请参阅有关使用 REST API 进行身份验证的 文档

PHP V2 REST API SDK

Home Image

为了统一跨各种渠道的支持,我们目前已关闭 GitHub 问题的功能。请访问 https://www.paypal.com/support 提交您的请求或在我们的社区论坛中提问。

欢迎使用 PayPal PHP SDK。此存储库包含 PayPal 的 PHP SDK 和用于 v2/checkout/ordersv2/payments API 的示例。

这是下一个主要 PayPal SDK 的一部分。它包括一个简化接口,仅提供简单的模型对象和 HTTP 调用的蓝图。此存储库当前包含 PayPal Checkout API 的功能,包括 Orders V2Payments V2

有关更多信息,请参阅 PayPal Checkout 集成指南

最新更新

从 2020 年 1 月开始,PayPal 将要求更新个人主页(PHP)Checkout 软件开发工具包(SDK)到版本 1.0.1。未将 PHP Checkout SDK 更新到版本 1.0.1 的商家将无法使用过时的 SDK 集成反序列化响应。所有 PHP Checkout SDK 集成预计将在 2020 年 3 月 1 日之前更新。鼓励商家尽快准备更新,以避免可能的服务中断。状态页已更新此信息。公告可在 此处 找到。

先决条件

PHP 5.6 及以上版本

支持 TLS 1.2 的环境(有关更多信息,请参阅 TLS-update 网站)

使用方法

二进制文件

使用 PayPal SDK 不必分支此存储库。您可以通过参考 PayPal Checkout 服务器 SDK 来配置和操作 SDK,而无需分支此代码。

要贡献或参考示例,您可以分支/引用此存储库。

设置凭据

通过访问 https://developer.paypal.com/developer/applications 并生成一个 REST API 应用来获取客户端 ID 和客户端密钥。从那里获取 客户端 ID密钥

require __DIR__ . '/vendor/autoload.php';
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
// Creating an environment
$clientId = "<<PAYPAL-CLIENT-ID>>";
$clientSecret = "<<PAYPAL-CLIENT-SECRET>>";

$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);

示例

创建订单

代码

// Construct a request object and set desired parameters
// Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
                     "intent" => "CAPTURE",
                     "purchase_units" => [[
                         "reference_id" => "test_ref_id1",
                         "amount" => [
                             "value" => "100.00",
                             "currency_code" => "USD"
                         ]
                     ]],
                     "application_context" => [
                          "cancel_url" => "https://example.com/cancel",
                          "return_url" => "https://example.com/return"
                     ] 
                 ];

try {
    // Call API with your client and get a response for your call
    $response = $client->execute($request);
    
    // If call returns body in response, you can get the deserialized version from the result attribute of the response
    print_r($response);
}catch (HttpException $ex) {
    echo $ex->statusCode;
    print_r($ex->getMessage());
}

示例输出

Status Code: 201
Id: 8GB67279RC051624C
Intent: CAPTURE
Gross_amount:
	Currency_code: USD
	Value: 100.00
Purchase_units:
	1:
		Amount:
			Currency_code: USD
			Value: 100.00
Create_time: 2018-08-06T23:34:31Z
Links:
	1:
		Href: https://api.sandbox.paypal.com/v2/checkout/orders/8GB67279RC051624C
		Rel: self
		Method: GET
	2:
		Href: https://www.sandbox.paypal.com/checkoutnow?token=8GB67279RC051624C
		Rel: approve
		Method: GET
	3:
		Href: https://api.sandbox.paypal.com/v2/checkout/orders/8GB67279RC051624C/capture
		Rel: capture
		Method: POST
Status: CREATED

捕获订单

在捕获之前,订单应由买家使用创建订单响应中返回的批准 URL 进行批准。

执行代码

use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
// Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
// $response->result->id gives the orderId of the order created above
$request = new OrdersCaptureRequest("APPROVED-ORDER-ID");
$request->prefer('return=representation');
try {
    // Call API with your client and get a response for your call
    $response = $client->execute($request);
    
    // If call returns body in response, you can get the deserialized version from the result attribute of the response
    print_r($response);
}catch (HttpException $ex) {
    echo $ex->statusCode;
    print_r($ex->getMessage());
}

示例输出

Status Code: 201
Id: 8GB67279RC051624C
Create_time: 2018-08-06T23:39:11Z
Update_time: 2018-08-06T23:39:11Z
Payer:
	Name:
		Given_name: test
		Surname: buyer
	Email_address: test-buyer@paypal.com
	Payer_id: KWADC7LXRRWCE
	Phone:
		Phone_number:
			National_number: 408-411-2134
	Address:
		Country_code: US
Links:
	1:
		Href: https://api.sandbox.paypal.com/v2/checkout/orders/3L848818A2897925Y
		Rel: self
		Method: GET
Status: COMPLETED

运行测试

要运行使用您的客户端 ID 和密钥的集成测试,请克隆此存储库并运行以下命令

$ composer install
$ CLIENT_ID=YOUR_SANDBOX_CLIENT_ID CLIENT_SECRET=OUR_SANDBOX_CLIENT_SECRET composer integration

示例

您可以从尝试 创建和捕获订单 开始

为了尝试不同样本以检查创建和授权意图,请点击此链接

注意:更新PayPalClient.php文件,使用您的沙盒客户端凭证,或者在执行样本时将客户端凭证作为环境变量传递。

许可证

代码在SDK 许可证下发布