Add subscription from cart page

This commit is contained in:
Emili Castells Guasch 2023-03-30 14:47:28 +02:00
parent 50dd59ec77
commit 610ba3e935
3 changed files with 69 additions and 1 deletions

View file

@ -9,6 +9,39 @@ class CartActionHandler {
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) => {
if (!data.success) {
console.log(data)
throw Error(data.data.message);
}
location.href = this.config.redirect;
});
},
onError: (err) => {
console.error(err);
}
}
}
configuration() {
const createOrder = (data, actions) => {
const payer = payerData();

View file

@ -50,6 +50,14 @@ class CartBootstrap {
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()
);

View file

@ -111,7 +111,7 @@ test.describe('Subscriptions Customer', () => {
test('Purchase Subscription from Checkout Page', async ({page}) => {
await loginAsCustomer(page);
await page.goto('/product/another-sub');
await page.goto('/product/subscription');
await page.click("text=Sign up now");
await page.goto('/checkout');
await fillCheckoutForm(page);
@ -159,4 +159,31 @@ test.describe('Subscriptions Customer', () => {
const title = page.locator('.entry-title');
await expect(title).toHaveText('Order received');
});
test('Purchase Subscription from Cart Page', async ({page}) => {
await loginAsCustomer(page);
await page.goto('/product/subscription');
await page.click("text=Sign up now");
await page.goto('/cart');
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.frameLocator('.component-frame').locator('[data-funding-source="paypal"]').click(),
]);
await popup.waitForLoadState();
await popup.fill('#email', CUSTOMER_EMAIL);
await popup.locator('#btnNext').click();
await popup.fill('#password', CUSTOMER_PASSWORD);
await popup.locator('#btnLogin').click();
await popup.locator('text=Continue').click();
await popup.locator('#confirmButtonTop').click();
await fillCheckoutForm(page);
await page.locator('text=Sign up now').click();
const title = page.locator('.entry-title');
await expect(title).toHaveText('Order received');
});
});