wip upgrade and clean payment process
This commit is contained in:
@@ -2,42 +2,39 @@
|
||||
import { resultMessage } from './result_message.js';
|
||||
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
//async function createOrder() {
|
||||
/*
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*
|
||||
*/
|
||||
export async function createOrder() {
|
||||
try {
|
||||
//const fetch_create_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders";
|
||||
//console.log("fetch_create_url:", fetch_create_url);
|
||||
//const response = await fetch(fetch_create_url, {
|
||||
const response = await PLGNTLS_fetch('/cipf_plugin/api/v1/orders', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
//// use the "body" param to optionally pass additional order information
|
||||
//// like product ids and quantities
|
||||
//body: JSON.stringify({
|
||||
// cart: [
|
||||
// {
|
||||
// id: "1234",
|
||||
// quantity: "1",
|
||||
// },
|
||||
// ],
|
||||
//}),
|
||||
});
|
||||
|
||||
const orderData = await response.json();
|
||||
|
||||
/*
|
||||
* is it necessary since the error is catched according to the presence of id ?
|
||||
* -> i think yes, because we want to know the type of http error, right ?
|
||||
* -> actually the http error code will be shown in the console anyway, so...
|
||||
*
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok : ' + orderData.data.status + ' message : ' + orderData.message);
|
||||
}
|
||||
*/
|
||||
|
||||
if (orderData.id) {
|
||||
return orderData.id;
|
||||
} else {
|
||||
}
|
||||
else { // throw an 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) {
|
||||
|
||||
@@ -2,24 +2,22 @@
|
||||
import { resultMessage } from './result_message.js';
|
||||
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
//async function onApprove(data, actions) {
|
||||
/*
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*
|
||||
*/
|
||||
export async function onApprove(data, actions) {
|
||||
try {
|
||||
const fetch_approve_url = PLGNTLS_data.fetch_url + "/cipf_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 PLGNTLS_fetch('/cipf_plugin/api/v1/orders/' + data.orderID + '/capture', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
//"X-WP-Nonce": PLGNTLS_data.rest_nonce,
|
||||
},
|
||||
});
|
||||
|
||||
const orderData = await response.json();
|
||||
|
||||
// Three cases to handle:
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
@@ -31,32 +29,24 @@ export async function onApprove(data, actions) {
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
|
||||
return actions.restart();
|
||||
} else if (errorDetail) {
|
||||
}
|
||||
else if (errorDetail) {
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
console.log("errorDetail:");
|
||||
console.log(errorDetail);
|
||||
throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
|
||||
} else if (!orderData.purchase_units) {
|
||||
}
|
||||
else if (!orderData.purchase_units) {
|
||||
throw new Error(JSON.stringify(orderData));
|
||||
} else {
|
||||
try {
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
// Or go to another URL: actions.redirect('thank_you.html');
|
||||
const transaction =
|
||||
orderData?.purchase_units?.[0]?.payments?.captures?.[0] ||
|
||||
orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
|
||||
// to show a message on page
|
||||
//resultMessage(`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`);
|
||||
resultMessage(eval(PLGNTLS_data.paypal_message_success));
|
||||
console.log(
|
||||
"Capture result",
|
||||
orderData,
|
||||
JSON.stringify(orderData, null, 2),
|
||||
);
|
||||
actions.redirect(PLGNTLS_data.paypal_redirection_success);
|
||||
} catch (error) {
|
||||
console.error("payment ok but error on traitment afterwards : ", error);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
// Or go to another URL: actions.redirect('thank_you.html');
|
||||
const transaction =
|
||||
orderData?.purchase_units?.[0]?.payments?.captures?.[0] ||
|
||||
orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
|
||||
// to show a message on page
|
||||
//resultMessage(`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`);
|
||||
resultMessage(eval(PLGNTLS_data.paypal_message_success));
|
||||
actions.redirect(PLGNTLS_data.paypal_redirection_success);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -66,49 +56,3 @@ export async function onApprove(data, actions) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/demo/checkout/#/pattern/server
|
||||
*
|
||||
// Call your server to finalize the transaction
|
||||
function onApprove (data, actions) {
|
||||
const fetch_approve_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders/" + data.orderID + "/capture";
|
||||
console.log("fetch_approve_url:", fetch_approve_url);
|
||||
return fetch(fetch_approve_url, {
|
||||
method: 'post'
|
||||
}).then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(orderData) {
|
||||
// Three cases to handle:
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
// (3) Successful transaction -> Show confirmation or thank you
|
||||
|
||||
// This example reads a v2/checkout/orders capture response, propagated from the server
|
||||
// You could use a different API or structure for your 'orderData'
|
||||
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
|
||||
|
||||
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
|
||||
return actions.restart(); // Recoverable state, per:
|
||||
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
|
||||
}
|
||||
|
||||
if (errorDetail) {
|
||||
var msg = 'Sorry, your transaction could not be processed.';
|
||||
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
|
||||
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
|
||||
return alert(msg); // Show a failure message (try to avoid alerts in production environments)
|
||||
}
|
||||
|
||||
// Successful capture! For demo purposes:
|
||||
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
|
||||
var transaction = orderData.purchase_units[0].payments.captures[0];
|
||||
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
|
||||
|
||||
// Replace the above to show a success message within this page, e.g.
|
||||
// const element = document.getElementById('paypal-button-container');
|
||||
// element.innerHTML = '';
|
||||
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
|
||||
// Or go to another URL: actions.redirect('thank_you.html');
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user