public static dynamic PopulateOrderFromConsole(string customerCid, string countryCode, string token) { bool InvalidInput = false; var offerCategoriesResponse = OfferCatalogPartnerCenterApi.GetOfferCategories(countryCode, token); Console.WriteLine("Hit enter to continue"); Console.ReadLine(); Console.Clear(); Console.WriteLine("Select Offer Category"); do { int index = 1; foreach (var offerCategory in offerCategoriesResponse.items) { Console.WriteLine("{0} {1}", index++, offerCategory.category.ToString()); } Console.Write("Enter index [1...{0}]:", index - 1); int input = Convert.ToInt32(Console.ReadLine().Trim()); if (input <= index) { InvalidInput = true; var offerCategoryResponse = offerCategoriesResponse.items[input - 1].id.ToString(); return(PopulateOrderFromConsoleForOfferCategory(offerCategoryResponse, customerCid, countryCode, token)); } }while (!InvalidInput); return(null); }
private static dynamic PopulateOrderFromConsoleForOfferCategory(string offerCategoryId, string customerCid, string countryCode, string token) { var offersResponse = OfferCatalogPartnerCenterApi.GetOffers(offerCategoryId, countryCode, token); dynamic order = new { line_items = new List <dynamic>(), recipient_customer_id = customerCid }; int nrOfLineItems = 0; bool done = false; Console.WriteLine("Hit enter to continue"); Console.ReadLine(); Console.Clear(); Console.WriteLine("Select offer category for placing an order"); do { Console.WriteLine("OfferCategory: {0}", offerCategoryId); int index = 1; foreach (var offerItem in offersResponse.items) { Console.WriteLine("{0} {1}", index++, offerItem.name.ToString()); } Console.Write("\nSelect Offer [1...{0}]:", index); int selectedOfferIndex = Convert.ToInt32(Console.ReadLine().Trim()); var selectedOffer = offersResponse.items[selectedOfferIndex - 1]; bool validQuantity = false; do { Console.Write("\nQuantity {0} to {1}: ", selectedOffer.minimumQuantity, selectedOffer.maximumQuantity); string inputQuantity = Console.ReadLine().Trim(); int quantity = 1; if (!int.TryParse(inputQuantity, out quantity)) { done = false; } int minimumQuantity = int.Parse(selectedOffer.minimumQuantity.ToString()); int maximumQuantity = int.Parse(selectedOffer.maximumQuantity.ToString()); if (quantity >= minimumQuantity && quantity <= maximumQuantity) { validQuantity = true; } Console.Write("\nFriendly Name (or hit Enter for none): "); string inputFriendlyName = Console.ReadLine().Trim(); if (!string.IsNullOrWhiteSpace(inputFriendlyName)) { order.line_items.Add(new { // has to be a unique number for each line item // recommendation is to start with 0 line_item_number = nrOfLineItems, // this is the offer uri for the offer that is being purchased, refer to the excel sheet for this offer_uri = selectedOffer.uri.ToString(), // This is the quantity for this offer quantity = quantity, // This is friendly name friendly_name = inputFriendlyName }); } else { order.line_items.Add(new { // has to be a unique number for each line item // recommendation is to start with 0 line_item_number = nrOfLineItems, // this is the offer uri for the offer that is being purchased, refer to the excel sheet for this offer_uri = selectedOffer.uri.ToString(), // This is the quantity for this offer quantity = quantity, }); } }while (!validQuantity); Console.Write("\nDo you want to add another line item (y/n)? "); string input = Console.ReadLine().Trim(); switch (input) { case "y": case "Y": nrOfLineItems++; done = false; break; default: done = true; break; } }while (!done); return(order); }
static void Main() { Console.Write("\nHave you updated the app.config, with the settings from https://partnercenter.microsoft.com (y/n)? "); string response = Console.ReadLine().Trim().ToUpperInvariant(); if (response != "Y" && response != "YES") { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\nUpdate AppId, Key, MicrosoftId, DefaultDomain in the app.config and run the app again"); Console.ResetColor(); Console.Write("\n\n\nHit enter to exit the app now"); Console.ReadLine(); return; } // This is the Microsoft ID of the reseller // Please work with your Admin Agent to get it from https://partnercenter.microsoft.com/en-us/pc/AccountSettings/TenantProfile string microsoftId = ConfigurationManager.AppSettings["MicrosoftId"]; // This is the default domain of the reseller // Please work with your Admin Agent to get it from https://partnercenter.microsoft.com/en-us/pc/AccountSettings/TenantProfile string defaultDomain = ConfigurationManager.AppSettings["DefaultDomain"]; // This is the appid that is registered for this application in Azure Active Directory (AAD) // Please work with your Admin Agent to get it from https://partnercenter.microsoft.com/en-us/pc/ApiIntegration/Overview string appId = ConfigurationManager.AppSettings["AppId"]; // This is the key for this application in Azure Active Directory // This is only available at the time your admin agent has created a new app at https://partnercenter.microsoft.com/en-us/pc/ApiIntegration/Overview // You could alternatively goto Azure Active Directory and generate a new key, and use that. string key = ConfigurationManager.AppSettings["Key"]; try { // Get Active Directory token first adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Get Partner Center API access token partnerCenterApiAuthorizationToken = Reseller.GetPartnerCenterApi_Token(adAuthorizationToken, appId); // get all offer categories var offerCategoriesResponse = OfferCatalogPartnerCenterApi.GetOfferCategories("US", partnerCenterApiAuthorizationToken.AccessToken); // write the number of offers in each category out to the console foreach (var offerCategory in offerCategoriesResponse.items) { var offersResponse = OfferCatalogPartnerCenterApi.GetOffers(offerCategory.id.ToString(), "US", partnerCenterApiAuthorizationToken.AccessToken); Console.WriteLine("Offers in {0} is {1}", offerCategory.id.ToString(), offersResponse.totalCount); } // Using the ADToken get the sales agent token saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(microsoftId, saAuthorizationToken.AccessToken); #if CREATE_CUSTOMER_SCENARIO // Get input from the console application for creating a new customer var customer = Customer.PopulateCustomerFromConsole(); // This is the created customer object that contains the cid, the microsoft tenant id etc var createdCustomer = Customer.CreateCustomer(customer, resellerCid, saAuthorizationToken.AccessToken); // Populate a multi line item order var newCustomerOrder = Order.PopulateOrderWithMultipleLineItems(createdCustomer.customer.id); // Place the order and subscription uri and entitlement uri are returned per each line item var newCustomerPlacedOrder = Order.PlaceOrder(newCustomerOrder, resellerCid, saAuthorizationToken.AccessToken); foreach (var line_Item in newCustomerPlacedOrder.line_items) { var subscription = Subscription.GetSubscriptionByUri(line_Item.resulting_subscription_uri, saAuthorizationToken.AccessToken); Console.WriteLine("Subscription: {0}", subscription.Id); } #endif #if true string ExistingCustomerMicrosoftId = ConfigurationManager.AppSettings["ExistingCustomerMicrosoftId"]; // You can cache this value too var existingCustomerCid = Customer.GetCustomerCid(ExistingCustomerMicrosoftId, microsoftId, saAuthorizationToken.AccessToken); customerAuthorizationToken = Customer.GetCustomer_Token(existingCustomerCid, adAuthorizationToken); // get the customer entity var customer = Customer.GetCustomer(existingCustomerCid, customerAuthorizationToken.AccessToken); // Get all subscriptions placed by the reseller for the customer var subscriptions = Subscription.GetSubscriptions(existingCustomerCid, resellerCid, saAuthorizationToken.AccessToken); // Get all orders placed by the reseller for this customer var orders = Order.GetOrders(existingCustomerCid, resellerCid, saAuthorizationToken.AccessToken); // Populate a multi line item order var existingCustomerOrder = Order.PopulateOrderFromConsole(existingCustomerCid); // Place the order and subscription uri and entitlement uri are returned per each line item var existingCustomerPlacedOrder = Order.PlaceOrder(existingCustomerOrder, resellerCid, saAuthorizationToken.AccessToken); foreach (var line_Item in existingCustomerPlacedOrder.line_items) { var subscription = Subscription.GetSubscriptionByUri(line_Item.resulting_subscription_uri, saAuthorizationToken.AccessToken); Console.WriteLine("Subscription: {0}", subscription.Id); } #endif } catch (System.FieldAccessException) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n\n\n Looks like you are debugging the application. Inorder to fix this exception: " + "\n 1. In Visual Studio, Right Click on the Project Microsoft.CSP.Api.V1.Samples" + "\n 2. Select the Debug Tab" + "\n 3. Uncheck the option \"Enable the Visual Studio hosting process\" (it is at the bottom of the page)" + "\n 4. Save the changes (File -> Save Selected Items)" + "\n 5. Debug the app now."); Console.Write("Make sure you copy/remember the above steps before exiting the app."); } catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n Make sure the app.config has all the right settings. The defaults in the app.config won't work." + "\n If the settings are correct, its possible you are hitting a service error. Try again." + "\n If the error persists, contact support"); } Console.ResetColor(); Console.Write("\n\n\nHit enter to exit the app..."); Console.ReadLine(); }