MCPcopy Create free account
hub / github.com/devinterface/startersaas-go-api / PaymentFailed

Method PaymentFailed

services/webhook.service.go:77–105  ·  view source on GitHub ↗

PaymentFailed function

(event stripe.Event)

Source from the content-addressed store, hash-verified

75
76// PaymentFailed function
77func (webhookService *WebhookService) PaymentFailed(event stripe.Event) (success bool, err error) {
78 paymentIntent := event.Data.Object["payment_intent"]
79 billingReason := event.Data.Object["billing_reason"]
80 if paymentIntent != nil && billingReason != "subscription_update" {
81 return false, err
82 }
83 sCustomerID := event.Data.Object["customer"]
84 account, err := accountService.OneBy(bson.M{"stripeCustomerId": sCustomerID})
85 if err != nil {
86 return false, err
87 }
88 account.PaymentFailed = true
89 if account.PaymentFailedFirstAt.IsZero() {
90 account.PaymentFailedFirstAt = time.Now()
91 }
92 paymentFailedRetryDays, _ := strconv.Atoi(os.Getenv("PAYMENT_FAILED_RETRY_DAYS"))
93 subscriptionDeactivatedAt := account.PaymentFailedFirstAt.AddDate(0, 0, paymentFailedRetryDays)
94 account.PaymentFailedSubscriptionEndsAt = subscriptionDeactivatedAt
95 _ = accountService.getCollection().Update(account)
96 formattedSubscriptionDeactivatedAt := strftime.Format("%d/%m/%Y", subscriptionDeactivatedAt)
97 user, err := userService.OneBy(bson.M{"accountId": account.ID, "accountOwner": true})
98 if err != nil {
99 return false, err
100 }
101 stripeHostedInvoiceUrl := event.Data.Object["hosted_invoice_url"]
102 go emailService.SendNotificationEmail(user.Email, i18n.Tr(user.Language, "webhookService.paymentFailed.subject"), i18n.Tr(user.Language, "webhookService.paymentFailed.message", map[string]interface{}{"Date": formattedSubscriptionDeactivatedAt, "StripeHostedInvoiceUrl": stripeHostedInvoiceUrl}), user.Language)
103 go emailService.SendNotificationEmail(os.Getenv("NOTIFIED_ADMIN_EMAIL"), i18n.Tr(os.Getenv("LOCALE"), "webhookService.paymentFailed.subject"), i18n.Tr(os.Getenv("LOCALE"), "webhookService.paymentFailed.messageAdmin", map[string]interface{}{"Subdomain": account.Subdomain, "Email": user.Email, "Date": formattedSubscriptionDeactivatedAt}), os.Getenv("LOCALE"))
104 return err != nil, err
105}
106
107// SubscriptionUpdated function
108func (webhookService *WebhookService) SubscriptionUpdated(event stripe.Event) (success bool, err error) {

Callers 1

HandleWebhookMethod · 0.95

Calls 4

SendNotificationEmailMethod · 0.80
OneByMethod · 0.45
UpdateMethod · 0.45
getCollectionMethod · 0.45

Tested by

no test coverage detected