views:

433

answers:

2

Im writing a form to post data off to paypal, and this works fine, i create the form with hidden fields and then have a submit button that submits everything to paypal.

However, when the user clicks that button, there is more that i want to do, for example change their cart status in the database. So, i want to be able to execute some code when they click submit and then post the data to paypal.

I dont want to use javascript for this.

My method at the moment is using cURL, the form posts back to the current page, i then check for $_POST data, do my other commands like updating the status of the cart, and then create a curl command, and post the form data to paypal. Now, it gets posted successfully, but the browser doesnt go off to paypal.

At first i was just retirieving the result in a string and then echoing it out and i could see that the post was successful, then i set CURLOPT_FOLLOWLOCATION to 1 assuming this would let the browser go off to paypal but it doesnt, what it seems to do is grab some stuff from paypal and put it on my page.

Is using cURL the right thing for this? and if so, how do i get round this problem? I want to stay away from javascript for this as only users with javascript enabled would have their cart updated.

The code im using for curl is below, the post_data is an array i created and then read key-value pairs into post_string.

//Set cURL Options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_MAXREDIRS, 20);

//Set Data to be Posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//Execute Request
curl_exec($curl_connection);
+1  A: 

Perhaps try to combine CURL with a header statement

<?php
header ("Location: https://www.paypal.com/"); 

This might work depending if the browser need to pass any state (session) info to paypal. This session info are gathered in your CURL call, but there is no way to pass that on to the browser unfortunately. So if you need to maintain the state, then this will not work.

Curl operates on the server side. It won't affect your browser. CURLOPT_FOLLOWLOCATION simply instructs CURL to obey when for instance paypal sends it a header like the one mentioned above. The only way to instruct the browser to do something is to respond with an HTTP header like above or with some kind of meta tag in your HTML response e.g.

<META HTTP-EQUIV="refresh" CONTENT="10;URL=https://www.paypal.com/"&gt;

As an alternative, does paypal not provide a redirect function that will, when the transaction is finished, redirect back to your server. If so then just change the card status during this callback and the skip the whole curl process?

Hannes de Jager
thanks, i was hoping i wouldnt have to do it in the callback from paypal, i wanted to set a cart status as it goes off to paypal and one when it returns just so can accuratley keep track of the progress and to help with correcting any errors, ill try setting some http headers and see, but i might just have to do it in the callback. I just assumed that autosubmitting a form and following the action would be easier.
cast01
I guess you could still do both for those users that have javascript enabled. For those that don't have javascript enabled only the callback is used but for those (majority) that have javascript enabled, you can set the 'going-of-to-paypal' flag in an ajax call and the set the 'complete' flag in the callback.
Hannes de Jager
+1  A: 

After receiving the form and doing what you need to do you should present another form with only a button (all other data in hidden fields) for the user to click and finally head off to paypal.

zaf
thanks for the reply, however this would defeat the purpose of what im trying to do. I want to set a cart as, say, "gone to paypal" when the user clicks pay. If, when they click pay, i then set the cart status and show them another form that they have to submit, then they could choose not to submit it, and then my cart status would be wrong.
cast01
Then you'll need one line of javascript to auto-submit the form. Otherwise, your server cannot redirect AND submit a form to paypal all by itself in one go.
zaf