Paypal Integration


Paypal is very common for transaction and in this article I’ll show you how to integrate paypal in your site.

Case 1:

Suppose you have a paypal account. You want to sell something and want to make money transaction through paypal. Now there are a critical situation. When someone buy your product through paypal, suppose a software, you want that automatically a license key will send to that user if payment make successful. So, here is the integration process step by step:

Put this code in your site:

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

<input type=”hidden” name=”cmd” value=”_xclick”>
<input type=”hidden” name=”business” value=”yourpaypalemail@email.com”>
<input type=”hidden” name=”item_name” value=”name of the item”>
<input type=”hidden” name=”item_number” value=”1″>
<input type=”hidden” name=”no_shipping” value=”1″>
<input type=”hidden” name=”return” value=”http://yoursite.com/success”>
<input type=”hidden” name=”notify_url” value=”http://yoursite.com/notifyme”>
<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”amount” value=”4″>
<input type=”image” src=”http://www.paypal.com/en_US/i/btn/x-click-but01.gif” name=”submit” alt=”Make payments with PayPal – it’s fast, free and secure!”>

</form>

This is the html code, that makes a visual paypal button in your site. Look carefully, the action of form remain same as here. If you want a normal buy button, then input name “cmd” its value should be =”_xclick”. input name “business” it’s value should be the email address you registered in paypal. Input name amount contains the price you set as a value. Now the most important input fields are “return” and “notify_url”. “return” field contains that url address, when purchase become successful and you want to show user some message like “congratulation, your purchase is successful”. As soon as purchase makes complete paypal dump some values to your “notify_url”. So, in this field, you should put that url of your site that makes some changes like database update or send any special email.

Now download this free code : http://www.hotscripts.com/Detailed/46734.html After downloading, unzip the zip file. You’ll get two php files. Now copy this file paypal.class.php into your project. Suppose your notify_url field is: http://yoursite.com/notify.php

Now write this code in notify.php


<?php 
  
 require_once('paypal.class.php');  // include the class file
 $p = new paypal_class;             // initiate an instance of the class if ($p--->validate_ipn()) {

// Payment has been recieved and IPN is verified.  This is where you
// update your database to activate or process the order, or setup
// the database with the user's order details, email an administrator,
// etc.  You can access a slew of information via the ipn_data() array.

// Check the paypal documentation for specifics on what information
// is available in the IPN POST variables.  Basically, all the POST vars
// which paypal sends, which we send back for validation, are now stored
// in the ipn_data() array.

// For this example, we'll just email ourselves ALL the data.
$subject = 'Instant Payment Notification - Recieved Payment';
$to = 'YOUR EMAIL ADDRESS HERE';    //  your email
$body =  "An instant payment notification was successfully recieved\n";
$body .= "from ".$p->ipn_data['payer_email']." on ".date('m/d/Y');
$body .= " at ".date('g:i A')."\n\nDetails:\n";

foreach ($p->ipn_data as $key => $value) { 
      $body .= "\n$key: $value"; 
}
mail($to, $subject, $body);
?>

So, if $p->validate_ipn() returns true, that means payment verified and you received the payment. And if it returns false, then you’ve to consider that for some reason payment doesn’t make successful.

Case :

Suppose you want to make a subscription form. Subscripted fee may be charged daily, weekly, monthly or yearly. Suppose you want a yearly subscription form. So, any user who pays you, he have to pay yearly, otherwise his membership will cancel.

Solution: Here is the html code for the form. Look the changes. form action remains same as above. input field cmd is changed. Look the value of it. 5 new input field added here.  For subscription form, notify_url be the same and you could use the paypal.class.php and like the previous way, you could check whether payment makes successful or not.

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

