post_subscription_buy

POST /subscriptions/buy

Purchase Subscription

Initiate a subscription purchase for the authenticated user's organization. For paid subscriptions, creates a Stripe checkout session for payment processing. For free subscriptions, immediately activates the subscription without payment processing. Allows subscription changes by automatically handling cancellation of existing subscriptions.

Parameters

Name
In
Type
Required
Description

subscriptionPeriodId

body

string

true

Unique identifier for the subscription period

TypeScript Client Library

public buySubscription = async (data: BuySubscriptionRequest): Promise<BuySubscriptionResponse> => {
  return this.makeRequest<BuySubscriptionResponse>('subscriptions/buy', 'POST', data);
};

Code Samples

curl -X POST https://backend.flashback.tech/subscriptions/buy \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer {access-token}' \
  -H 'Content-Type: application/json' \
  -d '{
    "subscriptionPeriodId": "123e4567-e89b-12d3-a456-426614174000"
  }'

Example responses

200 Response (Paid Subscription)

{
  "success": true,
  "checkoutUrl": "https://checkout.stripe.com/c/pay/cs_test_1234567890abcdef",
  "sessionId": "cs_test_1234567890abcdef",
  "isSubscriptionChange": false,
  "previousSubscription": null,
  "isFreeSubscription": false
}

200 Response (Free Subscription)

{
  "success": true,
  "checkoutUrl": null,
  "sessionId": "free_sub_1234567890abcdef_1642248600000",
  "isSubscriptionChange": false,
  "previousSubscription": null,
  "isFreeSubscription": true
}

200 Response (Subscription Change)

{
  "success": true,
  "checkoutUrl": "https://checkout.stripe.com/c/pay/cs_test_1234567890abcdef",
  "sessionId": "cs_test_1234567890abcdef",
  "isSubscriptionChange": true,
  "previousSubscription": {
    "id": "sub_old1234567890abcdef",
    "name": "Basic Plan"
  },
  "isFreeSubscription": false
}

400 Response (No Organization)

{
  "success": false,
  "error_code": "NO_ORGANIZATION",
  "message": "User must belong to an organization"
}

400 Response (Stripe ID Missing)

{
  "success": false,
  "error_code": "STRIPE_ID_MISSING",
  "message": "Subscription period is not configured for payments"
}

400 Response (Stripe Price Invalid)

{
  "success": false,
  "error_code": "STRIPE_PRICE_INVALID",
  "message": "Invalid Stripe price configuration"
}

404 Response

{
  "success": false,
  "error_code": "USER_NOT_FOUND",
  "message": "User not found"
}

404 Response (Subscription Not Found)

{
  "success": false,
  "error_code": "SUBSCRIPTION_PERIOD_NOT_FOUND",
  "message": "Subscription period not found or not active"
}

404 Response (Subscription Not Active)

{
  "success": false,
  "error_code": "SUBSCRIPTION_NOT_ACTIVE",
  "message": "Parent subscription is not active"
}

404 Response (Organization Not Found)

{
  "success": false,
  "error_code": "ORG_NOT_FOUND",
  "message": "Organization not found"
}

403 Response (Not Authorized)

{
  "success": false,
  "error_code": "NOT_AUTHORIZED",
  "message": "User does not have permission to buy subscriptions"
}

409 Response (Already Active)

{
  "success": false,
  "error_code": "SUBSCRIPTION_ALREADY_ACTIVE",
  "message": "You already have an active Professional Plan subscription"
}

409 Response (Payment In Progress)

{
  "success": false,
  "error_code": "PAYMENT_IN_PROGRESS",
  "message": "A payment is already in progress. Please complete or cancel the current payment before starting a new one."
}

500 Response

{
  "success": false,
  "error_code": "INTERNAL_ERROR",
  "message": "Failed to process subscription purchase"
}

Responses

Status
Meaning
Description
Schema

200

Subscription purchase initiated or completed

Inline

400

Invalid request or configuration

Inline

403

User lacks permission for billing operations

Inline

404

User, organization, or subscription not found

Inline

409

Active subscription exists or payment in progress

Inline

500

Server error

Inline

Response Schema

Status Code 200

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Indicates if the request was successful

» checkoutUrl

string

false

none

Stripe hosted checkout URL for redirect (null for free subscriptions)

» sessionId

string

false

none

Stripe checkout session identifier or free payment ID

» isSubscriptionChange

boolean

false

none

Indicates if this is changing from an existing subscription

» previousSubscription

object

false

none

Details of the previous subscription (null if not a change)

»» id

string

false

none

Previous subscription identifier

»» name

string

false

none

Previous subscription name

» isFreeSubscription

boolean

false

none

Indicates if this is a free subscription (no payment required)

Status Code 400

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Will be false for error responses

» error_code

string

false

none

Machine-readable error code (NO_ORGANIZATION, STRIPE_ID_MISSING, STRIPE_PRICE_INVALID)

» message

string

false

none

Human-readable error message

Status Code 403

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Will be false for error responses

» error_code

string

false

none

Machine-readable error code (NOT_AUTHORIZED)

» message

string

false

none

Human-readable error message

Status Code 404

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Will be false for error responses

» error_code

string

false

none

Machine-readable error code (USER_NOT_FOUND, SUBSCRIPTION_PERIOD_NOT_FOUND, SUBSCRIPTION_NOT_ACTIVE, ORG_NOT_FOUND)

» message

string

false

none

Human-readable error message

Status Code 409

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Will be false for error responses

» error_code

string

false

none

Machine-readable error code (SUBSCRIPTION_ALREADY_ACTIVE, PAYMENT_IN_PROGRESS)

» message

string

false

none

Human-readable error message

Status Code 500

Name
Type
Required
Restrictions
Description

» success

boolean

false

none

Will be false for error responses

» error_code

string

false

none

Machine-readable error code

» message

string

false

none

Human-readable error message

Last updated

Was this helpful?