OnApprove Posting all Parameters to PHP page (redirect - nonAjax)

rohitvedantwar
Contributor
Contributor

Hello,

 

Is it possible to post all parameters to a different PHP page after Payment is done.

In the documentation, the parameters are passed via Ajax and result is fetched back.

However, I want to transfer all parameters to a different PHP page where the processing will happen and Thank-you message will be shown.

 

Thanks

 

paypal.Buttons({
   createOrder: function(data, actions) {
     var add1 = '"'+$("#shipping_address_1").val()+'"';
     var add2= '"'+$("#shipping_address_2").val()+'"';
     var admin2= '"'+$("#shipping_state").val()+'"';
     var admin1= '"'+$("#shipping_city").val()+'"';
     var postal= '"'+$("#shipping_postcode").val()+'"';
     var country= '"'+$("#shipping_country_id").val()+'"';


     console.log("It right"+add1);
     return actions.order.create({
       payer: {
         name: {
           given_name: "",
           surname: ""
         },
         email_address: "",
         //payer_id: "DFRDTDRHJY",
         //country_code: "US",
         //national_number[Removed. Phone #s not permitted]
         
         // phone: {
         //   country_code: "+91",
         //   national_number[Removed. Phone #s not permitted]
         // }
       },
       purchase_units: [{
         shipping: {
           address: {
             address_line_1: add1,
             address_line_2: add2,
             admin_area_2: admin2,
             admin_area_1: admin1,
             postal_code: postal,
             country_code: "IN"
           }
         },
         amount: {
           value: ''
         },
         // payee: {
         //   email_address: ""
         // }
       }]
     });
   },
   onApprove: function(data, actions) {
     return actions.order.capture().then(function(details) {
       //alert('Transaction completed by ' + details.payer.name.given_name);
       // Call your server to save the transaction
       return fetch('confirm-order/paypal', {
         method: 'post',
         headers: {
           'content-type': 'application/json'
         },
         body: JSON.stringify({
           orderID: data.orderID
         })
       });
     });
   }
 }).render('#checkout-paypal-button-container');
Login to Me Too
6 REPLIES 6

southcoastkenny
Contributor
Contributor

Hi, did you ever figure this out ?

I have been through the documentation, it seems 10 times and cannot find the ideal solution.

 

I want a similar flow to you.

Use JS to create button and capture customer payment, then go to a php to process server side (in my opinion more secure)

Confirm the amount , take the money then use php to update my database, and then redirect off to a thank you page.

 

Many Thanks

Kenny

Login to Me Too

rohitvedantwar
Contributor
Contributor

 

 
 
 
 
 
 
 
 
 
No. Did you? I'm still have back and forth discussion on this & other issues via PayPal support cases(i think its about 3-4 months now).
 
Its weird, how such a popular payment gateway company doesnt care much about resolving basic integration issues. I agree with you the documentation is also not helpful.
Login to Me Too

southcoastkenny
Contributor
Contributor

Hi,

we have figured it to work with a php script.

I will get the code and add in here.

 

Im not guaranteeing it is correct but it works for our purpose, code will follow

 

Kenny

Login to Me Too

southcoastkenny
Contributor
Contributor

Hi

Try This:

 

 


    paypal.Buttons({
        debug: true,
        style: {
            label:   'pay'
        },
        createOrder: function(data, actions) {
            return actions.order.create({
                'PayPal-Partner-Attribution-Id': 'xxxx',
                purchase_units: [{
                    amount: {
                        value: ''
                    }
                }]
            });
        },
        onApprove: function(data, actions) {
            // show loader on screen whilst wating to redirect
            $('.checkout-loader').addClass('active');
            
            // excute php script
            var EXECUTE_URL = '/checkout/paypal/execute';
            // Authorize the transaction
            actions.order.capture().then(function(details) {
                // Call your server to validate and capture the transaction
                return fetch(EXECUTE_URL, {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json'
                    },
                    body: JSON.stringify({
                        orderID     : data.orderID
                    })
                }).then(function(response){
                    // redirect to the completed page if paid
                    window.location.href = '/checkout/completed';
                });
            });
        }
    }).render('#paypal-button-container');

 

 

If this does nt work for you let me know. 

 

Kenny 

Login to Me Too

southcoastkenny
Contributor
Contributor

Hi,

 

Been using the code on a few sites for a while now. Its not really a Paypal issue, but a compatability issue with 'fetch'.

 

Using the FetchAPI to send the order to your php script fails is some older browsers (Typically IE6-11) as Fetch is not compatable.

So it seems like 1 in 20 orders do not update the database although payment is taken.

 

We have updated the code above to use jquery $.post instead as its browser support is much better.

For our non-jquery sites we are using axios as an alternative.

 

Kenny

Login to Me Too

shawnz
Contributor
Contributor

Hey @rohitvedantwar 

 

I don't think this is really a question which is specific to Paypal so I'm not surprised that they were not able to resolve this for you.

 

Basically what you are asking for is to submit an HTML form back to your server with the order ID instead of using fetch() to send the order ID back to your server.

 

To do this, you could create an HTML form with a hidden field for the order ID. Then, instead of using fetch(), you would fill in the hidden field on the form and then call form.submit().

 

Here you can see some examples of filling a form and submitting it using Javascript -- https://stackoverflow.com/questions/4683331/how-to-fill-in-form-field-and-submit-using-javascript

 

Let me know if that helps

 

Shawn

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.