Mintcash
Guides

Send your own subscription emails

With Mintcash's customer emails turned off, the webhooks tell you when to email. This guide gives the rule for each event, including after a hard decline.

Mintcash emails your customers about failed renewals by default. If you have turned those emails off, nothing reaches the customer unless you send it. This guide tells you which webhook means which email.

Before you start

  • Handle events with type: "subscription". A renewal never sends a payment.* event.
  • Dedupe on eventId. A retried delivery carries the same one.
  • Decide from data.status, not from the event name alone. subscription.failed means one charge attempt failed. It does not always mean the customer has something to do.
  • Events can arrive out of order, so don't rely on subscription.failed reaching you before the subscription.cancelled it led to.
  • The customer's address is at data.payment.customer.email.

Which email to send

On a subscription.failed, read two fields of data.payment: card.blocked and failureMessage.

Eventdata.statusdata.paymentWhat happenedEmail
subscription.failedpast_duecard.blocked is falseA renewal or a scheduled retry was declined. Mintcash will retry.Payment failed. Offer a card-update link.
subscription.failedpast_duecard.blocked is trueA hard decline. The card is blocked and won't be charged again.The card can't be charged. Ask for a new one with a link.
subscription.failedpast_duefailureMessage is card_blockedA renewal was due on a card that was already blocked, so no charge was sent.The same, without saying a charge was tried.
subscription.failedactiveanyA late decline on an invoice that has since been paid.None
subscription.failedcancelledanyThe decline that ended the subscription. It comes with subscription.cancelled, in either order.None. Send the cancellation email from the other event.
subscription.failedfailedanyThe first charge at checkout was declined. The customer saw it on screen.None
subscription.succeededactiveanyA charge was paid. After a failure, the subscription has recovered.Optional: payment received
subscription.cancelledcancelledanyThe subscription ended.Your subscription has ended, once per subscription
subscription.pendinganyanyA charge is in progress.None
subscription.payment_method_updatedanyanyThe customer changed the card.None

A card_blocked payment always has card.blocked: true as well, so check failureMessage first.

data.payment.failureCode can be null on a subscription.failed. The charge then failed for a reason that is not the card, such as a processing error, and Mintcash retries it on the usual schedule. Do not tell the customer their card was declined or ask them for a new one.

What to tell the customer

When card.blocked is true, say the card "has expired" or "can't be charged, please update it". Don't pass on failureCode or card.blockedCode when it's a lost, stolen or fraud code: telling whoever holds a stolen card that it was reported tips them off.

The customer's own attempts send subscription.failed too

When a customer enters a card on a card-update link and it is declined, you receive subscription.failed with data.status: "past_due", the same as a scheduled retry. The customer has already seen the decline on the page. If you email on every past_due failure, they get an email for each card they try. Send at most one failure email per subscription per calendar day (UTC). Scheduled retries are at least a day apart, so you lose none of them.

After a hard decline

A hard decline, such as an expired, lost or stolen card, doesn't cancel the subscription. It runs the usual retry schedule, but the card is blocked and never charged again:

  1. You receive subscription.failed with data.status: "past_due", data.payment.card.blocked: true and the hard failureCode. If the customer saved the card, payment_method.updated arrives too.
  2. The steps on days 1, 3, 5 and 7 after the first failure still run, but charge nothing and send no webhook.
  3. If the customer enters a new card on a card-update link, it is charged for the open invoice at once. On success you receive subscription.payment_method_updated and subscription.succeeded, and data.status is active.
  4. If the new card is declined, you receive another subscription.failed, and the customer can try another card on the same link.
  5. If nothing is paid by the end of the schedule, the subscription is cancelled and you receive subscription.cancelled, the same as when retries run out.

So the first email is the one that matters: tell the customer the card can't be used and give them a link. No later webhook will remind you. Mintcash's own emails also remind the customer on days 1, 3 and 5. To do the same, schedule them yourself from the date of the first failure, and stop when subscription.succeeded or subscription.cancelled arrives.

If you'd rather end the subscription at once, see Cancel on a hard decline yourself.

Every email that asks for a new card needs a card-update link. Mint one when you send the email:

const res = await fetch(
  `https://api.mintcash.me/subscriptions/${event.data.id}/card-update`,
  {
    method: "POST",
    headers: { Authorization: ..., "Content-Type": "application/json" },
    body: JSON.stringify({ mode: "hosted" }),
  },
);

const { url } = await res.json();

A link lasts 7 days and an invoice accepts at most 5 submitted cards across all of its links. See Update a card on a subscription for the limits and for when the call is refused.

After a hard decline, no further subscription.failed arrives to remind you. If the link call fails, retry it yourself until it succeeds, or send the email without a link.

The call can be refused with 400 while a scheduled retry is mid-charge, and with 429 once the invoice's five cards are spent. Don't fail the webhook over it: answer 2xx, and for a 400 try the link again a few minutes later. After a 429 the customer cannot pay that invoice; contact support if they still want to.

Webhook handlers should answer within 10 seconds. If minting the link and sending the email could take longer, queue them and answer first.

A worked handler

It follows the table above row by row. loadState and saveState keep two values per subscription: the day you last sent a failure email, and whether you have sent "ended".

async function onSubscriptionEvent(event) {
  if (await alreadyHandled(event.eventId)) return;

  const sub = event.data;
  const payment = sub.payment;
  const state = await loadState(sub.id);
  const today = utcDay(new Date());

  if (event.event === "subscription.cancelled" && !state.endedSent) {
    await sendEmail(sub, "subscription_ended");
    await saveState(sub.id, { endedSent: true });
  }

  if (
    event.event === "subscription.failed" &&
    sub.status === "past_due" &&
    payment?.failureCode && // null: not the card, Mintcash retries it
    state.lastFailureEmailDay !== today
  ) {
    const url = await cardUpdateLink(sub.id);

    if (payment.failureMessage === "card_blocked") {
      // No charge was sent: don't say "we tried your card".
      await sendEmail(sub, "card_cannot_be_used", { url });
    } else if (payment.card?.blocked) {
      // A hard decline: this card won't be charged again.
      await sendEmail(sub, "card_declined_add_new", { url });
    } else {
      await sendEmail(sub, "payment_failed", { url });
    }

    await saveState(sub.id, { lastFailureEmailDay: today });
  }

  await markHandled(event.eventId);
}

const utcDay = (date) => date.toISOString().slice(0, 10);

cardUpdateLink is the call from Getting the link. If it is refused, send the email without a link, or not at all, and still answer the webhook.

What the webhook does not tell you

  • Which attempt this was. There is no attempt number or next retry date on the event, so you cannot reproduce the "final warning" email Mintcash sends before the last retry. After a hard decline, the steps that skip the blocked card send no event at all.
  • Who made the attempt. A scheduled retry and a card the customer entered themselves look the same.
  • Why the subscription was cancelled. subscription.cancelled does not say whether you cancelled it or the retry schedule ran out. Its data.payment is the last failed charge, so a hard decline still shows in failureCode and card.blocked. If you cancel through the API yourself, record that on your side before the event arrives.
  • Whether a paid charge was the first, a renewal or a recovery. Track the subscription's last status yourself if your emails differ.

See also

  • Retries — the retry schedule and hard declines
  • Emails — what Mintcash sends when its emails are on
  • Payload reference — every field on a subscription event, including card.blocked