From 38260912cdd02f310618a412cd73c1149c14de4d Mon Sep 17 00:00:00 2001 From: asus Date: Fri, 23 Feb 2024 23:43:23 +0100 Subject: [PATCH] try wordpress rest api instead of ajax --- plugins/fipfcard_plugin/js/paypal/paypal.js | 60 ++++++++++------- .../php/paypal/route_api_orders.php | 64 ++++++++++++++----- plugins/fipfcard_plugin/utils/plugin_ajax.js | 8 --- .../fipfcard_plugin/utils/plugin_tools.php | 10 +-- private | 2 +- 5 files changed, 91 insertions(+), 53 deletions(-) diff --git a/plugins/fipfcard_plugin/js/paypal/paypal.js b/plugins/fipfcard_plugin/js/paypal/paypal.js index 0ce5d9c..5e720ef 100644 --- a/plugins/fipfcard_plugin/js/paypal/paypal.js +++ b/plugins/fipfcard_plugin/js/paypal/paypal.js @@ -66,54 +66,66 @@ paypal.Buttons({ window.paypal.Buttons({ - //createOrder: function(data, actions) { - createOrder: function() { - PLGNTLS_fetch("paypal_api_orders", { - //method: "POST", - //headers: {"Content-Type": "application/json"}, - //body: { + createOrder: function(data, actions) { +// PLGNTLS_fetch("paypal_api_orders", { +// //method: "POST", +// //headers: {"Content-Type": "application/json"}, +// body: JSON.stringify({ +// cart: [ +// { +// id: "1234", +// quantity: "1", +// }, +// ], +// }), +// }) + fetch(PLGNTLS_data.fetch_url + "/fipf-plugin/v1/orders", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, body: JSON.stringify({ cart: [ { - id: "YOUR_PRODUCT_ID", - quantity: "YOUR_PRODUCT_QUANTITY", + id: "1234", + quantity: "1", }, ], }), }) .then((response) => response.json()) - .then((orderData) => + .then((order_data) => { - console.log("orderData:"); - console.log(orderData); - if (orderData.id) - return orderData.id; + if (order_data.id) + return order_data.id; else { + // https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratefrontend + console.log("inside error"); const errorDetail = orderData?.details?.[0]; const errorMessage = errorDetail ? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})` : JSON.stringify(orderData); - throw new Error(errorMessage); } }) .catch((error) => { console.error(error); - resultMessage(`Could not initiate PayPal Checkout...

${error}`); + resultMessage('Could not initiate PayPal Checkout...
(see console.log() errors)'); }) }, -/* - async onApprove(data, actions) { - try { - const response = await fetch(`/api/orders/${data.orderID}/capture`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - }); +/* + onApprove: function(data, actions) { + console.log("--data:"); + console.log(data); + PLGNTLS_fetch(`/api/orders/${data.orderID}/capture`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + }); const orderData = await response.json(); // Three cases to handle: diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index 1cc6188..8ecee1b 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -8,29 +8,61 @@ include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php'); */ 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'] ) ) ); -// error_log( "data_received" ); -// error_log( json_encode( $data_received ) ); + // not a good error handling + if ($_SERVER['REQUEST_METHOD'] !== 'POST') + return; + try + { + // json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573 + $data_received = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) ); - // 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); + // use the cart information passed from the front-end to calculate the order amount details + $cart = $data_received->cart; + $order_response = create_order($cart); + error_log( "order_response[json_response]" ); + error_log( json_encode( $order_response['json_response'] ) ); + + wp_send_json_success($order_response['json_response'], $order_response['http_status_code']); + } + catch (Exception $err) + { + error_log('Failed to create order: ' . $err->getMessage()); + wp_send_json_error(array('error' => 'Failed to create order')); + } - wp_send_json_success( - array( - 'from paypal_order', - "data_received" => $data_received, - ), - 200 - ); } add_action( 'wp_ajax_paypal_api_orders', 'fipfcard_paypal_orders' ); +// Endpoints for handling routes +function fipf_routes_endpoints() +{ + register_rest_route('fipf-plugin/v1', '/orders', array( + 'methods' => 'POST', + 'callback' => 'handle_orders_request', + )); +}; +add_action('rest_api_init', 'fipf_routes_endpoints'); +// Callback function for handling orders +function handle_orders_request($request_data) { + try { + // Extract cart information from request body + $cart = $request_data['cart']; + + // Process the order and get the response + $order_response = create_order($cart); + + // Return response + return new WP_REST_Response($order_response['json_response'], $order_response['http_status_code']); + } catch (Exception $e) { + // Handle errors + error_log('Failed to create order: ' . $e->getMessage()); + return new WP_Error('500', 'Failed to create order.', array('status' => 500)); + } +} + + /** diff --git a/plugins/fipfcard_plugin/utils/plugin_ajax.js b/plugins/fipfcard_plugin/utils/plugin_ajax.js index d80b7ec..131c94f 100644 --- a/plugins/fipfcard_plugin/utils/plugin_ajax.js +++ b/plugins/fipfcard_plugin/utils/plugin_ajax.js @@ -92,27 +92,19 @@ function PLGNTLS_fetch(src = null, options = null) data.append( '_ajax_nonce', PLGNTLS_data.ajax_nonce ); if ( is_plain_object( options.body ) ) { - console.log( "1" ); for ( const key in options.body ) data.append( key, JSON.stringify(options.body[key]) ); } // is string : https://stackoverflow.com/q/4059147/9497573 else if ( typeof options.body === 'string' || options.body instanceof String ) - { - console.log( "2" ); data.append( 'data', options.body ); - } else - { - console.log( "3" ); data.append('data', JSON.stringify(options.body)); - } options.body = data; } } else throw new Error('options not plain object or formData'); - console.log("options: ", options); return fetch(PLGNTLS_data.ajax_url, options); } diff --git a/plugins/fipfcard_plugin/utils/plugin_tools.php b/plugins/fipfcard_plugin/utils/plugin_tools.php index 0febca2..4cfa3b7 100644 --- a/plugins/fipfcard_plugin/utils/plugin_tools.php +++ b/plugins/fipfcard_plugin/utils/plugin_tools.php @@ -52,18 +52,20 @@ class PLGNTLS_class public function add_to_front($scripts_arr = null, $vars = null) { + if (!is_array($vars)) + $vars = array(); if (!is_null($scripts_arr)) { console_log("in is null scripts_arr"); // add ajax file at beginning of files list array_unshift($scripts_arr, "utils/plugin_ajax.js"); $nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce')); - $url = array("ajax_url" => admin_url('admin-ajax.php')); - if (!is_array($vars)) - $vars = array(); + $ajax_url = array("ajax_url" => admin_url('admin-ajax.php')); $vars = array_merge($vars, $nonce); - $vars = array_merge($vars, $url); + $vars = array_merge($vars, $ajax_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) { diff --git a/private b/private index d4c3e0c..0bb37fe 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit d4c3e0c142cc62c878e97fbc5d3c7633688c48df +Subproject commit 0bb37fe53116c533ba06e238980397282b6e2667