wip route api_order
This commit is contained in:
@@ -68,7 +68,7 @@ paypal.Buttons({
|
||||
window.paypal.Buttons({
|
||||
//createOrder: function(data, actions) {
|
||||
createOrder: function() {
|
||||
PLGNTLS_fetch("paypal_orders", {
|
||||
PLGNTLS_fetch("paypal_api_orders", {
|
||||
//method: "POST",
|
||||
//headers: {"Content-Type": "application/json"},
|
||||
//body: {
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
<?php
|
||||
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php');
|
||||
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function fipfcard_paypal_order()
|
||||
function fipfcard_paypal_orders()
|
||||
{
|
||||
// json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573
|
||||
$data_received = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) );
|
||||
@@ -30,30 +27,85 @@ function fipfcard_paypal_order()
|
||||
200
|
||||
);
|
||||
}
|
||||
add_action( 'wp_ajax_paypal_orders', 'fipfcard_paypal_order' );
|
||||
add_action( 'wp_ajax_paypal_api_orders', 'fipfcard_paypal_orders' );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create an order to start the transaction.
|
||||
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
|
||||
*/
|
||||
function create_order( $cart )
|
||||
{
|
||||
// use the cart information passed from the front-end to calculate the purchase unit details
|
||||
|
||||
$access_token = generate_access_token();
|
||||
error_log("access_token:");
|
||||
error_log($access_token);
|
||||
|
||||
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders';
|
||||
$payload = array(
|
||||
'intent' => "CAPTURE",
|
||||
'purchase_units' => array(
|
||||
array(
|
||||
'amount' => array(
|
||||
'currency_code' => "USD",
|
||||
'value' => "100.00",
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Initialize cURL session
|
||||
$ch = curl_init();
|
||||
// Set cURL options
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Bearer ' . $access_token,
|
||||
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
|
||||
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
|
||||
));
|
||||
|
||||
// Execute cURL session and get the response
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if ($response === false)
|
||||
throw new Exception('cURL error: ' . curl_error($ch));
|
||||
// Close cURL session
|
||||
curl_close($ch);
|
||||
|
||||
// in utils
|
||||
return handle_response($response);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Generate an OAuth 2.0 access token for authenticating with PayPal REST APIs.
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
* @see https://developer.paypal.com/api/rest/authentication/
|
||||
*/
|
||||
function generate_access_token( )
|
||||
function generate_access_token()
|
||||
{
|
||||
$base = "https://api-m.sandbox.paypal.com";
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
if ( !PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET ) {
|
||||
throw new Error( "MISSING_API_CREDENTIALS" );
|
||||
throw new Exception( "MISSING_API_CREDENTIALS" );
|
||||
}
|
||||
$credentials = PAYPAL_CLIENT_ID . ":" . PAYPAL_CLIENT_SECRET;
|
||||
$auth = base64_encode($credentials);
|
||||
|
||||
$url = $base . '/v1/oauth2/token';
|
||||
$url = PAYPAL_API_BASE_URL . '/v1/oauth2/token';
|
||||
$body = http_build_query(array('grant_type' => 'client_credentials'));
|
||||
// Initialize curl
|
||||
$ch = curl_init();
|
||||
@@ -69,7 +121,7 @@ function generate_access_token( )
|
||||
$data_json = curl_exec($ch);
|
||||
|
||||
if ( $data_json === false)
|
||||
throw new Error('cURL error: ' . curl_error($ch));
|
||||
throw new Exception('cURL error: ' . curl_error($ch));
|
||||
// Close curl
|
||||
curl_close($ch);
|
||||
|
||||
@@ -77,90 +129,12 @@ function generate_access_token( )
|
||||
|
||||
return $data->access_token;
|
||||
}
|
||||
catch (Throwable $error) {
|
||||
catch (Exception $error)
|
||||
{
|
||||
error_log("Failed to generate Access Token:");
|
||||
error_log($error->getMessage());
|
||||
}
|
||||
};
|
||||
/*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create an order to start the transaction.
|
||||
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
|
||||
*/
|
||||
|
||||
function create_order( $cart )
|
||||
{
|
||||
|
||||
// use the cart information passed from the front-end to calculate the purchase unit details
|
||||
|
||||
$access_token = generate_access_token();
|
||||
error_log("access_token:");
|
||||
error_log($access_token);
|
||||
|
||||
/*
|
||||
|
||||
|
||||
const url = `${base}/v2/checkout/orders`;
|
||||
|
||||
const payload = {
|
||||
|
||||
intent: "CAPTURE",
|
||||
|
||||
purchase_units: [
|
||||
|
||||
{
|
||||
|
||||
amount: {
|
||||
|
||||
currency_code: "USD",
|
||||
|
||||
value: "100.00",
|
||||
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
],
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
const response = await fetch(url, {
|
||||
|
||||
headers: {
|
||||
|
||||
"Content-Type": "application/json",
|
||||
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
|
||||
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
|
||||
|
||||
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
|
||||
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
|
||||
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
|
||||
|
||||
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
|
||||
|
||||
},
|
||||
|
||||
method: "POST",
|
||||
|
||||
body: JSON.stringify(payload),
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
return handleResponse(response);
|
||||
|
||||
*/
|
||||
};
|
||||
|
||||
?>
|
||||
|
||||
44
plugins/fipfcard_plugin/php/paypal/route_api_utils.php
Normal file
44
plugins/fipfcard_plugin/php/paypal/route_api_utils.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
function handle_response($response) {
|
||||
try
|
||||
{
|
||||
// Decode JSON response
|
||||
$json_response = json_decode($response);
|
||||
return array(
|
||||
'json_response' => $json_response,
|
||||
'http_status_code' => http_response_code()
|
||||
);
|
||||
}
|
||||
catch (Exception $err)
|
||||
{
|
||||
// Get error message from response
|
||||
$error_message = $response->text();
|
||||
throw new Exception($error_message);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
async function handleResponse(response) {
|
||||
try {
|
||||
const jsonResponse = await response.json();
|
||||
return {
|
||||
jsonResponse,
|
||||
httpStatusCode: response.status,
|
||||
};
|
||||
} catch (err) {
|
||||
const errorMessage = await response.text();
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
?>
|
||||
@@ -24,15 +24,25 @@ $fipfcard_ajax_file = "utils/ajax.js";
|
||||
|
||||
|
||||
/**
|
||||
* paypal credentials
|
||||
*
|
||||
* LIVE :
|
||||
const PAYPAL_CLIENT_ID = "Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO";
|
||||
const PAYPAL_CLIENT_SECRET = "EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra";
|
||||
*
|
||||
* const PAYPAL_CLIENT_ID = "Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO";
|
||||
* const PAYPAL_CLIENT_SECRET = "EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra";
|
||||
*
|
||||
* SANBOX :
|
||||
const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl";
|
||||
const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-";
|
||||
*
|
||||
* const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl";
|
||||
* const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-";
|
||||
*
|
||||
*/
|
||||
const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl";
|
||||
const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-";
|
||||
|
||||
/**
|
||||
* paypal api base url
|
||||
*/
|
||||
const PAYPAL_API_BASE_URL = "https://api-m.sandbox.paypal.com";
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user