try wordpress rest api instead of ajax
This commit is contained in:
@@ -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...<br><br>${error}`);
|
||||
resultMessage('Could not initiate PayPal Checkout... <br>(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:
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
2
private
2
private
Submodule private updated: d4c3e0c142...0bb37fe531
Reference in New Issue
Block a user