<input type=”hidden” name=”cmd” value=”_xclick-subscriptions”>
<input type=”hidden” name=”business” value=”yourpaypalemail@email.com”>
<input type=”hidden” name=”item_name” value=”name of the item”>
<input type=”hidden” name=”item_number” value=”1″>
<input type=”hidden” name=”no_shipping” value=”1″>
<input type=”hidden” name=”return” value=”http://yoursite.com/success”>
<input type=”hidden” name=”notify_url” value=”http://yoursite.com/notifyme”>
<input type=”hidden” name=”currency_code” value=”USD”>
<input type=”hidden” name=”amount” value=”4″>
<input type=”hidden” name=”a3″ value=”5″>
<input type=”hidden” name=”p3″ value=”1″>
<input type=”hidden” name=”t3″ value=”Y”>
<input type=”hidden” name=”src” value=”1″>
<input type=”hidden” name=”sra” value=”1″>

<input type=”image” src=”https://www.paypal.com/images/x-click-but20.gif” name=”submit” alt=”Make payments with PayPal – it’s fast, free and secure!”>
</form>

HTML Variables for PayPal Subscriptions

Variable Name Required or Optional Description
“post” action Required Must be set to “https://www.paypal.com/cgi-bin/webscr”
cmd Required Must be set to “_xclick-subscriptions”
business Required This is your PayPal ID, or email address. This email address must be confirmed and linked to your Verified Business or Premier
account
item_name Optional Description of item being sold (maximum 127 characters). If you are collecting aggregate payments, this can include a summary of all items purchased, tracking numbers, or generic terms such as “subscription.” If omitted, customer will see a field in which they have the option of entering an Item Name
return Optional An internet URL where the user will be returned after completing the payment. For example, a URL on your site that hosts a “Information on your new subscription” page. If omitted, users will be taken to the PayPal site
rm Optional Return URL behavior. If set to “1” and if a “return” value is
submitted, upon completion of the payment the buyer will be sent
back to the return URL using a GET method, and no transaction
variables will be submitted. If set to “2” and if a “return” value is
submitted, the buyer will be sent back to the return URL using a
POST method, to which all available transaction variables will
also be posted. If omitted or set to “0”, GET methods will be used
for all Subscriptions transactions and Buy Now, Donations, or
PayPal Shopping Cart transactions in which IPN is not enabled.
POST methods with variables will be used for the rest
cancel_retu Optional An internet URL where the user will be returned if payment is
rn cancelled. For example, a URL on your site which hosts a
“Payment Cancelled” page. If if omitted, users will be taken to
the PayPal site
a1 Optional Trial amount 1. This is the price of the first trial period. For a free
trial, use a value of 0
p1 Optional Trial period 1. This is the length of the first trial period. The number is modified by the trial period 1 units (t1, below)
t1 Optional Trial period 1 units. This is the units of trial period 1 (p1, above).
Acceptable values are: D (days), W (weeks), M (months), Y
(years)
a2 Optional Trial amount 2. This is the price of the second trial period
p2 Optional Trial period 2. This is the length of the second trial period. The number is modified by the trial period 1 units (t2, below)
t2 Optional Trial period 2 units. This is the units of trial period 2 (p2, above).
Acceptable values are: D (days), W (weeks), M (months), Y
(years)
a3 Required Regular rate. This is the price of the subscription
p3 Required Regular billing cycle. This is the length of the billing cycle. The number is modified by the regular billing cycle units (t3, below)
t3 Required [required] Regular billing cycle units. This is the units of the
regular billing cycle (p3, above) Acceptable values are: D (days),
W (weeks), M (months), Y (years)
src Optional Recurring payments. If set to “1,” the payment will recur unless
your customer cancels the subscription before the end of the
billing cycle. If omitted, the subscription payment will not recur
at the end of the billing cycle
sra Optional Reattempt on failure. If set to “1,” and the payment fails, the payment will be reattempted two more times. After the third failure, the subscription will be cancelled. If omitted and the payment fails, payment will not be reattempted and the subscription will be immediately cancelled
srt Optional Recurring Times. This is the number of payments which will occur at the regular rate. If omitted, payment will continue to recur at the regular rate until the subscription is cancelled
no_note Required This field makes sure your subscriber is not prompted to include a note with the subscription, a function which PayPal Subscriptions does not support. This field must be included, and the value must be set to 1
custom Optional User-defined field (maximum 255 characters) which will be passed through the system and returned to user in payment notification emails. This field will not be shown to your subscribers
invoice Optional User- defined field (maximum 127 characters) which must be unique with each subscription. The invoice number will be shown to subscribers with the other details of their transactions
usr_manage Optional Username and password generation field. If set to “1” PayPal will generate usernames and passwords for your subscribers. For use with Password Management (see page 33). If omitted, no passwords will be generated
cn Optional Label that will appear above the note field (maximum 30 characters). This value is not saved and will not appear in any of your notifications. If omitted, no variable will be passed back to you. “Special instructions (optional):” will be displayed.
cs Optional Sets the background color of your payment pages. If set to “1,” the background color will be black. If omitted or set to “0” the background color will be white
on0 Optional First option field name (maximum 30 characters). If omitted, no variable will be passed back to you
os0 Optional First set of option value(s). If this option is selected through a text box (or radio button), each value should be no more than 30 characters. If this value is entered by the customer through a text box, there is a 200-character limit. If omitted, no variable will be
passed back to you. “on0” must be defined in order for “os1” to be recognized
on1 Optional Second option field name (maximum 30 characters). If omitted, no variable will be passed back to you
os1 Optional Second set of option value(s). If this option is selected through a text box (or radio button), each value should be no more than 30 characters. If this value is entered by the customer through a text box, there is a 200-character limit. If omitted, no variable will be
passed back to you. “on1” must be defined in order for “os1” to be recognized
tax Optional Transaction-based tax override variable. Set to a flat tax amount you would like to apply to the transaction regardless of the buyer’s location. If present, this value overrides any tax settings that may be set in the seller’s Profile. If omitted, Profile tax settings (if any) will apply
currency_co de Optional The currency of the payment. Defines the currency in which the monetary variables (amount, shipping, shipping2, handling, tax) are denoted.
See table of supprted currency codes.
If omitted, all monetary fields are interpreted as U.S. Dollars
modify Optional Modification behavior. 0 or null = The button allows buyers to only create new subscriptions.
1 = The button allows buyers to modify current subscriptions if they have any and to sign up for new subscriptions if they do not.
2 = The button allows buyers only to modify existing subscriptions according to the other parameters specified by the button and does not allow sign-up for new subscriptions.
lc Optional Sets the default country and associated language for the login or signup page that your customers see when they click your button. If this variable is absent, the default will be set from the user’s
cookie, or will be set to the U.S. if there is no cookie. Can be set
to any of the countries currently available on PayPal.

