private async Task ChargeSucceededAsync(DomainEvent billingEvent) { // Retrieve company by charge data DomainCharge charge = await _billingChargeService.GetAsync(new DomainCharge { Id = billingEvent.ObjectId }); DomainCompany company = await _companyService.FindByCustomerAsync(charge.CustomerId); // Updating balance var balanceRecord = new DomainBalanceHistory { Amount = charge.AmountInCents, Description = BillingMessages.ChargeSucceeded, CompanyId = company.Id }; await _balanceService.AddAsync(balanceRecord); // Notify client about payment operation result try { await _notificationService.SendPaymentNotificationAsync(billingEvent, company, charge); } catch (Exception e) { Trace.TraceError("Failed to send payment notification e-mail to company {0}: {1}", company.Id, e); } }
public Task <DomainCharge> GetAsync(DomainCharge charge) { return(Task.Run(() => { try { StripeCharge c = _service.Get(charge.Id); return Task.FromResult(_mapper.Map <StripeCharge, DomainCharge>(c)); } catch (StripeException e) { throw new BillingException(string.Format("Failed to get charge by id {0}: {1}", charge.Id, e)); } })); }
private async Task ChargeFailedAsync(DomainEvent billingEvent) { // Retrieve company by charge data DomainCharge charge = await _billingChargeService.GetAsync(new DomainCharge { Id = billingEvent.ObjectId }); DomainCompany company = await _companyService.FindByCustomerAsync(charge.CustomerId); // Notify client about payment operation result try { await _notificationService.SendPaymentNotificationAsync(billingEvent, company, charge); } catch (Exception e) { Trace.TraceError("Failed to send payment notification e-mail to company {0}: {1}", company.Id, e); } }
private async Task ChargeRefundedAsync(DomainEvent billingEvent) { // Get charge info DomainCharge charge = await _billingChargeService.GetAsync(new DomainCharge { Id = billingEvent.ObjectId }); var refunds = billingEvent.Object["refunds"] as JArray; if (refunds != null) { // refund can be partial, accounting only last refund JToken lastRefund = refunds.Last; int refundInCents = Int32.Parse(lastRefund["amount"].ToString()); charge.AmountInCents = refundInCents; } // Retrieve company by customer DomainCompany company = await _companyService.FindByCustomerAsync(charge.CustomerId); // updating balance var balanceRecord = new DomainBalanceHistory { Amount = -charge.AmountInCents, Description = BillingMessages.ChargeRefunded, CompanyId = company.Id }; await _balanceService.AddAsync(balanceRecord); // Notify client about payment operation result try { await _notificationService.SendPaymentNotificationAsync(billingEvent, company, charge); } catch (Exception e) { Trace.TraceError("Failed to send payment notification e-mail to company {0}: {1}", company.Id, e); } }
public async Task SendPaymentNotificationAsync(DomainEvent billingEvent, DomainCompany company, DomainCharge charge) { if (billingEvent == null) { throw new ArgumentNullException("billingEvent"); } if (company == null) { throw new ArgumentNullException("company"); } if (charge == null) { throw new ArgumentNullException("charge"); } if (!_settings.EmailNotifications) { return; } var email = new SendEmailDomain { Address = _settings.EmailAddressAlerts, DisplayName = Emails.SenderDisplayName, Emails = new List <string> { company.Email } }; switch (billingEvent.Type) { case EventType.ChargeFailed: email.Subject = Emails.SubjectPaymentFailed; email.Body = string.Format( PortalResources.PaymentFailed, company.Name, billingEvent.Id, string.Format("{0} {1}", charge.AmountInCents * 0.01, charge.Currency), charge.Created); break; case EventType.ChargeSucceeded: email.Subject = Emails.SubjectPaymentCompleted; email.Body = string.Format( PortalResources.PaymentCompleted, company.Name, billingEvent.Id, string.Format("{0} {1}", charge.AmountInCents * 0.01, charge.Currency), charge.Created); break; case EventType.ChargeRefunded: email.Subject = Emails.SubjectPaymentRefunded; email.Body = string.Format( PortalResources.PaymentRefunded, company.Name, billingEvent.Id, string.Format("{0} {1}", charge.AmountInCents * 0.01, charge.Currency), charge.Created); break; default: return; } // Send email on user registration await _emailSenderService.SendEmailAsync(email); }