February 13, 2013 1:44 pm

How to Integrate Paypal Payment System in PHP & MySQL

I received a tutorial requests from my reader that asked to me how to implement payment gateway system with Paypal API. In this tutorial I want to explain how to work with Paypal Sandbox test accounts for payment system development and sending arguments while click buy now button. It’s simple and very easy to integrate in your web projects.

PayPalPaymennt

Step 1

Create a Paypal Sandbox account at https://developer.paypal.com/

Step 2

Now create test accounts for payment system. Take a look at Sandbox menu left-side top Sandbox->Test Accounts

PayPal1

Step 3

Here I have created two accounts Buyer (personal) and Seller (merchant/business)

PayPal2

index.php

Contains PHP code. Displaying products, product image, product name and product price. Here you have to give your business(seller) $paypal_id id. Modify paypal button form return and cancel_return URLs.

success.php

Paypal payment success return file. Getting Paypal argument like item_number. Paypal data success.php?tx=83437E384950D&st=Completed&amt=10.00&cc=USD&cm=&item_number=1

cancel.php

Paypal API cancel_return file.

 Step 4

When your web application test payment system workflow is completed. Change the form action development API URLs to original API URLs and give valid $paypal_id seller email id.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • Gethin

    Very good tutorial so far but I have a small issue with it. I always get the Payment failed in the success page. This is because $item_price and $item_currency are empty. They are empty because the page doesn’t get redirected from the paypal page in 10 seconds like the demo , you have to click return to store and this brings you back to success.php with out anything for $_REQUEST to retrieve instead of success.php?tx=83437E384950D&st=Completed&amt=10.00&cc=USD&cm=&item_number=1. Hope you can help Thanks

    • Huzoor Bux

      Hello Gethin

      Auto Return is turned off by default.
      To turn on Auto Return:

      Log in to your PayPal account at https://www.paypal.com. The My Account Overview page appears.

      Click the Profile sub-tab.
      The Profile Summary page appears.

      Under the Setting Preferences column, click the Website Payment Preferences link. The Website Payment Preferences page appears

      Under Auto Return for Website Payments, click the On radio button to enable Auto Return.

      In the Return URL field, enter the URL to which you want your payers redirected after they complete their payments.

      NOTE: PayPal checks the Return URL that you enter. If the URL is not properly formatted or cannot be validated, PayPal will not activate Auto Return.

      Scroll to the bottom of the page, and click the Save button.

      • Ahesanali Suthar

        step-1 :Please sign in with your sandbox business account on
        sandbox.paypal.com

        step-2 : than go to profile my selling tool link

        step-3: refer image-1

        step-4 : refer image-2

        • Ramesh Singh

          sry to say but the pay pal does not return me auttomatically after payment all the settings are done….any other suggestion

      • jayden

        The Problem with this is, you have to enter a static url. I wonder how it is possible, to make the redirect go automatically to the page, one has entered in the form from begin with, that url, that you get redirected to, when click on the various “back to page” usw buttons. I would like to have this automatically, so users content can be activated after payment. Or does anyone here know another maybe better way to achieve this?
        Thx

  • Sam

    Hi i followed this tutorial and your little extra bit about auto-return but the paypal still doesn’t either auto-return or pass along those variables and so keeps displaying the error message about the transcation not being completed. However when i check the actual test accounts in paypal the money has been transferred properly.

    • Huzoor Bux

      Hello Sam

      Auto Return is turned off by default.
      To turn on Auto Return:

      Log in to your PayPal account at https://www.paypal.com. The My Account Overview page appears.

      Click the Profile sub-tab.
      The Profile Summary page appears.

      Under the Setting Preferences column, click the Website Payment Preferences link. The Website Payment Preferences page appears

      Under Auto Return for Website Payments, click the On radio button to enable Auto Return.

      In the Return URL field, enter the URL to which you want your payers redirected after they complete their payments.

      NOTE: PayPal checks the Return URL that you enter. If the URL is not properly formatted or cannot be validated, PayPal will not activate Auto Return.

      Scroll to the bottom of the page, and click the Save button.

      • sam

        i have done this yet it still refuses to auto-return or to pass back the variables for the request[] to grab. Would you have any further suggestions?

  • simm

    tutorial is super simple.
    i have a question, Do we need to have a merchant paypal account for accepting payments? and activating Return URL? or it can be done in personal account too?

    • huzoorbux

      Yes you can do this on your personal account. Return url is necessary if you want your customers return on your website back after payment. Its upto your application’s requirement.

      • simm

        I logged into my paypal personal account but the problem is When i go to Profile page i do not see these options

        Setting Preferences column

        Under the Setting Preferences column, click the Website Payment Preferences link

        that is why i asked my problem 🙁

  • sudesh

    Hi,
    Can I know about paypal ID.. In sand box and real one.. is it. paypal login email..?

    • huzoorbux

      Its your login email on live.
      on sandbox your business id which you have created for testing as defined in Step 3.

      • Sanjeev

        HI @huzoorbux:disqus ! YOU THERE?
        Can you let me know how can we get parameters like tx and cc back on our website , as i have been trying the same from morning , and i cant get it .
        Please helo

  • gayatri

    Even if return url is given,transaction id and other details not getting in the redirected page…

  • rahul

    thank you!!!

  • mittul chauhan

    can you show me how to make refund now ? how can i make the refund ?

  • Tinny

    return url is given, but getting no transaction id and other details, please help me out

  • CCAvenue payment integration php
    Integrating CCavenue payment gateway is simple
    We need a merchant id and working for implementing payment gateway

    check link
    http://www.codingprogrammer.com/tutorialdemo/php-tutorial/ccavenue-payment-integration-php/

  • Shubham Jain

    Thanks.. its awesome article that you sharing with us. Thanks again

  • mukesh

    sandbox i couldnt find menu test thing

  • mahesh

    on success i m getting txt, amnt, tranction id values so it showing warning error

  • anil

    hey iam not directly redirecting untill i click on “Return to facilitators accounts test store”

    if i click on only its goes to next page and saying that within 10sec redirect to page

    iam attaching files

    • huzoorbux

      Auto Return is turned off by default.
      To turn on Auto Return:

      Log in to your PayPal account at https://www.paypal.com. The My Account Overview page appears.

      Click the Profile sub-tab.
      The Profile Summary page appears.

      Under the Setting Preferences column, click the Website Payment Preferences link. The Website Payment Preferences page appears

      Under Auto Return for Website Payments, click the On radio button to enable Auto Return.

      In the Return URL field, enter the URL to which you want your payers redirected after they complete their payments.

      NOTE: PayPal checks the Return URL that you enter. If the URL is not properly formatted or cannot be validated, PayPal will not activate Auto Return.

      Scroll to the bottom of the page, and click the Save button.

      • anil

        hey iam facing a problm with return url iam not getting parameters to success.php
        in my local host
        and if i run in this file from my server
        iam getting this message
        Cannot GET /us/cgi-bin/www.(my server url)

        whats problem

  • anil

    hey iam getting this message when i put in server

    Cannot GET /us/cgi-bin/www.(my server url)

  • Paresh

    I am trying to many things but no one things go right please someone make new demo for new paypal

  • Mr Long

    Notice: Undefined index: item_number in E:CTPvampwampwwwpayment_with_paypalsuccess.php on line 2

    Notice: Undefined index: tx in E:CTPvampwampwwwpayment_with_paypalsuccess.php on line 3


    help me.:(

  • Jeet

    Hi i have integrated the paypal it is working fine with the sandbox account but with the live account the transaction id is not generated sir please guide me what may be the error

  • Jeet

    jeet
    how to generate response code for error if transaction failed due to some reason .

    Thanks

  • Umair Shah

    To All Who Are Not Getting The Data At Return URL or Any Type of Notices So PHPGang Needs To Update There Code To This Updated Code..it Works 100% You Just Need To Add This Field To Your Form As :

    And Then Update Your Code In success.php As :

    <?php

    $item_no = $_POST['item_number'];

    $item_transaction = $_POST['txn_id']; // Paypal transaction ID

    $item_price = $_POST['payment_gross']; // Paypal received amount

    $item_currency = $_POST['mc_currency']; // Paypal received currency type

    $price = '10.00';

    $currency='USD';

    //Rechecking the product price and currency details

    if($item_price==$price && $item_currency==$currency)

    {

    echo "Item No : $item_no”;

    echo “Item Transaction ID : $item_transaction”;

    echo “Item Price : $$item_price”;

    echo “Item Currency : $item_currency”;

    echo “Welcome, Guest”;

    echo “Payment Successful”;

    }

    else

    {

    echo “Payment Failed”;

    }
    ?>

    If anyone has any question can contact me at skype as : nicefellow1111
    I am online any time…! besides I am a Web Developer you can contact for any type of web development or custom PHP/MySQLi Script..!

  • I am getting Payment Failed. But it is a successful transaction.
    And how can I replace “amirxx-facilitator@gmail.com”, this with my site name and image ?

  • Haider Abbas

    Add following to your form fields and variables will be returned as $POST fields:

    This will work with both auto return on or auto return off.

    Hope this will help all those complaining variables were not returning.

    • Bhavin Vaghela

      quick and effective solutions 🙂

  • amit ranjan

    Notice: Undefined index: item_number in varwwwpayment_with_paypalsuccess.php on line 2
    Please reply soon

  • Druva

    Hi All,
    I am getting payment failed but transaction was successful and not getting response to success.php
    Help??

  • jim_paris

    Hi, you’ve done great job with this script. I need your help with the following error. TheTransaction is OK but always I receive “Payment Failed”. Please can you advice me what to do?

  • kapil sharma

    i could not detail of success.php page plz hlp me how can get value on success.php page

  • Avadhesh Bhatt

    very good code snippet for payment gateway..i try this first time as i am beginner to payment gateway ..and it was successful ….thanx php gang !!! really appropriated to your team .

  • rushikesh

    Hi i have site where i get some money from one user’s paypal account in my paypal account and get response for it. IT working perfectly fine. from here i get the code for it. It is working 100% with perfection what i want.

    Now i want something different scenario, from admin site i want to pay some payment back to my customer as special discount back offer. When i log-in from my account and do transaction to my customer’s account it shows payment failed. what is solution for it?

    • heizas

      dont, just make a function that counts the discount and lets him pay less

  • gufoe

    Are you kidding? You’re not even checking the autenticity of the transaction id, anyone could fake any transaction!
    At least you should say that it is missing, otherwise people will implement this and (surprise surprise) get hacked!

  • gufoe

    Are you kidding? You’re not even checking the autenticity of the transaction id, anyone could fake any transaction!
    At least you should say that it is missing, otherwise people will implement this and (surprise surprise) get hacked!

  • gufoe

    Are you kidding? You’re not even checking the autenticity of the transaction id, anyone could fake any transaction!
    At least you should say that it is missing, otherwise people will implement this and (surprise surprise) get hacked!

    • 100% correct >> You’re not even checking the autenticity of the transaction id, anyone could fake any transaction!

    • That was an old article we are working on an other tutorial of Paypal integration and will remove that article.

  • amit

    Hi how can i send list of item on paypal please help me

  • Arif Ali Mughal

    Ive noticed that for me when doing this with a business sandbox email I created, the parameters tx, amt and cc do not work. have they been changed by PayPal?

    • Summayya Khaid

      same here

  • Harshad Patel

    I have also same problem. Did you found any solutions? or please post solution phpgang.

  • hello… is there a way to find tx paramater after the user submit the form from your index.php? basicly i need to know if i need this parameter to indentify the payment in success.php or cancel.php. Second question: can you describe parameters i need to submit in index.php form? item_number = product_number (in my site), credits? user_id = user_id (in my site), amount= product_price( in my site), handling? … thanks a lot

  • Gourav

    it’s helpness

  • Amol Navsupe

    hello nice article, but when i used your mail id it redirect correctly but when i used my email id it will not redirect. what is that error please explain me and one more thing i ON Auto Return checkbox.
    thanks.

  • hemanth

    Currently PayPal accounts in India are only able to send payments. This recipient is not eligible to receive funds.

    can any one let me know how to resolve this.?

    • Mite

      Use payu gateway

  • hemanth

    how to change the button.??

  • P Kalbi

    Hello Sir,
    your code is very nice but there is error.
    I change the product value by inspect element like a 10 to 1 that user pay only USD 1. That is the wrong.
    please give me your solution.

    • We are working on a new tutorial where you will not face issues like this.

  • mercy

    Hi,I keep getting these error kindly help
    Fatal error: Call to a member function fetch() on null in my index.php

  • lchhieu

    I want divide Paypal Standard Payments to two separate accounts. thanks

  • dasu

    Waste tutorials. It gives error everywhere.

  • inf haris

    hi you have to active PDT first, here is there reference https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/paymentdatatransfer/ and it works for me

    hope it help

  • Rishabh Gusain

    Do I have a great future with PHP ? or I should change my mind and should go with something like JAVA and C#

  • gokul krishanan

    Thanks a lot I can understand very easily..

  • rishiba pp

    how to send a mail using php in localhost with wamp server
    ?

    • MrMisery

      use phpmailer

    • anon

      not possible on localhost

    • rahul t

      download phpmailer zip file..extract it and put in ur project folder. then follow some good tutorials..feel free to mail me at shine.wid.rahul.1987@gmail.com for code or further explanation.

    • Ajanthan

      its working

  • angeliestolle

    Interesting piece . Just to add my thoughts , people are searching for a Qualify for no cost adult electronic monitor tulare county form , We saw a blank version here http://goo.gl/pyR9G2.

  • Nitin

    Hello
    In this PayPal example, where do we use these email ids – info_**_biz@phpgang.com & info_**_per@phpgang.com ? What will be the value of $paypal_id ?
    thanks

  • Anthony Francis Fernandes

    hello,
    i wanted to know if you could help me with the cancellation of the booking of which the payment is already done and get the payment back in the paypal account..

    Thanks.

  • shail

    Hello,
    My Question is Send & Receive Payment is Workin On Localhost(Wamp Server)?

    Thanks,