【问题标题】:With PayPal JS SDK, how to conditionally 'run' the onApprove part?使用 PayPal JS SDK,如何有条件地“运行”onApprove 部分?
【发布时间】:2022-01-21 13:29:18
【问题描述】:

我已经使用 PayPal JS SDK 和相应的 PHP SDK 实现了一个沙盒环境。

在 JavaScript 中我使用:

paypal.Buttons({
  createOrder: function(data, actions) {
    return fetch('../checkout-sdk/samples/CaptureIntentExamples/CreateOrder.php', {
      method: 'post',
      headers: {
        'content-type': 'application/json'
      }
    }).then(function(resp) {
      respjs = resp.json();
      return respjs;
    }).then(function(orderData) {
      order_id = orderData.id;
      return order_id;
    });
  },

  onApprove: function(data, actions) {
    return fetch('../checkout-sdk/samples/CaptureIntentExamples/CaptureOrder.php?order_id='+data.orderID, {
      method: 'post',
      headers: {
        'content-type': 'application/json'
      },
      body: JSON.stringify({
        orderID: data.orderID
      })
    }).then(function(appr_res) {
      apprjs = appr_res.json();
      return apprjs;
    }).then(function(orderData) {
      var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

      if (errorDetail) {
        if (errorDetail.issue === 'INSTRUMENT_DECLINED') {
          return actions.restart(); // Recoverable state, per:
          // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
        } else {
          var msg = 'Sorry, your transaction could not be processed.';
          if (errorDetail.description) msg += '\n\n' + errorDetail.description;
          if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
          return alert(msg); // Show a failure message
        }
      }

      // Successful capture! For demo purposes:
      console.log('Capture result', JSON.stringify(orderData, null, 2));
    })
    .catch(function(error) {
      console.error("There was a problem with the approval fetch: ", error);
    });
  },
  onError: function(err) {
  alert(err);
  }
}).render('#paypal-button-container');

当函数 createOrder 失败时,PHP 脚本 CreateOrder.php 应该向 javascript SDK 返回一条消息,以便通知客户。

失败时,onApprove 不需要“运行”。 如何更改代码以有条件地“运行” onApprove 函数?

我在 JSLint 上尝试过类似下面的操作:

paypal.Buttons({
  createOrder: function(data, actions) {
    return fetch(url, {
      method: "post"
    }).then(function(res) {
      return res.json();
    }).then(function(orderData) {
      return orderData.id;
    });
  },

  if (orderData !== false) { // error: Expected ')' and instead saw '!=='.
    onApprove: function(data, actions) {
      return fetch(url2 + data.orderID, {
        method: "post"
      }).then(function(res) {
        return res.json();
      }).then(function(orderData) {
        //
      });
    },
  }
}).render("#paypal-button-container");

但 JSLint 报告:应为 ')' 而看到的是 '!=='。

如何有条件地实现 if-then 条件?

【问题讨论】:

    标签: paypal


    【解决方案1】:

    onApprove 已经以创建成功和客户成功批准订单为条件。否则不会调用它。你不需要做任何事情。

    【讨论】:

    • 哦,这是个好消息。谢谢你的解释。
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2021-08-09
    • 2020-06-20
    • 2021-12-24
    • 2021-01-30
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多