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 async Task<DomainEvent> AddAsync(DomainEvent e) { BillingEventEntity entity = _mapper.Map<DomainEvent, BillingEventEntity>(e); entity = await _billingEventRepository.AddAsync(entity); DomainEvent result = _mapper.Map<BillingEventEntity, DomainEvent>(entity); return result; }
public Task<DomainEvent> GetAsync(DomainEvent e) { return Task.Run(() => { try { StripeEvent eventObject = _service.Get(e.Id); return Task.FromResult(_mapper.Map<StripeEvent, DomainEvent>(eventObject)); } catch (StripeException ex) { throw new BillingException(string.Format("Failed to get billing event data by id {0}: {1}", e.Id, ex)); } }); }
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); }
public Task DeleteAsync(DomainEvent e) { return _billingEventRepository.DeleteAsync(e.Id); }
public async Task<bool> ExistsAsync(DomainEvent e) { BillingEventEntity entity = await _billingEventRepository.GetAsync(e.Id); return entity != null; }