import { test, expect } from '@playwright/test'; import { mockPublicStoreApis, mockAuthenticatedUser } from './fixtures/apiMocks'; test.describe('Phase 4: Checkout, Billing Phone & Razorpay Flow', () => { test.beforeEach(async ({ page }) => { await mockPublicStoreApis(page); }); test('unauthenticated checkout opens AuthModal', async ({ page }) => { await page.goto('./'); await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible(); // Add item to cart and open cart drawer await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); // AuthModal should appear in register mode await expect(page.getByRole('heading', { name: 'Create Account' })).toBeVisible(); }); test('missing billing phone opens BillingPhoneModal, validates, saves, and completes Razorpay payment', async ({ page }) => { // Mock authenticated user with NO phone number await mockAuthenticatedUser(page, { phone_number: '' }); let profilePutPayload: any = null; await page.route('**/api/auth/profile', async (route) => { if (route.request().method() === 'PUT') { profilePutPayload = route.request().postDataJSON(); await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: 'success' }), }); } else { await route.fallback(); } }); await page.route('**/api/checkout/create-order', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ razorpay_order_id: 'order_mock_001', grand_total: 1000, currency: 'INR', }), }); }); await page.route('**/api/checkout/verify', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: 'success', invoice_number: 'INV-2026-0042', invoice_url: '/api/checkout/payment/pay_mock_12345/invoice/view', message: 'Payment verified successfully and module entitlements unlocked!', }), }); }); // Inject mock window.Razorpay that auto-succeeds on .open() await page.addInitScript(() => { (window as any).__openedUrls = []; window.open = ((url: string) => { (window as any).__openedUrls.push(url); return null; }) as any; (window as any).Razorpay = function (options: any) { this.options = options; this.events = {}; this.on = (event: string, cb: Function) => { this.events[event] = cb; }; this.open = () => { setTimeout(() => { options.handler({ razorpay_order_id: options.order_id, razorpay_payment_id: 'pay_mock_12345', razorpay_signature: 'sig_mock_xyz', }); }, 50); }; }; }); await page.goto('./'); await expect(page.getByText('Acu (EOV+Cat+Ext+MR)')).toBeVisible(); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); // BillingPhoneModal should appear await expect(page.getByText('Billing Contact Number')).toBeVisible(); // Test empty phone validation await page.getByRole('button', { name: 'Save Phone Number' }).click(); await expect(page.getByText(/Please enter your mobile number/i)).toBeVisible(); // Enter valid Indian 10-digit mobile number const phoneInput = page.locator('input[type="tel"]').last(); await phoneInput.fill('9876543210'); await page.getByRole('button', { name: 'Save Phone Number' }).click(); // Should save phone via PUT /api/auth/profile and auto-proceed to Razorpay -> PaymentSuccessModal await expect(page.getByText('Thank You for Your Purchase!')).toBeVisible({ timeout: 10000 }); expect(profilePutPayload).toEqual({ phone_number: '+919876543210', country: 'India', }); // Verify PaymentSuccessModal details await expect(page.getByText('INV-2026-0042')).toBeVisible(); await expect(page.getByText('pay_mock_12345')).toBeVisible(); await expect(page.getByText('₹1000')).toBeVisible(); // Pause 10s auto-redirect await page.getByRole('button', { name: /Stay on Web/i }).click(); await expect(page.getByText(/Auto-redirect paused/i)).toBeVisible(); // Click Download Invoice PDF -> opens invoice URL & navigates to Subscriptions tab await page.getByText('Download Invoice PDF').click(); const openedUrls = await page.evaluate(() => (window as any).__openedUrls); expect(openedUrls.some((u: string) => u.includes('/api/checkout/payment/pay_mock_12345/invoice/view'))).toBe(true); }); test('Razorpay modal dismiss cancels cleanly without calling verify', async ({ page }) => { await mockAuthenticatedUser(page, { phone_number: '+919876543210' }); let verifyCalled = false; await page.route('**/api/checkout/create-order', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ razorpay_order_id: 'order_mock_cancel', grand_total: 1000, currency: 'INR', }), }); }); await page.route('**/api/checkout/verify', async (route) => { verifyCalled = true; await route.fulfill({ status: 200, body: '{}' }); }); await page.addInitScript(() => { (window as any).Razorpay = function (options: any) { this.on = () => {}; this.open = () => { setTimeout(() => { options.modal.ondismiss(); }, 50); }; }; }); await page.goto('./'); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); // Button should return to enabled state, verify not called, success modal not shown await expect(page.getByRole('button', { name: /Secure Payment with Razorpay/i })).toBeEnabled(); expect(verifyCalled).toBe(false); await expect(page.getByText('Thank You for Your Purchase!')).not.toBeVisible(); }); test('Razorpay payment.failed and verify signature failure show error feedback', async ({ page }) => { await mockAuthenticatedUser(page, { phone_number: '+919876543210' }); await page.route('**/api/checkout/create-order', async (route) => { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ razorpay_order_id: 'order_mock_fail', grand_total: 1000, currency: 'INR', }), }); }); await page.route('**/api/checkout/verify', async (route) => { await route.fulfill({ status: 400, contentType: 'application/json', body: JSON.stringify({ detail: 'Invalid Razorpay signature' }), }); }); await page.addInitScript(() => { (window as any).Razorpay = function (options: any) { this.events = {}; this.on = (event: string, cb: Function) => { this.events[event] = cb; }; this.open = () => { setTimeout(() => { options.handler({ razorpay_order_id: options.order_id, razorpay_payment_id: 'pay_bad_sig', razorpay_signature: 'bad_sig', }); }, 50); }; }; }); await page.goto('./'); await page.getByText('Acu (EOV+Cat+Ext+MR)').click(); await page.getByRole('button', { name: /Secure Payment with Razorpay/i }).click(); await expect(page.getByText('Verification Failed')).toBeVisible(); await expect(page.getByText('Invalid Razorpay signature')).toBeVisible(); }); });