www.webdeveloper.com
Results 1 to 6 of 6

Thread: What's wrong with this code? (cURL post form)

  1. #1
    Join Date
    Nov 2009
    Posts
    8

    What's wrong with this code? (cURL post form)

    I'm trying to write a script to submit this html form.
    HTML Code:
    <!-- "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="shadow_1265397352_biz@samerica.com">  
    <!-- 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" >  
    </form>
    This works ok, but when I try to do the same thing in php, the target doesn't seem to receive any data.
    PHP Code:
    <?php 
    $post_data
    ['business'] = 'shadow_1265397352_biz@samerica.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');
        
        
    //set options
        
    curl_setopt($curl_connectionCURLOPT_CONNECTTIMEOUT30);
        
    curl_setopt($curl_connectionCURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        
    curl_setopt($curl_connectionCURLOPT_RETURNTRANSFERtrue);
        
    curl_setopt($curl_connectionCURLOPT_SSL_VERIFYPEERfalse);
        
    curl_setopt($curl_connectionCURLOPT_FOLLOWLOCATION1);
        
    curl_setopt($curl_connectionCURLOPT_POST1);
        
        
    //set data to be posted
        
    curl_setopt($curl_connectionCURLOPT_POSTFIELDS$post_data);
        
        
    //perform our request
        
    $result curl_exec($curl_connection);
        echo 
    $result;
        
    //close the connection
        
    curl_close($curl_connection);

    ?>
    What am I doing wrong? Thank you in advance for any help you can give.

  2. #2
    Join Date
    Nov 2009
    Posts
    8
    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 02:09 AM.

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    8
    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.

  5. #5
    Join Date
    Feb 2010
    Location
    Glasgow, UK
    Posts
    3
    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.

    That said, I DO know you could create a site-side cart, using JavaScript for example, that can then be submitted in the one go to PayPal. Examples of that are on the PayPal site under their merchant tools.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles