Перенаправление на страницу «Успех / Ошибка» из OnError и OnApproval в клиентской части PayPal SDK - PullRequest
1 голос
/ 04 марта 2020

Я пытаюсь перенаправить клиента на страницу успеха или ошибки с сервера . Как я могу сделать это с помощью функций 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')

Вот узел на стороне сервера (там больше кода на сервере, который одобряет заказ et c.):

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

Спасибо !! =)

1 Ответ

0 голосов
/ 05 марта 2020

res.sendFile('/error.html')

Почему вы отправляете файл в своем ответе? Разве вы не должны отправлять строку с URL-адресом?

Затем в клиенте вы устанавливаете window.location.href = <whatever string you build/receive>

...