Add save payment method for purchase later (WIP)

This commit is contained in:
Emili Castells Guasch 2023-10-26 16:36:38 +02:00
parent 9f41d5c874
commit 34cc1bffe4
8 changed files with 388 additions and 8 deletions

View file

@ -7,6 +7,58 @@ import {
import {setVisible} from "../../../ppcp-button/resources/js/modules/Helper/Hiding";
import {loadPaypalJsScript} from "../../../ppcp-button/resources/js/modules/Helper/ScriptLoading";
loadPaypalJsScript(
{
clientId: ppcp_add_payment_method.client_id,
merchantId: ppcp_add_payment_method.merchant_id,
dataUserIdToken: ppcp_add_payment_method.id_token,
},
{
createVaultSetupToken: async () => {
const response = await fetch(ppcp_add_payment_method.ajax.create_setup_token.endpoint, {
method: "POST",
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nonce: ppcp_add_payment_method.ajax.create_setup_token.nonce,
})
})
const result = await response.json()
if(result.data.id) {
return result.data.id
}
},
onApprove: async ({ vaultSetupToken }) => {
console.log(vaultSetupToken)
const response = await fetch(ppcp_add_payment_method.ajax.create_payment_token.endpoint, {
method: "POST",
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nonce: ppcp_add_payment_method.ajax.create_payment_token.nonce,
vault_setup_token: vaultSetupToken,
})
})
const result = await response.json()
console.log(result)
},
onError: (error) => {
console.error(error)
}
},
`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`
);
const init = () => {
setVisible(ORDER_BUTTON_SELECTOR, getCurrentPaymentMethod() !== PaymentMethods.PAYPAL);
setVisible(`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`, getCurrentPaymentMethod() === PaymentMethods.PAYPAL);
@ -18,12 +70,6 @@ document.addEventListener(
jQuery(document.body).on('click init_add_payment_method', '.payment_methods input.input-radio', function () {
init()
});
loadPaypalJsScript(
{},
{},
`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`
);
}
);