Normalize Ethiopian phone inputs for user lookup, accept web Google client IDs for ID token login, and update the checkout integration guide for OTP and social sign-in. Co-authored-by: Cursor <cursoragent@cursor.com>
634 lines
17 KiB
Markdown
634 lines
17 KiB
Markdown
# Web Checkout — Client Integration Guide
|
|
|
|
Minimal spec for a **standalone web checkout** used by iOS users (and anyone who pays outside the mobile app). The page sells subscriptions via **Chapa hosted checkout** and activates the same `user_subscriptions` rows the mobile apps use.
|
|
|
|
> **Scope:** Hosted Chapa checkout only (`POST /api/v1/subscriptions/checkout`). Do **not** use Chapa Direct Charge (`/payments/chapa/direct`) on web unless you build a custom in-page wallet flow — hosted checkout is simpler and sufficient.
|
|
|
|
---
|
|
|
|
## Table of contents
|
|
|
|
1. [Goals](#goals)
|
|
2. [iOS paywall: email/SMS checkout link](#ios-paywall-emailsms-checkout-link)
|
|
3. [Pages & user flow](#pages--user-flow)
|
|
4. [Environment](#environment)
|
|
5. [API base & auth](#api-base--auth)
|
|
6. [Screen specs](#screen-specs)
|
|
7. [API reference](#api-reference)
|
|
8. [State machine](#state-machine)
|
|
9. [Success & return URLs](#success--return-urls)
|
|
10. [Error handling](#error-handling)
|
|
11. [Security notes](#security-notes)
|
|
12. [Optional enhancements](#optional-enhancements)
|
|
13. [Integration checklist](#integration-checklist)
|
|
14. [Quick reference](#quick-reference)
|
|
|
|
---
|
|
|
|
## Goals
|
|
|
|
| Goal | Detail |
|
|
|------|--------|
|
|
| iOS-friendly acquisition | Subscriptions bought on web; iOS app is player-only |
|
|
| Reuse existing backend | No new payment APIs required |
|
|
| Same entitlements | After payment, `GET /subscriptions/me` unlocks content in iOS/Android |
|
|
| Minimal surface | 4 routes: plans → login → checkout → success |
|
|
|
|
---
|
|
|
|
## iOS paywall: email/SMS checkout link
|
|
|
|
When the iOS app shows **Subscription required** (locked content), it should **not** open the web checkout in-app. Instead, call the backend to **email or SMS the checkout link** to the learner.
|
|
|
|
### Mobile flow
|
|
|
|
```mermaid
|
|
sequenceDiagram
|
|
participant iOS as iOS app
|
|
participant API as Yimaru API
|
|
participant User as Email / SMS
|
|
participant Web as Web checkout app
|
|
|
|
iOS->>API: Content blocked (no subscription)
|
|
iOS->>iOS: Show "Subscription required" + optional "Send link"
|
|
iOS->>API: POST /subscriptions/request-checkout-link
|
|
API->>User: Checkout URL (email or SMS)
|
|
User->>Web: Opens link in browser
|
|
Web->>API: POST /subscriptions/checkout → Chapa
|
|
User->>iOS: Opens app, signs in
|
|
iOS->>API: GET /subscriptions/me
|
|
```
|
|
|
|
### API
|
|
|
|
```http
|
|
POST /api/v1/subscriptions/request-checkout-link
|
|
Authorization: Bearer <learner_token>
|
|
Content-Type: application/json
|
|
|
|
{
|
|
"plan_id": 1
|
|
}
|
|
```
|
|
|
|
Or filter by category when plan id is unknown:
|
|
|
|
```json
|
|
{ "category": "LEARN_ENGLISH" }
|
|
```
|
|
|
|
Both fields are optional; omit both for a generic `/subscribe` link.
|
|
|
|
**Response:**
|
|
|
|
| `message` (by channel) | |
|
|
|------------------------|---|
|
|
| Email | `Please check your email for the address` |
|
|
| SMS | `Please check your SMS for the address` |
|
|
|
|
**Response `data`:**
|
|
|
|
```json
|
|
{
|
|
"channel": "sms",
|
|
"destination_hint": "***5678"
|
|
}
|
|
```
|
|
|
|
| Field | Meaning |
|
|
|-------|---------|
|
|
| `channel` | `email` or `sms` (registration contact) |
|
|
| `destination_hint` | Masked address; full link is only in the message |
|
|
|
|
### Delivery channel rules
|
|
|
|
1. Verified phone → **SMS**
|
|
2. Else verified email → **email**
|
|
3. Else phone only on file → **SMS**
|
|
4. Else email only on file → **email**
|
|
5. Both on file, neither verified → **SMS** if phone present, else email
|
|
|
|
### Rate limit
|
|
|
|
One link per **15 minutes** per user (`429` if called too soon).
|
|
|
|
### Backend env
|
|
|
|
| Variable | Example |
|
|
|----------|---------|
|
|
| `LEARNER_WEB_CHECKOUT_BASE_URL` | `https://subscribe.yimaruacademy.com` |
|
|
|
|
Links are built as:
|
|
|
|
- `{base}/subscribe/checkout?plan_id={id}` when `plan_id` is sent
|
|
- `{base}/subscribe?category=IELTS` when `category` is sent
|
|
- `{base}/subscribe` otherwise
|
|
|
|
### iOS UI guidance
|
|
|
|
- Paywall: **“Subscription required”**
|
|
- Primary action: **“Email/SMS me a link”** → calls this API once
|
|
- Success in app: show the API `message` (e.g. **“Please check your SMS for the address”**)
|
|
- Do **not** display the checkout URL in the app UI
|
|
- After user pays on web, **Refresh** / reopen app → `GET /subscriptions/me`
|
|
|
|
### Errors
|
|
|
|
| HTTP | Meaning |
|
|
|------|---------|
|
|
| `409` | Already subscribed for that plan category |
|
|
| `429` | Link sent within the last 15 minutes |
|
|
| `503` | `LEARNER_WEB_CHECKOUT_BASE_URL` not configured |
|
|
| `400` | No email/phone on account |
|
|
|
|
---
|
|
|
|
## Pages & user flow
|
|
|
|
```mermaid
|
|
flowchart TD
|
|
A["/subscribe"] --> B{Logged in?}
|
|
B -->|No| C["/subscribe/login"]
|
|
C --> D["POST /auth/customer-login"]
|
|
D --> A
|
|
B -->|Yes| E["Pick plan"]
|
|
E --> F["/subscribe/checkout?plan_id="]
|
|
F --> G["POST /subscriptions/checkout"]
|
|
G --> H["Redirect to Chapa payment_url"]
|
|
H --> I["Chapa payment"]
|
|
I --> J["Backend callback + success page"]
|
|
J --> K["/subscribe/success?session_id="]
|
|
K --> L["GET /payments/verify/:session_id"]
|
|
L --> M["GET /subscriptions/me"]
|
|
M --> N["Open mobile app CTA"]
|
|
```
|
|
|
|
**Happy path (returning user):**
|
|
|
|
1. Land on plan list → pick plan
|
|
2. Login if needed
|
|
3. Confirm phone + email → **Pay with Chapa**
|
|
4. Complete payment on Chapa
|
|
5. Success page confirms subscription → prompt to open iOS app
|
|
|
|
---
|
|
|
|
## Environment
|
|
|
|
| Variable | Example | Purpose |
|
|
|----------|---------|---------|
|
|
| `VITE_API_BASE_URL` | `https://api.yimaruacademy.com/api/v1` | Yimaru API |
|
|
| `LEARNER_WEB_CHECKOUT_BASE_URL` (backend) | `https://subscribe.yimaruacademy.com` | Base URL embedded in email/SMS links |
|
|
| `VITE_APP_STORE_URL` | `https://apps.apple.com/...` | Optional iOS download link on success |
|
|
| `VITE_PLAY_STORE_URL` | `https://play.google.com/...` | Optional Android link |
|
|
|
|
Production API base: `https://api.yimaruacademy.com/api/v1`
|
|
|
|
---
|
|
|
|
## API base & auth
|
|
|
|
All authenticated calls:
|
|
|
|
```http
|
|
Authorization: Bearer <access_token>
|
|
Content-Type: application/json
|
|
```
|
|
|
|
Store tokens in `sessionStorage` (or httpOnly cookie if you add a BFF later):
|
|
|
|
```typescript
|
|
type AuthSession = {
|
|
access_token: string;
|
|
refresh_token: string;
|
|
user_id: number;
|
|
role: string;
|
|
};
|
|
```
|
|
|
|
Refresh when access token expires:
|
|
|
|
```http
|
|
POST /api/v1/auth/refresh
|
|
{ "refresh_token": "<refresh_token>" }
|
|
```
|
|
|
|
---
|
|
|
|
## Screen specs
|
|
|
|
### 1. `/subscribe` — Plan list
|
|
|
|
**Purpose:** Show purchasable plans (public; no auth required to view).
|
|
|
|
**API:**
|
|
|
|
```http
|
|
GET /api/v1/subscription-plans?active_only=true
|
|
GET /api/v1/subscription-plans?active_only=true&category=LEARN_ENGLISH
|
|
```
|
|
|
|
**Query params:**
|
|
|
|
| Param | Values | Default |
|
|
|-------|--------|---------|
|
|
| `active_only` | `true` / `false` | `true` |
|
|
| `category` | `LEARN_ENGLISH`, `IELTS`, `DUOLINGO` | all |
|
|
|
|
**Plan card fields** (from `data[]`):
|
|
|
|
| Field | UI use |
|
|
|-------|--------|
|
|
| `id` | Pass to checkout |
|
|
| `name` | Title |
|
|
| `description` | Subtitle |
|
|
| `category` | Tab/filter |
|
|
| `price`, `currency` | Price display |
|
|
| `duration_value`, `duration_unit` | Billing period (hide if `is_lifetime`) |
|
|
| `is_lifetime` | Show “One-time purchase” |
|
|
| `is_active` | Only listed when `active_only=true` |
|
|
|
|
**Actions:**
|
|
|
|
- **Subscribe** → if no session, `/subscribe/login?redirect=/subscribe/checkout?plan_id={id}`; else `/subscribe/checkout?plan_id={id}`
|
|
- Optional: **Already subscribed?** → login link
|
|
|
|
**Do not** embed Chapa or payment widgets on this page.
|
|
|
|
---
|
|
|
|
### 2. `/subscribe/login` — Sign in
|
|
|
|
**Purpose:** Authenticate before checkout. Registration is **not** offered on web — learners create accounts in the mobile app first.
|
|
|
|
#### Email + password
|
|
|
|
```http
|
|
POST /api/v1/auth/customer-login
|
|
```
|
|
|
|
```json
|
|
{
|
|
"email": "learner@example.com",
|
|
"password": "your-password"
|
|
}
|
|
```
|
|
|
|
#### Google / Apple sign-in
|
|
|
|
Same as the mobile app — exchange the provider identity token for Yimaru JWTs.
|
|
|
|
**Google (web ID token):**
|
|
|
|
```http
|
|
POST /api/v1/auth/google/android
|
|
```
|
|
|
|
```json
|
|
{
|
|
"id_token": "<google-credential-jwt>"
|
|
}
|
|
```
|
|
|
|
**Apple (identity token):**
|
|
|
|
```http
|
|
POST /api/v1/auth/apple
|
|
```
|
|
|
|
```json
|
|
{
|
|
"id_token": "<apple-identity-jwt>",
|
|
"email": "optional-on-first-sign-in",
|
|
"first_name": "optional",
|
|
"last_name": "optional"
|
|
}
|
|
```
|
|
|
|
**Web checkout env (frontend):**
|
|
|
|
| Variable | Backend counterpart |
|
|
|----------|---------------------|
|
|
| `VITE_GOOGLE_CLIENT_ID` | `GOOGLE_OAUTH_WEB_CLIENT_ID` (or comma-separated in `GOOGLE_OAUTH_CLIENT_ID`) |
|
|
| `VITE_APPLE_CLIENT_ID` | include Services ID in `APPLE_SIGN_IN_CLIENT_IDS` |
|
|
| `VITE_APPLE_REDIRECT_URI` | must match Apple Developer redirect URL (e.g. `https://subscribe.yimaruacademy.com/subscribe/login`) |
|
|
|
|
Buttons are hidden when the corresponding `VITE_*` values are unset.
|
|
|
|
#### Phone + OTP (same flow as the mobile app)
|
|
|
|
**Step 1 — send code**
|
|
|
|
```http
|
|
POST /api/v1/user/sendResetCode
|
|
```
|
|
|
|
```json
|
|
{
|
|
"phone_number": "0912345678"
|
|
}
|
|
```
|
|
|
|
Sends a 6-digit SMS code to an **existing** learner account.
|
|
|
|
**Phone formats:** all of these are accepted and normalized server-side:
|
|
|
|
| Input | Normalized |
|
|
|-------|------------|
|
|
| `0912345678` | `251912345678` |
|
|
| `+251912345678` | `251912345678` |
|
|
| `251912345678` | `251912345678` |
|
|
|
|
**Step 2 — verify code**
|
|
|
|
Either:
|
|
|
|
```http
|
|
POST /api/v1/user/verify-otp
|
|
```
|
|
|
|
```json
|
|
{
|
|
"phone_number": "0912345678",
|
|
"otp": "123456"
|
|
}
|
|
```
|
|
|
|
Or the equivalent login endpoint:
|
|
|
|
```http
|
|
POST /api/v1/auth/customer-login
|
|
```
|
|
|
|
```json
|
|
{
|
|
"phone_number": "0912345678",
|
|
"otp_code": "123456"
|
|
}
|
|
```
|
|
|
|
**Resend:** `POST /api/v1/user/resend-otp` with `{ "phone_number" }` (requires an active OTP row; otherwise call `sendResetCode` again).
|
|
|
|
**Response `data` (both verify paths):**
|
|
|
|
```json
|
|
{
|
|
"access_token": "...",
|
|
"refresh_token": "...",
|
|
"role": "STUDENT",
|
|
"user_id": 42
|
|
}
|
|
```
|
|
|
|
**After login:** redirect to `redirect` query param or `/subscribe`.
|
|
|
|
---
|
|
|
|
### 3. `/subscribe/checkout` — Confirm & pay
|
|
|
|
**Purpose:** Collect billing contact info and start Chapa hosted checkout.
|
|
|
|
**Query:** `plan_id` (required)
|
|
|
|
**Preload:**
|
|
|
|
```http
|
|
GET /api/v1/subscription-plans/{plan_id}
|
|
GET /api/v1/subscriptions/me # warn if already active in same category
|
|
```
|
|
|
|
**Form fields:**
|
|
|
|
| Field | Source | Required |
|
|
|-------|--------|----------|
|
|
| Plan summary | plan API | read-only |
|
|
| Phone | user profile or input | yes |
|
|
| Email | user profile or input | yes |
|
|
|
|
**Pay button** → single API call:
|
|
|
|
```http
|
|
POST /api/v1/subscriptions/checkout
|
|
Authorization: Bearer <token>
|
|
```
|
|
|
|
```json
|
|
{
|
|
"plan_id": 1,
|
|
"phone": "0912345678",
|
|
"email": "learner@example.com",
|
|
"provider": "CHAPA"
|
|
}
|
|
```
|
|
|
|
**Equivalent alias:** `POST /api/v1/payments/subscribe` (same handler logic).
|
|
|
|
**Success response `data`:**
|
|
|
|
```json
|
|
{
|
|
"payment_id": 100,
|
|
"session_id": "yimaru-tx-abc123",
|
|
"payment_url": "https://checkout.chapa.co/...",
|
|
"amount": 499.0,
|
|
"currency": "ETB",
|
|
"expires_at": "2026-08-24T12:00:00Z"
|
|
}
|
|
```
|
|
|
|
**Client action:** persist `session_id` (e.g. `sessionStorage.checkout_session_id`), then:
|
|
|
|
```javascript
|
|
window.location.href = data.payment_url;
|
|
```
|
|
|
|
Do **not** open Chapa in an iframe; full redirect only.
|
|
|
|
---
|
|
|
|
### 4. `/subscribe/success` — Confirmation
|
|
|
|
**Purpose:** Confirm payment and tell user to open the mobile app.
|
|
|
|
**Entry points:**
|
|
|
|
- Your page after user returns from Chapa (configure marketing site URL in success copy), **or**
|
|
- Link from backend HTML success page (`GET /api/v1/payments/chapa/success?tx_ref=...`)
|
|
|
|
**Verify payment:**
|
|
|
|
```http
|
|
GET /api/v1/payments/verify/{session_id}
|
|
Authorization: Bearer <token>
|
|
```
|
|
|
|
Use `session_id` from checkout response (stored before redirect) or `tx_ref` from Chapa redirect query.
|
|
|
|
**Response `data.status` values:**
|
|
|
|
| Status | UI |
|
|
|--------|-----|
|
|
| `SUCCESS` | ✅ Subscription active |
|
|
| `PENDING` / `PROCESSING` | ⏳ Poll again (see below) |
|
|
| `FAILED` / `CANCELLED` / `EXPIRED` | ❌ Retry checkout |
|
|
|
|
**Confirm entitlement:**
|
|
|
|
```http
|
|
GET /api/v1/subscriptions/me
|
|
```
|
|
|
|
Expect an `ACTIVE` subscription with matching `category`.
|
|
|
|
**Success UI copy (suggested):**
|
|
|
|
> Your subscription is active.
|
|
> Open **Yimaru Academy** on your iPhone and sign in with this account.
|
|
|
|
Optional buttons:
|
|
|
|
- **Open app** — custom URL scheme / Universal Link (`yimaru://` or `https://app.yimaru.com/open`) if configured
|
|
- **Download on App Store** — marketing link only; not a payment link
|
|
|
|
---
|
|
|
|
## API reference
|
|
|
|
| Step | Method | Path | Auth |
|
|
|------|--------|------|------|
|
|
| List plans | `GET` | `/subscription-plans` | No |
|
|
| Get plan | `GET` | `/subscription-plans/:id` | No |
|
|
| Login (email) | `POST` | `/auth/customer-login` | No |
|
|
| Send phone login OTP | `POST` | `/user/sendResetCode` | No |
|
|
| Verify phone OTP | `POST` | `/user/verify-otp` | No |
|
|
| Resend OTP | `POST` | `/user/resend-otp` | No |
|
|
| Refresh token | `POST` | `/auth/refresh` | No |
|
|
| Request checkout link (iOS paywall) | `POST` | `/subscriptions/request-checkout-link` | Yes |
|
|
| My subscriptions | `GET` | `/subscriptions/me` | Yes |
|
|
| Start checkout | `POST` | `/subscriptions/checkout` | Yes |
|
|
| Verify payment | `GET` | `/payments/verify/:session_id` | Yes |
|
|
| Chapa methods (optional) | `GET` | `/payments/methods` | No |
|
|
|
|
**Provider value:** `"CHAPA"` (also accepts `"ARIFPAY"` but use Chapa for this spec).
|
|
|
|
---
|
|
|
|
## State machine
|
|
|
|
```
|
|
IDLE → CHECKOUT_INITIATED → REDIRECTED_TO_CHAPA → VERIFYING → ACTIVE | FAILED
|
|
```
|
|
|
|
| State | Trigger | Next |
|
|
|-------|---------|------|
|
|
| `CHECKOUT_INITIATED` | `POST /subscriptions/checkout` OK | Redirect to `payment_url` |
|
|
| `REDIRECTED_TO_CHAPA` | User completes or cancels on Chapa | Land on success/cancel page |
|
|
| `VERIFYING` | `GET /payments/verify/:session_id` | Poll if `PENDING` |
|
|
| `ACTIVE` | `status === SUCCESS` && `/subscriptions/me` has ACTIVE | Show success |
|
|
| `FAILED` | terminal payment status | Offer retry → checkout |
|
|
|
|
**Polling (when `PENDING` / `PROCESSING`):**
|
|
|
|
- Interval: 3s → 5s → 8s (max ~60s)
|
|
- Same verify endpoint
|
|
- Backend also processes `GET /payments/chapa/callback` and webhooks asynchronously
|
|
|
|
---
|
|
|
|
## Success & return URLs
|
|
|
|
Chapa redirect targets are configured **server-side** when the payment session is created (Chapa `return_url` / `callback_url` → API routes):
|
|
|
|
| Backend route | Role |
|
|
|---------------|------|
|
|
| `GET /api/v1/payments/chapa/callback` | Server verifies payment (no UI) |
|
|
| `GET /api/v1/payments/chapa/success` | HTML success page (optional fallback) |
|
|
| `GET /payment/success` | Alias for Chapa success page |
|
|
|
|
Your marketing site can host `/subscribe/success` for branded UX; pass through `session_id` or read `tx_ref` from query and call verify API.
|
|
|
|
---
|
|
|
|
## Error handling
|
|
|
|
| HTTP | Meaning | UI |
|
|
|------|---------|-----|
|
|
| `401` | Token expired / missing | Redirect to login |
|
|
| `409` | User already has active subscription in that category | Show “Already subscribed” + link to app |
|
|
| `400` | Invalid plan, inactive plan, validation | Show `error` message |
|
|
| `503` | Chapa not configured | “Payments temporarily unavailable” |
|
|
|
|
**Checkout 409 example** — user already subscribed to Learn English; offer extend via support or pick another category (IELTS / Duolingo).
|
|
|
|
---
|
|
|
|
## Security notes
|
|
|
|
- Never expose Chapa secret keys in the browser; only use learner JWT + public API.
|
|
- Store tokens in `sessionStorage`, not localStorage, if possible; clear on logout.
|
|
- Use HTTPS only in production.
|
|
- Do not pass access tokens in URL query strings.
|
|
- The iOS app must **not** link to this checkout URL (App Store compliance).
|
|
|
|
---
|
|
|
|
## Optional enhancements
|
|
|
|
| Feature | Effort | Notes |
|
|
|---------|--------|-------|
|
|
| Pre-fill email/phone from `GET /user/profile` | Low | Better UX on checkout form |
|
|
| Category deep links | Low | `/subscribe?category=IELTS` |
|
|
| Cancel page | Low | `/subscribe/cancelled` when user backs out of Chapa |
|
|
| Universal Link back to iOS app | Medium | Apple associated domains |
|
|
| BFF with httpOnly cookies | Medium | Hides tokens from JS |
|
|
|
|
---
|
|
|
|
## Integration checklist
|
|
|
|
- [ ] Plan list loads from `GET /subscription-plans`
|
|
- [ ] Login/register stores `access_token` + `refresh_token`
|
|
- [ ] Checkout calls `POST /subscriptions/checkout` with `provider: "CHAPA"`
|
|
- [ ] `session_id` saved before redirect to `payment_url`
|
|
- [ ] Success page calls `GET /payments/verify/:session_id`
|
|
- [ ] Poll while `PENDING`; confirm with `GET /subscriptions/me`
|
|
- [ ] iOS app has **no** payment UI; only entitlement check
|
|
- [ ] Error states: 401 → login, 409 → already subscribed
|
|
- [ ] Production `VITE_API_BASE_URL` points to live API
|
|
|
|
---
|
|
|
|
## Quick reference
|
|
|
|
**Start checkout:**
|
|
|
|
```bash
|
|
curl -s -X POST "$API/api/v1/subscriptions/checkout" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"plan_id": 1,
|
|
"phone": "0912345678",
|
|
"email": "learner@example.com",
|
|
"provider": "CHAPA"
|
|
}'
|
|
```
|
|
|
|
**Verify after return:**
|
|
|
|
```bash
|
|
curl -s "$API/api/v1/payments/verify/$SESSION_ID" \
|
|
-H "Authorization: Bearer $TOKEN"
|
|
```
|
|
|
|
**Confirm access:**
|
|
|
|
```bash
|
|
curl -s "$API/api/v1/subscriptions/me" \
|
|
-H "Authorization: Bearer $TOKEN"
|
|
```
|
|
|
|
---
|
|
|
|
*Aligns with Yimaru API `production` branch — Chapa hosted checkout via `SubscribeWithPayment` / `InitiateSubscriptionPayment`.*
|