Back to Platform

Payment Service

services/payment/pod-payment/README.md

0.7.4133.9 KB
Original Source

Payment Service

Provider-agnostic payment service supporting multiple subscription providers (Polar.sh, Stripe, etc.).

Architecture

Client (UI)
    ↓ POST /api/v1/subscriptions/:workspace/subscribe
Payment Service → Accounts Service (caller auth)
    ↓ Create checkout via provider API
Payment Provider (Polar.sh, Stripe, etc.)
    ↓ Webhook → POST /api/v1/webhooks/:provider
Payment Service → Accounts Service (upsert subscription)

Subscription Creation Flow

Step 1: Client Creates Checkout

Request:

POST /api/v1/subscriptions/:workspace/subscribe
Authorization: Bearer {token}

{
  "type": "tier" | "support",
  "plan": "common" | "rare" | "epic" | "legendary",
  "customerEmail"?: string,
  "customerName"?: string
}

Response:

json
{
  "checkoutId": "checkout_abc123",
  "checkoutUrl": "https://checkout.polar.sh/session/abc123"
}

Step 2: Payment

User is redirected to checkoutUrl and completes payment with the provider.

Step 3: Success Redirect

After payment, user is redirected to the success URL with these query parameters:

FRONT_URL/workbench/setting/setting/billing?payment=success&checkout_id={CHECKOUT_ID}

API Endpoints

EndpointMethodPurpose
/api/v1/subscriptions/:workspace/subscribePOSTCreate subscription checkout
/api/v1/subscriptions/:subscriptionIdGETGet subscription details (admin only)
/api/v1/subscriptions/:subscriptionId/cancelPOSTCancel subscription
/api/v1/webhooks/:providerPOSTReceive webhook events from payment provider

Running Locally

To run the payment service locally:

bash
rushx run-local

Make sure you have all required environment variables configured (see Environment Configuration below).

Environment Configuration

Required Variables

VariableDescriptionExample
PORTPort to listen on3040
ACCOUNTS_URLAccounts service URLhttp://huly.local:3000
FRONT_URLFrontend URL for redirectshttps://huly.local:8087

Polar.sh Provider

To enable Polar.sh as the payment provider, set:

VariableDescriptionExample
POLAR_ACCESS_TOKENPolar.sh API access tokenpolar_...
POLAR_WEBHOOK_SECRETWebhook signature secretwhsec_...
POLAR_SUBSCRIPTION_PLANSPlan to product IDs mappingcommon@tier:prod_1a,prod_1b;rare@tier:prod_2;epic@tier:prod_3;legendary@tier:prod_4

Format: {plan}@{type}:{productIds};... where productIds are comma-separated uuids of products in your Polar.sh account.

Webhook endpoint is registered at /api/v1/webhooks/polar.

Stripe Provider

To enable Stripe as the payment provider, set:

VariableDescriptionExample
STRIPE_API_KEYStripe API secret keysk_live_... or sk_test_...
STRIPE_WEBHOOK_SECRETWebhook signing secretwhsec_...
STRIPE_SUBSCRIPTION_PLANSPlan to price ID mappingcommon@tier:price_1a;rare@tier:price_2;epic@tier:price_3;legendary@tier:price_4

Format: {plan}@{type}:{priceId};... where priceId is the Stripe Price ID (e.g., price_1abc123).

Note: Stripe uses Price IDs (not Product IDs like Polar.sh). You can find Price IDs in your Stripe Dashboard under Products.

Webhook endpoint is registered at /api/v1/webhooks/stripe.

Important: Only one provider can be active at a time. If Polar.sh is configured, Stripe will not be initialized. Remove Polar.sh environment variables to use Stripe instead.

Multiple Providers

To support additional providers (Lemonsqueezy, etc.):

  1. Add environment variables for that provider
  2. Create provider implementation extending PaymentProvider interface
  3. Register in PaymentProviderFactory.create()
  4. Provider registers its webhook endpoint via registerWebhookEndpoints()