Initial clean V5 repository
This commit is contained in:
38
paypal_advanced_checkout_in_php/CodexWorld-License.txt
Normal file
38
paypal_advanced_checkout_in_php/CodexWorld-License.txt
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
License by CodexWorld.com
|
||||
Author: CodexWorld
|
||||
Author URL: http://www.codexworld.com
|
||||
License: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
|
||||
License URL: http://creativecommons.org/licenses/by-nc-nd/4.0/
|
||||
*/
|
||||
----------------------------------
|
||||
NOTE : FREQUENTLY ASKED QUESTIONS
|
||||
----------------------------------
|
||||
|
||||
1. What is CodexWorld?
|
||||
|
||||
CodexWorld.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.
|
||||
|
||||
2. Is Tutorials and Scripts of CodexWorld.com Free?
|
||||
|
||||
Yes, all the tutorials, scripts and demos of codexworld.com are free to use for personal and non-commercial purpose. Please don<6F>t re-publish our tutorials, scripts, demos and downloaded ZIP file in any manner.
|
||||
|
||||
3. I want to Help CodexWorld, How can I?
|
||||
|
||||
You can help CodexWorld By donating any amount you wish or contributing article, scripts to CodexWorld.com
|
||||
|
||||
4. I want to use CodexWorld's scripts for commercial purpose?
|
||||
|
||||
You have to donate us $10 per script. If you want scripts for multiple domains or bulk scripts please contact admin@codexworld.com
|
||||
|
||||
5. Under which license you are providing these tutorials and scripts?
|
||||
|
||||
All the articles, tutorials and scripts are under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
|
||||
|
||||
|
||||
-----------------------------------
|
||||
DONATE US: CONTRIBUTE TO GROW
|
||||
-----------------------------------
|
||||
|
||||
Donate us from here - http://www.codexworld.com/support-us/
|
||||
|
||||
114
paypal_advanced_checkout_in_php/PaypalCheckout.class.php
Normal file
114
paypal_advanced_checkout_in_php/PaypalCheckout.class.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* This PayPal Checkout API handler class is a custom PHP library to handle the PayPal REST API calls.
|
||||
*
|
||||
* @class PaypalCheckout
|
||||
* @author CodexWorld
|
||||
* @link https://www.codexworld.com
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
// Include the configuration file
|
||||
include_once 'config.php';
|
||||
|
||||
class PaypalCheckout
|
||||
{
|
||||
//public $paypalAuthAPI = PAYPAL_SANDBOX ? 'https://api-m.sandbox.paypal.com/v1/oauth2/token' : 'https://api-m.paypal.com/v1/oauth2/token';
|
||||
//public $paypalAPI = PAYPAL_SANDBOX ? 'https://api-m.sandbox.paypal.com/v2/checkout' : 'https://api-m.paypal.com/v2/checkout';
|
||||
|
||||
public $paypalAuthAPI = PAYPAL_BASE_URL.'/v1/oauth2/tocken';
|
||||
public $paypalAPI = PAYPAL_BASE_URL.'/v2/checkout';
|
||||
public $paypalClientID = PAYPAL_CLIENT_ID;
|
||||
private $paypalSecret = PAYPAL_CLIENT_SECRET;
|
||||
|
||||
public function generateAccessToken()
|
||||
{
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_VERBOSE, 1);
|
||||
curl_setopt($ch, CURLOPT_URL, $this->paypalAuthAPI);
|
||||
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");
|
||||
$auth_response = json_decode(curl_exec($ch));
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code != 200 && !empty($auth_response->error)) {
|
||||
throw new Exception('Failed to generate Access Token: ' . $auth_response->error . ' >>> ' . $auth_response->error_description);
|
||||
}
|
||||
|
||||
if (!empty($auth_response)) {
|
||||
return $auth_response->access_token;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function createOrder($productInfo, $paymentSource)
|
||||
{
|
||||
$accessToken = $this->generateAccessToken();
|
||||
if (empty($accessToken)) {
|
||||
return false;
|
||||
} else {
|
||||
$postParams = $productInfo;
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $this->paypalAPI . '/orders/');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $accessToken));
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postParams));
|
||||
|
||||
$api_resp = curl_exec($ch);
|
||||
$api_data = json_decode($api_resp, true);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code != 200 && $http_code != 201) {
|
||||
throw new Exception('Failed to create Order sl (' . $http_code . '): ' . $api_resp . json_encode($postParams));
|
||||
}
|
||||
|
||||
return !empty($api_data) && ($http_code == 200 || $http_code == 201) ? $api_data : false;
|
||||
}
|
||||
}
|
||||
|
||||
public function captureOrder($orderId)
|
||||
{
|
||||
$accessToken = $this->generateAccessToken();
|
||||
if (empty($accessToken)) {
|
||||
return false;
|
||||
} else {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $this->paypalAPI . '/orders/' . $orderId . '/capture');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $accessToken));
|
||||
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
$api_resp = curl_exec($ch);
|
||||
$api_data = json_decode($api_resp, true);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$curlsl_code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
|
||||
// $result = urldecode ($ch );
|
||||
|
||||
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
|
||||
if ($http_code != 200 && $http_code != 201) {
|
||||
|
||||
throw new Exception('Failed to create Order sl4(' . $http_code . '): ' . '1- ' . $api_resp);
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
return !empty($api_data) && ($http_code == 200 || $http_code == 201) ? $api_data : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
paypal_advanced_checkout_in_php/ReadMe.txt
Normal file
21
paypal_advanced_checkout_in_php/ReadMe.txt
Normal file
@ -0,0 +1,21 @@
|
||||
Author: CodexWorld
|
||||
Author URL: http://www.codexworld.com/
|
||||
Author Email: contact@codexworld.com
|
||||
Tutorial Link: http://www.codexworld.com/paypal-advanced-checkout-card-payments-integration-in-php/
|
||||
|
||||
|
||||
============ Instructions ============
|
||||
1. Create a database (for example, codexworld_db) and import the "SQL/transactions.sql" file into this database.
|
||||
|
||||
2. Open the "config.php" file in an editor:
|
||||
===> Set PAYPAL_SANDBOX to FALSE.
|
||||
===> Specify the Client ID (PAYPAL_PROD_CLIENT_ID) and Secret (PAYPAL_PROD_CLIENT_SECRET) as per your PayPal REST API credentials.
|
||||
===> Specify the database host (DB_HOST), username (DB_USERNAME), password (DB_PASSWORD), and name (DB_NAME) as per your MySQL database credentials.
|
||||
|
||||
3. Open the "index.php" file on the browser ===> Enter the test card number, a valid future expiration date, and any random CVV number ===> Click Pay Now to proceed with the payment.
|
||||
|
||||
|
||||
============ May I Help You ===========
|
||||
Do reach out to us at {support@codexworld.com} if you have any trouble implementing this or if you need any help.
|
||||
|
||||
If you have any queries about this script, send the query by posting a comment here - http://www.codexworld.com/paypal-advanced-checkout-card-payments-integration-in-php/#respond
|
||||
74
paypal_advanced_checkout_in_php/SQL/transactions.sql
Normal file
74
paypal_advanced_checkout_in_php/SQL/transactions.sql
Normal file
@ -0,0 +1,74 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 5.2.1
|
||||
-- https://www.phpmyadmin.net/
|
||||
--
|
||||
-- Host: 127.0.0.1
|
||||
-- Generation Time: Jan 09, 2024 at 04:14 PM
|
||||
-- Server version: 10.4.28-MariaDB
|
||||
-- PHP Version: 8.2.4
|
||||
|
||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||
START TRANSACTION;
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8mb4 */;
|
||||
|
||||
--
|
||||
-- Database: `codexworld_db`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Table structure for table `transactions`
|
||||
--
|
||||
|
||||
CREATE TABLE `transactions` (
|
||||
`id` int(11) NOT NULL,
|
||||
`item_number` varchar(50) DEFAULT NULL,
|
||||
`item_name` varchar(255) DEFAULT NULL,
|
||||
`item_price` float(10,2) DEFAULT NULL,
|
||||
`item_price_currency` varchar(10) DEFAULT NULL,
|
||||
`order_id` varchar(50) NOT NULL,
|
||||
`transaction_id` varchar(50) NOT NULL,
|
||||
`paid_amount` float(10,2) NOT NULL,
|
||||
`paid_amount_currency` varchar(10) NOT NULL,
|
||||
`payment_source` varchar(50) DEFAULT NULL,
|
||||
`payment_source_card_name` varchar(50) DEFAULT NULL,
|
||||
`payment_source_card_last_digits` varchar(4) DEFAULT NULL,
|
||||
`payment_source_card_expiry` varchar(10) DEFAULT NULL,
|
||||
`payment_source_card_brand` varchar(25) DEFAULT NULL,
|
||||
`payment_source_card_type` varchar(25) DEFAULT NULL,
|
||||
`payment_status` varchar(25) NOT NULL,
|
||||
`created` datetime NOT NULL,
|
||||
`modified` datetime NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Indexes for dumped tables
|
||||
--
|
||||
|
||||
--
|
||||
-- Indexes for table `transactions`
|
||||
--
|
||||
ALTER TABLE `transactions`
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for dumped tables
|
||||
--
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT for table `transactions`
|
||||
--
|
||||
ALTER TABLE `transactions`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
|
||||
COMMIT;
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
22
paypal_advanced_checkout_in_php/config.php
Normal file
22
paypal_advanced_checkout_in_php/config.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
// Product Details
|
||||
$itemNumber = "MS112345";
|
||||
$itemName = "Evenement xyz";
|
||||
$itemPrice = 75;
|
||||
$currency = "USD";
|
||||
|
||||
/* PayPal REST API configuration
|
||||
* You can generate API credentials from the PayPal developer panel.
|
||||
* See your keys here: https://developer.paypal.com/dashboard/
|
||||
*/
|
||||
define('PAYPAL_SANDBOX', TRUE); //TRUE=Sandbox | FALSE=Production
|
||||
define('PAYPAL_SANDBOX_CLIENT_ID', 'ASX6ldixTmfKnk1w7IKhfTyjTKmNwf8CBYD_vLdEz2-K0msADQmnp9MevIgIvjiSFzwGg3R4bZnvr1FV'); //PayPal Client ID for Sandbox
|
||||
define('PAYPAL_SANDBOX_CLIENT_SECRET', 'ELuQIBgqgd1mk_ChzOJivkYBbxFy2nBe1d4ED6fIfYFQ7r6-0oBqVBHF5G0WaywNxlGgI8LiyN5yhISP'); //PayPal Client Secret for Sandbox
|
||||
define('PAYPAL_PROD_CLIENT_ID', ''); //PayPal Client ID for Production
|
||||
define('PAYPAL_PROD_CLIENT_SECRET', ''); //PayPal Client Secret for Production
|
||||
|
||||
// Database configuration
|
||||
define('DB_HOST', '158.69.235.152');
|
||||
define('DB_USERNAME', 'ms1inscription');
|
||||
define('DB_PASSWORD', 'ms1911');
|
||||
define('DB_NAME', 'dev_ms1inscription_preprod');
|
||||
BIN
paypal_advanced_checkout_in_php/css/loading.gif
Normal file
BIN
paypal_advanced_checkout_in_php/css/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 97 KiB |
133
paypal_advanced_checkout_in_php/css/style.css
Normal file
133
paypal_advanced_checkout_in_php/css/style.css
Normal file
@ -0,0 +1,133 @@
|
||||
.container{
|
||||
padding: 20px;
|
||||
}
|
||||
h1{
|
||||
color: #7a7a7a;
|
||||
font-size: 28px;
|
||||
text-transform: uppercase;
|
||||
text-align: center;
|
||||
font-family: math;
|
||||
}
|
||||
.panel {
|
||||
width: 375px;
|
||||
margin: 0 auto;
|
||||
background-color: #fff;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 4px;
|
||||
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
|
||||
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
|
||||
border-color: #ddd;
|
||||
}
|
||||
.panel-heading {
|
||||
padding: 10px 15px;
|
||||
border-bottom: 1px solid transparent;
|
||||
border-top-left-radius: 3px;
|
||||
border-top-right-radius: 3px;
|
||||
}
|
||||
.panel > .panel-heading {
|
||||
color: #333;
|
||||
background-color: #f5f5f5;
|
||||
border-color: #ddd;
|
||||
}
|
||||
.panel-title {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
font-size: 20px;
|
||||
color: #333;
|
||||
font-weight: 600;
|
||||
}
|
||||
.panel-body {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.panel{
|
||||
position: relative;
|
||||
}
|
||||
.overlay{
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: 9999;
|
||||
background-color: rgba(255,255,255,0.8);
|
||||
}
|
||||
.overlay-content {
|
||||
position: absolute;
|
||||
transform: translateY(-50%);
|
||||
-webkit-transform: translateY(-50%);
|
||||
-ms-transform: translateY(-50%);
|
||||
top: 50%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
text-align: center;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#paymentResponse{
|
||||
font-size: 17px;
|
||||
border: 1px dashed;
|
||||
padding: 10px;
|
||||
color: #EA4335;
|
||||
margin-top: 0;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.status{
|
||||
padding: 15px;
|
||||
color: #000;
|
||||
background-color: #f1f1f1;
|
||||
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 2px 10px 0 rgba(0,0,0,0.12);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.status h1{
|
||||
font-size: 1.8em;
|
||||
}
|
||||
.status h4{
|
||||
font-size: 1.3em;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.status p{
|
||||
font-size: 1em;
|
||||
margin-bottom: 0;
|
||||
margin-top: 8px;
|
||||
}
|
||||
.btn-link{
|
||||
display: inline-block;
|
||||
font-weight: 400;
|
||||
text-align: center;
|
||||
white-space: nowrap;
|
||||
vertical-align: middle;
|
||||
-webkit-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
border: 1px solid transparent;
|
||||
padding: .375rem .75rem;
|
||||
font-size: 1rem;
|
||||
line-height: 1.5;
|
||||
border-radius: .25rem;
|
||||
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
|
||||
text-decoration: none;
|
||||
}
|
||||
.btn-link {
|
||||
color: #007bff;
|
||||
background-color: transparent;
|
||||
border-color: #007bff;
|
||||
}
|
||||
.btn-link:hover, .btn-link:active, .btn-link:focus {
|
||||
color: #fff;
|
||||
background-color: #007bff;
|
||||
border-color: #007bff;
|
||||
text-decoration: none;
|
||||
}
|
||||
.success{
|
||||
color: #34A853;
|
||||
}
|
||||
.error{
|
||||
color: #EA4335;
|
||||
}
|
||||
9
paypal_advanced_checkout_in_php/dbConnect.php
Normal file
9
paypal_advanced_checkout_in_php/dbConnect.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// Connect with the database
|
||||
$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
|
||||
|
||||
// Display error if failed to connect
|
||||
if ($db->connect_errno) {
|
||||
printf("sl Connect failed: %s\n", $db->connect_error);
|
||||
exit();
|
||||
}
|
||||
223
paypal_advanced_checkout_in_php/index.php
Normal file
223
paypal_advanced_checkout_in_php/index.php
Normal file
@ -0,0 +1,223 @@
|
||||
<?php
|
||||
// Include the configuration file
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require_once 'config.php';
|
||||
|
||||
include_once 'dbConnect.php';
|
||||
$product_data2 = array(
|
||||
'item_number' =>"test",
|
||||
'item_name' => "test",
|
||||
'price' => "test",
|
||||
'currency' => "cad",
|
||||
);
|
||||
$product_data = array(
|
||||
"intent" => "CAPTURE", array( "purchase_units" => array(
|
||||
array(
|
||||
"reference_id" => "ms1testreference_id",
|
||||
"custom_id" => $product_data2['item_number'],
|
||||
"description" => $product_data2['item_name'],
|
||||
"amount" => array(
|
||||
"currency_code" => $product_data2['currency'],
|
||||
"value" => $product_data2['price']
|
||||
),
|
||||
"shipping" => array(
|
||||
"name" => array(
|
||||
"full_name" => "stephan leith test"),
|
||||
"address" => array(
|
||||
"address_line_1" => "123 Townsend St",
|
||||
"address_line_2" => "Floor 6",
|
||||
"admin_area_2" => "San Francisco",
|
||||
"admin_area_1" => "CA",
|
||||
"postal_code" => "94107",
|
||||
"country_code" => "US"
|
||||
|
||||
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
));
|
||||
print "<pre>";
|
||||
print_r($product_data);
|
||||
print "</pre>";
|
||||
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<title>Integrate PayPal Advanced Checkout with PHP by CodexWorld</title>
|
||||
<meta charset="utf-8">
|
||||
|
||||
<!-- Stylesheet file -->
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
|
||||
<!-- Card field's styles, to be replaced with your own stylesheet -->
|
||||
<link
|
||||
rel="stylesheet"
|
||||
type="text/css"
|
||||
href="https://www.paypalobjects.com/webstatic/en_US/developer/docs/css/cardfields.css"
|
||||
/>
|
||||
<!-- PayPal JavaScript SDK -->
|
||||
<script src="https://www.paypal.com/sdk/js?components=card-fields&client-id=<?php echo PAYPAL_SANDBOX?PAYPAL_SANDBOX_CLIENT_ID:PAYPAL_PROD_CLIENT_ID; ?>"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>PayPal Advanced Checkout Integration</h1>
|
||||
<div class="panel">
|
||||
<div class="overlay hidden"><div class="overlay-content"><img src="css/loading.gif" alt="Processing..."/></div></div>
|
||||
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Charge <?php echo '$'.$itemPrice; ?> with PayPal</h3>
|
||||
|
||||
<!-- Product Info -->
|
||||
<p><b>Item Name:</b> <?php echo $itemName; ?></p>
|
||||
<p><b>Price:</b> <?php echo '$'.$itemPrice.' '.$currency; ?></p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<!-- Display status message -->
|
||||
<div id="paymentResponse" class="hidden"></div>
|
||||
|
||||
<!-- Set up a container element for the button -->
|
||||
<div id="checkout-form">
|
||||
<div id="card-name-field-container"></div>
|
||||
<div id="card-number-field-container"></div>
|
||||
<div id="card-expiry-field-container"></div>
|
||||
<div id="card-cvv-field-container"></div>
|
||||
<button id="card-field-submit-button" type="button">
|
||||
Pay Now
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Create the Card Fields Component and define callbacks
|
||||
const cardField = paypal.CardFields({
|
||||
createOrder: function (data) {
|
||||
setProcessing(true);
|
||||
console.log("createOrder 1");
|
||||
var postData = {request_type: 'create_order', payment_source: data.paymentSource};
|
||||
return fetch("paypal_checkout_init.php", {
|
||||
method: "POST",
|
||||
headers: {'Accept': 'application/json'},
|
||||
body: encodeFormData(postData)
|
||||
})
|
||||
.then((res) => {
|
||||
console.log("createOrder 2");
|
||||
return res.json();
|
||||
})
|
||||
.then((result) => {
|
||||
setProcessing(false);
|
||||
console.log("createOrder 3");
|
||||
if(result.status == 1){
|
||||
console.log("createOrder 4");
|
||||
return result.data.id;
|
||||
}else{
|
||||
console.log("createOrder 5");
|
||||
resultMessage(result.msg);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
},
|
||||
onApprove: function (data) {
|
||||
setProcessing(true);
|
||||
console.log("onApprove 1");
|
||||
const { orderID } = data;
|
||||
var postData = {request_type: 'capture_order', order_id: orderID};
|
||||
return fetch('paypal_checkout_init.php', {
|
||||
method: "POST",
|
||||
headers: {'Accept': 'application/json'},
|
||||
body: encodeFormData(postData)
|
||||
})
|
||||
.then((res) => {
|
||||
console.log("onApprove 2");
|
||||
return res.json();
|
||||
})
|
||||
.then((result) => {
|
||||
// Redirect to success page
|
||||
if(result.status == 1){
|
||||
console.log("onApprove 3");
|
||||
window.location.href = "payment-status.php?checkout_ref_id="+result.ref_id;
|
||||
}else{
|
||||
console.log("onApprove 4");
|
||||
resultMessage(result.msg);
|
||||
}
|
||||
setProcessing(false);
|
||||
});
|
||||
},
|
||||
onError: function (error) {
|
||||
// Do something with the error from the SDK
|
||||
},
|
||||
});
|
||||
|
||||
// Render each field after checking for eligibility
|
||||
if (cardField.isEligible()) {
|
||||
const nameField = cardField.NameField();
|
||||
nameField.render("#card-name-field-container");
|
||||
|
||||
const numberField = cardField.NumberField();
|
||||
numberField.render("#card-number-field-container");
|
||||
|
||||
const cvvField = cardField.CVVField();
|
||||
cvvField.render("#card-cvv-field-container");
|
||||
|
||||
const expiryField = cardField.ExpiryField();
|
||||
expiryField.render("#card-expiry-field-container");
|
||||
|
||||
// Add click listener to submit button and call the submit function on the CardField component
|
||||
document
|
||||
.getElementById("card-field-submit-button")
|
||||
.addEventListener("click", () => {
|
||||
cardField.submit().then(() => {
|
||||
alert(JSON.stringify(cardField));
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("erreur avant start");
|
||||
resultMessage(`Sorry, your transaction could not be processed... >>> ${error}`);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Hides card fields if the merchant isn't eligible
|
||||
document.querySelector("#checkout-form").style = "display: none";
|
||||
}
|
||||
|
||||
const encodeFormData = (data) => {
|
||||
var form_data = new FormData();
|
||||
|
||||
for ( var key in data ) {
|
||||
form_data.append(key, data[key]);
|
||||
}
|
||||
return form_data;
|
||||
}
|
||||
|
||||
// Show a loader on payment form processing
|
||||
const setProcessing = (isProcessing) => {
|
||||
if (isProcessing) {
|
||||
document.querySelector(".overlay").classList.remove("hidden");
|
||||
} else {
|
||||
document.querySelector(".overlay").classList.add("hidden");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Display status message
|
||||
const resultMessage = (msg_txt) => {
|
||||
const messageContainer = document.querySelector("#paymentResponse");
|
||||
|
||||
messageContainer.classList.remove("hidden");
|
||||
messageContainer.textContent = msg_txt;
|
||||
|
||||
setTimeout(function () {
|
||||
messageContainer.classList.add("hidden");
|
||||
messageContainer.textContent = "";
|
||||
}, 5000);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
80
paypal_advanced_checkout_in_php/payment-status.php
Normal file
80
paypal_advanced_checkout_in_php/payment-status.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// Include the configuration file
|
||||
require_once 'config.php';
|
||||
|
||||
// Include the database connection file
|
||||
require_once 'dbConnect.php';
|
||||
|
||||
$payment_ref_id = $statusMsg = '';
|
||||
$status = 'error';
|
||||
|
||||
// Check whether the payment ID is not empty
|
||||
if(!empty($_GET['checkout_ref_id'])){
|
||||
$payment_txn_id = base64_decode($_GET['checkout_ref_id']);
|
||||
|
||||
// Fetch transaction data from the database
|
||||
$sqlQ = "SELECT id,order_id,transaction_id,paid_amount,paid_amount_currency,payment_source,payment_source_card_name,payment_source_card_last_digits,payment_source_card_expiry,payment_source_card_brand,payment_source_card_type,payment_status,created FROM transactions WHERE transaction_id = ?";
|
||||
$stmt = $db->prepare($sqlQ);
|
||||
$stmt->bind_param("s", $payment_txn_id);
|
||||
$stmt->execute();
|
||||
$stmt->store_result();
|
||||
|
||||
if($stmt->num_rows > 0){
|
||||
// Get transaction details
|
||||
$stmt->bind_result($payment_ref_id, $order_id, $transaction_id, $paid_amount, $paid_amount_currency, $payment_source, $payment_source_card_name, $payment_source_card_last_digits, $payment_source_card_expiry, $payment_source_card_brand, $payment_source_card_type, $payment_status, $created);
|
||||
$stmt->fetch();
|
||||
|
||||
$status = 'success';
|
||||
$statusMsg = 'Your Payment has been Successful!';
|
||||
}else{
|
||||
$statusMsg = "Transaction has been failed!";
|
||||
}
|
||||
}else{
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<title>PayPal Advanced Checkout Payment Status - CodexWorld</title>
|
||||
<meta charset="utf-8">
|
||||
|
||||
<!-- Stylesheet file -->
|
||||
<link href="css/style.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="status">
|
||||
<?php if(!empty($payment_ref_id)){ ?>
|
||||
<h1 class="<?php echo $status; ?>"><?php echo $statusMsg; ?></h1>
|
||||
|
||||
<h4>Payment Information</h4>
|
||||
<p><b>Reference Number:</b> <?php echo $payment_ref_id; ?></p>
|
||||
<p><b>Order ID:</b> <?php echo $order_id; ?></p>
|
||||
<p><b>Transaction ID:</b> <?php echo $transaction_id; ?></p>
|
||||
<p><b>Paid Amount:</b> <?php echo $paid_amount.' '.$paid_amount_currency; ?></p>
|
||||
<p><b>Payment Status:</b> <?php echo $payment_status; ?></p>
|
||||
<p><b>Date:</b> <?php echo $created; ?></p>
|
||||
|
||||
<h4>Payment Source</h4>
|
||||
<p><b>Method:</b> <?php echo strtoupper($payment_source); ?></p>
|
||||
<p><b>Card Type:</b> <?php echo $payment_source_card_type; ?></p>
|
||||
<p><b>Card Brand:</b> <?php echo $payment_source_card_brand; ?></p>
|
||||
<p><b>Card Number:</b> <?php echo 'XXXX XXXX XXXX '.$payment_source_card_last_digits; ?></p>
|
||||
<p><b>Card Expiry:</b> <?php echo $payment_source_card_expiry; ?></p>
|
||||
<p><b>Card Holder Name:</b> <?php echo $payment_source_card_name; ?></p>
|
||||
|
||||
<h4>Product Information</h4>
|
||||
<p><b>Name:</b> <?php echo $itemName; ?></p>
|
||||
<p><b>Price:</b> <?php echo $itemPrice.' '.$currency; ?></p>
|
||||
<?php }else{ ?>
|
||||
<h1 class="error">Your Payment has been failed!</h1>
|
||||
<p class="error"><?php echo $statusMsg; ?></p>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<a href="index.php" class="btn-link">Back to Payment Page</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
146
paypal_advanced_checkout_in_php/paypal_checkout_init.php
Normal file
146
paypal_advanced_checkout_in_php/paypal_checkout_init.php
Normal file
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
// Include the configuration file
|
||||
require_once 'config.php';
|
||||
|
||||
// Include the database connection file
|
||||
include_once 'dbConnect.php';
|
||||
|
||||
// Include the PayPal API library
|
||||
require_once 'PaypalCheckout.class.php';
|
||||
$paypal = new PaypalCheckout;
|
||||
|
||||
$response = array('status' => 0, 'msg' => 'Request Failed!');
|
||||
$api_error = '';
|
||||
if(!empty($_POST['request_type']) && $_POST['request_type'] == 'create_order'){
|
||||
$payment_source = $_POST['payment_source'];
|
||||
|
||||
$product_data2 = array(
|
||||
'item_number' => $itemNumber,
|
||||
'item_name' => $itemName,
|
||||
'price' => $itemPrice,
|
||||
'currency' => $currency,
|
||||
);
|
||||
$product_data = array(
|
||||
"intent" => "CAPTURE",
|
||||
"purchase_units" => array(
|
||||
array(
|
||||
"custom_id" => $product_data2['item_number'],
|
||||
"description" => $product_data2['item_name'],
|
||||
"amount" => array(
|
||||
"currency_code" => $product_data2['currency'],
|
||||
"value" => $product_data2['price']
|
||||
),
|
||||
|
||||
"shipping" => array(
|
||||
"name" => array(
|
||||
"full_name" => "stephan leith test"),
|
||||
"address" => array(
|
||||
"address_line_1" => "123 Townsend St",
|
||||
"address_line_2" => "Floor 6",
|
||||
"admin_area_2" => "San Francisco",
|
||||
"admin_area_1" => "CA",
|
||||
"postal_code" => "94107",
|
||||
"country_code" => "US"
|
||||
|
||||
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
);
|
||||
// Create order with PayPal Orders API
|
||||
try {
|
||||
$order = $paypal->createOrder($product_data, $payment_source);
|
||||
} catch(Exception $e) {
|
||||
$api_error = $e->getMessage();
|
||||
}
|
||||
|
||||
if(!empty($order)){
|
||||
$response = array(
|
||||
'status' => 1,
|
||||
'data' => $order
|
||||
);
|
||||
}else{
|
||||
$response['msg'] = $api_error;
|
||||
}
|
||||
}elseif(!empty($_POST['request_type']) && $_POST['request_type'] == 'capture_order'){
|
||||
$order_id = $_POST['order_id'];
|
||||
|
||||
// Create order with PayPal Orders API
|
||||
try {
|
||||
$order = $paypal->captureOrder($order_id);
|
||||
} catch(Exception $e) {
|
||||
$api_error = $e->getMessage();
|
||||
}
|
||||
|
||||
if(!empty($order)){
|
||||
$order_id = $order['id'];
|
||||
$order_status = $order['status'];
|
||||
|
||||
$payment_source = $payment_source_card_name = $payment_source_card_last_digits = $payment_source_card_expiry = $payment_source_card_brand = $payment_source_card_type = '';
|
||||
if(!empty($order['payment_source'])){
|
||||
foreach($order['payment_source'] as $key=>$value){
|
||||
$payment_source = $key;
|
||||
if($payment_source == 'card'){
|
||||
$payment_source_card_name = $value['name'];
|
||||
$payment_source_card_last_digits = $value['last_digits'];
|
||||
$payment_source_card_expiry = $value['expiry'];
|
||||
$payment_source_card_brand = $value['brand'];
|
||||
$payment_source_card_type = $value['type'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($order['purchase_units'][0])){
|
||||
$purchase_unit = $order['purchase_units'][0];
|
||||
if(!empty($purchase_unit['payments'])){
|
||||
$payments = $purchase_unit['payments'];
|
||||
if(!empty($payments['captures'])){
|
||||
$captures = $payments['captures'];
|
||||
if(!empty($captures[0])){
|
||||
$transaction_id = $captures[0]['id'];
|
||||
$payment_status = $captures[0]['status'];
|
||||
$custom_id = $captures[0]['custom_id'];
|
||||
$amount_value = $captures[0]['amount']['value'];
|
||||
$currency_code = $captures[0]['amount']['currency_code'];
|
||||
$create_time = date("Y-m-d H:i:s", strtotime($captures[0]['create_time']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($order_id) && $order_status == 'COMPLETED'){
|
||||
// Check if any transaction data is exists already with the same TXN ID
|
||||
$sqlQ = "SELECT id FROM transactions WHERE transaction_id = ?";
|
||||
$stmt = $db->prepare($sqlQ);
|
||||
$stmt->bind_param("s", $transaction_id);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($row_id);
|
||||
$stmt->fetch();
|
||||
|
||||
$payment_id = 0;
|
||||
if(!empty($row_id)){
|
||||
$payment_id = $row_id;
|
||||
}else{
|
||||
// Insert transaction data into the database
|
||||
$sqlQ = "INSERT INTO transactions (item_number,item_name,item_price,item_price_currency,order_id,transaction_id,paid_amount,paid_amount_currency,payment_source,payment_source_card_name,payment_source_card_last_digits,payment_source_card_expiry,payment_source_card_brand,payment_source_card_type,payment_status,created,modified) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
|
||||
$stmt = $db->prepare($sqlQ);
|
||||
$stmt->bind_param("ssdsssdsssssssss", $custom_id, $itemName, $itemPrice, $currency, $order_id, $transaction_id, $amount_value, $currency_code, $payment_source, $payment_source_card_name, $payment_source_card_last_digits, $payment_source_card_expiry, $payment_source_card_brand, $payment_source_card_type, $payment_status, $create_time);
|
||||
$insert = $stmt->execute();
|
||||
|
||||
if($insert){
|
||||
$payment_id = $stmt->insert_id;
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($payment_id)){
|
||||
$ref_id_enc = base64_encode($transaction_id);
|
||||
$response = array('status' => 1, 'msg' => 'Transaction completed!', 'ref_id' => $ref_id_enc);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$response['msg'] = $api_error;
|
||||
}
|
||||
}
|
||||
echo json_encode($response);
|
||||
Reference in New Issue
Block a user