public string Payment(string userId, string storeId, string cartId) { try { var customers = new StripeCustomerService(); var charges = new StripeChargeService(); var userCardDetails = _receiptRepository.GetUserCardDetails(userId); if (userCardDetails == null) { throw new Exception("Kreditkortsuppgifter existerar inte."); } else { if (string.IsNullOrWhiteSpace(userCardDetails.CreditCardId)) { StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripPublishKey"]); var tokenService = new StripeTokenService(); // Get token if user card is not created var cardnumber = _cryptoGraphy.DecryptString(userCardDetails.CardNumber); var token = tokenService.Create(new StripeTokenCreateOptions { Card = new StripeCreditCardOptions { Cvc = userCardDetails.CVC.ToString(), Number = cardnumber.Replace(" ", ""), ExpirationMonth = userCardDetails.ExpiredMonth, ExpirationYear = userCardDetails.ExpiredYear } }); // Create customer in stripe StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripSecretKey"]); var customerId = customers.Create(new StripeCustomerCreateOptions { SourceToken = token.Id }); _receiptRepository.UpdateUserCreditCardId(customerId.Id, userId); //update customer id in database for future reference var cartAmount = _receiptRepository.GetCartAmount(cartId); var tmpTotalAmount = (int)cartAmount; var vat1 = (tmpTotalAmount * CommonConstants.Vat1InPercentage) / 100; var vat2 = (tmpTotalAmount * CommonConstants.Vat2InPercentage) / 100; var totalAmount = tmpTotalAmount + vat1 + vat2; var message = string.Empty; // Payment process if (totalAmount > 0) { var paymentResponse = charges.Create(new StripeChargeCreateOptions { Amount = totalAmount, Description = "Lifvs purchase item payment", Currency = "SEK", CustomerId = customerId.Id }); if (paymentResponse.Status.Equals("succeeded", StringComparison.CurrentCultureIgnoreCase) && string.IsNullOrWhiteSpace(paymentResponse.FailureMessage)) { var receiptId = _receiptRepository.AddReceiptItems(userId, storeId, cartId); if (receiptId > 0) { message = _receiptRepository.CreateTransaction(userId, paymentResponse.Status, paymentResponse.Id, paymentResponse.StripeResponse.ResponseJson, receiptId.ToString()); } var receiptItmes = _receiptRepository.GetAllReceiptItems(receiptId.ToString()); if (receiptItmes != null) { SendEmail(receiptItmes[0].Email, paymentResponse.Id, DateTime.Now, storeId, receiptItmes); // send receipt in mail } return(message); } else { throw new Exception(paymentResponse.FailureMessage); } } else { message = "Det finn ingen produkt i din varukorg så du behöver inte skapa ett kvitto."; return(message); } } else { StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripSecretKey"]); var cartAmount = _receiptRepository.GetCartAmount(cartId); var tmpTotalAmount = (int)cartAmount; var vat1 = (tmpTotalAmount * CommonConstants.Vat1InPercentage) / 100; var vat2 = (tmpTotalAmount * CommonConstants.Vat2InPercentage) / 100; var totalAmount = tmpTotalAmount + vat1 + vat2; var message = string.Empty; // Payment process if (totalAmount > 0) { var paymentResponse = charges.Create(new StripeChargeCreateOptions { Amount = totalAmount, Description = "Lifvs purchase item payment", Currency = "SEK", CustomerId = userCardDetails.CreditCardId }); if (paymentResponse.Status.Equals("succeeded", StringComparison.CurrentCultureIgnoreCase) && string.IsNullOrWhiteSpace(paymentResponse.FailureMessage)) { var receiptId = _receiptRepository.AddReceiptItems(userId, storeId, cartId); if (receiptId > 0) { message = _receiptRepository.CreateTransaction(userId, paymentResponse.Status, paymentResponse.Id, paymentResponse.StripeResponse.ResponseJson, receiptId.ToString()); } var receiptItmes = _receiptRepository.GetAllReceiptItems(receiptId.ToString()); if (receiptItmes != null) { SendEmail(receiptItmes[0].Email, paymentResponse.Id, DateTime.Now, storeId, receiptItmes); // send receipt in mail } return(message); } else { throw new Exception(paymentResponse.FailureMessage); } } else { _receiptRepository.AddReceiptItems(userId, storeId, cartId); message = "Det finn ingen produkt i din varukorg så du behöver inte skapa ett kvitto."; return(message); } } } } catch (Exception ex) { throw ex; } }
public void Payment(string userId, string storeId, string cartId) { try { var customers = new StripeCustomerService(); var charges = new StripeChargeService(); var userCardDetails = _receiptRepository.GetUserCardDetails(userId); if (userCardDetails == null) { throw new Exception("Kreditkortsuppgifter existerar inte."); } else { if (string.IsNullOrWhiteSpace(userCardDetails.CreditCardId)) { StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripPublishKey"]); var tokenService = new StripeTokenService(); // Get token if user card is not created var cardnumber = _cryptoGraphy.DecryptString(userCardDetails.CardNumber); var token = tokenService.Create(new StripeTokenCreateOptions { Card = new StripeCreditCardOptions { Cvc = userCardDetails.CVC.ToString(), Number = cardnumber.Replace(" ", ""), ExpirationMonth = userCardDetails.ExpiredMonth, ExpirationYear = userCardDetails.ExpiredYear } }); // Create customer in stripe StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripSecretKey"]); var customerId = customers.Create(new StripeCustomerCreateOptions { SourceToken = token.Id }); _receiptRepository.UpdateUserCreditCardId(customerId.Id, userId); //update customer id in database for future reference var cartAmount = _receiptRepository.GetCartAmount(cartId); var tmpTotalAmount = (int)cartAmount; var vat1 = (tmpTotalAmount * CommonConstants.Vat1InPercentage) / 100; var vat2 = (tmpTotalAmount * CommonConstants.Vat2InPercentage) / 100; var totalAmount = tmpTotalAmount + vat1 + vat2; // Payment process if (totalAmount > 0) { var paymentResponse = charges.Create(new StripeChargeCreateOptions { Amount = totalAmount, Description = "Lifvs purchase item payment", Currency = "SEK", CustomerId = customerId.Id }); if (paymentResponse.Status.Equals("succeeded", StringComparison.CurrentCultureIgnoreCase) && string.IsNullOrWhiteSpace(paymentResponse.FailureMessage)) { var receiptId = _receiptRepository.AddReceiptItems(userId, storeId, cartId); if (receiptId > 0) { _receiptRepository.CreateTransaction(userId, paymentResponse.Status, paymentResponse.Id, paymentResponse.StripeResponse.ResponseJson, receiptId.ToString()); } var receiptItmes = _receiptRepository.GetAllReceiptItems(receiptId.ToString()); var user = _user.GetUser(Convert.ToInt64(userId)); SendEmail(user.Email, paymentResponse.Id, DateTime.Now.Date, storeId, receiptItmes); } else { throw new Exception(paymentResponse.FailureMessage); } } } else { StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["StripSecretKey"]); var cartAmount = _receiptRepository.GetCartAmount(cartId); var tmpTotalAmount = (int)cartAmount; var vat1 = (tmpTotalAmount * CommonConstants.Vat1InPercentage) / 100; var vat2 = (tmpTotalAmount * CommonConstants.Vat2InPercentage) / 100; var totalAmount = tmpTotalAmount + vat1 + vat2; // Payment process if (totalAmount > 0) { var paymentResponse = charges.Create(new StripeChargeCreateOptions { Amount = totalAmount, Description = "Lifvs purchase item payment", Currency = "SEK", CustomerId = userCardDetails.CreditCardId }); if (paymentResponse.Status.Equals("succeeded", StringComparison.CurrentCultureIgnoreCase) && string.IsNullOrWhiteSpace(paymentResponse.FailureMessage)) { var receiptId = _receiptRepository.AddReceiptItems(userId, storeId, cartId); if (receiptId > 0) { _receiptRepository.CreateTransaction(userId, paymentResponse.Status, paymentResponse.Id, paymentResponse.StripeResponse.ResponseJson, receiptId.ToString()); } var receiptItmes = _receiptRepository.GetAllReceiptItems(receiptId.ToString()); var user = _user.GetUser(Convert.ToInt64(userId)); SendEmail(user.Email, paymentResponse.Id, DateTime.Now.Date, storeId, receiptItmes); } else { throw new Exception(paymentResponse.FailureMessage); } } } } } catch (StripeException ex) { switch (ex.StripeError.ErrorType) { case "card_error": throw new Exception(ex.StripeError.Message); case "api_connection_error": break; case "api_error": break; case "authentication_error": break; case "invalid_request_error": break; case "rate_limit_error": break; case "validation_error": break; default: // Unknown Error Type break; } } }