0

I currently have a custom shopping cart which sends straight to 2Checkout and need to use the same form for different payment methods.

To do this I need to get the form to submit to my own server first, then the server to redirect to the chosen payment gateway OR a page on my site detailing other payment options.

How would I go about redirecting the user with the variables as a post method in PHP (not javascript)?

My current code doesn't work as the user doesn't leave the page and they need to...

//$url = 'https://www.2checkout.com/checkout/spurchase';
$url = 'getpost.php';

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);  // RETURN THE CONTENTS OF THE CALL
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
trvo
  • 665
  • 1
  • 10
  • 23

2 Answers2

1

Generally, when you have to POST to yourself, and then POST from PHP again to a new location, is a good pointer that you need to go back and look at your application's structure.

A smoother solution could be breaking the payment flow up into steps.

Step 1 - Information
Have the form which you currently have, and let it post as you would at the moment.

Step 2 - Choose gateway
Have all the $_POST variables stored in hidden fields on the page, and have a list of gateways, which the user is able to choose. When you click on one of these, you could send the user to a confirmation page.

Step 3 - Confirmation page
The primary purpose of this page, is for the user to review the data he entered, and confirm he wants to buy the item.

The secondary purpose of this page is to take all the data you have, and put them into hidden inputs ( Again ), but this time you know where to send the data, and thus know which format / naming the fields should have.


This being said, nothing is to hinder you from actually posting the data form php, but as I said, it's a good pointer that your application structure no longer makes sense.

Kao
  • 2,242
  • 3
  • 22
  • 31
  • Thanks, I think you are correct, I was just trying the reduce the number of clicks that the user has to do. – trvo Feb 11 '13 at 13:54
0
curl_setopt($ch,CURLOPT_POST, count($fields));

it accept boolean use it like this

curl_setopt($ch,CURLOPT_POST, TRUE);

here is a perfect function of curl that work with every thing post,get http https login and every thing.

use it like curl_grab_page($url,$data) put url url and data thats it

function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")

            {
                if($login == 'true') {
                    $fp = fopen("cookie.txt", "w");
                    fclose($fp);
                }
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
                curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

                curl_setopt($ch, CURLOPT_TIMEOUT, 60);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                if ($proxystatus == 'true') {
                    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
                    curl_setopt($ch, CURLOPT_PROXY, $proxy);
                }
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

                if($secure=='true')
                {
                    curl_setopt($ch, CURLOPT_SSLVERSION,3);
                }

                curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );


                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_REFERER, $ref_url);
                curl_setopt($ch, CURLOPT_HEADER, TRUE);
                curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                ob_start();

                return curl_exec ($ch); // execute the curl command

                curl_getinfo($ch);
                ob_end_clean();
                curl_close ($ch);
                unset($ch);
            }
rohitarora
  • 1,380
  • 2
  • 13
  • 20