/// <summary> /// Get the latest AD token given the reseller domain and client credentials /// </summary> /// <param name="domain">domain of the reseller</param> /// <param name="clientId">clientID of the application</param> /// <param name="clientSecret">client secret of the application, also refered to as key</param> /// <param name="adAuthorizationToken">ad authorization token, can be null</param> /// <returns>Latest AD Authorization token</returns> public static AuthorizationToken GetAD_Token(string domain, string clientId, string clientSecret, AuthorizationToken adAuthorizationToken = null) { if (adAuthorizationToken == null || (adAuthorizationToken != null && adAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var adToken = GetADToken(domain, clientId, clientSecret); adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); } return adAuthorizationToken; }
/// <summary> /// Get the token for authenticating requests to Azure Resource Manager in customer tenant. /// </summary> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="credentialName">Internet or network address of entry in Credential Manager (Windows Credentials / Generic Credential) </param> /// <param name="customerTenantId">cid of the customer</param> /// <param name="azureAuthToken">Existing token (if available)</param> /// <returns>Azure Auth Token in the context of the customer tenant.</returns> public static string GetAzureAuthTokenForCustomerTenant(string appId, string credentialName, string customerTenantId, AuthorizationToken azureAuthToken = null) { if (azureAuthToken == null || (azureAuthToken != null && azureAuthToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var azToken = GetAzureAuthTokenForCustomerTenant(appId, credentialName, customerTenantId); azureAuthToken = new AuthorizationToken(azToken.AccessToken, azToken.ExpiresOn); } return(azureAuthToken.AccessToken); }
/// <summary> /// Get the token for authenticating requests to Azure Resource Manager in customer tenant. /// </summary> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="credentialName">Internet or network address of entry in Credential Manager (Windows Credentials / Generic Credential) </param> /// <param name="customerTenantId">cid of the customer</param> /// <param name="azureAuthToken">Existing token (if available)</param> /// <returns>Azure Auth Token in the context of the customer tenant.</returns> public static string GetAzureAuthTokenForCustomerTenant(string appId, string credentialName, string customerTenantId, AuthorizationToken azureAuthToken = null) { if (azureAuthToken == null || (azureAuthToken != null && azureAuthToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var azToken = GetAzureAuthTokenForCustomerTenant(appId, credentialName, customerTenantId); azureAuthToken = new AuthorizationToken(azToken.AccessToken, azToken.ExpiresOn); } return azureAuthToken.AccessToken; }
/// <summary> /// Get the latest AD token given the reseller domain and client credentials /// </summary> /// <param name="domain">domain of the reseller</param> /// <param name="clientId">clientID of the application</param> /// <param name="clientSecret">client secret of the application, also refered to as key</param> /// <param name="adAuthorizationToken">ad authorization token, can be null</param> /// <returns>Latest AD Authorization token</returns> public static AuthorizationToken GetAD_Token(string domain, string clientId, string clientSecret, AuthorizationToken adAuthorizationToken = null) { if (adAuthorizationToken == null || (adAuthorizationToken != null && adAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var adToken = GetADToken(domain, clientId, clientSecret); adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); } return(adAuthorizationToken); }
/// <summary> /// Get the latest sales agent token given the AD Authorization Token /// </summary> /// <param name="adAuthorizationToken">AD Authorization Token</param> /// <param name="saAuthorizationToken">Sales agent authorization token, can be null</param> /// <returns>Latest sales agent token</returns> public static AuthorizationToken GetSA_Token(AuthorizationToken adAuthorizationToken, AuthorizationToken saAuthorizationToken = null) { if (saAuthorizationToken == null || (saAuthorizationToken != null && saAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var saToken = GetSA_Token(adAuthorizationToken.AccessToken); saAuthorizationToken = new AuthorizationToken(saToken.access_token, Convert.ToInt64(saToken.expires_in)); } return saAuthorizationToken; }
/// <summary> /// This method is used to get the customer token given a customer cid and the ad token /// </summary> /// <param name="customerCid">cid of the customer</param> /// <param name="adAuthorizationToken">active directory authorization token</param> /// <param name="customerAuthorizationToken">customer authorization token if available</param> /// <returns>customer authorization token</returns> public static AuthorizationToken GetCustomer_Token(string customerCid, AuthorizationToken adAuthorizationToken, AuthorizationToken customerAuthorizationToken = null) { if (customerAuthorizationToken == null || (customerAuthorizationToken != null && customerAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var customerToken = GetCustomer_Token(customerCid, adAuthorizationToken.AccessToken); customerAuthorizationToken = new AuthorizationToken(customerToken.access_token, Convert.ToInt64(customerToken.expires_in)); } return(customerAuthorizationToken); }
/// <summary> /// Get all orders placed by the reseller for this customer /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Partner Center</param> /// <param name="key">Key for this application in Partner Center</param> /// <param name="customerMicrosoftId">Microsoft Id of the customer</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> /// <returns>object that contains orders</returns> public static dynamic GetOrderById(string defaultDomain, string appId, string key, string orderId, string resellerMicrosoftId) { // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken); return(Order.GetOrderById(resellerCid, orderId, saAuthorizationToken.AccessToken)); }
/// <summary> /// Get the latest sales agent token given the AD Authorization Token /// </summary> /// <param name="adAuthorizationToken">AD Authorization Token</param> /// <param name="saAuthorizationToken">Sales agent authorization token, can be null</param> /// <returns>Latest sales agent token</returns> public static AuthorizationToken GetSA_Token(AuthorizationToken adAuthorizationToken, AuthorizationToken saAuthorizationToken = null) { if (saAuthorizationToken == null || (saAuthorizationToken != null && saAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var saToken = GetSA_Token(adAuthorizationToken.AccessToken); saAuthorizationToken = new AuthorizationToken(saToken.access_token, Convert.ToInt64(saToken.expires_in)); } return(saAuthorizationToken); }
/// <summary> /// Get the latest AD token given the reseller domain and client credentials /// </summary> /// <param name="domain">domain of the reseller</param> /// <param name="clientId">clientID of the application</param> /// <param name="clientSecret">client secret of the application, also refered to as key</param> /// <param name="adAuthorizationToken">ad authorization token, can be null</param> /// <returns>Latest AD Authorization token</returns> public static AuthorizationToken GetAD_Token(string domain, string clientId, string clientSecret, AuthorizationToken adAuthorizationToken = null) { if (adAuthorizationToken == null || (adAuthorizationToken != null && adAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry //var adToken = GetADToken(domain, clientId, clientSecret); //adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); // MOCK API simulates tokens var adToken = Json.Decode("{\"access_token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ijd3YzlFTnVyWGZaRWt5VkxfZkM5cTZpeHNMRSJ9.eyJpc3MiOiJ1cm46Y3BzdHMiLCJhdWQiOiJ1cm46Y3BzdmM6Y2E6ODZmZDM1YjktMzhhMi00MTNjLWE5YjEtMzNiYjc1ZjNiZDRmIiwibmJmIjoxNDM4MzY5OTIyLCJleHAiOjE0MzgzNzA4MjIsIm5hbWVpZCI6IjkwNDg1QTVFLUQyRTMtNDU5RC04NzFFLUM1OTVBQzhBMEUzNiIsInVuaXF1ZV9uYW1lIjoiVGhpcmRQYXJ0eUFwcCIsIklkZW50aWZpZXJJZCI6Ijg2ZmQzNWI5LTM4YTItNDEzYy1hOWIxLTMzYmI3NWYzYmQ0ZiIsImFwcGlkIjoiNGIzZGFkOWMtZTUxNi00NzZmLTlmOTItMmQ3OWRhOWZjYmNmIiwiQ2FpZCI6Ijg2ZmQzNWI5LTM4YTItNDEzYy1hOWIxL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pCWTNSUGJsSmxjMlZzYkdWeVEyaGhibTVsYkNKOS4ifQ.GHg0RLBo-rdX4CINR6N54rL5m741df4QWh_aRyQW2gIbtqpOHZlJ-3CYBHtgy-rcfMFXia2N1BppBFZ4rGLuqRtyyStUDbWMoMVyMU2LwXyUyNVtiEg5K7YY9KaZLqzEgMxe5VM9HLwC0evM6yyjXiFABr7mTJe4OOpJcPXDAvh2DKjA5reqf4y9HODth1GLpN0bsNNTcGzeOP2rsEsEwMIaUeP0mBE60fVQ3lR5yt1_gZlUObi_CIC_nAnW3l5Je1JtWsYzq4AlLhnGrfsywi5V2TQLp6Wo6w0x6btZNBEExOryS691flzL6Cyr_PUXKSH0_-rgEu_s3zWvSZJoqw\",\"token_type\":\"bearer\",\"expires_in\":900}"); adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); } return adAuthorizationToken; }
/// <summary> /// Get the latest AD token given the reseller domain and client credentials /// </summary> /// <param name="domain">domain of the reseller</param> /// <param name="clientId">clientID of the application</param> /// <param name="clientSecret">client secret of the application, also refered to as key</param> /// <param name="adAuthorizationToken">ad authorization token, can be null</param> /// <returns>Latest AD Authorization token</returns> public static AuthorizationToken GetAD_Token(string domain, string clientId, string clientSecret, AuthorizationToken adAuthorizationToken = null) { if (adAuthorizationToken == null || (adAuthorizationToken != null && adAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry //var adToken = GetADToken(domain, clientId, clientSecret); //adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); // MOCK API simulates tokens var adToken = Json.Decode("{\"access_token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ijd3YzlFTnVyWGZaRWt5VkxfZkM5cTZpeHNMRSJ9.eyJpc3MiOiJ1cm46Y3BzdHMiLCJhdWQiOiJ1cm46Y3BzdmM6Y2E6ODZmZDM1YjktMzhhMi00MT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VEwWmk4aUxDSmhjSEJwWkdGamNpSTZJakVpTENKdVlXMWxhV1FpT2lJNU1EUTROVUUxUlMxRU1rVXpMVFExT1VRdE9EY3hSUzFETlRrMVFVTTRRVEJGTXpZaUxDSjFibWx4ZFdWZmJtRnRaU0k2SWxSb2FYSmtVR0Z5ZEhsQmNIQWlMQ0poY0hCcFpDSTZJalJpTTJSaFpEbGpMV1UxTVRZdE5EYzJaaTA1WmpreUxUSmtOemxrWVRsbVkySmpaaUlzSWtsa1pXNTBhV1pwWlhKSlpDSTZJamcyWm1Rek5XSTVMVE00WVRJdE5ERXpZeTFoT1dJeExUTXpZbUkzTldZelltUTBaaUlzSW5KdmJHVWlPaUpCWTNSUGJsSmxjMlZzYkdWeVEyaGhibTVsYkNKOS4ifQ.GHg0RLBo-rdX4CINR6N54rL5m741df4QWh_aRyQW2gIbtqpOHZlJ-3CYBHtgy-rcfMFXia2N1BppBFZ4rGLuqRtyyStUDbWMoMVyMU2LwXyUyNVtiEg5K7YY9KaZLqzEgMxe5VM9HLwC0evM6yyjXiFABr7mTJe4OOpJcPXDAvh2DKjA5reqf4y9HODth1GLpN0bsNNTcGzeOP2rsEsEwMIaUeP0mBE60fVQ3lR5yt1_gZlUObi_CIC_nAnW3l5Je1JtWsYzq4AlLhnGrfsywi5V2TQLp6Wo6w0x6btZNBEExOryS691flzL6Cyr_PUXKSH0_-rgEu_s3zWvSZJoqw\",\"token_type\":\"bearer\",\"expires_in\":900}"); adAuthorizationToken = new AuthorizationToken(adToken.access_token, Convert.ToInt64(adToken.expires_in)); } return(adAuthorizationToken); }
/// <summary> /// Gets the customer entity for the given customer id /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="customerMicrosoftId">Microsoft Id of the customer</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> /// <returns>customer object</returns> public static dynamic GetCustomer(string defaultDomain, string appId, string key, string customerMicrosoftId, string resellerMicrosoftId) { // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); var existingCustomerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId, saAuthorizationToken.AccessToken); // Get Customer token AuthorizationToken customerAuthorizationToken = Customer.GetCustomer_Token(existingCustomerCid, adAuthorizationToken); return(Customer.GetCustomer(existingCustomerCid, customerAuthorizationToken.AccessToken)); }
/// <summary> /// Create a customer /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Partner Center</param> /// <param name="key">Key for this application in Partner Center</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> public static void CreateCustomer(string defaultDomain, string appId, string key, string resellerMicrosoftId) { // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken); // 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); }
/// <summary> /// Get all subscriptions placed by the reseller for the customer /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="key">Key for this application in Azure Active Directory</param> /// <param name="customerMicrosoftId">Microsoft Id of the customer</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> /// <returns>object that contains all of the subscriptions</returns> public static dynamic GetSubscriptions(string defaultDomain, string appId, string key, string customerMicrosoftId, string resellerMicrosoftId) { // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken); // You can cache this value too var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId, saAuthorizationToken.AccessToken); return(Subscription.GetSubscriptions(customerCid, resellerCid, saAuthorizationToken.AccessToken)); }
/// <summary> /// Get the latest partner center api token given the AD Authorization Token /// </summary> /// <param name="adAuthorizationToken">AD Authorization Token</param> /// <param name="clientId">clientID of the application</param> /// <param name="partnerCenterApiAuthorizationToken">partner center api authorization token, can be null</param> /// <returns>Latest partner center api authorization token</returns> public static AuthorizationToken GetPartnerCenterApi_Token(AuthorizationToken adAuthorizationToken, string clientId, AuthorizationToken partnerCenterApiAuthorizationToken = null) { if (partnerCenterApiAuthorizationToken == null || (partnerCenterApiAuthorizationToken != null && partnerCenterApiAuthorizationToken.IsNearExpiry()) ) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var partnerCenterApiToken = GetPartnerCenterApi_Token(adAuthorizationToken.AccessToken, clientId); var accessToken = partnerCenterApiToken.access_token.ToString(); var expiresInOffset = System.Convert.ToInt64(partnerCenterApiToken.expires_in); partnerCenterApiAuthorizationToken = new AuthorizationToken(accessToken, expiresInOffset); } return(partnerCenterApiAuthorizationToken); }
/// <summary> /// Create a new customer /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="key">Key for this application in Azure Active Directory</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> /// <returns></returns> internal static string CreateCustomer(string defaultDomain, string appId, string key, string resellerMicrosoftId) { // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken); // 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); if (createdCustomer == null) { throw new Exception("Error creating customer"); } string newCustomerMicrosoftId = createdCustomer.customer.identity.data.tid; // 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); } return(newCustomerMicrosoftId); }
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); // 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(); }
/// <summary> /// Get a customer's usage information for the last 1 month, calculates the total cost using RateCard API /// and Suspends the subscription if the total cost is more than the credit limit. /// </summary> /// <param name="defaultDomain">default domain of the reseller</param> /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param> /// <param name="key">Key for this application in Azure Active Directory</param> /// <param name="customerMicrosoftId">Microsoft Id of the customer</param> /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param> public static void GetRateCardAndUsage(string defaultDomain, string appId, string key, string customerMicrosoftId, string resellerMicrosoftId) { var correlationId = Guid.NewGuid().ToString(); // Get Active Directory token first AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key); // Using the ADToken get the sales agent token AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken); // Get the Reseller Cid, you can cache this value string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken); // You can cache this value too var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId, saAuthorizationToken.AccessToken); // Get Customer token AuthorizationToken customerAuthorizationToken = Customer.GetCustomer_Token(customerCid, adAuthorizationToken); // Gets the RateCard to get the prices var rateCard = RateCard.GetRateCard(resellerCid, saAuthorizationToken.AccessToken); var startTime = String.Format("{0:u}", DateTime.Today.AddDays(-30)); var endTime = String.Format("{0:u}", DateTime.Today.AddDays(-1)); // Get all of a Customer's entitlements var entitlements = Usage.GetEntitlements(customerCid, customerAuthorizationToken.AccessToken); try { foreach (var entitlement in entitlements.items) { // Get the usage for the given entitlement for the last 1 month var usageRecords = Usage.GetUsageRecords(resellerCid, entitlement.id, saAuthorizationToken.AccessToken, startTime, endTime); if (usageRecords.items.Count > 0) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("================================================================================"); Console.WriteLine("\nPrices for Entitlement: {0}", entitlement.id); Console.WriteLine("================================================================================"); double totalCost = 0; // Looping through the usage records to calculate the cost of each item foreach (UsageType usageRecord in usageRecords.items) { string meterId = usageRecord.meter_id; // Gets the price corresponding to the given meterId from the ratecard. Console.WriteLine("\nMeter Name\t\t: {0}", usageRecord.meter_name); double includedQty = Usage.GetIncludedQuantityByMeterID(rateCard, meterId); Console.WriteLine("Included Quantity\t\t: {0}", includedQty); double consumedQty = (double)usageRecord.quantity; Console.WriteLine("Consumed Quantity\t\t: {0}", consumedQty); double ratableUsage = Usage.GetRatableUsage(consumedQty, includedQty); double cost = Usage.computeRatedUsagePerMeter(Usage.GetRatesForMeterID(rateCard, meterId), ratableUsage); Console.WriteLine("Cost\t\t: {0}", cost); totalCost += cost; } Console.WriteLine("\nTOTAL COST: {0}", totalCost); // Setting the credit limit below the total cost for testing this scenario double creditLimit = 100; // Suspends the subscription if the total cost is above the credit limit. if (totalCost > creditLimit) { var subscription = Subscription.GetSubscriptionByUri(entitlement.billing_subscription_uri, saAuthorizationToken.AccessToken); Subscription.SuspendSubscription(subscription.id, resellerCid, saAuthorizationToken.AccessToken); } } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
/// <summary> /// This method is used to get the customer token given a customer cid and the ad token /// </summary> /// <param name="customerCid">cid of the customer</param> /// <param name="adAuthorizationToken">active directory authorization token</param> /// <param name="customerAuthorizationToken">customer authorization token if available</param> /// <returns>customer authorization token</returns> public static AuthorizationToken GetCustomer_Token(string customerCid, AuthorizationToken adAuthorizationToken, AuthorizationToken customerAuthorizationToken = null) { if (customerAuthorizationToken == null || (customerAuthorizationToken != null && customerAuthorizationToken.IsNearExpiry())) { //// Refresh the token on one of two conditions //// 1. If the token has never been retrieved //// 2. If the token is near expiry var customerToken = GetCustomer_Token(customerCid, adAuthorizationToken.AccessToken); customerAuthorizationToken = new AuthorizationToken(customerToken.access_token, Convert.ToInt64(customerToken.expires_in)); } return customerAuthorizationToken; }