123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // api/webhooks/webhook.service.js
- import moment from "moment";
- import i18n from "../../common/i18n.js";
- import BaseService from "../../services/base.service.js";
- import stripeConf from "../../stripe.conf.js";
- import AccountService from "../accounts/account.service.js";
- import EmailService from "../emails/email.service.js";
- import UserService from "../users/user.service.js";
- import Webhook from "./webhook.model.js";
- class WebhookService extends BaseService {
- getModel() {
- return Webhook;
- }
- async handleWebhook(data) {
- this.create({ payload: data });
- switch (data.type) {
- case "customer.subscription.updated":
- this.subscriptionUpdated(data);
- break;
- case "customer.subscription.created":
- this.subscriptionUpdated(data);
- break;
- case "invoice.paid":
- this.paymentSuccessful(data);
- break;
- case "invoice.payment_failed":
- this.paymentFailed(data);
- break;
- default:
- console.log(`Webhook: ${data.type}: No handler`);
- }
- }
- async paymentSuccessful(data) {
- const stripeCustomerId = data.data.object.customer;
- const account = await AccountService.oneBy({
- stripeCustomerId: stripeCustomerId,
- });
- if (!account) {
- return;
- }
- const user = await UserService.oneBy({
- accountId: account.id,
- accountOwner: true,
- });
- if (data.data.object.billing_reason === "subscription_create") {
- EmailService.generalNotification(
- user.email,
- i18n.t("webhookService.newSubscription.subject"),
- i18n.t("webhookService.newSubscription.message"),
- user.language
- );
- EmailService.generalNotification(
- process.env.NOTIFIED_ADMIN_EMAIL,
- i18n.t("webhookService.newSubscription.subject"),
- i18n.t("webhookService.newSubscription.messageAdmin", {
- email: user.email,
- subdomain: account.subdomain,
- })
- );
- }
- if (data.data.object.billing_reason === "subscription_update") {
- EmailService.generalNotification(
- user.email,
- i18n.t("webhookService.subscriptionUpdated.subject"),
- i18n.t("webhookService.subscriptionUpdated.message"),
- user.language
- );
- EmailService.generalNotification(
- process.env.NOTIFIED_ADMIN_EMAIL,
- i18n.t("webhookService.subscriptionUpdated.subject"),
- i18n.t("webhookService.subscriptionUpdated.messageAdmin", {
- email: user.email,
- subdomain: account.subdomain,
- })
- );
- }
- if (data.data.object.billing_reason === "subscription_cycle") {
- EmailService.generalNotification(
- user.email,
- i18n.t("webhookService.paymentSuccessful.subject"),
- i18n.t("webhookService.paymentSuccessful.message"),
- user.language
- );
- EmailService.generalNotification(
- process.env.NOTIFIED_ADMIN_EMAIL,
- i18n.t("webhookService.paymentSuccessful.subject"),
- i18n.t("webhookService.paymentSuccessful.messageAdmin", {
- email: user.email,
- subdomain: account.subdomain,
- })
- );
- }
- AccountService.update(account.id, {
- paymentFailed: false,
- active: true,
- paymentFailedFirstAt: null,
- paymentFailedSubscriptionEndsAt: null,
- trialPeriodEndsAt: null,
- });
- }
- async subscriptionUpdated(data) {
- const stripeCustomerId = data.data.object.customer;
- if (data.data.object.status !== "active") {
- return;
- }
- const account = await AccountService.oneBy({
- stripeCustomerId: stripeCustomerId,
- });
- if (!account) {
- return;
- }
- const user = await UserService.oneBy({
- accountId: account.id,
- accountOwner: true,
- });
- const expiresAt = data.data.object.cancel_at;
- account.subscriptionExpiresAt = expiresAt ? moment.unix(expiresAt) : null;
- account.stripePlanId = data.data.object.plan.id;
- account.planType = stripeConf.plans.find(
- (p) => p.id === data.data.object.plan.id
- ).planType;
- account.save();
- }
- async paymentFailed(data) {
- const stripeCustomerId = data.data.object.customer;
- if (
- data.data.object.billing_reason === "subscription_create" ||
- data.data.object.billing_reason === "subscription_update"
- ) {
- return;
- }
- // if (data.data.object.attempt_count >= 1) {
- let account = await AccountService.oneBy({
- stripeCustomerId: stripeCustomerId,
- });
- if (!account) {
- return;
- }
- const user = await UserService.oneBy({
- accountId: account.id,
- accountOwner: true,
- });
- if (!account.paymentFailedFirstAt) {
- const paymentFailedSubscriptionEndsAt = moment(Date.now()).add(
- process.env.PAYMENT_FAILED_RETRY_DAYS,
- "days"
- );
- account = await AccountService.update(account.id, {
- paymentFailed: true,
- paymentFailedFirstAt: Date.now(),
- paymentFailedSubscriptionEndsAt: paymentFailedSubscriptionEndsAt,
- });
- } else {
- account = await AccountService.update(account.id, {
- paymentFailed: true,
- });
- }
- const stripeHostedInvoiceUrl = data.data.object.hosted_invoice_url;
- EmailService.generalNotification(
- user.email,
- i18n.t("webhookService.paymentFailed.subject"),
- i18n.t("webhookService.paymentFailed.message", {
- date: moment(account.paymentFailedSubscriptionEndsAt).format(
- "DD/MM/YYYY"
- ),
- stripeHostedInvoiceUrl: stripeHostedInvoiceUrl,
- }),
- user.language
- );
- EmailService.generalNotification(
- process.env.NOTIFIED_ADMIN_EMAIL,
- i18n.t("webhookService.paymentFailed.subject"),
- i18n.t("webhookService.paymentFailed.messageAdmin", {
- email: user.email,
- subdomain: account.subdomain,
- date: moment(account.paymentFailedSubscriptionEndsAt).format(
- "DD/MM/YYYY"
- ),
- })
- );
- // }
- }
- }
- export default new WebhookService();
|