An Engineers Notebook

Setting up PayPal Express Checkout API in PHP

It took me a while to find out how to do this properly, but the youtube video below is perfect, easy to follow. I have posted my code to assist.  

Start.php

<?php
require ('vendor/autoload.php');
define('SITE_URL', 'http://www.your-domain.com');
$paypal = new PayPalRestApiContext(
new PayPalAuthOAuthtokenCredential(
'AZ50OpZ7uXSt-t_yJJl_ZjkV8-PrZzqqj3bMQDIU-Ni8SxhupHZXcRd4ZRZM922U3HfgGGwP5gTgN5aj',
'EJT_I5lvrv9IxRz-GX-qOH8bagklmservijaieor ghjba[oudYPMGHHvfgfgfgfgfaweqfwe'
	)
);
?>

checkout.php

<?php 
////Global Stuff
	require('config/config.php');
	require('config/connection.php');
	require('config/mail_config.php');
session_start();
$transactionID = $_GET['transactionID'];
if (isset($_SESSION['username'])){
	$username = $_SESSION['username'];
}								
use PayPalApiPayment;
use PayPalApiPaymentExecution;
require ('start.php');
if(isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'], $_GET['transactionID'])){

if((bool)$_GET['success'] === false) {
	echo "success did not equal TRUE";
die();
}
$paymentId = $_GET['paymentId'];
$payerId = $_GET['PayerID'];
$token = $_GET['token'];
$payment = Payment::get($paymentId, $paypal);
$execute = new PaymentExecution();
$execute->setPayerId($payerId);

try {
	$result = $payment->execute($execute, $paypal);
} catch (Exception $e) {
	$data = json_decode($e->getData());
	var_dump($data);
	die();
}
echo 'Payment Made, Thanks!';
//////Do Data base stuff now.

?>

 

Leave a Reply

Your email address will not be published.