Adding an Advanced Paypal Buy Now Button

PHP tutorial
This tutorial explains in some detail what you can do to get the most from a Paypal Buy Now button. There are many options available to you and I include example code where possible. Paypal has some really good documentation on the subject so I have also included links to their site.


HTML variables

Buy Now buttons

Website Payments Standard uses a normal HTML Form to get information from your website. When you use their Create a Button wizard the HTML created is basically an HTML Form containing hidden HTML input variables that contain all the relevant details of the purchase.

1. Saved Buttons

Buttons created and saved on the Paypal website will have much of the information associated with them stored on the Paypal site. Consequently the HTML form on your website will be much simpler than one that is not stored on Paypal’s site. A saved button will have a variable with button id such as

<input type=”hidden” name=”hosted_button_id” value=”10954267″>

You should not edit this or try to generate your own hosted button ids but you can enhance the HTML form by adding extra value as custom variables. These should be added to a Paypal hosted button in Step 3 (Customise Advanced Features)

If you try to mix a hosted button with a manually created button you can get unexpected results. Paypal may ignore your manual additions. So I would recommend you go for one or the other.

2. The Form tag

The form tag will look like this. You should leave this alone.

<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>

3. Input variables

The input variables don’t need to be seen by the purchaser so they are marked as hidden. They will take the following format.

<input type=”hidden” name=”variableName” value=”variableValue”>

4. cmd variable

The “cmd” variable is always required in the Form. It always takes the value “_s-xclick” or “_xclick” for a “Buy Now” button. Don’t alter this.

<input type=”hidden” name=”cmd” value=”_s-xclick”>

5. Passthru variables.

Paypal lets you set variables and it will pass them on unaltered. Their use is optional and will depend on your situation. The following are passthru variables.

<input type=”hidden” name=”item_number” value=”test1″> (up to 127 chars)
<input type=”hidden” name=”custom” value=”anythingyouwant”> (up to 256 chars)
<input type=”hidden” name=”invoice” value=”123456″> (up to 127 chars. Intended for use as invoice number)

6. Character Set and Currency code

You can specify which character set you wish to use. e.g. to use UTF-8

<input type=”hidden” name=”charset” value=”utf-8″>

and currency code (USD is assumed if not present) Codes accepted can be found here Currency Codes

<input type=”hidden” name=”currency_code” value=”CurrencyCode”>

7. Return URL for specific transactions.

There is the option to set a global return URL in your Paypal account which will apply in all cases for Paypal buttons you create. You may however want to override this for specific buttons or you may want to include session related information in the return url. How to use session information is beyond the scope of this tutorial.

<input type=”hidden” name=”return” value=”URLspecificToThisTransaction”>

“cancel_ return” is a URL to which the payer’s browser is redirected if payment is cancelled; for example, a URL on your website that displays a “Payment Canceled” page.

“rm” specifies the Return method. The FORM METHOD used to send data to the URL specified by the return variable after payment completion. Allowable values:

0 – all shopping cart transactions use the GET method
1 – the payer’s browser is redirected to the return URL by the GET method, and no transaction variables are sent
2 – the payer’s browser is redirected to the return URL by the POST method, and all transaction variables are also posted

The default is 0.
Note: The rm variable takes effect only if the return variable is also set.

“cbt” sets the text for the Return to Merchant button on the PayPal Payment Complete page. For Business accounts, the return button displays your business name in place of the word “Merchant” by default. For Donate buttons, the text reads “Return to donations coordinator” by default.
Note: The return variable must also be set.

8. Pre filling the form to make things easier for your customer.

If you already know the answer to some of the questions Paypal will need to know such as the Buyers Billings Address you can pre populate the form. This feature is not available in all countries. It is available in the US and UK.

Keep in mind that regardless of the address you fill in, when address_override=1 is passed a member’s confirmed address on file will be used as the shipping address and will be uneditable. When a non-PayPal account holder pays, their billing address and shipping address will need to match.

