php paypal支付接口文档,php 实现PayPal支付

网上找到好多PayPal的PHP写法都是基于REST API SDK for PHP

注意:官方已经开始弃用composer中的paypal/rest-api-sdk-php

源码:https://github.com/paypal/PayPal-PHP-SDK

composer require paypal/paypal-checkout-sdk

一般框架都会支持composer引入,如果不支持,则加上

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

代码实现

use PayPalCheckoutSdk\Core\PayPalHttpClient;

use PayPalCheckoutSdk\Core\SandboxEnvironment;

use PayPalCheckoutSdk\Orders\OrdersCreateRequest;

class PayPal

{

public function index(){

$clientId = 'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS';

$clientSecret = 'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL';

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

$client = new PayPalHttpClient($environment);

$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 {

$response = $client->execute($request);

print_r($response);

}catch (HttpException $ex) {

echo $ex->statusCode;

print_r($ex->getMessage());

}

}

}

打印出结果

PayPalHttp\HttpResponse Object

(

[statusCode] => 201

[result] => stdClass Object

(

[id] => 9JY17372TM548415U

[intent] => CAPTURE

[status] => CREATED

[purchase_units] => Array

(

[0] => stdClass Object

(

[reference_id] => test_ref_id1

[amount] => stdClass Object

(

[currency_code] => USD

[value] => 100.00

)

[payee] => stdClass Object

(

[email_address] => jaypatel512-facilitator@hotmail.com

[merchant_id] => 2BP3WP8PK6566

)

)

)

[create_time] => 2021-03-06T07:42:06Z

[links] => Array

(

[0] => stdClass Object

(

[href] => https://api.sandbox.paypal.com/v2/checkout/orders/9JY17372TM548415U

[rel] => self

[method] => GET

)

[1] => stdClass Object

(

[href] => https://www.sandbox.paypal.com/checkoutnow?token=9JY17372TM548415U

[rel] => approve

[method] => GET

)

[2] => stdClass Object

(

[href] => https://api.sandbox.paypal.com/v2/checkout/orders/9JY17372TM548415U

[rel] => update

[method] => PATCH

)

[3] => stdClass Object

(

[href] => https://api.sandbox.paypal.com/v2/checkout/orders/9JY17372TM548415U/capture

[rel] => capture

[method] => POST

)

)

)

[headers] => Array

(

[] =>

[Cache-Control] => max-age=0, no-cache, no-store, must-revalidate

[Content-Length] => 752

[Content-Type] => application/json

[Date] => Sat, 06 Mar 2021 07

[Paypal-Debug-Id] => 2b37472d909cd

)

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值