# Subscriptions

Charge your customers on a schedule through your own provider accounts. Veyqo saves the card after the first payment and renews it each period.

## 1. Create a plan

A plan sets what a customer pays and how often: day, week, month or year, with an optional interval_count (for example, 3 with month for quarterly). Add trial_days for a free trial. Your API key needs subscriptions:write.

```bash
curl -X POST https://api.veyqo.org/api/v1/plans \
  -H "Authorization: Bearer $VEYQO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: plan-pro-monthly" \
  -d '{"name":"Pro monthly","amount":250000,"currency":"NGN","interval":"month"}'
```

## 2. Start a subscription

Starting a subscription creates its first payment. Redirect the customer to data.checkout_url. When they pay, the subscription becomes active and you receive subscription.activated.

With a free trial (the plan’s trial_days, or trial_days in this request, where 0 skips it) the subscription starts as trialing and there is nothing to pay yet. When the trial ends you receive subscription.payment_required with a checkout_url for the first payment; once it is paid, the card is saved for renewals.

To take the card before the trial, add trial_requires_card: true. The customer pays a small card check at checkout_url (₦50, or 50 cents or the like), which is refunded at once; the trial then starts, and the first period is charged to that card when it ends.

```bash
curl -X POST https://api.veyqo.org/api/v1/subscriptions \
  -H "Authorization: Bearer $VEYQO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: sub-customer-42" \
  -d '{"plan":"rplan_...","customer":{"email":"customer@example.com"},"reference":"SUB-42"}'
```

## 3. Renewals

1. When a period ends, Veyqo charges the saved card through the same provider and sends subscription.renewed.
2. If the customer paid by bank transfer or USSD, there is no card to charge. You receive subscription.payment_required with a checkout_url to send them.
3. A failed charge sends subscription.payment_failed, marks the subscription past_due and retries daily. After three failed attempts it is cancelled and you receive subscription.cancelled.

## 4. Change plan

Move a customer to another plan in the same currency. By default the new plan starts at the next renewal and nothing is charged now; it shows as pending_plan until then.

Pass at_period_end: false to switch now. The customer pays the new plan’s price less what is left of the current period: on the saved card straight away, or at the checkout_url in the response. A new period starts once it is paid, and you receive subscription.plan_changed. A plan worth less than what is left can only change at the end of the period.

```bash
curl -X POST https://api.veyqo.org/api/v1/subscriptions/SUB-42/plan \
  -H "Authorization: Bearer $VEYQO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"plan":"rplan_business","at_period_end":false}'
```

## 5. Cancel

Cancelling stops renewal at the end of the period the customer has paid for. Pass at_period_end: false to end it immediately. Nothing is refunded, and the saved card is deleted.

```bash
curl -X POST https://api.veyqo.org/api/v1/subscriptions/SUB-42/cancel \
  -H "Authorization: Bearer $VEYQO_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"at_period_end":true}'
```
