【问题标题】:PayPal Smart Buttons and REST Checkout SDK - SyntaxError: Unexpected end of JSON input OR Expected an order id to be passedPayPal 智能按钮和 REST Checkout SDK - SyntaxError:JSON 输入意外结束或期望传递订单 ID
【发布时间】:2021-01-28 09:48:30
【问题描述】:

我正在尝试使用REST checkout SDK 实现 PayPal 的智能按钮,并使用服务器端调用 API。

但是我对其中的 createOrder 部分有一些问题。

JS代码:

paypal.Buttons({
    style: {
        size: 'responsive',
        layout: 'vertical'
    },
    createOrder: function() {
        return fetch(blm_custom_vars.wp_home + 'classes/paypal/paypal-create-order.php', {
            method: 'post',
            headers: {
                'content-type': 'application/json'
            }
        }).then(function(res) {
            return res.json();
        }).then(function(data) {
            return data.id;
        });
    },
    onApprove: function(data) {
        return fetch(blm_custom_vars.wp_home + 'classes/paypal/paypal-capture-order.php', {
            method: 'post',
            headers: {
                'content-type': 'application/json'
            },
            body: JSON.stringify({
                orderID: data.id
            })
        }).then(function(res) {
            return res.json();
        }).then(function(details) {
            alert('Transaction funds captured from ' + details.payer_given_name);
        })
    },
}).render('.purchase-modal');

PHP 代码来自 paypal-create-order.php:

namespace Sample\CaptureIntentExamples;

require(__DIR__ . '/paypal-client.php');

use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use Sample\PayPalClient;

class CreateOrder {

    /**
     * This is the sample function to create an order. It uses the
     * JSON body returned by buildRequestBody() to create an order.
     */

    public static function createOrder($debug = false) {

        $request = new OrdersCreateRequest();
        $request->prefer('return=representation');
        $request->body = self::buildRequestBody();

        // Call PayPal to set up a transaction
        $client = PayPalClient::client();
        $response = $client->execute($request);

        if ($debug) {

            print "Status Code: {$response->statusCode}\n";
            print "Status: {$response->result->status}\n";
            print "Order ID: {$response->result->id}\n";
            print "Intent: {$response->result->intent}\n";
            print "Links:\n";

            foreach ($response->result->links as $link) {
                print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
            }

            // To print the whole response body, uncomment the following line
             echo json_encode($response->result, JSON_PRETTY_PRINT);

        }

        return $response;

    }

    /**
     * Setting up the JSON request body for creating the order with minimum request body. The intent in the
     * request body should be "AUTHORIZE" for authorize intent flow.
     *
     */

    private static function buildRequestBody() {

        return array(
            'intent' => 'CAPTURE',
            'application_context' =>
                array(
                    'shipping_preference' => 'NO_SHIPPING',
                    'user_action' => 'PAY_NOW',
                    'payment_method' => array(
                        'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED',
                    ),
                    //'custom_id' => '',
                    'return_url' => 'https://example.com/return',
                    'cancel_url' => 'https://example.com/cancel'
                ),
            'purchase_units' => array(
                    0 => array(
                            'amount' => array(
                                    'currency_code' => 'AUD',
                                    'value' => '70.00',
                                    'breakdown' => array(
                                        'item_total' => array(
                                            'currency_code' => 'AUD',
                                            'value' => '75.00',
                                        ),
                                        'discount' => array(
                                            'currency_code' => 'AUD',
                                            'value' => '5.00',
                                        ),
                                    ),
                            ),
                            'description' => 'something',
                            'items' => array(
                                0 => array(
                                    'name' => 'something',
                                    'unit_amount' => array(
                                        'currency_code' => 'AUD',
                                        'value' => '75.00',
                                    ),
                                    'quantity' => 1,
                                    'category' => 'DIGITAL_GOODS',
                                ),
                            ),
                    )
            )
        );

    }

}


/**
 * This is the driver function that invokes the createOrder function to create
 * a sample order.
 */

$debug = (ACTIVE_SERVER == 'dev') ? true : false;

if (!count(debug_backtrace())) {
    CreateOrder::createOrder($debug);
}

现在,当我有$debug = true 时,服务器会输出看似正确的响应。那里什么都没有。虽然我必须关闭调试以避免其他 JSON 错误。

所以一旦我关闭它,我就会收到错误:

SyntaxError: JSON 输入意外结束

我认为这是因为它是一个空页面,因为 createOrder 脚本 返回 数据而不是输出数据?但这就是 PayPal 所说的。

所以我尝试将其更改为:echo json_encode($response);,然后我得到另一个错误:

期望传递一个订单id

在我原来的 JS 中是这样的:

}).then(function(data) {
    return data.orderID;
});

...但后来意识到 createOrder 脚本返回的数组引用了id 而不是orderID,所以我将其更改为return data.id;,但它没有帮助。

我在这里做错了什么?

【问题讨论】:

    标签: javascript php json paypal paypal-rest-sdk


    【解决方案1】:

    只需确保您正在回显/返回整个响应,或者至少是 {"id":"......"} 的内容

    您可以在浏览器的“开发工具网络”选项卡中查看正在发生的事情。

    所以从 PHP,做:

    echo json_encode($response->result, JSON_PRETTY_PRINT);
    

    ..应该没问题,但在 $debug=true if 块下方/之外,并且在其上方或周围没有任何其他打印或回显行,因为您必须输出有效的 JSON,并且只输出有效的 JSON(对于 JS createOrder 获取到您的服务器以便能够解析它)

    【讨论】:

    • 好的,我明白了。由于 PayPal 提供的示例代码返回 $response 我试图回应它,这导致了问题。但是将其更改为$response->result 似乎已经修复了它。我假设 return $response 可以被这个删除/替换?
    • 函数可以返回对象,以备调用者需要使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2020-10-10
    • 2021-05-01
    • 2020-08-07
    • 2020-08-17
    • 2018-08-24
    相关资源
    最近更新 更多