Hope this article will help you to understand how to integrate paypal.

mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

9 Comments

  • Fuad Hassan
    December 25, 2008 at 7:55 am

    Great post Mahmud………….. will be very helpful for the beginners

  • ranacse05
    December 29, 2008 at 6:53 pm

    so much handy for the new developer . It took a long time to learn 🙁

  • Naseer Ahmad
    January 3, 2009 at 12:34 pm

    i agree with both of them (@Fuad and @ranacse05)

  • Saijan Thomas
    March 18, 2010 at 10:32 am

    Can I know how to have a Paypal account. How much money i need to have to start my account? How safe is to do transactions via paypal?

  • mahmud ahsan
    mahmud ahsan
    March 18, 2010 at 12:26 pm

    @Saijan, Paypal is a very popular money transaction system. You can open a personal account free. Please visit http://www.paypal.com to know more about it.

  • Kyli
    October 22, 2010 at 9:21 am

    You really rock!!!

  • Nicko Picazo
    September 6, 2011 at 12:36 pm

    Why I can’t received any email (data from paypal), I’m using paypal sandbox. Thanks

  • Mr. Yap
    October 5, 2011 at 5:17 pm

    I need help maybe you can write another blogpost.

    I want to charge 2% based on each item customer buy.
    For example, if customer select item cost $100 – it will be $102. Similarly, if customer select item cost $200 – it will be $204

    How do one do that?

    Thanks

    • mahmud ahsan
      mahmud ahsan
      October 6, 2011 at 9:22 pm

      Write a program in your client side, so that if your user select 100 USD you’ll automatically set 102 in the paypal form.

Comments are closed here.

About Me

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

Flickr

Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed

Twitter Feed

Fanpage