<input type=”hidden” name=”first_name” value=”John”>
<input type=”hidden” name=”last_name” value=”Doe”>
<input type=”hidden” name=”address1″ value=”9 Elm Street”>
<input type=”hidden” name=”address2″ value=”Apt 5″>
<input type=”hidden” name=”city” value=”Berwyn”>
<input type=”hidden” name=”state” value=”PA”> (or as appropriate for non US)
<input type=”hidden” name=”country” value=”US”>
<input type=”hidden” name=”zip” value=”19312″>
<input type=”hidden” name=”night_phone_a” value=”610″> (Buyers home phone. Area code for US. Country code for non US. 3 digit max)
<input type=”hidden” name=”night_phone_b” value=”555″> (Buyers home phone. 3 digit prefix for US. Entire phone number for non US. 16 char max)
<input type=”hidden” name=”night_phone_c” value=”1234″> (Buyers home phone. 4 digit phone number for US.)
<input type=”hidden” name=”email” value=”jdoe@zyzzyu.com”>
<input type=”hidden” name=”lc” value=”US”> (payers language for the billing info/log in page only. Default is US. Use UK for United Kingdom)
<input type=”hidden” name=”charset” value=”utf-8″>

9. Product Options

Specify product options such as size on a dropdown box or text box. You can specify up to 7 options on a single button (Up to 5 dropdown and 2 text)
You can either use the create button wizrd to do this or specify the code manually.

for example

<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>


<input type=”hidden” name=”business” value=”herschelgomez@xyzzyu.com”>


<input type=”hidden” name=”cmd” value=”_xclick”>


<input type=”hidden” name=”item_name” value=”Hot Sauce-12 oz. Bottle”>
<input type=”hidden” name=”amount” value=”5.95″>
<input type=”hidden” name=”currency_code” value=”USD”>


<input type=”hidden” name=”on0″ value=”Type”>Type of sauce

<select name=”os0″>
<option value=”Select a type”>– Select a type –

<option value=”Red”>Red sauce

<option value=”Green”>Green sauce

</select>


<input type=”image” name=”submit” border=”0″ src=”https://www.paypal.com/en_US/i/btn/btn_buynow_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>

10. Product Quantity.

The default is 1 item but you can use the button wizard to change this. Or use this example code.

<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>


<input type=”hidden” name=”business” value=”herschelgomez@xyzzyu.com”>


<input type=”hidden” name=”cmd” value=”_xclick”>


<input type=”hidden” name=”item_name” value=”Hot Sauce-12 oz. Bottle”>
<input type=”hidden” name=”amount” value=”5.95″>
<input type=”hidden” name=”currency_code” value=”USD”>


<input type=”hidden” name=”undefined_quantity” value=”1″>


<input type=”image” name=”submit” border=”0″ src=”https://www.paypal.com/en_US/i/btn/btn_buynow_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>

11. Offering a discount.

Paypal allows you to offer a discount.
- “Discount_amount” must be less than “amount”.
- If you don’t specify “discount_amount2″ then “discount_amount” will apply to all items not just the first.
- To use “discount_amount2″ you must specify “discount” as greater than or equal to 0.

You can specify “discount_rate” and “discount_rate2″ as an alternative to “discount_amount” and “discount_amount2″. This applies a percentage from 0 to 100.
Unlike “discount_amount” if you don’t specify “discount_rate2″ then “discount_rate” only applies to the first item.

Use “discount_num” to specify an upper limit on the number of items to which “discount_amount2″ or “discount_rate2″ applies.

Use the button creation tool wizard or this example code that provides a $0.15 discount on the first item ordered and a $0.20 discount on the next four items.
<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>


<input type=”hidden” name=”business” value=”herschelgomez@xyzzyu.com”>


<input type=”hidden” name=”cmd” value=”_xclick”>


<input type=”hidden” name=”item_name” value=”Hot Sauce-12 oz. Bottle”>
<input type=”hidden” name=”amount” value=”5.95″>
<input type=”hidden” name=”currency_code” value=”USD”>


<input type=”hidden” name=”discount_amount” value=”0.15″>
<input type=”hidden” name=”discount_amount2″ value=”0.20″>
<input type=”hidden” name=”discount_num” value=”4″>


