private async Task <Dictionary <bool, string> > ChargeCustomerCard(string customer_Id, int amount, string tokenId) { Dictionary <bool, string> result; StripeConfiguration.SetApiKey(StripeClientConstants.ApiKey); var charges = new StripeChargeService(); try { var charge = charges.Create(new StripeChargeCreateOptions { Amount = amount, Currency = "gbp", Description = "Top up charge", CustomerId = customer_Id, SourceTokenOrExistingSourceId = tokenId, }, new StripeRequestOptions() { ApiKey = _stripeSecretTestkey }); if (charge != null) { await SendPurchaseOntraport(amount, customer_Id); return(result = new Dictionary <bool, string>() { { true, "Topup Success" } }); } else { await SendTagFailedPaymentOntraport(); return(result = new Dictionary <bool, string>() { { false, "Charge Failed" } }); } } catch (StripeException e) { await SendTagFailedPaymentOntraport(); return(result = new Dictionary <bool, string>() { { false, e.Message } }); } }
public async Task <List <RetrievedCard> > GetAllCurrentCards(string email) { var list = new List <RetrievedCard>(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {StripeClientConstants.ApiKey}"); var response = await client.GetStringAsync(_stripeSearchQueryUri + email + "&prefix=false"); var data = JsonConvert.DeserializeObject <RootStripeApiJsonData>(response); var customerId = data.data.Count == 0 ? string.Empty : data.data[0].id; StripeConfiguration.SetApiKey(StripeClientConstants.ApiKey); var cardService = new StripeCardService(); StripeList <StripeCard> cardlist = cardService.List(customerId, new StripeListOptions() { Limit = 5 }, false, new StripeRequestOptions() { ApiKey = _stripeSecretTestkey } ); if (cardlist.Any()) { foreach (var item in cardlist) { list.Add(new RetrievedCard() { Name = item.Name, Last4 = item.Last4, Id = item.Id }); } } else { return(list); } return(list); } }
// 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>()); }