Created
January 10, 2019 20:32
-
-
Save dampee/19b57382d655f28a0e2d3529dac9565f to your computer and use it in GitHub Desktop.
chargebee in webapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[RoutePrefix("Chargebee")] | |
public class ChargebeeController : ApiController | |
{ | |
private readonly ILoggingService _loggingService; | |
private readonly TicketCountRepository _ticketCountRepository; | |
private readonly IBusinessRepository _businessRepository; | |
private readonly IBus _bus; | |
public ChargebeeController(ILoggingService loggingService, TicketCountRepository ticketCountRepository, IBusinessRepository businessRepository, | |
IBus bus) | |
{ | |
_loggingService = loggingService; | |
_ticketCountRepository = ticketCountRepository; | |
_businessRepository = businessRepository; | |
_bus = bus; | |
_loggingService.Info<ChargebeeController>("ChargebeeController() ctor"); | |
} | |
[System.Web.Http.Route("Callback")] | |
public async Task<HttpResponseMessage> Callback() | |
{ | |
_loggingService.Info<ChargebeeController>("Chargebee/Callback()"); | |
// The body can only be parsed once an another handler or module has already done so, | |
// so reset the stream to the beginning | |
var stream = await Request.Content.ReadAsStreamAsync(); | |
stream.Seek(0, System.IO.SeekOrigin.Begin); | |
// convert the stream to a chargebee event | |
var chargebeeEvent = new ChargeBee.Models.Event(stream); | |
// Choose the right event to handle | |
_loggingService.Info<ChargebeeController>("Handeling {chargebeeEventType}", chargebeeEvent.EventType); | |
switch (chargebeeEvent.EventType) | |
{ | |
case EventTypeEnum.PendingInvoiceCreated: | |
PendingInvoiceCreated(chargebeeEvent); | |
break; | |
case EventTypeEnum.SubscriptionCreated: | |
SubscriptionCreated(chargebeeEvent); | |
break; | |
default: | |
break; | |
} | |
return Request.CreateResponse(HttpStatusCode.Accepted, true); | |
} | |
private void SubscriptionCreated(Event data) | |
{ | |
var eventContent = data.Content; | |
Subscription subscription = eventContent.Subscription; | |
Customer customer = eventContent.Customer; | |
_loggingService.Info<ChargebeeController>("A new subscription was created in chargebee"); | |
} | |
private void PendingInvoiceCreated(ChargeBee.Models.Event data) | |
{ | |
// * find back the BUSINESS ID | |
var business = _businessRepository.GetByChargeBeeIdentifier(data.Content.Invoice.CustomerId); | |
if (business == null) | |
{ | |
_loggingService.Error<ChargebeeController>("business not found vor chargebee customerId {chargebeeCustomerId}", data.Content.Invoice.CustomerId); | |
return; | |
} | |
// todo prepare invoice lines | |
_bus.SendCommand(new PendingChargebeeInvoiceCreated | |
{ | |
ChargebeeInvoiceId = data.Content.Invoice.Id, | |
InvoiceLines = new List<PendingChargebeeInvoiceCreated.InvoiceLineItem>(), | |
SubscriptionId = data.Content.Invoice.SubscriptionId, | |
CustomerId = data.Content.Invoice.CustomerId, | |
BusinessId = business.BusinessId | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment