Points to Coupon Conversion Architecture

Technical deep-dive into the mechanism from point consumption to coupon issuance

coupon issuancepoint consumptionconversion flowdesign
4 min read

About This Article

We replaced the act of "using points" with a mechanism that "consumes points to issue a coupon, then receives a discount with that coupon." This article explains the technical architecture in detail.

Conversion Process Overview

System Integration

Systems Involved in Conversion Process
Frontend (Next.js)

Input UI, result display, copy function

Own Server (API Routes)

Process control, authentication verification, transactions

External APIs

Shopify coupon creation API, POS point operation API

Processing Steps in Detail

Receive Conversion Request

Receive conversion request from customer, verify authentication

Check Point Balance

Get real-time balance via POS API, verify conversion eligibility

Generate Coupon

Create customer-specific coupon via Shopify Admin API

Deduct Points

Deduct specified points from balance via POS API

Save to Metafield

Record issued coupon info in customer's metafield

Return Result

Return coupon code and details to frontend

Step Details

Steps 1-2: Request Reception and Balance Check

Balance Check Flow
Request Received

Customer ID: 12345, Requested points: 500pt, Auth token: xxx

Authentication Verification

Token validity check → OK / Customer ID ownership check → OK

POS Balance Query

API call: GET /customers/12345/points → Balance 1250pt

Conversion Eligibility Check

Balance(1250) >= Requested(500) → OK / Minimum unit: 100pt → OK / Daily limit: 10000pt → OK

Proceed to Step 3

All checks passed

Step 3: Shopify Coupon Generation

Discount type
SettingFixed amount
Discount amount
Setting-500 yen
Usage count
SettingOnce only
Customer restriction
SettingSpecific customer only
Validity period
Setting1 year
Minimum purchase
SettingNone

Shopify Admin API call: Endpoint priceRules + discountCodes

Generated code example: 70934-AB12CD-500

Response: priceRuleId, discountCodeId, code

Step 4: POS Point Deduction

Endpoint
ValuePOST /customers/12345/points/deduct
Request amount
Value500
Request reason
ValueCoupon conversion
Request reference
ValueCOUPON-70934-AB12CD-500
Response success
Valuetrue
Response previous_balance
Value1250
Response deducted
Value500
Response new_balance
Value750

Important: Record reference information to track conversions

Step 5: Metafield Storage

Storage location: Customer's Shopify metafield (namespace: loyalty, key: coupons)

code
Example70934-AB12CD-500
amount
Example500
points_used
Example500
created_at
Example2024-01-15T10:30:00Z
expires_at
Example2025-01-15T23:59:59Z
status
Exampleactive
shopify_price_rule_id
Example12345678

Uses: My page list display, usage tracking, cancellation reference

Transaction Design

Why Order Matters

Ideal order: 1. Create coupon → 2. Deduct points → 3. Save record

1st
ProcessCoupon creation
ReasonReversible. Don't deduct points if creation fails
2nd
ProcessPoint deduction
ReasonDeduct after coupon exists. Delete coupon if deduction fails
3rd
ProcessRecord storage
ReasonRecord after all success. Actuals are complete even if recording fails

Rollback on Error

Coupon creation fails
ResponseDo nothing (nothing changed yet)
ResultDisplay error message
Point deduction fails
ResponseDelete created coupon
ResultDisplay error message
Metafield save fails
ResponseLog warning (don't rollback)
ResultTreat as success, notify admin

Note: Metafield save is "nice to have." If actuals (coupon and points) are correct, operations can cover it

API Call Implementation Points

Retry Strategy

Shopify coupon creation
RetryUp to 3 times
ReasonPossible temporary error
POS point deduction
RetryNo retry
ReasonPrevent double deduction
Metafield save
RetryUp to 3 times
ReasonLow impact if fails

Timeout Settings

Shopify coupon creation
Timeout10 seconds
ReasonShopify API is stable
POS point deduction
Timeout15 seconds
ReasonSome POS systems can be slow
Metafield save
Timeout5 seconds
ReasonShopify API is stable

Performance Considerations

Processing Time Breakdown

Authentication verification
Duration~100ms
POS balance query
Duration~500ms
Shopify coupon creation
Duration~800ms
POS point deduction
Duration~600ms
Metafield save
Duration~300ms
Total
Duration~2.3 seconds

UX response: Show "Processing..." to customer with loading animation to improve perceived speed

Benefits of This Design

Reliability

  • Optimized processing order minimizes error impact
  • Rollback strategy prevents inconsistencies
  • All processes logged for traceability

Maintainability

  • Each step is independent and testable
  • Easy to identify error causes
  • Easily accommodates future features (point rate changes, etc.)

Related Topics