Allow free trial subscription without payment token for logged-in users

This commit is contained in:
Emili Castells Guasch 2024-04-06 17:31:57 +02:00
parent 5917f69c4c
commit 457e9f0182
4 changed files with 114 additions and 0 deletions

View file

@ -144,6 +144,55 @@ class CheckoutActionHandler {
}
}
}
addPaymentMethodConfiguration() {
return {
createVaultSetupToken: async () => {
const response = await fetch(this.config.ajax.create_setup_token.endpoint, {
method: "POST",
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nonce: this.config.ajax.create_setup_token.nonce,
})
});
const result = await response.json()
if (result.data.id) {
return result.data.id
}
console.error(result)
},
onApprove: async ({vaultSetupToken}) => {
const response = await fetch(this.config.ajax.create_payment_token.endpoint, {
method: "POST",
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nonce: this.config.ajax.create_payment_token.nonce,
vault_setup_token: vaultSetupToken,
return_url: location.href,
})
})
const result = await response.json();
if (result.success === true) {
window.location.href = location.href;
return;
}
console.error(result)
},
onError: (error) => {
console.error(error)
}
}
}
}
export default CheckoutActionHandler;

View file

@ -116,6 +116,14 @@ class CheckoutBootstap {
return;
}
if(
PayPalCommerceGateway.is_free_trial_cart
&& PayPalCommerceGateway.vault_v3_enabled
) {
this.renderer.render(actionHandler.addPaymentMethodConfiguration(), {}, actionHandler.configuration());
return;
}
this.renderer.render(actionHandler.configuration(), {}, actionHandler.configuration());
}