mardi 27 août 2019

How can I redirect only after transaction is finalized?

In the sample code I have for checking out with paypal, there is a redirect request to a route purchase/complete. I'd like to ensure that this route is requested only after the post request to /paypal/purchase/complete has been completed so that I can get the orderID created on the server side and pass it as a get parameter to the purchase/complete route so that it would resemble something like purchase/complete?orderId=XXXX. XXXX here would be the received data.orderID

Client side:

    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({

      // Set up the transaction
      createOrder: function(data, actions) {
        var payableAmount = $("#finalAmountTotalOrders").attr("amountData");
        return actions.order.create({
          purchase_units: [{
            amount: {
              value: payableAmount
            }
          }]
        });
      },

      // Finalize the transaction
      onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
          //will redirect user to custom page change values as desired
          window.location.replace("/purchase/complete");
          // Call your server to save the transaction
          return fetch('/paypal/purchase/complete', {
            method: 'post',
            headers: {
              'content-type': 'application/json',
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            body: JSON.stringify({
              orderID: data.orderID,
              details: details

            })

          });

        });
      }


    }).render('#paypal-button-container');

Edit:

The reason I'd like to do this is because although I am able to create an order and store it in my database once the /paypal/purchase/complete post request is made, I'm not sure when I would do something like email the customer and merchant with the ID of the order (database row) I just created. Trying to do it in the post request throws an error so if I could just get the ID on the /purchase/complete route, I can query the database and get the order details and email it. Any other approach would be useful to me.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire