php tutorial - PayPal Transaction Details using Transaction ID - php programming - learn php - php code - php script



In our previous blog post we have covered the PayPal Express Checkout for Single Product using which you can collect more user information entered at the time of purchase.

What happened if admin have lots of payer transaction id and admin wants to see details of any specific transaction.?

In this blog we are going to demonstrate how we can get all transaction data using transaction id for doing this we are using PayPal PHP SDK.

paypal-transaction-details-using-transaction-id

Learn php - php tutorial - paypal-transaction-details-using-transaction-id - php examples - php programs

Project Integartion

In our last post we have seen how to get Paypal App Id and App Secret, which is necessary to access PayPal PHP SDK.

Follow that post to learn how to get the credentials and put it in bootstrap.php file of this project to setup the required settings.

Then you will be ready to run the project. You can also refer the install.txt file given in download code folder.

Bootstrap.php

This file will help to load and connect PayPal PHP SDK with your application.

<?php
// Include the composer Autoloader
// The location of your project's vendor autoloader.
$composerAutoload = 'PayPal-PHP-SDK/autoload.php';
if (!file_exists($composerAutoload)) {
//If the project is used as its own project, it would use rest-api-sdk-php composer autoloader.
$composerAutoload = 'PayPal-PHP-SDK/vendor/autoload.php';
if (!file_exists($composerAutoload)) {
echo "The 'vendor' folder is missing. You must run 'composer update' to resolve application dependencies.\nPlease see the README for more information.\n";
exit(1);
}
}
require $composerAutoload;

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

error_reporting(E_ALL);
ini_set('display_errors', '1');

// Replace these values by entering your own ClientId and Secret by visiting https://developer.paypal.com/webapps/developer/applications/myapps
$clientId = 'Your PayPal App Id';
$clientSecret = 'Your PayPal App Secret';
$mode = 'sandbox';

/** @var \Paypal\Rest\ApiContext $apiContext */
$apiContext = getApiContext($clientId, $clientSecret);

return $apiContext;

function getApiContext($clientId, $clientSecret) {

// ### Api context
// Use an ApiContext object to authenticate
// API calls. The clientId and clientSecret for the
// OAuthTokenCredential class can be retrieved from
// developer.paypal.com

$apiContext = new ApiContext(
new OAuthTokenCredential(
$clientId, $clientSecret
)
);

// Comment this line out and uncomment the PP_CONFIG_PATH
// 'define' block if you want to use static file
// based configuration
global $mode;
$apiContext->setConfig(
array(
'mode' => $mode,
'log.LogEnabled' => true,
'log.FileName' => '../PayPal.log',
'log.LogLevel' => 'DEBUG', // PLEASE USE `FINE` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
'validation.level' => 'log',
'cache.enabled' => true,
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
)
);

// Partner Attribution Id
// Use this header if you are a PayPal partner. Specify a unique BN Code to receive revenue attribution.
// To learn more or to request a BN Code, contact your Partner Manager or visit the PayPal Partner Portal
// $apiContext->addRequestHeader('PayPal-Partner-Attribution-Id', '123123123');
return $apiContext;
}
click below button to copy the code. php tutorial - team

Index.php

This is the main file showing Transaction detail via TransactionID.

<?php
// # GetPaymentSample
// This sample code demonstrate how you can
// retrieve a list of all Payment resources
// you've created using the Payments API.
// ### Retrieve payment
// Retrieve the payment object by calling the
// static `get` method
// on the Payment class by passing a valid
// Payment ID
// (See bootstrap.php for more on `ApiContext`)
require __DIR__ . '/bootstrap.php';

use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\CreditCard;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Transaction;

