What's wrong with this code? (cURL post form)
I'm trying to write a script to submit this html form.
This works ok, but when I try to do the same thing in php, the target doesn't seem to receive any data.
<!-- "Add to cart" button -->
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="firstname.lastname@example.org">
<!-- Specify a PayPal Shopping Cart Add to Cart button. -->
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="add" value="1">
<!-- Specify details about the item that buyers will purchase. -->
<input type="hidden" name="item_name" value="widget">
<input type="hidden" name="amount" value="5.95">
<input type="hidden" name="currency_code" value="USD">
<!-- Require a shipping address. -->
<input type="hidden" name="no_shipping" value="2">
<!-- Set your "continue shopping" page -->
<input type="hidden" name="shopping_url" value="http://www.google.com/">
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >
What am I doing wrong? Thank you in advance for any help you can give.
$post_data['business'] = 'email@example.com';
$post_data['cmd'] = '_cart';
$post_data['add'] = '1';
$post_data['item_name'] = 'widget';
$post_data['amount'] = '5.95';
$post_data['currency_code'] = 'USD';
$post_data['no_shipping'] = '2';
//create cURL connection
$curl_connection = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
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, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_POST, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
//perform our request
$result = curl_exec($curl_connection);
//close the connection
I just tried sending it to another page I wrote, and it received every field of data just fine. Could there be something odd about the paypal site that doesn't allow this?
EDIT: Ok, I have a theory on what might be the problem, or at least part of it. Can anyone tell me how likely this is? Since paypal uses sessions to store the shopping cart, that has to be connected to an IP address, right? But when I submit the form, it uses the server's ip, not the user's. This means that when you view the cart after the script executes, it doesn't show anything.
Actually, now that I write this, that can't be the reason this doesn't work. I've been running it off my hard drive, so my ip IS the server's ip. Also, the page it shows isn't what it should look like if it successfully added to the cart.
Still, this will be a problem in the future, right? Is there any way I can avoid this, or am I just out of luck?
Last edited by Topazan; 02-07-2010 at 01:09 AM.
What is your end goal by avoiding the paypal interface? Users won't be able to complete the payment(s) if they are never given the payment page from paypal.
I was trying to help a friend set up a website for her business. She though it would look unprofessional if the customers are directed to the paypal site every time they add something to their cart, so I was trying to set up a system that would store the cart on her site and send everything to paypal at once when the user is ready to checkout.
After reading some more of the paypal documentation, it seems like the suggested procedure is combining all the purchases into one and sending that to paypal through a form. The problem I see with that is there would have to be a separate system in place for recording what was purchased.
It's a moot point anyway, she decided to go with some pre-made online store software.
I'd suggest you check with PayPal terms and conditions before doing that your doing. I understand that it's not the most professional look, but in the end PayPal might object to you avoiding the add-to-cart function.
Usually when you are trying to create a more cohesive look-and-feel using paypal you use your own cart (server-side preferable) and pass the whole order to paypal when the user is ready to pay. Then you use the paypal IPN callback to mark the account paid once paypal has processed payment. By sending the data using Curl you were removing the user from the payment process which would in essence break your payment process since no payments would be made/received that way. It is the same technique used when you purchase payment gateway services such as authorize.net or others.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)