private static async Task OpenNewBill(Client ebrizaDataReaderClient, Client ebrizaBillOpenerClient, CompanyLocation[] companyLocations, Item[] products) { LocationTable[] tables = await ebrizaDataReaderClient.Get <LocationTable[]>("tables", new System.Collections.Generic.Dictionary <string, object> { { "locationid", companyLocations.First().ID } }); LocationTable tableToPutTheBillOn = tables.First(); string result = await ebrizaBillOpenerClient.Post("bills/open", new OpenBillRequest { TableID = tableToPutTheBillOn.ID, Items = new OpenBillItem[] { new OpenBillItem { ID = products.First().ID, Quantity = 10, } }, }); }
static async Task MainAsync(string[] args) { //Construct the Ebriza Client API interactor. Available as NuGet Package: https://www.nuget.org/packages/EbrizaAPI EbrizaAPI.Client ebrizaDataReaderClient = new EbrizaAPI.Client(appPublicKey, appSecretKey, appClientId); EbrizaAPI.Client ebrizaBillOpenerClient = new EbrizaAPI.Client(billOpenerAppPublicKey, billOpenerAppSecretKey, billOpenerAppClientId); //Now we have access to the Ebriza Client's; Available Endpoints: https://ebriza.com/docs/api //First, we must get all the client's locations, because we'll need them for most of the API requests (API: https://ebriza.com/docs/api#Getcompanylocations): //A company can (and many do) have multiple locations, with specific products, prices, etc. CompanyLocation[] companyLocations = await ebrizaDataReaderClient.Get <CompanyLocation[]>("company/locations"); Console.WriteLine(); Console.WriteLine($"{companyLocations.Length} locations: {string.Join(", ", companyLocations.Select(x => x.Name))}"); Console.WriteLine(); //Let's get the company's products and categories for the first location (https://ebriza.com/docs/api#Listitems): Item[] productsAndCategories = await ebrizaDataReaderClient.Get <Item[]>("items", new System.Collections.Generic.Dictionary <string, object> { { "locationid", companyLocations.First().ID } } ); Item[] products = productsAndCategories.Where(x => !x.IsCategory).ToArray(); Item[] categories = productsAndCategories.Where(x => x.IsCategory).ToArray(); Console.WriteLine(); Console.WriteLine($"{categories.Length} categories: {string.Join(", ", categories.Select(x => x.Name))}"); Console.WriteLine(); Console.WriteLine($"{products.Length} products: {string.Join(", ", products.Select(x => x.Name))}"); Console.WriteLine(); //Now, the juciy stuff: let's open a bill on the company's POS //First we need the list of tables on the location to know on which table we put the order: //If we want to open a delivery bill (that's a bill that will be delivered to the client, and not bound to a table at the location), than we MUST omit this part (not send the TableID) LocationTable[] tables = await ebrizaDataReaderClient.Get <LocationTable[]>("tables", new System.Collections.Generic.Dictionary <string, object> { { "locationid", companyLocations.First().ID } }); LocationTable tableToPutTheBillOn = tables.First(); string result = await ebrizaBillOpenerClient.Post("bills/open", new OpenBillRequest { TableID = tableToPutTheBillOn.ID, Items = new OpenBillItem[] { new OpenBillItem { ID = products.First().ID, Quantity = 10, } }, }); //Console.WriteLine(); Console.WriteLine($"Open Bill Result: {result}"); Console.WriteLine(); //Now let's play with WebHooks //First we need to start an HTTP Server. We'll run a Nancy Self Hosted HTTP server, publicly exposed using ngrok; //In a real scenario this is usually replaced by the Web App itself, served via IIS or Apache or some other production wise HTTP Server. Console.WriteLine(); Console.WriteLine($"Webhooks Playground Starts here"); HttpServer httpServer = new HttpServer(); //This is, of course, not needed in a real app, since the URL is already exposed to the Internet httpServer.OnPublicUrlAcquired += async(_, arg) => { string webhookUrl = $"{arg.PublicUrl}/ebrizawebhook"; //Let's register our webhook with Ebriza await ebrizaDataReaderClient.Post("webhooks/subscribe", new WebHookSubscribeRequest { CallbackUrl = webhookUrl, Entity = WebHookEntityType.Bill }); await ebrizaDataReaderClient.Post("webhooks/subscribe", new WebHookSubscribeRequest { CallbackUrl = webhookUrl, Entity = WebHookEntityType.Order }); //Now let's open a Bill so we trigger the webhook await OpenNewBill(ebrizaDataReaderClient, ebrizaBillOpenerClient, companyLocations, products); }; HttpServer.OnEbrizaWebhook += async(_, arg) => { Console.WriteLine("Received Notification from Ebriza"); Console.WriteLine($"Entity: {arg.Entity} with id {arg.ID}"); //Here we can use the API to fetcg the changed Order or Bill Details and see its changes //For instance let's fetch the entity if (arg.Entity == WebHookEntityType.Bill) { dynamic bill = await ebrizaBillOpenerClient.Get <dynamic>($"bills/get/{arg.ID}"); } else if (arg.Entity == WebHookEntityType.Order) { dynamic closedBill = await ebrizaDataReaderClient.Get <dynamic>($"orders/get/{arg.ID}"); } }; httpServer.Start(); Console.ReadLine(); httpServer.Stop(); //Let's unregister our webhook with Ebriza await ebrizaDataReaderClient.Post("webhooks/unsubscribe", new WebHookUnsubscribeRequest { Entity = WebHookEntityType.Bill }); await ebrizaDataReaderClient.Post("webhooks/unsubscribe", new WebHookUnsubscribeRequest { Entity = WebHookEntityType.Order }); Console.WriteLine(); Console.WriteLine($"Done @ {DateTime.Now}"); Console.ReadLine(); }