# Webhooks
## `customer.subscription.updated`
### in `/callbacks/stripe.json`
```
def mark_subscription_delinquent!
update subscription_delinquent: true
update attempt_to_pay_delinquent_subscription_at: 7.days.from_now
send_delinquent_subscription_email
end
```
After the subscription is marked `past_due`.
- When Stripe subscription status is `upaid` mark the subscription as delinquent.
- Send user email(s) to update their card details.
- Attempt to pay the subscriptions's last invoice.
### in `/api/v1/stripe-events`
- Updates Subscriptions in the DB with Stripe subscripton plan.
- Tracks `incomplete_expired` subscriptions.
--------------------------------------------------------
## `charge.succeeded`
Charge is an older object and is now being replaced with PaymentItnent object. [We should use Payment Intents API.](https://stripe.com/docs/payments/payment-intents/migration/charges)
### in `/callbacks/stripe.json`
- If charge for subscription update local DB records with the plan and set user's account type.
- If `HML-Base` then create `HmlPaymentReceipt` in DB.
### in `/api/v1/stripe-events` (not related to subscriptions)
- Used for publishing subaccount only.
- It raises tax invoice for publishing subaccount.
--------------------------------------------------------
## `invoice.payment_succeeded`
### in `/callbacks/stripe.json`
Doesn't exist.
### in `/api/v1/stripe-events`
- Updates user with `payment_cycle_active` and `paid_until` (on user and subscription) which are checked in the `has_pro[plus/premium]_benefits`.
- Creates or updates `Subscription::Payments` record with on invoice data.
- Creates `Subscriptions::TaxInvoice` record.
- Send a reciept to the user for the subscription chage.
- Tracks with `Subscriptions::LTVTracker`
- Also updates an invoice id in `Companies::BundledLeads::Payment`? Why? What does this has to do with subscriptions?
--------------------------------------------------------
## `customer.subscription.created`
### in `/callbacks/stripe.json`
Doesn't exist.
### in `/api/v1/stripe-events`
- Populates metadata with more location data in `SubscriptionGeolocationWorker`.
- Tracks created subcriptions with `Subscriptions::ChurnTracking`. (?)
--------------------------------------------------------
## `payment_intent.created`
### in `/callbacks/stripe.json`
Doesn't exist.
### in `/api/v1/stripe-events`
- Works with `watch_payment_intent_webhook` Flipper.
- Updates `Stripe::PaymentIntent` with an email address of accociated company owner from `Companies::LeadBundleSubscription`.
--------------------------------------------------------
## `invoice.payment_failed`
### in `/callbacks/stripe.json`
- Handle payment failed for pros and plus
- Disabled because Flipper `subscription_delinquency_emails` disabled on prod.
### in `/api/v1/stripe-events`
- For both subscription and bundle lead subscriptions
- Add failed invoice to the LeadBundle list of stripe_invoice_ids.
- mark invoice for memberships subscription as failed locally.
--------------------------------------------------------
## `charge.refunded`
### in `/callbacks/stripe.json`
- only does the tracking
### in `/api/v1/stripe-events`
- Doesn't exist.
--------------------------------------------------------
## `customer.subscription.deleted`
### in `/callbacks/stripe.json`
```
user.update payment_cycle_active: false
user.send_cancellation_email user.subscription_delinquent?
user.unmark_subscription_delinquent!
```
### in `/api/v1/stripe-events`
- sets subscription status to cancelled
- `Subscriptions::ChurnTracking`
--------------------------------------------------------
## `customer.discount.created`
### in `/callbacks/stripe.json`
- Creates StripeCouponUse record
### in `/api/v1/stripe-events`
- Doesn't exist.
--------------------------------------------------------
## `charge.dispute.created`
### in `/callbacks/stripe.json`
- Deletes a subscription.
- Sets `payment_cycle_active: false`
### in `/api/v1/stripe-events`
- Doesn't exist.
--------------------------------------------------------
## `"customer.card.updated", "customer.source.updated", "customer.card.created", "customer.source.created"`
### in `/callbacks/stripe.json`
- Pay last unpaid invoice.
### in `/api/v1/stripe-events`
- Doesn't exist.
--------------------------------------------------------
## `customer.updated`
### in `/callbacks/stripe.json`
- Doesn't exist.
### in `/api/v1/stripe-events`
-Locally updates all active subscriptions with new card details.