- struggled to make ajax works, but now is ok
- starting to make serverside works
This commit is contained in:
168
plugins/fipfcard_plugin/php/paypal/paypal.php
Normal file
168
plugins/fipfcard_plugin/php/paypal/paypal.php
Normal file
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function fipfcard_paypal_order()
|
||||
{
|
||||
// json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573
|
||||
$data_received = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) );
|
||||
// error_log( "data_received" );
|
||||
// error_log( json_encode( $data_received ) );
|
||||
|
||||
// use the cart information passed from the front-end to calculate the order amount detals
|
||||
$cart = $data_received->cart;
|
||||
error_log( "cart" );
|
||||
error_log( json_encode( $cart ) );
|
||||
create_order($cart);
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'from paypal_order',
|
||||
"data_received" => $data_received,
|
||||
),
|
||||
200
|
||||
);
|
||||
}
|
||||
add_action( 'wp_ajax_paypal_orders', 'fipfcard_paypal_order' );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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( )
|
||||
{
|
||||
$base = "https://api-m.sandbox.paypal.com";
|
||||
|
||||
try {
|
||||
if ( !PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET ) {
|
||||
throw new Error( "MISSING_API_CREDENTIALS" );
|
||||
}
|
||||
$credentials = PAYPAL_CLIENT_ID . ":" . PAYPAL_CLIENT_SECRET;
|
||||
$auth = base64_encode($credentials);
|
||||
|
||||
$url = $base . '/v1/oauth2/token';
|
||||
$body = http_build_query(array('grant_type' => 'client_credentials'));
|
||||
// Initialize curl
|
||||
$ch = curl_init();
|
||||
// Set curl options
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||||
'Authorization: Basic ' . $auth,
|
||||
));
|
||||
// Execute curl and get the response
|
||||
$data_json = curl_exec($ch);
|
||||
|
||||
if ( $data_json === false)
|
||||
throw new Error('cURL error: ' . curl_error($ch));
|
||||
// Close curl
|
||||
curl_close($ch);
|
||||
|
||||
$data = json_decode($data_json);
|
||||
|
||||
return $data->access_token;
|
||||
}
|
||||
catch (Throwable $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);
|
||||
|
||||
*/
|
||||
};
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -4,12 +4,15 @@
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
https://stackify.com/how-to-log-to-console-in-php/
|
||||
*/
|
||||
function console_log($output) {
|
||||
function console_log(...$outputs) {
|
||||
if (FIPFCARD_CONSOLE_OFF)
|
||||
return;
|
||||
$json_output = json_encode($output, JSON_HEX_TAG);
|
||||
$js_code = '<script>console.log(' . $json_output . ');</script>';
|
||||
echo $js_code;
|
||||
foreach($outputs as $output)
|
||||
{
|
||||
$json_output = json_encode($output, JSON_HEX_TAG);
|
||||
$js_code = '<script>console.log(' . $json_output . ');</script>';
|
||||
echo $js_code;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,12 +11,28 @@ const CONSOLE_OFF = true;
|
||||
*/
|
||||
const FIPFCARD_CONSOLE_OFF = false;
|
||||
|
||||
/* a variable that will contain the name of the first script enqueued
|
||||
*/
|
||||
/**
|
||||
* a variable that will contain the name of the first script enqueued
|
||||
*/
|
||||
$fipfcard_first_script = null;
|
||||
|
||||
/* path to ajax.js file, from root of js dir
|
||||
*/
|
||||
|
||||
/**
|
||||
* path to ajax.js file, from root of js dir
|
||||
*/
|
||||
$fipfcard_ajax_file = "utils/ajax.js";
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* LIVE :
|
||||
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-";
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user