Redirect to Success / Error page from OnError and OnApproval in PayPal SDK Client-Side

Viewed 169

I am trying to redirect the client to success or error page from the server. How can I do it with the OnError and OnApproval functions?

Here is what I tried to do:

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')

Here is the node server-side (There is more code in the server that approve the order etc.):

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

Thanks!! =)

1 Answers

res.sendFile('/error.html')

Why are you sending a file in your response? Shouldn't you be sending a string with a URL?

Then in the client you set window.location.href = <whatever string you build/receive>

Related