try wordpress rest api instead of ajax
This commit is contained in:
@@ -66,49 +66,61 @@ paypal.Buttons({
|
|||||||
|
|
||||||
|
|
||||||
window.paypal.Buttons({
|
window.paypal.Buttons({
|
||||||
//createOrder: function(data, actions) {
|
createOrder: function(data, actions) {
|
||||||
createOrder: function() {
|
// PLGNTLS_fetch("paypal_api_orders", {
|
||||||
PLGNTLS_fetch("paypal_api_orders", {
|
// //method: "POST",
|
||||||
//method: "POST",
|
// //headers: {"Content-Type": "application/json"},
|
||||||
//headers: {"Content-Type": "application/json"},
|
// body: JSON.stringify({
|
||||||
//body: {
|
// cart: [
|
||||||
|
// {
|
||||||
|
// id: "1234",
|
||||||
|
// quantity: "1",
|
||||||
|
// },
|
||||||
|
// ],
|
||||||
|
// }),
|
||||||
|
// })
|
||||||
|
fetch(PLGNTLS_data.fetch_url + "/fipf-plugin/v1/orders", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
cart: [
|
cart: [
|
||||||
{
|
{
|
||||||
id: "YOUR_PRODUCT_ID",
|
id: "1234",
|
||||||
quantity: "YOUR_PRODUCT_QUANTITY",
|
quantity: "1",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((orderData) =>
|
.then((order_data) =>
|
||||||
{
|
{
|
||||||
console.log("orderData:");
|
if (order_data.id)
|
||||||
console.log(orderData);
|
return order_data.id;
|
||||||
if (orderData.id)
|
|
||||||
return orderData.id;
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratefrontend
|
||||||
|
console.log("inside error");
|
||||||
const errorDetail = orderData?.details?.[0];
|
const errorDetail = orderData?.details?.[0];
|
||||||
const errorMessage = errorDetail
|
const errorMessage = errorDetail
|
||||||
? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
|
? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
|
||||||
: JSON.stringify(orderData);
|
: JSON.stringify(orderData);
|
||||||
|
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
resultMessage(`Could not initiate PayPal Checkout...<br><br>${error}`);
|
resultMessage('Could not initiate PayPal Checkout... <br>(see console.log() errors)');
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
async onApprove(data, actions) {
|
/*
|
||||||
try {
|
onApprove: function(data, actions) {
|
||||||
const response = await fetch(`/api/orders/${data.orderID}/capture`, {
|
console.log("--data:");
|
||||||
|
console.log(data);
|
||||||
|
PLGNTLS_fetch(`/api/orders/${data.orderID}/capture`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|||||||
@@ -7,30 +7,62 @@ include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php');
|
|||||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||||
*/
|
*/
|
||||||
function fipfcard_paypal_orders()
|
function fipfcard_paypal_orders()
|
||||||
|
{
|
||||||
|
// not a good error handling
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST')
|
||||||
|
return;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573
|
// json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573
|
||||||
$data_received = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) );
|
$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
|
// use the cart information passed from the front-end to calculate the order amount details
|
||||||
$cart = $data_received->cart;
|
$cart = $data_received->cart;
|
||||||
error_log( "cart" );
|
$order_response = create_order($cart);
|
||||||
error_log( json_encode( $cart ) );
|
error_log( "order_response[json_response]" );
|
||||||
create_order($cart);
|
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' );
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -92,27 +92,19 @@ function PLGNTLS_fetch(src = null, options = null)
|
|||||||
data.append( '_ajax_nonce', PLGNTLS_data.ajax_nonce );
|
data.append( '_ajax_nonce', PLGNTLS_data.ajax_nonce );
|
||||||
if ( is_plain_object( options.body ) )
|
if ( is_plain_object( options.body ) )
|
||||||
{
|
{
|
||||||
console.log( "1" );
|
|
||||||
for ( const key in options.body )
|
for ( const key in options.body )
|
||||||
data.append( key, JSON.stringify(options.body[key]) );
|
data.append( key, JSON.stringify(options.body[key]) );
|
||||||
}
|
}
|
||||||
// is string : https://stackoverflow.com/q/4059147/9497573
|
// is string : https://stackoverflow.com/q/4059147/9497573
|
||||||
else if ( typeof options.body === 'string' || options.body instanceof String )
|
else if ( typeof options.body === 'string' || options.body instanceof String )
|
||||||
{
|
|
||||||
console.log( "2" );
|
|
||||||
data.append( 'data', options.body );
|
data.append( 'data', options.body );
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
console.log( "3" );
|
|
||||||
data.append('data', JSON.stringify(options.body));
|
data.append('data', JSON.stringify(options.body));
|
||||||
}
|
|
||||||
options.body = data;
|
options.body = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new Error('options not plain object or formData');
|
throw new Error('options not plain object or formData');
|
||||||
|
|
||||||
console.log("options: ", options);
|
|
||||||
return fetch(PLGNTLS_data.ajax_url, options);
|
return fetch(PLGNTLS_data.ajax_url, options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,18 +52,20 @@ class PLGNTLS_class
|
|||||||
|
|
||||||
|
|
||||||
public function add_to_front($scripts_arr = null, $vars = null) {
|
public function add_to_front($scripts_arr = null, $vars = null) {
|
||||||
|
if (!is_array($vars))
|
||||||
|
$vars = array();
|
||||||
if (!is_null($scripts_arr))
|
if (!is_null($scripts_arr))
|
||||||
{
|
{
|
||||||
console_log("in is null scripts_arr");
|
console_log("in is null scripts_arr");
|
||||||
// add ajax file at beginning of files list
|
// add ajax file at beginning of files list
|
||||||
array_unshift($scripts_arr, "utils/plugin_ajax.js");
|
array_unshift($scripts_arr, "utils/plugin_ajax.js");
|
||||||
$nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce'));
|
$nonce = array("ajax_nonce" => wp_create_nonce('wp-pageviews-nonce'));
|
||||||
$url = array("ajax_url" => admin_url('admin-ajax.php'));
|
$ajax_url = array("ajax_url" => admin_url('admin-ajax.php'));
|
||||||
if (!is_array($vars))
|
|
||||||
$vars = array();
|
|
||||||
$vars = array_merge($vars, $nonce);
|
$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();
|
$scripts = array();
|
||||||
foreach($scripts_arr as $key => $script_name) {
|
foreach($scripts_arr as $key => $script_name) {
|
||||||
|
|||||||
2
private
2
private
Submodule private updated: d4c3e0c142...0bb37fe531
Reference in New Issue
Block a user