Skip to Content
Docs

Create a subscription

Start recurring billing from your server with a recurring price from your catalog and a customer's saved payment method.

Create a subscription from your server when you already have the customer's saved payment method. The subscription bills a recurring price from your catalog, on the price's schedule. To collect the payment method and start the subscription in one step, use checkout with mode set to subscription instead. This guide runs the subscription on Pay.com's engine. To run subscriptions on another engine, see Choose a subscription engine.

Requirements

Before you begin, make sure you have the following set up:

  • A payment method saved for off-session use (pm_card_...), with the customer it belongs to. See Save a payment method.
  • A product for what you sell, from POST /v1/products or the Commerce tab. See Products and prices.
  • Your API keys. The examples use the sandbox (https://api.sandbox.pay.com/v1) with an sk-pay-com-sandbox- key.

Set up the subscription

Create a recurring price for the product, then create a subscription that bills it to the customer's saved payment method.

  1. 1

    Create a recurring price

    A price is an amount for a product. A recurring price adds a schedule, so it defines how much to charge, in which currency, and how often. You create it once and use it for as many customers as you need.

    Send a POST request to /v1/prices with the following parameters:

    ParameterDescription
    productThe product (prod_...) the price belongs to.
    currencyThree-letter ISO currency code in lowercase (for example, usd).
    unit_amountThe amount for each period, in the smallest currency unit. For example, 1999 equals $19.99 USD.
    scheduleObject with interval (day, week, month, or year) and interval_count (number of intervals between charges).
    tax_behaviorOptional. exclusive adds tax to unit_amount, and inclusive includes it.
    referenceOptional. Your ID for the price, unique for its product.
    curl https://api.sandbox.pay.com/v1/prices \
      -u ':sk-pay-com-sandbox-…' \
      -H "Content-Type: application/json" \
      -H "idempotency-key: $(uuidgen)" \
      -d '{
        "product": "prod_691180236711641088",
        "currency": "usd",
        "unit_amount": 1999,
        "schedule": { "interval": "month", "interval_count": 1 },
        "reference": "premium-monthly"
      }'

    Store the id from the response (prc_...). You'll use it when creating subscriptions in the next step. A later change to the amount adds a version of the price, and live subscriptions keep theirs. See Prices are kept as versions.

  2. 2

    Create the subscription

    A subscription links a customer and their saved payment method to the prices it bills. Once active, Pay.com charges the customer on the prices' schedule.

    Send a POST request to /v1/subscriptions with the following parameters:

    ParameterDescription
    customer_reference_idYour internal customer identifier.
    itemsWhat the subscription bills, each with price (prc_...) and quantity.
    payment_methodOptional. The saved payment method ID (pm_card_...). Defaults to the customer's default payment method.
    subscription_optionsOptional. The engine, a trial, your reference for the subscription, and how it starts. See Subscription options.
    curl https://api.sandbox.pay.com/v1/subscriptions \
      -u ':sk-pay-com-sandbox-…' \
      -H "Content-Type: application/json" \
      -H "idempotency-key: $(uuidgen)" \
      -d '{
        "customer_reference_id": "cust-jane-doe-001",
        "items": [{ "price": "prc_691180240037724160", "quantity": 1 }],
        "payment_method": "pm_card_684790996696828928",
        "subscription_options": { "reference": "sub-jane-doe-premium-001" }
      }'

    The subscription is active once its first payment succeeds, or once its trial ends and the first period is paid. If the first payment is declined, the subscription is failed, and Pay.com sends subscription.failed. When a renewal fails, Pay.com retries it on your subscription settings' failed_payment rules, or the price's own. See Failed renewals.

Listen for webhooks

Pay.com charges subscriptions in the background on your behalf. Subscribe to these events to keep your system in sync:

  • subscription.active: the subscription started, or recovered after a failed charge.
  • subscription.charged: a renewal charge succeeded.
  • subscription.unpaid: a renewal charge failed, and retries started.
  • subscription.canceled: the subscription ended. cancellation_reason indicates why.
  • subscription.access.updated: the customer's access changed. Grant or revoke access from it.

See Commerce events for all subscription events.

Always verify webhook signatures using the Pay-Signature header and your endpoint's signing secret before acting on any event. See Verify webhook signatures for the full verification steps.

Next steps

Was this page helpful?