try wordpress rest api instead of ajax

This commit is contained in:
asus
2024-02-23 23:43:23 +01:00
parent 0f15b67e8b
commit 38260912cd
5 changed files with 91 additions and 53 deletions

View File

@@ -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: