Reputation: 13
I have a class called PayPal Express which is Handling API URL Requests for retrieving results such as the ClientID, Secret and PayPal Base URL are already set this class called config.php
define('PRO_PayPal', 0);
// Configuration for Live Paypal Transactions
if(PRO_PayPal){
define("PayPal_CLIENT_ID", "#########################");
define("PayPal_SECRET", "###################");
define("PayPal_BASE_URL", "https://api.paypal.com/v1/");
}
// configurations for sandbox Transactions
else{
define("PayPal_CLIENT_ID", "ATDV3X3ftdxIDC97H99-XgtTca7cpMsyPDtOrxuGKSMYzphNivsu8YFFWTVkRK5CnXbr93BY9liDNAeY");
define("PayPal_SECRET", "EPFvBgOjceLpCmoCKKrsXEFzcR4OtJh4hDimKoobA5Md3qpJGrByUgxh6leUMqzwrxrp_BwR8fXP9Vv3");
define("PayPal_BASE_URL", "https://api-m.sandbox.paypal.com/v1/");
}
The PayPal Express Class
require 'Config.php';
class PaypalExpress{
public $paypalClientID = PayPal_CLIENT_ID;
public $paypalSecret = PayPal_SECRET;
public function validate($paymentRef, $paymentToken, $payerID, $productID){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PayPal_BASE_URL.'oauth2/token');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->paypalClientID.":".$this->paypalSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$response = curl_exec($ch);
curl_close($ch);
if(empty($response)) die("Error:No response from paypal");
else{
$jsonData = json_decode($response);
$curl = curl_init(PayPal_BASE_URL.'payments/payment/'.$paymentRef);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $jsonData->access_token,
'Accept: application/json',
'Content-Type: application/xml'
));
$response = curl_exec($curl);
curl_close($curl);
// Transaction data
$result = json_decode($response);
return $result;
}
}
}
I have this Process.php page which gets the Returned URL data from PayPal and it is supposed to insert into the database the verified payment details
require 'config.php';
require 'session.php';
require 'class/paypalExpress.php';
require 'class/property.php';
require 'class/payment.php';
try {
if(!empty($_GET['paymentRef']) && !empty($_GET['payerID']) && !empty($_GET['token']) && !empty($_GET['propid']) ){
$paymentRef = $_GET['paymentRef'];
$payerID = $_GET['payerID'];
$token = $_GET['token'];
$propid = $_GET['propid'];
$payment = new payment();
$property = new property();
$paypalExpress = new paypalExpress();
$newId = $property->getProperty($propid);
$Check=$paypalExpress->validate($paymentRef, $propid, $payerID, $token);
if(isset($Check) && $Check->state == 'approved')
{
$id = $Check->id;
$state = $Check->state;
$payerFirstName = $Check->payer->payer_info->first_name;
$payerLastName = $Check->payer->payer_info->last_name;
$payerName = $payerFirstName.' '.$payerLastName;
$payerEmail = $Check->payer->payer_info->email;
$payerID = $Check->payer->payer_info->payer_id;
$payerCountryCode = $Check->payer->payer_info->country_code;
$paidAmount = $Check->transactions[0]->amount->details->subtotal;
$currency = $Check->transactions[0]->amount->currency;
if($newId->Price == $paidAmount && $newId->Currency == $currency)
{
$newpayment = $payment->updatePayments($propid,$paymentRef,$payerID,$token);
// redirecting information to reciept page
header("Location:Reciept.php?pyfname=$payerFirstName&pylname=$payerLastName&pymail=$payerEmail");
}
}
}
else{
header('Location:index.php'); // redirecting users if there is no transaction details on the url
}
} catch (PDOException $th) {
echo "Error : " .$th->getMessage();
}
Now when the transaction is done and Paypal redirects the ransaction details to Process.php, instead of inserting the payment details i am just getting a blank page with transaction details from Paypal on the URL. Is there something i am missing on the code?
Upvotes: 1
Views: 398
Reputation: 36
Thanks for the Code! Ive been trying to figure out how to complete paypal server side integration for several weeks! Your code works if you move the paypal credentials from your configuration.php to your paypalExpress.php
<?php //confiuration.php
define('PRO_PayPal', 0);
// define("PayPal_CLIENT_ID", "#########################");
// define("PayPal_SECRET", "'###################");
define("PayPal_BASE_URL", "https://api-m.paypal.com/v1/");
//////////////////////////////////////////////////////////
<?php //paypalExpress.php
require 'configuration.php';
class PaypalExpress{
//public $paypalClientID = '#########################'; // SANDBOX
// public $paypalSecret = '#########################'; // SANDBOX
public $paypalClientID = '#########################'; // LIVE
public $paypalSecret = '#########################'; // LIVE
public function validate($paymentRef, $paymentToken, $payerID, $productID){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PayPal_BASE_URL.'oauth2/token');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->paypalClientID.":".$this->paypalSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$response = curl_exec($ch);
curl_close($ch);
/*
// Configuration for Live Paypal Transactions
if(PRO_PayPal){
define("PayPal_CLIENT_ID", "#########################");
define("PayPal_SECRET", "###################");
define("PayPal_BASE_URL", "https://api.paypal.com/v1/");
}
// configurations for sandbox Transactions
else{
define
("PayPal_CLIENT_ID","ATDV3X3ftdxIDC97H99-
XgtTca7cpMsyPDtOrxuGKSMYzphNivsu8YFFWTVkRK5CnXbr93BY9liDNAeY");
define
("PayPal_SECRET","EPFvBgOjceLpCmoCKKrsXEFzcR4OtJh4
hDimKoobA5Md3qpJGrByUgxh6leUMqzwrxrp_BwR8fXP9Vv3");
define("PayPal_BASE_URL", "https://api-m.sandbox.paypal.com/v1/");
}
*/
?>
Upvotes: 2