PayPal Payment Gateway with PHP MySQL Database

Today, We want to share with you PayPal Payment Gateway with PHP MySQL Database.In this post we will show you wordpress plugin require another plugin, hear for paypal payment gateway integration in php example we will give you demo and example for implement.In this post, we will learn about PayPal Standard Payment Gateway Integration in PHP with an example.

PayPal Payment Gateway with PHP MySQL Database

There are the Following The simple About PayPal Payment Gateway with PHP MySQL Database Full Information With Example and source code.

As I will cover this Post with live Working example to develop paypal integration in php with sandbox, so the paypal payment gateway integration in php source code download is used for this example is following below.

  Clear All Cache From Laravel project 5.7

Step 1 : MySQL Database structure

Table structure for table `trans_details`

CREATE TABLE `trans_details` (
  `id` int(10) UNSIGNED NOT NULL,
  `broker_id` int(11) NOT NULL,
  `plan_id` varchar(255) NOT NULL,
  `hash` varchar(255) NOT NULL,
  `complete` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Indexes for table `trans_details`

ALTER TABLE `trans_details`
  ADD PRIMARY KEY (`id`);

Table structure for table `users_dtl`

CREATE TABLE `users_dtl` (
  `id` int(10) UNSIGNED NOT NULL,
  `username` varchar(20) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `broker` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Dumping data for table `users_dtl`

INSERT INTO `users_dtl` (`id`, `username`, `email`, `broker`) VALUES
(1, 'modi124', '[email protected]_name.com', 0);

Indexes for table `users_dtl`

ALTER TABLE `users_dtl`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for table `users_dtl`
--

ALTER TABLE `users_dtl`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;  

index.php

<?php

require 'src/init_model.php';
?>

<!DOCTYPE html>
<html>
<head><title>PayPal PHP Website Integration Tutorial</title></head>
<body>

<?php if($user->broker): ?>
	<p>You are a broker!</p>
<?php else: ?>
	<p>You are not a broker! <a href="broker/payment.php">Become a Member</a></p>
<?php endif; ?>	
</body>
</html>

composer.json

{
    "require": {
        "paypal/rest-api-sdk-php": "*"
    }
}

/paypal/broker/payment.php

<?php

use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Exception\PPConnectionException;


require '../src/init_model.php';


$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();

//Payer
$payer->setPaymentMethod('paypal');


// Details
$details->setShipping('32.00')
->setTax('0.00')
->setSubtotal('21.00');


// Amount
$amount->setCurrency('USD')
->setTotal('22.00')
->setDetails($details);


// Transaction
$transaction->setAmount($amount)
->setDescription('Membership');

// Payment
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);


// RedirectUrls
$redirectUrls->setReturnUrl('http://localhost/paypal/paypal/pay.php?approved=true')
->setCancelUrl('http://localhost/paypal/paypal/pay.php?approved=false');

$payment->setRedirectUrls($redirectUrls);


try {

	$payment->create($api);

	//Generate and store hash
	$hash = md5($payment->getId());
	$_SESSION['tamil_hash'] = $hash; 

	//transaction storage
	$store = $db->prepare("
		INSERT INTO trans_details (broker_id, payment_id, hash, complete)
		VALUES (:broker_id, :payment_id, :hash, 0)
		");

	$store->execute([
		'broker_id' => $_SESSION['broker_id'],
		'payment_id' => $payment->getId(),
		'hash' => $hash
	]);

} catch(PPConnectionException $e){
	header("Location: ../paypal/error.php");
}

//var_dump($Payment->getLinks());

foreach ($payment->getLinks() as $link) {
	if($link->getRel() == 'approval_url'){
		$redirectUrl = $link->getHref();
	}
}

header("Location: ". $redirectUrl);
?>

/paypal/broker/complete.php

<!DOCTYPE html>
<html>
<head><title>Paypal payment gateway integration code with PHP, Mysql project
</title></head>

<body>

 <p>Payment complete. <a href="../index.php">Go Home</a></p>
 
</body>
</html>

/paypal/paypal/pay.php

<?php
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;

require '../src/init_model.php';

if(isset($_GET['approved'])){
	$approved = $_GET['approved'] === "true";
	if($approved){
		$payerId = $_GET['PayerID'];

		$paymentId = $db->prepare("
			SELECT payment_id
			FROM trans_details
			WHERE hash = :hash
			");
		$paymentId->execute([
			'hash' => $_SESSION['tamil_hash']
		]);

		$paymentId = $paymentId->fetchObject()->payment_id;

		// Get The Paypal Payment
		$payment = Payment::get($paymentId, $api);

		$execution = new PaymentExecution();
		$execution->setPayerId($payerId);

		// Execute Paypal Payment (Charge)
		$payment->execute($execution, $api);


		//Update transaction

		$updateTransaction = $db->prepare("
			UPDATE trans_details
			SET complete = 1
			WHERE payment_id = :payment_id
			");

		$updateTransaction->execute([
			'payment_id' => $paymentId
		]);

		// Set the user as broker

		$setMember = $db->prepare("
			UPDATE users_dtl
			SET broker = 1
			WHERE id = :broker_id
			");

		$setMember->execute([
			'broker_id' => $_SESSION['broker_id']
		]);

		//Unset paypal Hash
		unset($_SESSION['tamil_hash']);
		header('Location: ../broker/complete.php');

	} else {
		header('Location: ../paypal/cancelled.php');
	}
}
?>

/paypal/paypal/cancelled.php

<!DOCTYPE html>
<html>
<head><title>paypal payment gateway tutorial</title></head>
<body>

 <p>You Cancelled.</p>
 
</body>
</html>

error.php

<!DOCTYPE html>
<html>
<head><title>PayPal PHP Website Integration Tutorial</title></head>
<body>

 <p>Something Went Wrong.</p>
 
</body>
</html>

/paypal/src/init_model.php

<?php

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;

session_start();

$_SESSION['broker_id'] = 1;

require __DIR__ . '/../vendor/autoload.php';

// API
$api = new ApiContext(
	new OAuthTokenCredential(
		'KLn3rLlkDFGHcCpTytgRRn_K692qwc4dd3yFGxOzedrwFFhj-DMA4866qaRwurhgA1QE7jyTtfjbgEDX',
		'FJDD8DFDRuEsmL3HelvjE0CmqscVKG9eM3jYY9GoxYDd75YUY5CacZSITwVgxWED8Di_TbFHuTiNCy-U'
	)
);

$api->setConfig([
	'mode' => 'sandbox',
	'http.ConnectionTimeOut' => 30,
	'log.LogEnabled' => false,
	'log.FileName' => '',
	'log.LogLevel' => 'FINE',
	'validation.level' => 'log'
]);

$db = new PDO('mysql:host=localhost;dbname=paypal_site', 'root', '');

$user = $db->prepare("select * from users_dtl where id= :broker_id");

$user->execute(['broker_id' => $_SESSION['broker_id']]);
$user = $user->fetchObject();

Web Programming Tutorials Example with Demo

Read :

  How to Set Headers in Laravel 6 Example?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about paypal integration in php step by step pdf.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of