diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 227ad7b..77c24cf 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -20,7 +20,7 @@ if (!defined('ABSPATH')) { /** * plugin dir root */ -include_once( plugin_dir_path(__FILE__) . '/utils/plugin_tools.php'); +include_once( plugin_dir_path(__FILE__) . '/utils/plgntls_class.php'); PLGNTLS_class::set_root_dir( plugin_dir_path(__FILE__), plugin_dir_url(__FILE__) ); /** @@ -369,7 +369,7 @@ function fipfcard_ajax_handler() wp_send_json_success( array ( 'It works', - "data_received" => $_POST['postid'], + "data_received" => $_POST, ), 200 ); diff --git a/plugins/fipfcard_plugin/js/menu/example_menu.js b/plugins/fipfcard_plugin/js/menu/example_menu.js index 8c9dc06..237e41b 100644 --- a/plugins/fipfcard_plugin/js/menu/example_menu.js +++ b/plugins/fipfcard_plugin/js/menu/example_menu.js @@ -2,13 +2,16 @@ const inputElement = document.getElementById('mytext'); const sendButton = document.getElementById('mybutton'); sendButton.addEventListener('click', () => { - const inputValue = inputElement.value; + let inputValue = inputElement.value; + inputValue = { + key1: 'value1', + key2: 'value2' + }; + inputValue = JSON.stringify(inputValue); console.log("inputValue:"); console.log(inputValue); - //PLGNTLS_ajax('get_data', inputValue) - PLGNTLS_fetch('get_data', { - body: {inputValue}, - }) + PLGNTLS_ajax('get_data', inputValue) + //PLGNTLS_fetch('get_data', {body: {inputValue}}) .then((response) => response.json()) .then((data) => { console.log("dataaa: "); diff --git a/plugins/fipfcard_plugin/js/paypal/create_order.js b/plugins/fipfcard_plugin/js/paypal/create_order.js index b839999..e4b5299 100644 --- a/plugins/fipfcard_plugin/js/paypal/create_order.js +++ b/plugins/fipfcard_plugin/js/paypal/create_order.js @@ -1,18 +1,20 @@ -import { resultMessage } from './result_message.js'; +//import { resultMessage } from './result_message.js'; /** * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend */ -export async function createOrder() { +//export async function createOrder() { +async function createOrder() { try { - const fetch_create_url = PLGNTLS_data.fetch_url + "/fipf_plugin/api/v1/orders"; - console.log("fetch_create_url:", fetch_create_url); - const response = await fetch(fetch_create_url, { + //const fetch_create_url = PLGNTLS_data.fetch_url + "/fipf_plugin/api/v1/orders"; + //console.log("fetch_create_url:", fetch_create_url); + //const response = await fetch(fetch_create_url, { + const response = await PLGNTLS_fetch('/fipf_plugin/api/v1/orders', { method: "POST", headers: { "Content-Type": "application/json", - "X-WP-Nonce": PLGNTLS_data.rest_nonce, + //"X-WP-Nonce": PLGNTLS_data.rest_nonce, }, // use the "body" param to optionally pass additional order information // like product ids and quantities diff --git a/plugins/fipfcard_plugin/js/paypal/on_approve.js b/plugins/fipfcard_plugin/js/paypal/on_approve.js index 399c867..5a90b23 100644 --- a/plugins/fipfcard_plugin/js/paypal/on_approve.js +++ b/plugins/fipfcard_plugin/js/paypal/on_approve.js @@ -1,18 +1,20 @@ -import { resultMessage } from './result_message.js'; +//import { resultMessage } from './result_message.js'; /** * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend */ -export async function onApprove(data, actions) { +//export async function onApprove(data, actions) { +async function onApprove(data, actions) { try { const fetch_approve_url = PLGNTLS_data.fetch_url + "/fipf_plugin/api/v1/orders/" + data.orderID + "/capture"; console.log("fetch_approve_url:", fetch_approve_url); - const response = await fetch(fetch_approve_url, { + //const response = await fetch(fetch_approve_url, { + const response = await PLGNTLS_fetch('/fipf_plugin/api/v1/orders/' + data.orderID + '/capture', { method: "POST", headers: { "Content-Type": "application/json", - "X-WP-Nonce": PLGNTLS_data.rest_nonce, + //"X-WP-Nonce": PLGNTLS_data.rest_nonce, }, }); @@ -39,14 +41,14 @@ export async function onApprove(data, actions) { const transaction = orderData?.purchase_units?.[0]?.payments?.captures?.[0] || orderData?.purchase_units?.[0]?.payments?.authorizations?.[0]; - resultMessage( - `Transaction ${transaction.status}: ${transaction.id}

See console for all available details`, - ); + // to show a message on page + //resultMessage(`Transaction ${transaction.status}: ${transaction.id}

See console for all available details`,); console.log( "Capture result", orderData, JSON.stringify(orderData, null, 2), ); + actions.redirect('https://local_fipfcard_plugin.com/'); } } catch (error) { console.error(error); diff --git a/plugins/fipfcard_plugin/js/paypal/paypal.js b/plugins/fipfcard_plugin/js/paypal/paypal.js index ef59cc9..509d80e 100644 --- a/plugins/fipfcard_plugin/js/paypal/paypal.js +++ b/plugins/fipfcard_plugin/js/paypal/paypal.js @@ -1,5 +1,5 @@ -import { createOrder } from './create_order.js'; -import { onApprove } from './on_approve.js'; +//import { createOrder } from './create_order.js'; +//import { onApprove } from './on_approve.js'; window.paypal.Buttons({ diff --git a/plugins/fipfcard_plugin/js/paypal/result_message.js b/plugins/fipfcard_plugin/js/paypal/result_message.js index 5bac641..e7afd0d 100644 --- a/plugins/fipfcard_plugin/js/paypal/result_message.js +++ b/plugins/fipfcard_plugin/js/paypal/result_message.js @@ -1,6 +1,7 @@ // Example function to show a result to the user. Your site's UI library can be used instead. -export function resultMessage(message) { +//export function resultMessage(message) { +function resultMessage(message) { const container = document.querySelector("#result-message"); container.innerHTML = message; } diff --git a/plugins/fipfcard_plugin/php/paypal/paypal.php b/plugins/fipfcard_plugin/php/paypal/paypal.php index 7f2e8fe..dfc2b6b 100644 --- a/plugins/fipfcard_plugin/php/paypal/paypal.php +++ b/plugins/fipfcard_plugin/php/paypal/paypal.php @@ -56,6 +56,9 @@ function fipf_paypal_shortcode_content() $added_to_front = $fipfcard_paypal->add_to_front( array( $pp_sdk_src, + 'js/paypal/result_message.js', + 'js/paypal/create_order.js', + 'js/paypal/on_approve.js', "js/paypal/paypal.js", "html/paypal/paypal.html", ), diff --git a/plugins/fipfcard_plugin/utils/plugin_tools.php b/plugins/fipfcard_plugin/utils/plgntls_class.php similarity index 91% rename from plugins/fipfcard_plugin/utils/plugin_tools.php rename to plugins/fipfcard_plugin/utils/plgntls_class.php index 497cc6a..6f147af 100644 --- a/plugins/fipfcard_plugin/utils/plugin_tools.php +++ b/plugins/fipfcard_plugin/utils/plgntls_class.php @@ -14,7 +14,6 @@ class PLGNTLS_class private static $_root_path; private static $_root_url; - private static $_ajax_already_there; private $_first_script; private $_prefix; private $_js_dependencies; @@ -23,8 +22,6 @@ class PLGNTLS_class /** */ public function __construct() { - if (! isset( self::$_ajax_already_there )) - self::$_ajax_already_there = false; $this->_prefix = "PLGNTLS"; $this->_first_script = null; $this->_js_dependencies = array(); @@ -56,20 +53,16 @@ class PLGNTLS_class $vars = array(); if (!is_null($scripts_arr)) { - // add ajax file at beginning of files list - array_unshift($scripts_arr, "utils/plugin_ajax.js"); - $ajax_nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce')); + // add fetch script at beginning of scripts list + array_unshift($scripts_arr, "utils/plgntls_fetch.js"); // for the custom endpoints in rest api to work // they need to have a nonce named 'wp_rest' // see : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ - $rest_nonce = array("rest_nonce" => wp_create_nonce('wp_rest')); - $ajax_url = array("ajax_url" => admin_url('admin-ajax.php')); - $vars = array_merge($vars, $ajax_nonce); - $vars = array_merge($vars, $rest_nonce); - $vars = array_merge($vars, $ajax_url); + $fetch_nonce = array("fetch_nonce" => wp_create_nonce('wp_rest')); + $fetch_url = array("fetch_url" => get_site_url() . "/wp-json"); + $vars = array_merge($vars, $fetch_nonce); + $vars = array_merge($vars, $fetch_url); } - $fetch_url = array("fetch_url" => get_site_url() . "/wp-json"); - $vars = array_merge($vars, $fetch_url); $scripts = array(); foreach($scripts_arr as $key => $script_name) { @@ -203,10 +196,10 @@ class PLGNTLS_class } /* * uncomment to print all enqueued scripts, can be usefull + */ global $wp_scripts; error_log("wp_scripts->queue:"); error_log(json_encode($wp_scripts->queue)); - */ } private function check_dependencies(&$script, $previous_basename) diff --git a/plugins/fipfcard_plugin/utils/plgntls_fetch.js b/plugins/fipfcard_plugin/utils/plgntls_fetch.js new file mode 100644 index 0000000..c641c18 --- /dev/null +++ b/plugins/fipfcard_plugin/utils/plgntls_fetch.js @@ -0,0 +1,12 @@ + +function PLGNTLS_fetch(url, options = {}) { + console.log("inside PLGNTLS_fetch"); + url = PLGNTLS_data.fetch_url + url; + + options.headers = options.headers || {}; + options.headers['X-WP-Nonce'] = PLGNTLS_data.fetch_nonce, + console.log("options:", options); + + return fetch(url, options); +} + diff --git a/plugins/fipfcard_plugin/utils/plugin_ajax.js b/plugins/fipfcard_plugin/utils/plugin_ajax.js index 131c94f..e6a272f 100644 --- a/plugins/fipfcard_plugin/utils/plugin_ajax.js +++ b/plugins/fipfcard_plugin/utils/plugin_ajax.js @@ -8,20 +8,25 @@ // OLD VERSION console.log("PLGNTLS_data"); console.log(PLGNTLS_data); +*/ function PLGNTLS_ajax(action, data_obj) { const data = new FormData(); data.append("action", action); data.append("_ajax_nonce", PLGNTLS_data.ajax_nonce); - for (const key in data_obj) - data.append(key, data_obj[key]); +// for (const key in data_obj) +// { +// data.append(key, data_obj[key]); +// } +// console.log("data:", data); + data.append(data_obj); return fetch(PLGNTLS_data.ajax_url, { method: "POST", credentials: "same-origin", - body: data, + hedears: {"Content-Type": "application/json"}, + body: JSON.stringify(data), }); } -*/ // https://stackoverflow.com/q/52657814/9497573 function is_plain_object(obj) @@ -35,6 +40,7 @@ function is_plain_object(obj) return true; } +/* function PLGNTLS_fetch(src = null, options = null) { if (src === null) @@ -108,3 +114,4 @@ function PLGNTLS_fetch(src = null, options = null) return fetch(PLGNTLS_data.ajax_url, options); } +*/ diff --git a/private b/private index 20b675a..4d4c187 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit 20b675aa3862c92b691b98ac64aa3b9048e7faae +Subproject commit 4d4c187fe50ec1e78199fcb18ba66ef4194a9ecc