if (isset($_POST['submit'])) {
$paymentId = $_POST['paymentId'];
try {
$payment = Payment::get($paymentId, $apiContext);
$obj = json_decode($payment); // $obj contains the All Transaction Information.Some of them,I have displayed Below.
} catch (Exception $ex) {
$payment = 'Not Valid';
}
}
?>
<html>
<head>
<title>PayPal Transaction Details using Transaction Id</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/popup-style.css" />
<script src="js/jquery-latest.js" type="text/javascript"></script>
</head>
<body>
<div id = "main">
<h1>PayPal Transaction Details using Transaction Id</h1>
<div id = "login">
<h2>Transaction Detail</h2>
<hr/>
<div id="search">
<table id="results" >
<thead>
<tr class="head">
<th>Enter TransactionID</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<form action="index.php" method="POST">
<input type="text" name="paymentId">
<input type="submit" id="submit" value="Search Detail By TransactionID" name="submit">
</form>
</td>
</tr>
</tbody>
</table>
</div>
<div id="demo_id">
<table id="results" >
<thead>
<tr class="head">
<th>Demo TransactionId</th>
</tr>
</thead>
<tbody id="left90">
<tr>
<td>PAY-2WN6537138591315GKUTEVSI</td>
</tr>
<tr>
<td>PAY-95C53794T9204671MKUTEWFQ</td>
</tr>
<tr>
<td>PAY-1GX074103A011743DKUTEWYA</td>
</tr>
<tr>
<td>PAY-1C959132H6742844RKUTVN5A</td>
</tr>
</tbody>
</table>
</div>
<?php
if (isset($_POST['submit'])) {
if ($payment !== 'Not Valid') {
?>
<table id="results" class="messages">
<thead>
<tr class="head">
<th colspan="2">Payer Detail</th>

</tr>
</thead>
<tbody id="left90">
<tr>
<td>TransactionID</td>
<td><?php echo $obj->id; ?></td>
</tr>
<tr>
<td>Payment Method</td>
<td><?php echo $obj->payer->payment_method; ?></td>

</tr>
<tr>
<td>Full Name</td>
<td><?php
echo $obj->payer->payer_info->first_name;
echo ' ';
echo $obj->payer->payer_info->last_name;
?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $obj->payer->payer_info->email; ?></td>
</tr>
<tr>
<td>Payer ID</td>
<td><?php echo $obj->payer->payer_info->payer_id; ?></td>
</tr>
<tr>
<td>Address</td>
<td><ul style="text-align: left;">
<li><?php echo "Line1 --> " . $obj->payer->payer_info->shipping_address->line1 ?></li>
<li><?php echo "City --> " . $obj->payer->payer_info->shipping_address->city ?></li>
<li><?php echo "State --> " . $obj->payer->payer_info->shipping_address->state ?></li>
<li><?php echo "Postal Code --> " . $obj->payer->payer_info->shipping_address->postal_code ?></li>
<li><?php echo "Country Code --> " . $obj->payer->payer_info->shipping_address->country_code ?></li>
<li><?php echo "Recipient Name --> " . $obj->payer->payer_info->shipping_address->recipient_name ?></li>
</ul></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo $obj->payer->status; ?>
</td>
</tr>
</tbody>
<thead>
<tr class = "head">
<th colspan = "2">Transactions Detail</th>
</tr>
</thead>
<tbody id="left90">
<tr><td>Invoice_Number</td>
<td><?php echo $obj->transactions[0]->invoice_number; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Total Amount</td>
<td>
<ul style="text-align: left;">
<li><?php echo "Total Amount --> " . $obj->transactions[0]->amount->total ?></li>
<li><?php echo "Currency --> " . $obj->transactions[0]->amount->currency ?></li>
</ul>
</td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Description</td>
<td><?php echo $obj->transactions[0]->description; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>State</td>
<td><?php echo $obj->transactions[0]->related_resources[0]->sale->state; ?></td>
</tr>
</tbody>
<thead id="left90">
<tr class = "head">
<th colspan = "2">Item Detail</th>
</tr>
</thead>
<tbody id="left90">
<tr><td>Item Name</td>
<td><?php echo $obj->transactions[0]->item_list->items[0]->name; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Price</td>
<td><?php echo $obj->transactions[0]->item_list->items[0]->price; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Currency</td>
<td><?php echo $obj->transactions[0]->item_list->items[0]->currency; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Quantity</td>
<td><?php echo $obj->transactions[0]->item_list->items[0]->quantity; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Create Time</td>
<td><?php echo $obj->create_time; ?></td>
</tr>
</tbody>
<tbody id="left90">
<tr><td>Update Time</td>
<td><?php echo $obj->update_time; ?></td>
</tr>
</tbody>
</table>
<?php } else if ($payment === 'Not Valid') {
?>
<table id="results" class="messages">
<thead>
<tr class="head">
<th colspan="2"><span>Enter Valid Transaction ID.</span></th>

</tr>
</thead>
</table>
<?php }
}
?>
</div>
<img id="paypal_logo" style="margin-left: 722px;" src="images/secure-paypal-logo.jpg">
</div>
<div id="pop2" class="simplePopup">
<div id="loader"><img src="images/ajax-loader.gif"/><img id="processing_animation" src="images/processing_animation.gif"/></div>
</div>
<script src="js/jquery.simplePopup.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input#submit').click(function() {
$('#pop2').simplePopup();
});
});
</script>
</body>
</html>
click below button to copy the code. php tutorial - team

Style.css

Includes basic styling of HTML elements.

@import url(http://fonts.googleapis.com/css?family=Raleway);
h1{
text-align: center;
}
#main {
width: 950PX;
margin: 50PX auto;
font-family: raleway;
}
#login {
width: 834px;
float: left;
border-radius: 10px;
font-family: raleway;
border: 2px solid #ccc;
padding: 10px 40px 11px;
margin: 16PX;
}
h2 {
background-color: #FEFFED;
text-align: center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}
hr {
border: 0;
border-bottom: 1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#search {
float: left;
width: 48%;
margin-right: 2%;
}
#results {
width: 100%;
margin-top: 30px;
border: 1px solid #ccc;
table-layout: fixed;
margin-bottom: 50px;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
thead {
display: table-header-group;
vertical-align: middle;
border-color: inherit;
}
.head {
font-size: 15px;
font-family: "proxima_novasemibold", sans-serif;
background: #FEFFED;
color: #1d4c55;
}
tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
.head th, td {
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
line-height: normal;
padding: 10px 0 10px;
text-align: center;
}
tbody {
display: table-row-group;
vertical-align: middle;
border-color: inherit;
}
tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
input[type=text]{
width: 90%;
padding: 10px;
margin-top: 20px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 17px;
font-family: raleway;
}
input[type=submit] {
width: 90%;
margin-top: 20px;
background-color: #FFBC00;
color: white;
border: 1px solid #FFCB00;
padding: 10px;
font-size: 17px;
cursor: pointer;
border-radius: 5px;
margin-bottom: 0px;
}
#demo_id {
width: 50%;
float: right;
}
span{
color:red;
}
ul{
line-height: 22px;
}
#loader
{
margin-left: 8%;
margin-bottom: 45px;
}
#loader #processing_animation {
width: 70%;
height: 12%;
}

#left90 tr td{
padding-left: 90px;
text-align: left;
}
click below button to copy the code. php tutorial - team


Related Searches to PayPal Transaction Details using Transaction ID