Reference
https://developer.paypal.com/docs/checkout/integrate/
and server side setup https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/
- Create paypal developer account
- Create an app and get client id and secret id
- Use sandbox or business and personal accounts
- Install Composer in your machine https://getcomposer.org/
- Create a project
- Create dependency for PHP
- Inside project folder execute “composer require paypal/paypal-checkout-sdk 1.0.1” reference from and server side setup https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/
- create index.php with below code
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- Add meta tags for mobile and IE -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title> PayPal Checkout Integration | Server Demo </title>
</head>
<body>
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=Giveyourclientid"></script>
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '10.01'
}
}]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
if (details.status === 'COMPLETED') {
// This function shows a transaction success message to your buyer.
$.ajax({
url: 'gettransaction.php',
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert(msg);
}
}).then(function(response) {
// redirect to the completed page if paid
console.log(response);
window.location.href = 'ThankYou.php';
}). catch (function(error) {
// redirect to failed page
console.log(error);
window.location.href = 'paymentFailed.php?reason=internalerror';
});
// alert('Transaction completed by ' + details.payer.name.given_name);
} else {
window.location.href = 'paymentFailed.php?reason=failed';
}
});
},
onCancel: function(data) {
window.location.href = 'paymentFailed.php?reason=cancelled';
}
}).render('#paypal-button-container');
</script>
</body>
</html>
9. Create Thank you page, payment failed pages.. etc for your redirections.
10. Create get transaction page, which has code to save it to db
<?php
namespace Sample;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
* You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
echo json_encode($response->result, JSON_PRETTY_PRINT);
die();
/**
* Enable the following line to print complete response as JSON.
*/
print json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
$sl = $response->result->status;
//code to db
$db = mysqli_connect('localhost:3306', 'root', '', 'paymentdetails');
echo "database connected";
$sql = "INSERT INTO transaction(orderid) VALUES('$sl')";
mysqli_query($db, $sql);
echo "data inserted successfully";
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
}
/**
* This driver function invokes the getOrder function to retrieve
* sample order details.
*
* To get the correct order ID, this sample uses createOrder to create a new order
* and then uses the newly-created order ID with GetOrder.
*/
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
print_r($data);
echo 'order id = ' . $data->orderID;
if (!count(debug_backtrace())) {
GetOrder::getOrder($data->orderID, true);
}
?>
execute from index.php
Leave a comment