better file organisation

This commit is contained in:
asus
2024-02-23 19:49:56 +01:00
parent b7685cbbc1
commit bebb346ff9
4 changed files with 295 additions and 274 deletions

View File

@@ -1,168 +1,60 @@
<?php
/**
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
*/
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_orders.php');
/**
*
*/
function fipfcard_paypal_order()
/*
function check_paypal_request()
{
// 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 ) );
error_log("----");
if (is_page('test_paypal_payment'))
error_log("on test_paypal_payment");
else if (is_page('test_paypal_ok'))
error_log("on test_paypal_ok");
else if (is_page('test_paypal_infos'))
error_log("on test_paypal_infos");
else
return;
error_log("_GET");
error_log(json_encode($_GET));
error_log("_POST");
error_log(json_encode($_POST));
// error_log("_COOKIE");
// error_log(json_encode($_COOKIE));
}
add_action('template_redirect', 'check_paypal_request');
*/
// 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(
function paypal_shortcode_content()
{
$fipfcard_paypal = new PLGNTLS_class();
/*
<input type="hidden" name="custom" value="5678" />
*/
$pp_sdk_currency = "EUR";
$pp_sdk_debug = "true";
$pp_sdk_base_url="https://sandbox.paypal.com";
$pp_sdk_base_url="https://www.paypal.com";
// $pp_sdk_client_token="abc123xyz==";
$pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID . "&currency=$pp_sdk_currency&debug=$pp_sdk_debug";
$pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID ;
// $pp_sdk_attributes="src='$pp_sdk_src' data-client-token='$pp_sdk_client_token'";
// $pp_sdk_attributes="src='$pp_sdk_src'";
// $pp_sdk_html_script="<script $pp_sdk_attributes></script>";
return $fipfcard_paypal->add_to_front(
array(
'from paypal_order',
"data_received" => $data_received,
$pp_sdk_src,
"js/paypal/paypal.js",
"html/paypal/paypal.html",
),
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);
*/
};
?>

View File

@@ -0,0 +1,166 @@
<?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);
*/
};
?>

View File

@@ -0,0 +1,67 @@
<?php
function fipfcard_test_class_tools()
{
$fipfcard_tools = new PLGNTLS_class();
$myvar_1 = "I am one";
$myvar_2 = "I am two";
$names = ["hugo", "camille"];
$ages = ["13", "34", "56"];
$data = wp_get_current_user();
$data = $data->data;
$data = $data->user_email;
//error_log("data->data");
//error_log($data);
//delete_post_meta(get_the_ID(), "_data_user_email");
//delete_post_meta(get_the_ID(), "_data");
//add_post_meta(get_the_ID(), "data_user_email", $data);
$post_metadata = get_metadata( 'post', get_the_ID() );
$post_meta = get_post_meta( get_the_ID() );
$user_metadata = get_metadata( 'user', get_current_user_id() );
$user_meta = get_user_meta( get_current_user_id() );
$acf_get_fields = get_fields( get_the_ID() );
$user_data = get_userdata( get_current_user_id() );
$current_user = wp_get_current_user();
$user_email = get_field('user_email', 'MarieM');
return $fipfcard_tools->add_to_front
(
array
(
"css/example_style.css",
"js/example_init.js",
"js/example_script2.js",
"js/example_script3.js",
"html/example_index.html",
"html/example_index2.html",
),
compact
(
"myvar_1",
"myvar_2",
"post_metadata",
"post_meta",
"user_metadata",
"user_meta",
"acf_get_fields",
"user_data",
"current_user",
"user_email",
"names",
"ages",
)
);
}
?>