Paypal支付-怎么获取不到订单id?

问题描述 投票:0回答:1

我正在编写向页面添加贝宝按钮的 WP 插件。我有一个 js 文件(来自贝宝网站的示例here)。在第一部分 (createOrder()) 中,我需要获取订单号:

    ```paypal.Buttons({
        createOrder() {   
            return fetch( dir + '/includes/api/create-paypal-order.php', {           
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({
              cart: [
                {
                  sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
                  quantity: "YOUR_PRODUCT_QUANTITY",
                },
              ],
            }),
            referrerPolicy: "origin-when-cross-origin" 
          })
          .then((response) => response.json())
          .then((order) => order.id)
          .then((order) => {console.log(order); return order;} );
        },
        // Finalize the transaction on the server after payer approval
        onApprove(order) {  
          ...
        },
      }).render('#paypal-button-container');```

还有 php 文件(create-paypal-order.php):

function authenticate()
{
    $ch = false;
    $client_id = "Aeh...bbX";
    $client_secret = "EPRl...yZFw";
    $http_header = array("Content-Type: application/json");
    $post_fields = 'grant_type=client_credentials';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

    $result = curl_exec($ch);
    $json = json_decode($result);
    $access_token = $json->access_token;
    curl_close($ch);
    return $access_token;
}

function create_order($access_token)
{
    $ch = false;
    $http_header = array
    (
        "Content-Type: application/json",
        "Authorization: Bearer " . $access_token,
        "PayPal-Request-Id: 7b92603e-77ed-4896-8e78-5dea2050476a",
    );
    $post_fields = '{}';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $result = curl_exec($ch);
    $json = json_encode($result);

    curl_close($ch);
    return $json;
}

$access_token = authenticate();
create_order($access_token);

这个变量 $post_fields(在函数 create_order 中)我从this page获得。 大约 10 天前,我设法获得了一个 order.id。然后,使用相同的代码,我无法获得 order.id。现在我收到错误消息“JSON 输入意外结束 在 h…IwSW2JlwdPGZBlSA_Moyiy5KHbbX¤cy=USD:3:91171”。 pic with error 我该如何解决这个错误并获取 order.id?也许php文件有问题?我在 php 文件中正确使用了“return”吗?

php paypal
1个回答
0
投票

使用浏览器开发工具“网络”选项卡观察单击按钮时路由

/includes/api/create-paypal-order.php
返回的内容。

返回体必须是JSON,而且只能是JSON。没有其他 HTML 或文本。

© www.soinside.com 2019 - 2024. All rights reserved.