ADVERTISEMENTS

PayPal Payment Gateway integration with PHP and MySQL

Coder DakshOct 02, 2020
PayPal Payment Gateway integration with PHP and MySQL

As we know that today ecommerce plateform is growing too much day by day and everyone wants to be a ecommerce expert. Just by thinking ecommerce, we got first word in our mind is payment integration which is most important phase ecommerce website development.

Here we are going to elaborate about paypal integration with PHP and MySql. It's quite simple and not to worry about bulk amount of code. Initially, we need to create sandbox account for testing purpose. This article would help you to implement a basic level of paypal intergration. Later on you can extend paypal payment features in this integration.

ADVERTISEMENTS

Step 1: Paypal Accounts

Our first step would be creation of sandbox accounts. As this would be sandbox account, we'll have to generate two different type of accounts i.e. Buyer & Seller. You can easily create these by paypal link given below :

https://developer.paypal.com

After login to paypal, you'll find sandbox menu on top left side from where you can easily create buyer and sellers test accounts.

Paypal Screen

You have to take care about email id of seller which we'll pass in our next step as a hidden field.

Step 2: Code Implementation

In this step, we'll do code implementation of paypal payment gateway integration with php and mysql. We are displaying code implementation of We'll do this in three files i.e. checkout.php, success.php and cancel.php

a) checkout.php

This file contains a form to checkout to paypal platform. We'll pass couple of hidden values in this form which will get understood by paypal.

<?php
$paypalUrl  = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; // Sandbox Paypal API URL
$successUrl = '[PATh_OF_SUCCESS_File i.e. success.php]';
$cancelUrl  = '[PATh_OF_CANCEL_File i.e. cancel.php]';
$seller_id  = '[SELLER_EMAIL_ID]'; // Email ID of Seller Account
?>
<style type="text/css">
.container11{width: 380px; margin: 0 auto; border: 2px solid #ccc; padding: 10px; background: #eee; text-align:center;}
</style>
<div class="product container11">            
	<form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1">
		<h4>Paypal Payment Gateway</h4>
		<h5>Etutorialz Test Payment</h5>
		<h3>Price: $12</h3>
		<div class="btn">
			<input type="hidden" name="credits" value="150">
			<input type="hidden" name="userid" value="1">
			<input type="hidden" name="amount" value="10">
			<input type="hidden" name="cpp_header_image" value="https://etutorialz.com/img/etutorials_logo.png">
			<input type="hidden" name="business" value="<?php echo $seller_id; ?>">
			<input type="hidden" name="cmd" value="_xclick">
			<input type="hidden" name="item_name" value="Etutorialz Test Payment">
			<input type="hidden" name="cancel_return" value="<?= $cancelUrl ?>">
			<input type="hidden" name="return" value="<?= $successUrl ?>">
			<input type="hidden" name="item_number" value="12">
			<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.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" alt="PayPal Payment!" name="submit" >
			<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
		</div>
    </form> 
</div>
ADVERTISEMENTS

No, we are done with checkout page but you have to update successUrl, cancelUrl and seller_id with actual value.

Test Payment Screen

b) success.php

Now, we have to set code of success.php. Once payment will get successful, paypal will redirect to success page url with data in query string like [Website_URL]/success.php?tx=8863F754H850D&st=Completed&amt=12.00&cc=USD&cm=&item_number=12

In this file, you can add your mysql queries for saving payment details.

<?php
$trans_price      = $_REQUEST['amt'];
$trans_item       = $_REQUEST['item_number'];
$trans_currency   = $_REQUEST['cc']; 
$trans_id         = $_REQUEST['tx']; 
/*------- START - MYSQL CODE ----------*/
// here you can add your mysql code for saving this payment in databse.
//..
/*------- END - MYSQL CODE ----------*/
echo "<h1>Payment Successful and your transaction id is ".$trans_id."</h1>";
?>
ADVERTISEMENTS

c) cancel.php

It's a simple file which will contains a message.

echo "<h1>Payment Declined</h1>";

Step 3: Final Changes

You have to perform this step when you'll get ready for moving to production. In this step, we just have to update $paypalUrl.

$paypalUrl='https://www.sandbox.paypal.com/cgi-bin/webscr';

Now, we are ready with paypal integration with PHP & Mysql.

ADVERTISEMENTS