public PaymentCardInfo AddPaymentCard(CustomerInfo customer, PaymentCardInfo paymentCard)
        {
            HttpClient client = InitializeClient();

            List <KeyValuePair <string, string> > postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("card", paymentCard.CardToken));

            FormUrlEncodedContent postContent = new FormUrlEncodedContent(postData);

            postContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            string requestUrl            = string.Format("customers/{0}/cards", customer.Key);
            HttpResponseMessage response = client.PostAsync(requestUrl, postContent).Result;

            string responseContent = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
            {
                throw new InvalidOperationException(responseContent);
            }

            dynamic cardObject = JsonConvert.DeserializeObject(responseContent);

            PaymentCardInfo createdCard = new PaymentCardInfo();

            createdCard.CardToken      = cardObject.id;
            createdCard.Id             = Guid.NewGuid();
            createdCard.LastFourDigits = cardObject.last4;

            return(createdCard);
        }
        public string GenerateCardToken(PaymentCardInfo paymentCard)
        {
            HttpClient client = InitializeClient();

            List <KeyValuePair <string, string> > postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("card[number]", paymentCard.Number));
            postData.Add(new KeyValuePair <string, string>("card[exp_month]", paymentCard.Expiration.ToString("MM")));
            postData.Add(new KeyValuePair <string, string>("card[exp_year]", paymentCard.Expiration.ToString("yyyy")));
            postData.Add(new KeyValuePair <string, string>("card[cvc]", paymentCard.CVC));

            FormUrlEncodedContent postContent = new FormUrlEncodedContent(postData);

            postContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            HttpResponseMessage response = client.PostAsync("tokens", postContent).Result;

            string responseContent = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
            {
                throw new InvalidOperationException(responseContent);
            }

            dynamic tokenObject = JsonConvert.DeserializeObject(responseContent);

            return(tokenObject.id);
        }
        public PaymentConfirmationInfo SubmitPayment(PaymentCardInfo paymentCard, PaymentInfo payment)
        {
            HttpClient client = InitializeClient();

            //payment amounts are submitted as decimals representing cents
            int totalPayment = (int)(payment.TotalPaymentAmount * 100);

            List <KeyValuePair <string, string> > postData = new List <KeyValuePair <string, string> >();

            postData.Add(new KeyValuePair <string, string>("card", paymentCard.CardToken));
            postData.Add(new KeyValuePair <string, string>("amount", totalPayment.ToString()));
            postData.Add(new KeyValuePair <string, string>("currency", CURRENCY_CODE));

            FormUrlEncodedContent postContent = new FormUrlEncodedContent(postData);

            postContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

            HttpResponseMessage response = client.PostAsync("charges", postContent).Result;

            string responseContent = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
            {
                throw new InvalidOperationException(responseContent);
            }

            dynamic paymentConfirmation = JsonConvert.DeserializeObject(responseContent);

            PaymentConfirmationInfo confirmation = new PaymentConfirmationInfo();

            confirmation.Id = Guid.NewGuid();
            confirmation.ConfirmationToken = paymentConfirmation.id;
            confirmation.Status            = paymentConfirmation.created;

            return(confirmation);
        }