In this article, you will learn about How to Integrate PayPal Payment System with PayPal API in PHP and MySQL. Here i will show you how to use PayPal Sandbox test account for payment system development and passing arguments while click on buy now button. It’s a simple and easy way to integrate in your own web projects to process payment with PayPal gateway.
You need to follow our step by step process:
Step-1: Create a Sandbox account at https://developer.paypal.com/
Step-2: Now signup a test account for payment system. Take a look at top menu right-side button. you need to create personal and business account both for testing purpose.
You can skip next 2 step like details with credit card details.
After account creation you need to create sandbox credential, in the below image you will get sandbox menu in left-side menu bar.
Step-3: Create an index.php page to store php code displaying products, product images, Product Name and product price.
index.php
In the following code you need to put your business $paypal_id and change the url of return and cancel_return button.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $paypal_url='https://www.sandbox.paypal.com/cgi-bin/webscr'; // Test Paypal API URL $paypal_id='your_seller_id'; // Business email ID ?> <h4>Welcome, Guest</h4> <div class="product"> <div class="image"> <img src="https://www.tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png" /> </div> <div class="name"> Tutorialwebsite Paypal Payment </div> <div class="price"> Price:$100 </div> <div class="btn"> <form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1"> <input type="hidden" name="business" value="<?php echo $paypal_id; ?>"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="item_name" value="Tutorialswebsite Paypal Payment"> <input type="hidden" name="item_number" value="1"> <input type="hidden" name="credits" value="510"> <input type="hidden" name="userid" value="1"> <input type="hidden" name="amount" value="100"> <input type="hidden" name="cpp_header_image" value="<img src="https://www.tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png" data-mce-src="https://www.tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png">https://www.tutorialswebsite.com/wp-content/uploads/2016/01/cropped-LOGO-1.png"> <input type="hidden" name="no_shipping" value="1"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="handling" value="0"> <input type="hidden" name="cancel_return" value="https://demo.tutorialswebsite.com/payment_with_paypal/cancel.php"> <input type="hidden" name="return" value="https://demo.tutorialswebsite.com/payment_with_paypal/success.php"> <input type="image" src="https://www.sandbox.paypal.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.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"> </form> </div> </div> |
Step-4: create a success.php file for gateway response.
success.php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $item_no = $_REQUEST['item_number']; $item_transaction = $_REQUEST['tx']; // Paypal transaction ID $item_price = $_REQUEST['amt']; // Paypal received amount $item_currency = $_REQUEST['cc']; // Paypal received currency type $price = '100.00'; $currency='USD'; //Rechecking the product price and currency details if($item_price==$price && $item_currency==$currency) { echo "<h1>Welcome, Guest</h1>"; echo "<h1>Payment Successful</h1>"; } else { echo "<h1>Payment Failed</h1>"; } |
After successful payment, Paypal return file or response with some argument like item_number, amount, currency.
Paypal data success.php?tx=xxxxxxxx&st=Completed&amt=100&cc=USD&cm=&item_number=1
Step-5: create a cancle.php page , Paypal API return you to this page when payment get error or cancel status.
2 3 4 5 6 7 |
<?php echo "<h1>Welcome, Guest</h1>"; echo "<h1>Payment Canceled</h1>"; ?> |
Step-6: When you have done with test payment system workflow. you need to change the form action development API URLs to Live API URLs and change live valid $paypal_id seller email id.
2 3 4 5 6 |
$paypal_url='https://www.sandbox.paypal.com/cgi-bin/webscr'; //to $paypal_url='https://www.paypal.com/cgi-bin/webscr'; |
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co