morcano/paypal-checkout-sdk

PayPal的Checkout REST API的PHP SDK

dev-master 2021-05-15 12:27 UTC

This package is not auto-updated.

Last update: 2024-09-30 03:36:38 UTC


README

Home Image

欢迎使用PayPal PHP SDK。本仓库包含PayPal的PHP SDK以及用于v2/checkout/ordersv2/payments API的示例。

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

请参阅PayPal Checkout集成指南以获取更多信息。有关设置SDK的更多信息,请参阅设置您的SDK

最新更新

从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更新网站)

安装

使用以下命令将SDK添加到您的composer包中

composer require paypal/paypal-checkout-sdk

之后,只需添加

require __DIR__ . '/vendor/autoload.php';

如果没有为项目中其他依赖项添加

使用方法

二进制文件

使用PayPal SDK不需要分支此仓库。您可以参考PayPal Checkout Server 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
$ cp phpunit.xml.dist phpunit.xml

现在将客户端ID和客户端密钥添加到phpunit.xml中,然后可以执行测试。

$ composer full
$ composer integration
$ composer unit

示例

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

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

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

许可证

代码在SDK 许可证下发布