PaymentFailed function
(event stripe.Event)
| 75 | |
| 76 | // PaymentFailed function |
| 77 | func (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 |
| 108 | func (webhookService *WebhookService) SubscriptionUpdated(event stripe.Event) (success bool, err error) { |
no test coverage detected