从PayPal SDK客户端中的OnError和OnApproval重定向到成功/错误页面

时间:2020-03-04 14:58:34

标签: javascript node.js paypal paypal-rest-sdk

我正在尝试将客户端从服务器重定向到成功页面或错误页面 。 如何使用OnError和OnApproval函数?

这是我想要做的:

paypal
  .Buttons({
    style: {
      color: 'blue',
    },
    onInit: function(data, actions) {
      actionStatus = actions
      actionStatus.disable()
    },
    onClick: function(data, actions) {
      url = searchInput.value
      const isURL = validURL(url)
      if (isURL) {
        // Remove Existing Error message
        if (searchInput.nextElementSibling.classList[1]) {
          searchInput.nextElementSibling.classList.remove('err-message--show')
        }

        // Calculate the price
        const photoAmount = document.querySelector('.header__photos').value * 1
        price = String(0.9 * photoAmount)        

        // Enable The Paypal Button
        actionStatus.enable()
      } else {
        searchInput.nextElementSibling.classList.add('err-message--show')
      }
    },
    enableStandardCardFields: true,
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [
          {
            amount: {
              value: price,
              currency_code: 'ILS'
            }
          }
        ]
      })
    },
    onApprove: function(data, actions) {
      // Where is should redirect?
      return actions.order.capture().then(function(details) {
        alert('Transaction completed by ' + details.payer.name.given_name)
        // Call your server to save the transaction
        return axios.post('/paypal-transaction-complete', {
          orderID: data.orderID,
        })
      })
    },
    onError: function(err) {
      // Show an error page here, when an error occurs
      console.log('error')
     // Redirect to an Error Page...
     // ???
      }
  })
  .render('#paypal-button-container')

这是节点服务器端(服务器中有更多代码可以批准订单等):

app.get('/error', (req,res) => {
  res.sendFile('/error.html')
})
app.get('/success', (req,res) => {
  res.sendFile('/success.html')
})

谢谢! =)

1 个答案:

答案 0 :(得分:0)

res.sendFile('/error.html')

为什么要在响应中发送文件?您不应该发送带有URL的字符串吗?

然后在您设置的客户端window.location.href = <whatever string you build/receive>

相关问题