PayPal Payment Gateway integration with PHP and MySQL

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.

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.



Paypal Payment Gateway

Etutorialz Test Payment

Price: $12

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.

Payment Successful and your transaction id is ".$trans_id."";
?>

c) cancel.php

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

Payment Declined";
?>

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.