- forget old ajax wp, now use fetch wp

- trying js files without imports
This commit is contained in:
asus
2024-02-26 13:18:19 +01:00
parent 5794e4cafe
commit 0177531924
11 changed files with 65 additions and 42 deletions

View File

@@ -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: ");

View File

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

View File

@@ -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}<br><br>See console for all available details`,
);
// to show a message on page
//resultMessage(`Transaction ${transaction.status}: ${transaction.id}<br><br>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);

View File

@@ -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({

View File

@@ -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;
}