As we all know that PayPal is the biggest Online Payment Solution available these days. Many websites need to accept payments from their customers/users for providing services or selling products. This can be done very easily by integrating PayPal as a payment gateway in a website. Today we are going to see how to integrate PayPal as payment gateway in any web page using PHP.
Introduction:
Now we are going to setup PayPal for receiving payments on our website using a simple Buy Now Button. We are going to it with demo accounts available for developers by PayPal. After everything will be successfully completed and working we will replace demo account with real PayPal account.
Step 1:
Create PayPal sandbox account using this link: https://developer.paypal.com/
Step 2:
Go to Applications Tab and go to Sandbox accounts. Create one business and one personal account.
Here you can see, I have created one Personal (Buyer) & Business (Merchant/Seller) account.
Step 3:
Check below example for creating personal account.
PHP Code:
Example.php:
This file contains PHP code which displays product name, product image, product price & payment button. Clicking the Payment button will send user to PayPal website for payment. You can modify product information. You can modify the URLs where user will be redirected if payment is successful or cancelled. Check input field <input name=”cancel_return” & <input name=”return”.
1 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?PHP require_once("config.php"); ?> <style> .title{ font-weight:bold; } .buy { border: 1px solid #cecece; padding: 10px; margin: 5px; padding: 5px; text-align: center; width: 300px; } </style> <div style="text-align:center"> <h2>Paypal Payment plugin for websites using PHP and MYSQL</h2> <h3>Please create a testing buyer account to test this demo. <a rel="nofollow" target="_blank" href="https://developer.paypal.com">Click here</a> to create test buyer account</h3> <h4>Buy this item now</h4> <div class="buy"> <div class="image"> <img src="http://earlysandwich.com/logo.png" /> </div> <div class="title"> Early Sandwich Payment Script <br /><br /> </div> <div class="price"> Price:$20 <br /> <br /> </div> <div class="btn"> <form action="<?php echo $payment_url; ?>" method="post" name="frmPayPal1"> <input type="hidden" name="cancel_return" value="http://demos.earlysandwich.com/paypal-payment-integrate-website-php/assets/cacelled.php"> <input type="hidden" name="return" value="http://demos.earlysandwich.com/paypal-payment-integrate-website-php/assets/processed.php"> <input type="hidden" name="business" value="<?php echo $merchant_email; ?>"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="item_name" value="Early Sandwich Paypal Payment Script"> <input type="hidden" name="item_number" value="1"> <input type="hidden" name="credits" value="510"> <input type='hidden' name='rm' value='2'> <input type="hidden" name="userid" value="1"> <input type="hidden" name="amount" value="20"> <input type="hidden" name="cpp_header_image" value="http://earlysandwich.com/wp-content/themes/newsplus/images/logo.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="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> </div> </div> </div> |
Config.php:
This File contains PayPal Payment API URL. Currently for testing purpose we are using PayPal Sandbox API URL. Please enter merchant/seller account email at $merchant_email.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $payment_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; // PayPal API URL /* Please note that after complete development change the above URL and remove ".sandbox" from the URL or comment the above line and uncomment the below line. */ //$payment_url = "https://www.paypal.com/cgi-bin/webscr"; // PayPal API URL ?> |
Processed.php:
It payment is successful, user will be redirected to this page along with product payment data. The data received from PayPal can be stored in database and records can be maintained.
1 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 |
<?php // Here is the price and currency code of our product according to our records $price = '20.00'; $currency='USD'; // Here is the product payment info received from PAYPAL after payment echo "<br />Product #:".$product_no = $_POST['item_number']; // product ID echo "<br />Transaction ID:".$product_transaction = $_POST['txn_id']; // PayPal transaction ID echo "<br />Price:".$product_price = $_POST['mc_gross']; // PayPal received amount echo "<br />Currency:".$product_currency = $_POST['mc_currency']; // PayPal currency type of received amount echo "<br />Payment Date:".$payment_date = $_POST['payment_date']; echo "<br />Merchant ID:".$merchant = $_POST['receiver_email']; // Confirming the product price and currency code of payment made by buyer against // product price and currency code according to our records if($price == $product_price && $product_currency==$currency) { echo "<h1>Payment successfully processed</h1>"; } else { echo "<h1>Payment details does not match our records. Please contact our support at [email protected]</h1>"; } ?> |
Cancelled.php:
User will be redirected to cancelled.php if payment is not processed. You can change this URL in example.php.
1 2 3 |
<h1>Welcome, User</h1> <h2>For some reason payment has been cancelled known to you.</h2> |
last step:
If you have successfully integrated everything using PayPal Sandbox Account. Its time to change the Sandbox API to real PayPal API.
1 2 3 4 5 6 7 8 9 10 11 |
<?php //$payment_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; // Paypal API URL /* Please note that after complete development change the above URL and remove ".sandbox" from the url or comment the above line and uncomment the below line. */ $payment_url = "https://www.paypal.com/cgi-bin/webscr"; // Paypal API URL $merchant_email = "merchant email" ; // merchant email id ?> |
Just comment the above $payment_url line and uncomment below $payment_url line and insert your real PayPal Merchant ID.
Hope you like it.
FINALLY! A WORKING ONE!:)
Actually, it isn’t working at all. Several errors.
Hi Umar,
I would like to know how I could make a payment list: e.g like ticketing systems. If a person wants to buy two products of the same price.