【问题标题】:How to properly add headers, paypal tutorial?如何正确添加标题,贝宝教程?
【发布时间】:2021-03-26 20:18:12
【问题描述】:

我正在做贝宝教程服务器端集成。 用 PHP。 我遇到了问题。

我想测试融资失败,最后一部分 https://developer.paypal.com/docs/business/checkout/server-side-api-calls/handle-funding-failures/

我想我在正确的位置添加了模拟标题,但什么也没做,它一直说 Transaction completed 。 正确的放置方式是什么? 贝宝说:

In the call to Capture payment for order, include the PayPal-Mock-Response header with the mock_application_codes value set to INSTRUMENT_DECLINED. Sample code: -H "PayPal-Mock-Response: {"mock_application_codes" : "INSTRUMENT_DECLINED"}"
<script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({

      // Call your server to set up the transaction
      createOrder: function() {
        return fetch('examplecreateorder.php', {
          method: 'post',
          headers: {
            'content-type': 'application/json'
          }
        }).then(function(res) {
          return res.json();
        }).then(function(data) {
        
          return data.id;
        });
      },
      // Call your server to finalize the transaction
      onApprove: function(data) {
        return fetch('examplecaptureorder.php', {
            method: 'post',
            headers: {
              'content-type': 'application/json',
              'PayPal-Mock-Response': {
                'mock_application_codes': 'INSTRUMENT_DECLINED'
              }
            },
            body: JSON.stringify({
              orderID: data.orderID
            })

          }

        ).then(function(res) {

          return res.json().catch(error => console.error('Error:', error));
        }).then(function(orderData) {
          // Three cases to handle:
          //   (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
          //   (2) Other non-recoverable errors -> Show a failure message
          //   (3) Successful transaction -> Show confirmation or thank you

          // This example reads a v2/checkout/orders capture response, propagated from the server
          // You could use a different API or structure for your 'orderData'
          var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

          if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
            return actions.restart(); // Recoverable state, per:
            // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
          }

          if (errorDetail) {
            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
          }

          // Show a success message

          alert('Transaction completed by  + orderData.payer.name.given_name);
 
        })
      }
    }).render('#paypal-button-container');
  </script>

【问题讨论】:

标签: javascript php paypal paypal-sandbox


【解决方案1】:

在标题中,PayPal-Mock-Response 的值应该只是一个字符串而不是对象:

... 
headers: {
    'content-type': 'application/json',
    'PayPal-Mock-Response': '{"mock_application_codes" : "INSTRUMENT_DECLINED" }'
},
...

注意 OPs 代码中作为对象字面量指定的内容之间的区别:

'PayPal-Mock-Response': {
    'mock_application_codes': 'INSTRUMENT_DECLINED'
}

以及将PayPal-Mock-Response 的值视为字符串的工作代码:

'PayPal-Mock-Response': '{"mock_application_codes" : "INSTRUMENT_DECLINED" }'

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 2021-12-05
    • 2011-08-01
    • 2014-09-06
    • 2023-03-16
    • 2012-03-23
    • 2014-03-08
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多