美文网首页
php 实现PayPal支付

php 实现PayPal支付

作者: php转go | 来源:发表于2021-03-06 16:10 被阅读0次

网上找到好多PayPal的PHP写法都是基于REST API SDK for PHP
注意:官方已经开始弃用composer中的paypal/rest-api-sdk-php
源码:https://github.com/paypal/PayPal-PHP-SDK

现在官方建议用v2版本
https://github.com/paypal/Checkout-PHP-SDK
composer指令

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
        )

)

网页端访问links rel为approve的链接
https://www.sandbox.paypal.com/checkoutnow?token=9JY17372TM548415U

相关文章

网友评论

      本文标题:php 实现PayPal支付

      本文链接:https://www.haomeiwen.com/subject/xtqsqltx.html