Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing price variable to PayPal with custom button

Tags:

php

paypal

I have a form and a custom PayPal button, but how do I pass the value/price variable to PayPal?

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="ZEFZFYBY2SZB8">
    <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

I have a variable $total = "238.00";

like image 396
Denoteone Avatar asked Mar 10 '11 04:03

Denoteone


People also ask

Can I customize PayPal button?

PayPal buttonChanges the background color of the button and adjusts the wordmark color to be visible. The default value is gold , but you can also set it to: black , blue , silver , or white . Overrides the shape of the button. The default value is softEdges , but you can also set it to hardEdges or rounded .

How do I add quantity to PayPal button?

I found the answer. Go to Custom advanced features and select yes for add quantities.


4 Answers

The previous code did not work for me. After much headache I finally figured out you have to go into PayPal and on step 2 of creating the button code make sure you click the unhosted button, and then copy the unencrypted button code, which will give you something like this (I blanked out my business value for security):

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="XXX">
    <input type="hidden" name="lc" value="CA">
    <input type="hidden" name="item_name" value="Tangled Roots">
    <input type="hidden" name="button_subtype" value="services">
    <input type="hidden" name="no_note" value="0">
    <input type="hidden" name="cn" value="Add special instructions to the seller">
    <input type="hidden" name="no_shipping" value="2">
    <input name="amount" value="16.99">
    <input type="hidden" name="currency_code" value="CAD">
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

This code creates a button where the user can input the amount, which by default starts at 16.99, but you could just as easily replace that with a PHP variable.

like image 84
Julian Avatar answered Oct 13 '22 22:10

Julian


I tried and failed with all of the above. I found this to be the answer from the PayPal website.

<form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="[email protected]">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="item_name" value="Teddy Bear">
    <input type="hidden" name="amount" value="12.99">
    <input type="image" src="http://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"         border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

Test this and you'll understand how it works... change the business to the email address of person you want to pay etc.

like image 45
IanOSullivan Avatar answered Oct 13 '22 23:10

IanOSullivan


Add one more hidden field for amount

<input type="hidden" name="amount" value="<?php echo $total; ?>">
like image 26
Shakti Singh Avatar answered Oct 13 '22 23:10

Shakti Singh


Here is the 2013 version: Go to create a button, when you get to step 2, uncheck the box, proceed to step 3 then create the button. Once you have the code, it will look like this:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="XXXXXXXX">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Payments">
<input type="hidden" name="amount" value="100.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="cn" value="Add special instructions to the seller:">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://YOURSITE.com/">
<input type="hidden" name="cancel_return"     value="http://YOURSITE.com/payments.html">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif"     border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

Your "business" value will not be XXXXXXXX, so make sure you leave the one Paypal gives you. You can also set your cancel and return URL's.

For more advanced PHP users: I actually setup a PHP string and it works great! For example, see below:

 https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&businesss=XXXXXXXXX&lc=US&item_name=$mydescription&amount=$myprice&........

And so on.....As you can see $mydescription is a PHP variable and $myprice is a PHP variable. What I did was setup a HTML form to collect data and used that form as a payment processing form. Once the user clicks submit, I have it going to a PHP page to use as a Mailer, Database Insertion, Autoresponder, and finally a Header redirect. The URL for the redirect is the Paypal URL with the Variables in the string! This thread actually helped me find the correct Paypal button code so that string will work properly with price variations! FYI - If you are a beginner PHP person, the image field is not used in the string. Only the URL and then the Hidden Names and Values.

like image 5
Phil Mulkins Avatar answered Oct 13 '22 23:10

Phil Mulkins