コード例 #1
0
        /// <summary>
        /// Get Billing Token from Recurly
        /// </summary>
        /// <param name="request">TokenRequest containing billing information</param>
        /// <returns>TokenResponse</returns>
        internal override async Task <TokenResponse> getBillingToken(TokenRequest request, bool isPreProd)
        {
            string apiPath = Properties.AppSettings.recurlyTokenPath;
            string apiKey  = getApiKey(isPreProd);

            //setup request data
            var formContent = new[]
            {
                new KeyValuePair <string, string>("number", Convert.ToString(request.cardNumber.Replace("-", ""))),
                new KeyValuePair <string, string>("year", Convert.ToString(request.expirationYear)),
                new KeyValuePair <string, string>("month", Convert.ToString(request.expirationMonth)),
                new KeyValuePair <string, string>("cvv", Convert.ToString(request.cardCVV)),
                new KeyValuePair <string, string>("first_name", Convert.ToString(request.firstName)),
                new KeyValuePair <string, string>("last_name", Convert.ToString(request.lastName)),
                new KeyValuePair <string, string>("address1", Convert.ToString(request.street1)),
                new KeyValuePair <string, string>("city", Convert.ToString(request.city)),
                new KeyValuePair <string, string>("state", Convert.ToString(request.state)),
                new KeyValuePair <string, string>("country", Convert.ToString(request.country)),
                new KeyValuePair <string, string>("postal_code", Convert.ToString(request.postalCode)),
                new KeyValuePair <string, string>("key", apiKey)
            };

            //call recurly api endpoint for token
            var apiResponse = await RestHelper.PostFormContentRequest(baseAddress, apiPath, null, formContent);

            //parse the response body to object
            RecurlyTokenResponse recurlyResponse = JsonConvert.DeserializeObject <RecurlyTokenResponse>(apiResponse.Item2);


            //prepare TokenResponse object
            //TokenResponse response = new TokenResponse(recurlyResponse.id, true, creditCard);
            TokenResponse response = new TokenResponse();

            if (recurlyResponse.error != null)
            {
                if ((recurlyResponse.error.fields != null))
                {
                    foreach (var field in recurlyResponse.error.fields)
                    {
                        response.addError(new Error(translateFieldName(field), recurlyResponse.error.message));
                    }
                }
                else
                {
                    response.addError(new Error(translateFieldName(recurlyResponse.error.code), recurlyResponse.error.message));
                }
            }
            else
            {
                //populate the card details for the response from the request
                CreditCard creditCard = new CreditCard(request.expirationMonth, request.expirationYear,
                                                       getTypeOfCreditCard(request.cardNumber),
                                                       request.cardNumber.Substring(request.cardNumber.Length - 4, 4));

                BillingAddress address = new BillingAddress(request.street1, null, request.city,
                                                            request.state, request.postalCode);

                BillingInfo billingInfo = new BillingInfo(recurlyResponse.id, false, address, creditCard);
                response.addBillingInfo(billingInfo);
            }

            return(response);
        }