0

I am doing a Paypal integration to my web application. The code works, but I need to have a function that will automatically refresh the page after the payment. Where can I put the function for Auto-refresh the page and what would be the code? Apologies as I am new to JavaScript.

Additional info: The main language that I use is Python/Django Framework, but to integrate Paypal payment, I need to use this code from Sandbox.

  <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=PHP"></script>
  
  <script>
    function getCookie(name) {
      let cookieValue = null;
      if (document.cookie && document.cookie !== '') {
          const cookies = document.cookie.split(';');
          for (let i = 0; i < cookies.length; i++) {
              const cookie = cookies[i].trim();
              // Does this cookie string begin with the name we want?
              if (cookie.substring(0, name.length + 1) === (name + '=')) {
                  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                  break;
              }
          }
      }
      return cookieValue;
  }
  const csrftoken = getCookie('csrftoken');

    var total = '{{object.get_total}}'
    var orderID = '{{object.id}}'

    function completeOrder(){
      var url = "{% url 'complete' %}"

      fetch(url, {
        method: 'POST',
        headers:{
          'Content-type' : "application/json",
          'X-CSRFToken': csrftoken,
        },
        body: JSON.stringify({'orderID':orderID, 'total':total})
      })
    }

      // Render the PayPal button into #paypal-button-container
      paypal.Buttons({
  
        style: {
          color:  'blue',
          shape:  'pill',
          label:  'pay',
          height: 40
      },
          // Set up the transaction
          createOrder: function(data, actions) {
              return actions.order.create({
                  purchase_units: [{
                      amount: {
                          value: total
                      }
                  }]
              });
          },
  
          // Finalize the transaction
          onApprove: function(data, actions) {
              return actions.order.capture().then(function(details) {
                  // Show a success message to the buyer
                  completeOrder()
                  alert('Transaction completed by ' + details.payer.name.given_name + '!');
              });
          }
  
  
      }).render('#paypal-button-container');
  </script>
ian
  • 91
  • 1
  • 8

0 Answers0