如何通过 post 方法使 PayPalCheckout return 值?

How to make PayPalCheckout return value by post method?

我正在按照示例使用 PayPalCheckoutSdk 库,我有以下内容:

<?php
    require __DIR__ . '/PayPalCheckout/vendor/autoload.php';

    use PayPalCheckoutSdk\Core\PayPalHttpClient;
    use PayPalCheckoutSdk\Core\SandboxEnvironment;

    $clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

    $invoiceNumber = uniqid();
    
    $items = array();

    $items[0] = [
        'name' => 'HTML5',
        'description' => 'Video streaming service',
        'type' => 'SERVICE',
        'sku' => 'sku03',
        'unit_amount' =>
            [
                'currency_code' => 'USD',
                'value' => '90.00',
            ],
        'quantity' => '1',
        'category' => 'DIGITAL_GOODS',
    ];
    $new_item = [
        'name' => 'CSS3',
        'description' => 'Video streaming service',
        'type' => 'SERVICE',
        'sku' => 'sku02',
        'unit_amount' =>
            [
                'currency_code' => 'USD',
                'value' => '45.00',
            ],
        'quantity' => '2',
        'category' => 'DIGITAL_GOODS',
    ];

    array_push($items , $new_item);

    use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
    $request = new OrdersCreateRequest();

    $request->prefer('return=representation');

    $request->body = [
        'intent' => 'CAPTURE',
        'application_context' => [
            'brand_name' => 'COMPANY',
            'locale' => 'us-US',
            'user_action' => 'PAY_NOW',
            "cancel_url" => "http://localhost/PayPal/cancel.php",
            "return_url" => "http://localhost/PayPal/return.php",
            'landing_page' => 'BILLING',
        ],
        'purchase_units' => [0 => [
            'reference_id' => $invoiceNumber,
            'amount' => [
                'currency_code' => 'USD',
                'value' => '160.00',
                'breakdown' => [
                    'item_total' => [
                        'currency_code' => 'USD',
                        'value' => '180.00',
                    ],
                    'shipping_discount' => [
                        'currency_code' => 'USD',
                        'value' => '20.00',
                    ],
                ],
            ],
            'items' =>
                $items,
        ]],
    ];

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

        if ($response->statusCode == 201){
            for ($i = 0; $i < count($response->result->links); ++$i){
                $link = $response->result->links[$i];
                if ($link->rel =='approve') {
                    header("location: $link->href");
                }
            }
        } else {
            exit(1);
        }
    } catch (HttpException $ex) {
        echo $ex->statusCode;
        print_r($ex->getMessage());
    }
?>

我正在通过get方法接收数据print_r($_REQUEST);:

Array ( [token] => 3JX899952R0552721 [PayerID] => J95XSJRX4WXVS

并且,该信息在文件 return.php 中处理,该文件具有以下代码:https://ideone.com/ncVjIt

我希望能够通过post方法接收信息,我应该如何配置才能让数据通过post而不是get发送?

如评论中所述,您无法从 PayPal 改回重定向方法。它将始终是附加到您的 return_url.

的 GET 字符串

但是,理想且推荐的解决方案是不使用任何重定向。完全没有。相反,使用 PayPal-Checkout-SDK 你必须在你的服务器上创建两条路由,一条用于 'Create Order',一条用于 'Capture Order',documented here,仅 return JSON 数据(无 HTML 或文本)。后者应该(成功)在执行 return(特别是 purchase_units[0].payments.captures[0].id、PayPal 交易 ID)

之前将付款详细信息存储在您的数据库中

将这两个 JSON-only 路由与以下不使用任何重定向的审批流程配对,而是在付款审批期间始终让您的网站在后台加载(灯箱):https://developer.paypal.com/demo/checkout/#/pattern/server