Abdallah Nasir
Abdallah Nasir

Reputation: 64

cant redirect to success page in django paypal

i have a Django Store Website which use Paypal for payment

but in my views.py,Django do everything except go to the directed page that i choose this is my views.py

def capture(request,id):
    do some stuff
    return redirect(reverse("shop:success")) 
and this is JavaScript

<script type="text/javascript">

  function completeOrder(){
    var url = "{% url 'shop:paypal' id=orders.id %}"

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


     // Render the PayPal button into #paypal-button-container
     paypal.Buttons({
      style: {
        layout: 'horizontal',
        color:"blue",
        label:"checkout",
      tagline:"false",
      shape:"pill",
      size:"small",


    },
         // Set up the transaction
         createOrder: function(data, actions) {
             return actions.order.create({
                 purchase_units: [{ 
                     amount: {
                         value: '{{orders.converter}}'
                     }
                 }]
             });
         },

         // 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>

Upvotes: 0

Views: 409

Answers (1)

Preston PHX
Preston PHX

Reputation: 30477

Do not use actions.order.create() and actions.order.capture() to create+capture on the client side and then call a server with fetch after a client-side capture. This is thoroughly bad design when using a server.

Instead, switch to a proper server-side integration: make two routes on the server, one for 'Create Order' and one for 'Capture Order', documented here; there is a Checkout-Python-SDK you can use. These two routes should return only JSON data (no HTML or text). The latter one should (on success) store the payment details in your database before it does the return (particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID)

Pair your two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

In the success code of that flow, the redirect can be done with actions.redirect(), or simply setting window.location.href (general JavaScript solution)

Upvotes: 1

Related Questions