@quantaroute/checkout 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -286,6 +286,8 @@ npx expo run:android
286
286
 
287
287
  ## Props
288
288
 
289
+ ### Core props
290
+
289
291
  | Prop | Type | Default | Platform |
290
292
  |---|---|---|---|
291
293
  | `apiKey` | `string` | **required** | all |
@@ -301,6 +303,209 @@ npx expo run:android
301
303
  | `style` | `CSSProperties \| StyleProp<ViewStyle>` | – | all |
302
304
  | `indiaBoundaryUrl` | `string` | – | web only |
303
305
 
306
+ ### Search bar
307
+
308
+ | Prop | Type | Default | Platform |
309
+ |---|---|---|---|
310
+ | `enableSearch` | `boolean` | `true` | all |
311
+
312
+ When `enableSearch` is true, a unified search bar appears above the map. It handles two modes automatically:
313
+
314
+ - **Address search** — calls `/v1/digipin/autocomplete` (debounced 400 ms, location-biased)
315
+ - **DigiPin input** — decoded **100 % offline**, zero API cost, instant map flyTo
316
+
317
+ As India Post promotes DigiPin nationally, buyers can paste their 10-character code and land on their exact ±4 m location.
318
+
319
+ ### Phone OTP + saved addresses (D2C checkout)
320
+
321
+ | Prop | Type | Default | Platform |
322
+ |---|---|---|---|
323
+ | `enablePhoneAuth` | `boolean` | `false` | all |
324
+ | `merchantId` | `string` | – | all |
325
+ | `supabaseFunctionBaseUrl` | `string` | – | all |
326
+
327
+ When `enablePhoneAuth` is true the widget starts with a phone-entry step. Returning buyers can pick a previously saved address from any brand in the network instead of dropping the pin again.
328
+
329
+ **Requires:**
330
+
331
+ 1. A merchant account — sign up at [developers.quantaroute.com/onboarding](https://developers.quantaroute.com/onboarding/merchant) to get your `merchantId` and `supabaseFunctionBaseUrl`.
332
+ 2. The `checkout-otp` and `checkout-addresses` Edge Functions deployed (see [Supabase setup](#supabase-setup)).
333
+
334
+ ```tsx
335
+ <CheckoutWidget
336
+ apiKey={process.env.NEXT_PUBLIC_QUANTAROUTE_KEY!}
337
+ merchantId="your-merchant-uuid"
338
+ supabaseFunctionBaseUrl="https://<project>.supabase.co/functions/v1"
339
+ enablePhoneAuth={true}
340
+ enableSearch={true}
341
+ onComplete={(addr) => console.log(addr)}
342
+ />
343
+ ```
344
+
345
+ ### Email OTP auth (`enableEmailAuth`)
346
+
347
+ | Prop | Type | Default | Platform |
348
+ |---|---|---|---|
349
+ | `enableEmailAuth` | `boolean` | `false` | all |
350
+ | `merchantId` | `string` | – | all |
351
+ | `supabaseFunctionBaseUrl` | `string` | – | all |
352
+
353
+ When `enableEmailAuth` is true the widget starts with an email-entry step. The buyer receives a 6-digit OTP via **Resend**. On success a checkout session is created and saved addresses are shown if available.
354
+
355
+ > If both `enablePhoneAuth` and `enableEmailAuth` are set, phone OTP takes precedence.
356
+
357
+ ```tsx
358
+ <CheckoutWidget
359
+ apiKey={process.env.NEXT_PUBLIC_QUANTAROUTE_KEY!}
360
+ merchantId="your-merchant-uuid"
361
+ supabaseFunctionBaseUrl="https://<project>.supabase.co/functions/v1"
362
+ enableEmailAuth={true}
363
+ onComplete={(addr) => console.log(addr)}
364
+ onCheckoutEvent={(event) => {
365
+ // email-specific events: email_step_viewed | email_otp_sent | email_verified
366
+ console.log('[checkout]', event.type, event);
367
+ }}
368
+ />
369
+ ```
370
+
371
+ **Client-side events emitted during email auth:**
372
+
373
+ | Event type | When |
374
+ |---|---|
375
+ | `email_step_viewed` | Email input screen rendered |
376
+ | `email_otp_sent` | OTP dispatched via Resend |
377
+ | `email_verified` | OTP correct → session created |
378
+
379
+ ---
380
+
381
+ #### Setting up Resend (required for `enableEmailAuth`)
382
+
383
+ 1. **Create a Resend account** at [resend.com](https://resend.com) — free tier covers ~3 000 emails / month.
384
+
385
+ 2. **Verify your sending domain** (e.g. `checkout.yourbrand.com` or `yourbrand.com`):
386
+ - Go to **Resend → Domains → Add domain**
387
+ - Add the DNS records shown (SPF, DKIM, DMARC) to your DNS provider
388
+ - Wait for "Verified" status (usually under 5 minutes with Cloudflare; up to 48 h with others)
389
+
390
+ 3. **Get an API key** — Resend → API Keys → Create. Restrict it to **Send access** only.
391
+
392
+ 4. **Set secrets on the `checkout-email-otp` Edge Function:**
393
+
394
+ In [Supabase Dashboard](https://supabase.com/dashboard) → your project → Edge Functions → Secrets:
395
+
396
+ | Secret | Value |
397
+ |---|---|
398
+ | `RESEND_API_KEY` | `re_...` from Resend |
399
+ | `RESEND_FROM_EMAIL` | A verified sender address, e.g. `checkout@yourbrand.com` |
400
+
401
+ 5. **Deploy the Edge Function** (if not already done):
402
+
403
+ ```bash
404
+ supabase functions deploy checkout-email-otp --project-ref <your-project-ref> --no-verify-jwt
405
+ ```
406
+
407
+ **Required env vars (frontend):**
408
+
409
+ ```env
410
+ NEXT_PUBLIC_QUANTAROUTE_KEY=dp_...
411
+ NEXT_PUBLIC_MERCHANT_ID=your-uuid-from-developer-portal
412
+ NEXT_PUBLIC_SUPABASE_FUNCTION_URL=https://<project-ref>.supabase.co/functions/v1
413
+ ```
414
+
415
+ ---
416
+
417
+ #### Per-merchant email template customisation
418
+
419
+ Set these columns directly on your `checkout.merchants` row (SQL or Supabase Table Editor). All are optional — omitting any falls back to the QuantaRoute default.
420
+
421
+ | Column | Type | Default | Effect |
422
+ |---|---|---|---|
423
+ | `email_from_name` | `text` | `"{brand} Checkout"` | "From" display name in the inbox, e.g. `"Fabindia"` |
424
+ | `email_logo_url` | `text` | *(text brand name)* | HTTPS URL of your logo (PNG/SVG, 180 × 48 px recommended) shown at the top of every OTP email |
425
+ | `email_subject_template` | `text` | `"{code} is your {brand} checkout code"` | Subject line; supports `{code}` and `{brand}` placeholders |
426
+
427
+ **Example SQL:**
428
+
429
+ ```sql
430
+ UPDATE checkout.merchants
431
+ SET
432
+ email_from_name = 'Fabindia',
433
+ email_logo_url = 'https://cdn.fabindia.com/logo-email.png',
434
+ email_subject_template = '{code} — verify your Fabindia order'
435
+ WHERE id = 'your-merchant-uuid';
436
+ ```
437
+
438
+ ---
439
+
440
+ ### Branding
441
+
442
+ | Prop | Type | Default | Platform |
443
+ |---|---|---|---|
444
+ | `brandColor` | `string` (hex) | `#0ea5e9` | all |
445
+ | `logoUrl` | `string` (URL) | – | all |
446
+ | `headerBanner` | `ReactNode` | – | web only |
447
+
448
+ ```tsx
449
+ <CheckoutWidget
450
+ apiKey="..."
451
+ brandColor="#7c3aed"
452
+ logoUrl="https://yourbrand.com/logo.png"
453
+ headerBanner={<p>🚚 Free shipping on orders over ₹499</p>}
454
+ onComplete={...}
455
+ />
456
+ ```
457
+
458
+ ### Analytics (client-side)
459
+
460
+ | Prop | Type | Default | Platform |
461
+ |---|---|---|---|
462
+ | `onCheckoutEvent` | `(event: CheckoutEvent) => void` | – | all |
463
+
464
+ Called on every step transition. Use it to forward events to MoEngage, Clevertap, GTM, or your own backend — no server-side integration required.
465
+
466
+ ```tsx
467
+ <CheckoutWidget
468
+ apiKey="..."
469
+ onCheckoutEvent={(event) => {
470
+ // event.type — e.g. 'phone_verified', 'location_confirmed', 'address_completed'
471
+ // event.step — widget step the user moved INTO
472
+ // event.buyerId — internal buyer UUID (after phone verification)
473
+ // event.digipin — DigiPin of the confirmed location
474
+ // event.timestamp — ISO 8601 timestamp
475
+
476
+ // MoEngage example:
477
+ Moengage.track_event(event.type, {
478
+ step: event.step,
479
+ buyerId: event.buyerId,
480
+ digipin: event.digipin,
481
+ });
482
+
483
+ // GTM example:
484
+ window.dataLayer?.push({ event: event.type, ...event });
485
+ }}
486
+ onComplete={...}
487
+ />
488
+ ```
489
+
490
+ **All event types:**
491
+
492
+ | Event | When fired | Auth channel |
493
+ |---|---|---|
494
+ | `phone_step_viewed` | Phone entry screen rendered | Phone |
495
+ | `otp_sent` | OTP dispatched via MSG91 | Phone |
496
+ | `phone_verified` | SMS OTP correct; session created | Phone |
497
+ | `email_step_viewed` | Email input screen rendered | Email |
498
+ | `email_otp_sent` | OTP dispatched via Resend | Email |
499
+ | `email_verified` | Email OTP correct; session created | Email |
500
+ | `auth_skipped` | Buyer tapped "Skip" | Both |
501
+ | `saved_addresses_viewed` | Saved address list rendered | – |
502
+ | `saved_address_selected` | Buyer chose a saved address (skips map) | – |
503
+ | `map_step_viewed` | Map pin step rendered | – |
504
+ | `location_confirmed` | Buyer tapped "Confirm location" | – |
505
+ | `form_step_viewed` | Address detail form rendered | – |
506
+ | `address_completed` | Full address submitted | – |
507
+ | `widget_reset` | Buyer tapped "Change address" | – |
508
+
304
509
  ---
305
510
 
306
511
  ## `CompleteAddress` output
@@ -389,6 +594,115 @@ function MyCheckout() {
389
594
 
390
595
  ---
391
596
 
597
+ ## Supabase setup
598
+
599
+ > Only needed when using **`enablePhoneAuth`**. Skip this section for the basic map-only widget.
600
+
601
+ ### 1 · Get your merchant credentials
602
+
603
+ Sign up at **[developers.quantaroute.com/onboarding/merchant](https://developers.quantaroute.com/onboarding/merchant)**. The wizard auto-provisions:
604
+
605
+ - `merchant_id` — your UUID in `checkout.merchants`
606
+ - Geocoding API key (for address search and reverse-geocoding)
607
+ - Integration snippet with all values filled in
608
+
609
+ ### 2 · Deploy Edge Functions
610
+
611
+ The Edge Functions live in `supabase/functions/` inside this repo. Deploy all of them:
612
+
613
+ ```bash
614
+ # Address CRUD + COD eligibility
615
+ supabase functions deploy checkout-addresses --project-ref <your-project-ref> --no-verify-jwt
616
+
617
+ # Email OTP auth (Resend — no TRAI dependency, ship now)
618
+ supabase functions deploy checkout-email-otp --project-ref <your-project-ref> --no-verify-jwt
619
+
620
+ # Phone OTP auth (MSG91 + TRAI DLT — deploy now, enable when DLT is ready)
621
+ supabase functions deploy checkout-otp --project-ref <your-project-ref> --no-verify-jwt
622
+
623
+ # Session expiry — fires session.expired webhooks; runs every 10 min via pg_cron
624
+ supabase functions deploy checkout-session-expiry --project-ref <your-project-ref> --no-verify-jwt
625
+ ```
626
+
627
+ ### 3 · Set Edge Function secrets
628
+
629
+ In the [Supabase Dashboard](https://supabase.com/dashboard) → your project → Edge Functions → Secrets:
630
+
631
+ **`checkout-email-otp` (required for email auth):**
632
+
633
+ | Secret | Value |
634
+ |---|---|
635
+ | `RESEND_API_KEY` | `re_...` — from [resend.com](https://resend.com) |
636
+ | `RESEND_FROM_EMAIL` | Verified sender address, e.g. `checkout@yourbrand.com` |
637
+
638
+ **`checkout-otp` (required for phone/SMS auth — set when TRAI DLT is approved):**
639
+
640
+ | Secret | Value |
641
+ |---|---|
642
+ | `MSG91_AUTHKEY` | Your MSG91 AuthKey ([setup guide](./docs/MSG91_INDIA_OTP_SETUP.md)) — must match the Edge Function env var name exactly |
643
+ | `MSG91_TEMPLATE_ID` | Your approved DLT OTP template ID |
644
+
645
+ ### 4 · Add your domain to the allowed list
646
+
647
+ In the developer portal at **[developers.quantaroute.com/dashboard/checkout](https://developers.quantaroute.com/dashboard/checkout)**, add your storefront's domain (e.g. `yourbrand.com`). The `checkout-otp` Edge Function enforces this list to block abuse.
648
+
649
+ ### Minimal Next.js integration
650
+
651
+ #### Email OTP (available now — no TRAI paperwork)
652
+
653
+ ```tsx
654
+ // app/checkout/page.tsx
655
+ 'use client';
656
+ import dynamic from 'next/dynamic';
657
+
658
+ const CheckoutWidget = dynamic(
659
+ () => import('@quantaroute/checkout').then((m) => m.CheckoutWidget),
660
+ { ssr: false }
661
+ );
662
+
663
+ export default function CheckoutPage() {
664
+ return (
665
+ <CheckoutWidget
666
+ apiKey={process.env.NEXT_PUBLIC_QUANTAROUTE_KEY!}
667
+ merchantId={process.env.NEXT_PUBLIC_MERCHANT_ID!}
668
+ supabaseFunctionBaseUrl={process.env.NEXT_PUBLIC_SUPABASE_FUNCTION_URL!}
669
+ enableEmailAuth={true} {/* ← email OTP via Resend */}
670
+ enableSearch={true}
671
+ brandColor="#your-brand-hex"
672
+ logoUrl="https://yourbrand.com/logo.png"
673
+ onComplete={(address) => {
674
+ // address.digipin, address.formattedAddress, address.pincode, ...
675
+ // → pass to your backend / payment gateway
676
+ }}
677
+ onCheckoutEvent={(event) => {
678
+ // email_step_viewed | email_otp_sent | email_verified | address_completed | ...
679
+ console.log('[checkout]', event.type, event);
680
+ }}
681
+ />
682
+ );
683
+ }
684
+ ```
685
+
686
+ #### Phone OTP (enable when TRAI DLT is approved — same widget, one prop swap)
687
+
688
+ ```tsx
689
+ <CheckoutWidget
690
+ ...
691
+ enablePhoneAuth={true} {/* swap enableEmailAuth → enablePhoneAuth when SMS is ready */}
692
+ enableEmailAuth={false}
693
+ />
694
+ ```
695
+
696
+ **Required env vars:**
697
+
698
+ ```env
699
+ NEXT_PUBLIC_QUANTAROUTE_KEY=dp_...
700
+ NEXT_PUBLIC_MERCHANT_ID=your-uuid-from-developer-portal
701
+ NEXT_PUBLIC_SUPABASE_FUNCTION_URL=https://<project-ref>.supabase.co/functions/v1
702
+ ```
703
+
704
+ ---
705
+
392
706
  ## Architecture
393
707
 
394
708
  ```
@@ -451,6 +765,16 @@ The offline DigiPin algorithm is the official India Post implementation:
451
765
 
452
766
  ---
453
767
 
768
+ ## Documentation
769
+
770
+ | Doc | Description |
771
+ |-----|-------------|
772
+ | [Checkout roadmap](./docs/CHECKOUT_ROADMAP.md) | Feature phases and implementation status |
773
+ | [MSG91 OTP setup](./docs/MSG91_INDIA_OTP_SETUP.md) | India SMS / DLT configuration for phone OTP |
774
+ | [Brand teardown](./docs/brand-teardown.md) | Competitive analysis (Shopflo) |
775
+
776
+ ---
777
+
454
778
  ## Development
455
779
 
456
780
  ```bash
@@ -483,6 +807,27 @@ npx expo run:android # requires Android Studio
483
807
 
484
808
  ## Changelog
485
809
 
810
+ ### v1.4.0 (current)
811
+
812
+ - **Email OTP auth** — `enableEmailAuth` prop; `EmailStep` / `EmailOtpStep` components; `checkout-email-otp` Edge Function (Resend); no TRAI dependency
813
+ - **Per-merchant email template** — `email_from_name`, `email_logo_url`, `email_subject_template` columns on `checkout.merchants`; `{code}` / `{brand}` placeholders in subject
814
+ - **COD controls** — `enableCod` prop + `CodAvailabilityBanner`; `cod_blocked_pincodes[]`, `cod_max_cart_value_paise`, `enable_cod` on merchant row; `checkCodEligibility()` in SDK
815
+ - **Server-side webhooks** — `session.phone_verified`, `session.email_verified`, `session.address_completed`, `session.expired`; HMAC-SHA256 signed; 3-retry delivery log in `checkout.webhook_deliveries`
816
+ - **Automatic session expiry** — `checkout-session-expiry` Edge Function; pg_cron job fires every 10 min
817
+ - **New SDK exports** — `sendEmailOtp`, `verifyEmailOtp`, `VerifyEmailOtpResult`, `checkCodEligibility`, `notifyAddressCompleted`
818
+ - **Event types** — added `email_step_viewed`, `email_otp_sent`, `email_verified` to `onCheckoutEvent`
819
+
820
+ ### v1.3.x
821
+
822
+ - **Phone OTP + saved addresses** — `enablePhoneAuth`, `merchantId`, `supabaseFunctionBaseUrl` props; `PhoneStep`, `OtpStep`, `SavedAddressStep` components
823
+ - **Address autocomplete + DigiPin search** — unified search bar via `enableSearch`; "near me" / location-biased 3-pass Nominatim strategy on backend
824
+ - **Pincode validation** — `validatePincode()` in `AddressForm`; fake codes (e.g. `123456`) are blocked with an inline error; non-delivery areas show a warning banner
825
+ - **Per-merchant branding** — `brandColor` (hex), `logoUrl`, `headerBanner`; map pin and all accent elements use the brand colour on web and native
826
+ - **Cross-merchant "Saved at [Brand]" chip** — address cards show the brand name where the address was first saved
827
+ - **Client-side analytics hook** — `onCheckoutEvent` fires on every step transition for MoEngage / GTM / custom backends
828
+ - `CheckoutEvent` and `CheckoutEventType` exported from the package
829
+ - Checkout API client (`checkout-api.ts`) and types exported (`sendOtp`, `verifyOtp`, `listAddresses`, `createAddress`, `deleteAddress`, `setDefaultAddress`)
830
+
486
831
  ### v1.2.0
487
832
 
488
833
  - **iOS & Android support** via `expo-osm-sdk` (MapLibre GL Native)