PAYPAL WORKING
This commit is contained in:
62
plugins/fipfcard_plugin/js/paypal/create_order.js
Normal file
62
plugins/fipfcard_plugin/js/paypal/create_order.js
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
import { resultMessage } from './result_message.js';
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
export 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, {
|
||||
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();
|
||||
|
||||
if (orderData.id) {
|
||||
return orderData.id;
|
||||
} else {
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/demo/checkout/#/pattern/server
|
||||
*
|
||||
// Call your server to set up the transaction
|
||||
function createOrder(data, actions) {
|
||||
const fetch_create_url = PLGNTLS_data.fetch_url + "/fipf_plugin/api/v1/orders";
|
||||
console.log("fetch_create_url:", fetch_create_url);
|
||||
return fetch(fetch_create_url, {
|
||||
method: 'post'
|
||||
}).then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(orderData) {
|
||||
return orderData.id;
|
||||
});
|
||||
},
|
||||
*/
|
||||
|
||||
103
plugins/fipfcard_plugin/js/paypal/on_approve.js
Normal file
103
plugins/fipfcard_plugin/js/paypal/on_approve.js
Normal file
@@ -0,0 +1,103 @@
|
||||
|
||||
import { resultMessage } from './result_message.js';
|
||||
|
||||
/**
|
||||
* @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 + "/fipf_plugin/api/v1/orders/" + data.orderID + "/capture";
|
||||
console.log("fetch_approve_url:", fetch_approve_url);
|
||||
const response = await fetch(fetch_approve_url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
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
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
|
||||
const errorDetail = orderData?.details?.[0];
|
||||
|
||||
if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
|
||||
// (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) {
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
|
||||
} else if (!orderData.purchase_units) {
|
||||
throw new Error(JSON.stringify(orderData));
|
||||
} 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];
|
||||
resultMessage(
|
||||
`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`,
|
||||
);
|
||||
console.log(
|
||||
"Capture result",
|
||||
orderData,
|
||||
JSON.stringify(orderData, null, 2),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
resultMessage(
|
||||
`Sorry, your transaction could not be processed...<br><br>${error}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 + "/fipf_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');
|
||||
});
|
||||
}
|
||||
*/
|
||||
@@ -1,217 +1,56 @@
|
||||
/*
|
||||
paypal.Buttons({
|
||||
style: {
|
||||
layout: 'vertical',
|
||||
color: 'blue',
|
||||
shape: 'rect',
|
||||
label: 'paypal',
|
||||
},
|
||||
// Order is created on the server and the order id is returned
|
||||
createOrder() {
|
||||
return fetch("/my-server/create-paypal-order", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
// Use the "body" param to optionally pass additional order information
|
||||
// such as product SKUs and quantities
|
||||
body: JSON.stringify({
|
||||
cart: [
|
||||
{
|
||||
sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
|
||||
quantity: "YOUR_PRODUCT_QUANTITY",
|
||||
},
|
||||
],
|
||||
}),
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((order) => order.id);
|
||||
}
|
||||
}).render('#paypal-button-container');
|
||||
*/
|
||||
|
||||
/*
|
||||
paypal.Buttons({
|
||||
style: {
|
||||
layout: 'vertical',
|
||||
color: 'blue',
|
||||
shape: 'rect',
|
||||
label: 'paypal',
|
||||
},
|
||||
createOrder: function(data, actions) {
|
||||
// This function sets up the details of the transaction, including the amount and line item details.
|
||||
const my_order = actions.order.create({
|
||||
purchase_units: [{
|
||||
amount: {
|
||||
value: '0.01'
|
||||
}
|
||||
}]
|
||||
});
|
||||
console.log("my_order: ", my_order);
|
||||
return my_order;
|
||||
},
|
||||
onApprove: function(data, actions) {
|
||||
// This function captures the funds from the transaction.
|
||||
console.log("data: ", data);
|
||||
console.log("actions: ", actions);
|
||||
return actions.order.capture().then(function(details) {
|
||||
console.log("details: ", details);
|
||||
// This function shows a transaction success message to your buyer.
|
||||
alert('Transaction completed by ' + details.payer.name.given_name);
|
||||
});
|
||||
}
|
||||
}).render('#paypal-button-container');
|
||||
//This function displays Smart Payment Buttons on your web page.
|
||||
*/
|
||||
import { createOrder } from './create_order.js';
|
||||
import { onApprove } from './on_approve.js';
|
||||
|
||||
|
||||
window.paypal.Buttons({
|
||||
async createOrder() {
|
||||
try {
|
||||
const response = await fetch(PLGNTLS_data.fetch_url + "/fipf_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();
|
||||
|
||||
if (orderData.id) {
|
||||
return orderData.id;
|
||||
} else {
|
||||
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}`);
|
||||
}
|
||||
/*
|
||||
style: {
|
||||
layout: 'vertical',
|
||||
color: 'blue',
|
||||
shape: 'rect',
|
||||
label: 'paypal',
|
||||
},
|
||||
|
||||
async onApprove(data, actions) {
|
||||
try {
|
||||
const fetch_url = PLGNTLS_data.fetch_url + "/fipf_plugin/api/v1/orders/" + data.orderID + "/capture";
|
||||
console.log("fetch_url:");
|
||||
console.log(fetch_url);
|
||||
const response = await fetch(fetch_url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
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
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
|
||||
const errorDetail = orderData?.details?.[0];
|
||||
|
||||
if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
|
||||
// (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) {
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
|
||||
} else if (!orderData.purchase_units) {
|
||||
throw new Error(JSON.stringify(orderData));
|
||||
} 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];
|
||||
resultMessage(
|
||||
`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`,
|
||||
);
|
||||
console.log(
|
||||
"Capture result",
|
||||
orderData,
|
||||
JSON.stringify(orderData, null, 2),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
resultMessage(
|
||||
`Sorry, your transaction could not be processed...<br><br>${error}`,
|
||||
);
|
||||
}
|
||||
},
|
||||
/*
|
||||
onApprove: function(data, actions) {
|
||||
console.log("--data:");
|
||||
console.log(data);
|
||||
fetch(`${PLGNTLS_data.fetch_url}/fipf_plugin/api/v1/orders/${data.orderID}/capture`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
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
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
|
||||
const errorDetail = orderData?.details?.[0];
|
||||
|
||||
if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
|
||||
// (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) {
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
|
||||
} else if (!orderData.purchase_units) {
|
||||
throw new Error(JSON.stringify(orderData));
|
||||
} 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];
|
||||
resultMessage(
|
||||
`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`,
|
||||
);
|
||||
console.log(
|
||||
"Capture result",
|
||||
orderData,
|
||||
JSON.stringify(orderData, null, 2),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
resultMessage(
|
||||
`Sorry, your transaction could not be processed...<br><br>${error}`,
|
||||
);
|
||||
}
|
||||
},
|
||||
*/
|
||||
*/
|
||||
createOrder: createOrder,
|
||||
onApprove: onApprove,
|
||||
})
|
||||
.render("#paypal-button-container");
|
||||
|
||||
// Example function to show a result to the user. Your site's UI library can be used instead.
|
||||
function resultMessage(message) {
|
||||
const container = document.querySelector("#result-message");
|
||||
container.innerHTML = message;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* customize card fields
|
||||
* from : https://developer.paypal.com/docs/checkout/advanced/integrate#link-addpaypalbuttonsandcardfields
|
||||
*
|
||||
// Create the Card Fields Component and define callbacks
|
||||
const cardField = paypal.CardFields({
|
||||
createOrder: createOrder,
|
||||
onApprove: onApprove,
|
||||
});
|
||||
|
||||
// Render each field after checking for eligibility
|
||||
if (cardField.isEligible()) {
|
||||
const nameField = cardField.NameField();
|
||||
nameField.render("#card-name-field-container");
|
||||
|
||||
const numberField = cardField.NumberField();
|
||||
numberField.render("#card-number-field-container");
|
||||
|
||||
const cvvField = cardField.CVVField();
|
||||
cvvField.render("#card-cvv-field-container");
|
||||
|
||||
const expiryField = cardField.ExpiryField();
|
||||
expiryField.render("#card-expiry-field-container");
|
||||
|
||||
// Add click listener to submit button and call the submit function on the CardField component
|
||||
document
|
||||
.getElementById("card-field-submit-button")
|
||||
.addEventListener("click", () => {
|
||||
cardField.submit().then(() => {
|
||||
// submit successful
|
||||
});
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
6
plugins/fipfcard_plugin/js/paypal/result_message.js
Normal file
6
plugins/fipfcard_plugin/js/paypal/result_message.js
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
// Example function to show a result to the user. Your site's UI library can be used instead.
|
||||
export function resultMessage(message) {
|
||||
const container = document.querySelector("#result-message");
|
||||
container.innerHTML = message;
|
||||
}
|
||||
Reference in New Issue
Block a user