贝宝智能按钮服务器端集成在实时模式下失败

问题描述

我正在使用 PHP 开发一个应用程序,我必须将 PayPal 智能按钮集成到我的应用程序中。在沙盒模式下一切正常。但是当我把它转到现场或生产环境时。它给出了 JSON 错误

PayPal Smart Buttons returns me JSON error

这个答案在为沙箱和其他一切设置环境时对我帮助很大,这真的很好用。但仅适用于沙盒模式!

我在 paypal 上查看了帐户,在那里创建了交易,但没有将响应发送到服务器以进行进一步处理。它表示 API 设置成功并且密钥良好。但唯一的问题是我无法捕获响应并将其发送回服务器,这是我的代码

create-paypal-transaction.PHP

public static function createOrder($debug=false)
  {
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = self::buildrequestBody();
   // 3. Call PayPal to set up a transaction
    $client = PayPalClient::client();
    $response = $client->execute($request);

    // 4. Return a successful response to the client.
    $json_obj= array('id'=>$response->result->id);
    $jsonstring = json_encode($json_obj);
    echo $jsonstring;
  }

在客户端

createOrder: function() {
      return fetch('payments/create-paypal-transaction.PHP',{
        method: 'post',headers: {
          'content-type': 'application/json'
        },body: JSON.stringify($('#form_add').serializeObject())
      }).then(function(res) {
        console.log(res);
        return res.json();
      }).then(function(data) {
        console.log(data);
        return data.id;
      });

我使用 console.log() 来调试服务器响应,但它总是

{
...
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
...
}

控制台也显示以下错误

create_order_error 

click_initiate_payment_reject

Uncaught SyntaxError: Unexpected token < in JSON at position 0

感谢任何帮助:)

解决方法

我遇到了同样的问题,我读到您需要像这样将它们重定向到批准网址

    public static function createOrder($debug=false)
  {
    $request = new OrdersCreateRequest();
    $request->prefer('return=representation');
    $request->body = self::buildRequestBody();

    // 3. Call PayPal to set up a transaction
    $client = PayPalClient::client();
    $response = $client->execute($request);
    $res_links = $response->result->links;
    $approve_index = array_search('approve',array_column($res_links,'rel'));
    redirect($res_links[$approve_index]->href);

    // 4. Return a successful response to the client.
    $json_obj= array('id'=>$response->result->id);
    $jsonstring = json_encode($json_obj);
    echo $jsonstring;
   }

这最终会导致 cors 错误,当绕过 chrome 中的 cors 检查(这很糟糕)时,它只会关闭结帐弹出窗口,并且批准 url 的响应是页面的 html。

在开发控制台的网络选项卡中,检查 create-paypal-transaction.php 文件的响应。它会给你实际的错误 json > 错误是无关的。您也可以直接从浏览器访问该文件,看看您在那里遇到了什么错误。

如果你希望我也在努力让这个工作正常,你可以在不和谐时给我发消息。红#9269