Add subscriptions api logic to Single product (WIP)

This commit is contained in:
Emili Castells Guasch 2023-03-20 11:58:34 +01:00
parent 999231fd01
commit 01ab5d6003
3 changed files with 47 additions and 1 deletions

View file

@ -18,6 +18,34 @@ class SingleProductActionHandler {
this.errorHandler = errorHandler;
}
subscriptionsConfiguration() {
return {
createSubscription: (data, actions) => {
return actions.subscription.create({
'plan_id': this.config.subscription_plan_id
});
},
onApprove: (data, actions) => {
fetch(this.config.ajax.approve_subscription.endpoint, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({
nonce: this.config.ajax.approve_subscription.nonce,
order_id: data.orderID,
subscription_id: data.subscriptionID
})
}).then((res)=>{
return res.json();
}).then((data) => {
location.href = this.config.redirect;
});
},
onError: (err) => {
console.error(err);
}
}
}
configuration()
{
return {

View file

@ -110,6 +110,14 @@ class SingleProductBootstap {
this.errorHandler,
);
if(
PayPalCommerceGateway.data_client_id.has_subscriptions
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled
) {
this.renderer.render(actionHandler.subscriptionsConfiguration());
return;
}
this.renderer.render(
actionHandler.configuration()
);