<input type=”hidden” name=”undefined_quantity” value=”1″>


<input type=”image” name=”submit” border=”0″ src=”https://www.paypal.com/en_US/i/btn/btn_buynow_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>

12. Shipping and Handling charges

“shipping” and “shipping2″ specify the cost of shipping an item and subsequent items.
“handling” applies regardless of number of items.

13. Tax

Use “tax” and “tax_rate” to apply a transaction based tax amount or tax rate irrespective of customers location.
“tax_rate” can be 0.001 to 100

14. Weight

“weight” and “weight_unit” (lbs or kgs)

15. How the Paypal Checkout Looks

“page_style” can be set to “paypal” to use the Paypal page style
“page_style” can be set to “primary” to use page style you defined as primary in your account
“page_style” can also be set to any custon style you created in your account profile.

“image_url” specifies where the 150×50 pixel image is that will be used as your logo. If you don’t specifiy this then your business name will be used if you have a business acccount otheriwise your email address.

“cpp_header_image” specifies the image at the top left of the checkout page. maximum size is 750×90 pixels. PayPal recommends that you provide an image that is stored only on a secure (https) server.

“cpp_headerback_color” specifies the background color for the header of the checkout page. Valid value is case-insensitive six-character HTML hexadecimal color code in ASCII.

“cpp_headerborder_color”specifies the border color around the header of the checkout page. The border is a 2-pixel perimeter around the header space, which has a maximum size of 750 pixels wide by 90 pixels high. Valid value is case-insensitive six-character HTML hexadecimal color code in ASCII.

“cpp_payflow_color” specifies the background color for the checkout page below the header. Valid value is case-insensitive six-character HTML hexadecimal color code in ASCII. Note: Background colors that conflict with PayPal’s error messages are not allowed; in these cases, the default color is white.

“cs” specifies the background color of the checkout page. Allowable values:

0 – background color is white
1 – background color is black

The default is 0.

“lc” specifies the language of the login or sign-up page that subscribers see when they click the Subscribe button. If unspecified, the language is determined by a PayPal cookie in the subscriber’s browser. If there is no PayPal cookie, the default language is U.S. English.

“no_note” specifies to not prompt payers to include a note with their payments. Allowable values:
0 – provide a text box and prompt for the note
1 – hide the text box and the prompt

The default is 0.

“cn” is a label that appears above the note field. This value is not saved and will not appear in any of your notifications. If omitted, the default label above the note field is “Add special instructions to merchant.” The cn variable is not valid with Subscribe buttons or if you include no_note=”1″.

“no_shipping” specifies to not prompt payers for shipping address. Allowable values:

0 – prompt for an address, but do not require one
1 – do not prompt for an address
2 – prompt for an address, and require one

The default is 0.

A Basic Non hosted Buy Now button sample code

<form action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>


<input type=”hidden” name=”business” value=”herschelgomez@xyzzyu.com”>


<input type=”hidden” name=”cmd” value=”_xclick”>


<input type=”hidden” name=”item_name” value=”Hot Sauce-12 oz. Bottle”>
<input type=”hidden” name=”amount” value=”5.95″>
<input type=”hidden” name=”currency_code” value=”USD”>


<input type=”image” name=”submit” border=”0″ src=”https://www.paypal.com/en_US/i/btn/btn_buynow_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>

A Basic hosted Buy Now button sample code

<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=”10954267″>
<input type=”image” src=”https://www.paypal.com/en_US/GB/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.paypal.com/en_GB/i/scr/pixel.gif” width=”1″ height=”1″>
</form>

Have you seen the other Paypal tutorials on DesignerTuts?

Adding a More Complex Buy Now Button
Adding a Really Simple Buy Now Button
Paypal IPN
Paypal IPN Receiving Instant Payments

Leave a Reply

New Discussion Forum

We have teamed up with CrypticGFX.com who are providing a place to discuss our tutorials.

You can still post blog comments as before but if you want a more in-depth discussion have a look at Cryptic GFX.