Example #1
0
        /// <summary>
        /// Adds the described card to Amex.
        /// </summary>
        /// <param name="amexCardSyncRequest">
        /// Description of the card to remove.
        /// </param>
        /// <returns>
        /// The response from Amex for the remove card attempt.
        /// </returns>
        public async Task <AmexCardResponse> RemoveCardAsync(AmexCardUnSyncRequest amexCardUnSyncRequest)
        {
            AmexOAuthResponse amexOAuthResponse = await GetOAuthTokenAsync();

            return(await AddOrRemoveCardAsync(amexCardUnSyncRequest, ConfigurationManager.AppSettings[AmexConstants.AmexRemoveCardUri], amexOAuthResponse));
        }
Example #2
0
        private async Task <AmexCardResponse> AddOrRemoveCardAsync <T>(T requestPayload, string requestUri, AmexOAuthResponse amexOAuthResponse)
        {
            if (amexOAuthResponse != null)
            {
                string     timestamp = ((int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds).ToString();
                string     nonce     = string.Concat(timestamp, ":AMEX");
                UriBuilder uri       = new UriBuilder(requestUri);

                //Format = timestamp \n timestamp:AMEX \n method \n path \n port \n\n
                string message = string.Format("{0}\n{1}\nPOST\n{2}\n{3}\n{4}\n\n", timestamp, nonce, Regex.Replace(HttpUtility.UrlEncode(uri.Path.ToLowerInvariant()), @"%[a-f0-9]{2}", c => c.Value.ToUpper()), uri.Host.ToLowerInvariant(), uri.Port);

                using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(amexOAuthResponse.MacKey)))
                {
                    string mac = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)));
                    // Format -> MAC id="access_token",ts="timestamp",nonce="timestamp:AMEX",mac="HMACSHA256 hash"
                    string    authorizationHeader = string.Format("MAC id=\"{0}\",ts=\"{1}\",nonce=\"{2}\",mac=\"{3}\"", amexOAuthResponse.AccessToken, timestamp, nonce, mac);
                    Stopwatch timer = Stopwatch.StartNew();

                    try
                    {
                        using (HttpClient httpClient = new HttpClient())
                        {
                            httpClient.DefaultRequestHeaders.Accept.Clear();
                            //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("MAC", authorizationHeader.ToString());
                            httpClient.DefaultRequestHeaders.Add("Authorization", authorizationHeader.ToString());
                            httpClient.DefaultRequestHeaders.Add("X-AMEX-API-KEY", ConfigurationManager.AppSettings[AmexConstants.AmexOAuthClientId]);
                            httpClient.DefaultRequestHeaders.Add("X-AMEX-MSG-ID", CommerceLog.ActivityId.ToString("N"));

                            HttpResponseMessage response = await httpClient
                                                           .PostAsJsonAsync(requestUri, requestPayload)
                                                           .ConfigureAwait(false);

                            string content = await response.Content.ReadAsStringAsync();

                            if (response.IsSuccessStatusCode)
                            {
                                CommerceLog.Verbose("Received Amex AddOrRemoveCardAsync response. \r\nUri: {0} \r\nContent: {1}", requestUri, content);
                                return(JsonConvert.DeserializeObject <AmexCardResponse>(content));
                            }
                            else
                            {
                                CommerceLog.Critical("Unable to get Amex AddOrRemoveCardAsync response. \r\nUri: {0} \r\nStatus: {1} \r\nContent: {2}", null, requestUri, response.StatusCode, content);
                            }
                        }
                    }
                    finally
                    {
                        timer.Stop();
                        PerformanceInformation.Add(requestUri, string.Format("{0} ms", timer.ElapsedMilliseconds));
                    }
                }
            }

            return(null);
        }