Skip to content

Instantly share code, notes, and snippets.

@natanfeitosa
Last active May 25, 2024 03:29
Show Gist options
  • Save natanfeitosa/8aef22c8bf8660f65811ec60e8375d38 to your computer and use it in GitHub Desktop.
Save natanfeitosa/8aef22c8bf8660f65811ec60e8375d38 to your computer and use it in GitHub Desktop.
async  function  processWebhookInvoiceSuccess(event:  Stripe.Invoice) {
  console.log(JSON.stringify(event, null, 2));

  await prisma.user.update({
    where: { id: user.id },
    data: {
      latestInvoiceStart: event.period_start,
      latestInvoiceEnds: event.period_end,
    },
  });
}
{
  "object": "invoice",
  "amount_due": 0,
  "amount_paid": 0,
  "amount_remaining": 0,
  "amount_shipping": 0,
  "attempt_count": 0,
  "attempted": true,
  "auto_advance": false,
  "billing_reason": "subscription_create",
  "collection_method": "charge_automatically",
  "created": 1716606529,
  "customer_tax_exempt": "none",
  "effective_at": 1716606529,
  "ending_balance": 0,
  "issuer": {
    "type": "self"
  },
  "lines": {
    "object": "list",
    "data": [
      {
        "object": "line_item",
        "amount": 0,
        "amount_excluding_tax": 0,
        "discountable": true,
        "livemode": false,
        "period": {
          "end": 1719284929,
          "start": 1716606529
        },
        "plan": {
          "object": "plan",
          "active": true,
          "amount": 0,
          "amount_decimal": "0",
          "billing_scheme": "per_unit",
          "created": 1716407590,
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "usage_type": "licensed"
        },
        "price": {
          "object": "price",
          "active": true,
          "billing_scheme": "per_unit",
          "created": 1716407590,
          "livemode": false,
          "recurring": {
            "interval": "month",
            "interval_count": 1,
            "usage_type": "licensed"
          },
          "tax_behavior": "unspecified",
          "type": "recurring",
          "unit_amount": 0,
          "unit_amount_decimal": "0"
        },
        "proration": false,
        "quantity": 1,
        "type": "subscription",
        "unit_amount_excluding_tax": "0"
      }
    ],
    "has_more": false,
    "total_count": 1,
  },
  "livemode": false,
  "paid": true,
  "paid_out_of_band": false,
  "period_end": 1716606529,
  "period_start": 1716606529,
  "post_payment_credit_notes_amount": 0,
  "pre_payment_credit_notes_amount": 0,
  "starting_balance": 0,
  "status": "paid",
  "status_transitions": {
    "finalized_at": 1716606529,
    "paid_at": 1716606529,
  },
}
async function  processWebhookSubscription(event:  Stripe.Subscription) {
  console.log(JSON.stringify(event, null, 2));

  await prisma.user.update({
    where: { id: user.id },
    data: {
      latestInvoiceStart: event.current_period_start,
      latestInvoiceEnds: event.current_period_end,
    },
  });
}
{
  "object": "subscription",
  "billing_cycle_anchor": 1716606529,
  "collection_method": "charge_automatically",
  "created": 1716606529,
  "current_period_end": 1719284929,
  "current_period_start": 1716606529,
  "invoice_settings": {
    "issuer": {
      "type": "self"
    }
  },
  "items": {
    "object": "list",
    "data": [
      {
        "object": "subscription_item",
        "created": 1716606529,
        "plan": {
          "object": "plan",
          "active": true,
          "amount": 0,
          "amount_decimal": "0",
          "billing_scheme": "per_unit",
          "created": 1716407590,
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "usage_type": "licensed"
        },
        "price": {
          "object": "price",
          "active": true,
          "billing_scheme": "per_unit",
          "created": 1716407590,
          "livemode": false,
          "recurring": {
            "interval": "month",
            "interval_count": 1,
            "usage_type": "licensed"
          },
          "tax_behavior": "unspecified",
          "type": "recurring",
          "unit_amount": 0,
          "unit_amount_decimal": "0"
        },
        "quantity": 1,
      }
    ],
    "has_more": false,
    "total_count": 1,
  },
  "livemode": false,
  "payment_settings": {
    "save_default_payment_method": "off"
  },
  "plan": {
    "object": "plan",
    "active": true,
    "amount": 0,
    "amount_decimal": "0",
    "billing_scheme": "per_unit",
    "created": 1716407590,
    "interval": "month",
    "interval_count": 1,
    "livemode": false,
    "usage_type": "licensed"
  },
  "quantity": 1,
  "start_date": 1716606529,
  "status": "active",
  "trial_settings": {
    "end_behavior": {
      "missing_payment_method": "create_invoice"
    }
  },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment