コード例 #1
0
        // sk_test_E01Kh96YOzRtkhD5wItn8CDd portal api

        public async Task <Dictionary <bool, string> > ProcessCustomerCard(Models.Card card, int amount, string email)
        {
            Dictionary <bool, string> result;
            var customerId = await CheckCustomerHasStripeAccnt(email);

            if (!string.IsNullOrEmpty(customerId))
            {
                StripeToken stripeToken = new StripeToken();

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                StripeConfiguration.SetApiKey(StripeClientConstants.ApiKey);

                var tokenOptions = new StripeTokenCreateOptions()
                {
                    Card = new StripeCreditCardOptions()
                    {
                        Number          = card.Number,
                        ExpirationYear  = card.ExpiryYear,
                        ExpirationMonth = card.ExpiryMonth,
                        Cvc             = card.CVC,
                        Name            = card.Name
                    }
                };
                var tokenService = new StripeTokenService();
                try
                {
                    stripeToken = tokenService.Create(tokenOptions, new StripeRequestOptions()
                    {
                        ApiKey = Stripe.StripeClient.DefaultPublishableKey
                    });
                    var cardOptions = new StripeCardCreateOptions()
                    {
                        SourceToken = stripeToken.Id,
                    };

                    var        cardService = new StripeCardService();
                    StripeCard newCard     = cardService.Create(customerId, cardOptions, new StripeRequestOptions()
                    {
                        ApiKey = _stripeSecretTestkey
                    });
                    return(result = await ChargeCustomerCard(customerId, amount, newCard.Id));
                }
                catch (StripeException e)
                {
                    return(result = new Dictionary <bool, string>()
                    {
                        { false, e.Message }
                    });
                }
            }
            else
            {
                StripeToken stripeToken = new StripeToken();

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                StripeConfiguration.SetApiKey(StripeClientConstants.ApiKey);

                var tokenOptions = new StripeTokenCreateOptions()
                {
                    Card = new StripeCreditCardOptions()
                    {
                        Number          = card.Number,
                        ExpirationYear  = card.ExpiryYear,
                        ExpirationMonth = card.ExpiryMonth,
                        Cvc             = card.CVC,
                        Name            = card.Name
                    }
                };
                var tokenService = new StripeTokenService();
                try
                {
                    stripeToken = tokenService.Create(tokenOptions, new StripeRequestOptions()
                    {
                        ApiKey = Stripe.StripeClient.DefaultPublishableKey
                    });
                }
                catch (StripeException e)
                {
                    return(result = new Dictionary <bool, string>()
                    {
                        { false, e.Message }
                    });
                }
                try
                {
                    var customerOptions = new StripeCustomerCreateOptions()
                    {
                        Email       = email,
                        SourceToken = stripeToken.Id,
                    };

                    var            customerService = new StripeCustomerService();
                    StripeCustomer customer        = customerService.Create(customerOptions, new StripeRequestOptions()
                    {
                        ApiKey = _stripeSecretTestkey
                    });
                    return(result = await ChargeCustomerCard(customer.Id, amount, stripeToken.StripeCard.Id));
                }
                catch (StripeException e)
                {
                    return(result = new Dictionary <bool, string>()
                    {
                        { false, e.Message }
                    });
                }
            }
            return(result = new Dictionary <bool, string>());
        }