【问题标题】:PayPal Smart Buttons Server Side Integration is failing on live modePayPal Smart Buttons 服务器端集成在实时模式下失败
【发布时间】:2021-04-22 06:16:16
【问题描述】:

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

PayPal Smart Buttons returns me JSON error

这个答案在为沙盒和其他所有东西设置环境时帮助了我很多,这真的很好。但仅适用于沙盒模式!

我检查了贝宝上的帐户,交易在那里创建,但没有响应发送到服务器进行进一步处理。说明 API 设置成功且密钥正常。但唯一的问题是我无法捕获响应并将其发送回服务器这是我的代码

创建-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

任何帮助都将不胜感激:)

【问题讨论】:

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


    【解决方案1】:

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

        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 错误,并且当绕过 cors 签入 chrome(这很糟糕)时,它只会关闭结帐弹出窗口,并且批准 url 的响应是页面的 html。

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 2020-04-07
      • 2013-09-05
      • 2020-06-21
      • 2013-04-13
      • 2021-01-28
      • 2021-04-04
      • 2018-02-21
      • 2016-02-05
      相关资源
      最近更新 更多