public static stripe.Charge CreateCharge(this ProcessPaymentRequest processPaymentRequest, StripePaymentSettings stripePaymentSettings, CurrencySettings currencySettings, Store store, ICustomerService customerService, ICurrencyService currencyService, IGenericAttributeService genericAttributeService) { int substep = 0; try { var customer = customerService.GetCustomerById(processPaymentRequest.CustomerId); if (customer == null) { throw new NopException("Customer cannot be loaded"); } substep = 1; var currency = currencyService.GetCurrencyById(currencySettings.PrimaryStoreCurrencyId); if (currency == null) { throw new NopException("Primary store currency cannot be loaded"); } substep = 2; if (!Enum.TryParse(currency.CurrencyCode, out StripeCurrency stripeCurrency)) { throw new NopException($"The {currency.CurrencyCode} currency is not supported by Stripe"); } substep = 3; var stripeCustomerService = new stripe.CustomerService(stripePaymentSettings.GetStripeClient()); var chargeService = new stripe.ChargeService(stripePaymentSettings.GetStripeClient()); var tokenService = new stripe.TokenService(stripePaymentSettings.GetStripeClient()); substep = 4; var stripeCustomer = stripeCustomerService.GetOrCreateCustomer(customer, genericAttributeService, stripePaymentSettings); substep = 5; var tokenOptions = processPaymentRequest.CreateTokenOptions(customer, stripeCurrency); substep = 6; var token = tokenService.Create(tokenOptions); substep = 7; var chargeOptions = processPaymentRequest.CreateChargeOptions(store, token, stripePaymentSettings.TransactionMode, stripeCurrency); substep = 8; var charge = chargeService.Create(chargeOptions); substep = 9; return(charge); } catch (Exception ex) { throw new Exception($"Failed at substep {substep}", ex); } }
public static stripe.Charge CreateCharge(this ProcessPaymentRequest processPaymentRequest, StripePaymentSettings stripePaymentSettings, CurrencySettings currencySettings, Store store, ICustomerService customerService, IStateProvinceService stateProvinceService, ICountryService countryService, ICurrencyService currencyService, IGenericAttributeService genericAttributeService) { var customer = customerService.GetCustomerById(processPaymentRequest.CustomerId); if (customer == null) { throw new NopException("Customer cannot be loaded"); } var currency = currencyService.GetCurrencyById(currencySettings.PrimaryStoreCurrencyId); if (currency == null) { throw new NopException("Primary store currency cannot be loaded"); } if (!Enum.TryParse(currency.CurrencyCode, out StripeCurrency stripeCurrency)) { throw new NopException($"The {currency.CurrencyCode} currency is not supported by Stripe"); } var stripeCustomerService = new stripe.CustomerService(stripePaymentSettings.GetStripeClient()); var chargeService = new stripe.ChargeService(stripePaymentSettings.GetStripeClient()); var tokenService = new stripe.TokenService(stripePaymentSettings.GetStripeClient()); var stripeCustomer = stripeCustomerService.GetOrCreateCustomer(customer, genericAttributeService, stripePaymentSettings); var tokenOptions = processPaymentRequest.CreateTokenOptions(customerService, stateProvinceService, countryService, stripeCurrency); var token = tokenService.Create(tokenOptions); var chargeOptions = processPaymentRequest.CreateChargeOptions(store, token, stripePaymentSettings.TransactionMode, stripeCurrency); var charge = chargeService.Create(chargeOptions); return(charge); }
public static bool TryConnect(this StripePaymentSettings stripePaymentSettings) { try { var stripeService = new stripe.CustomerService(stripePaymentSettings.GetStripeClient()); return(stripeService.List() != null); } catch (Exception ex) { return(false); } }
public static stripe.Charge CreateCapture(this CapturePaymentRequest capturePaymentRequest, StripePaymentSettings stripePaymentSettings, Store store) { var chargesService = new stripe.ChargeService(stripePaymentSettings.GetStripeClient()); var chargeId = capturePaymentRequest.Order.AuthorizationTransactionId; stripe.Charge charge = chargesService.Capture(chargeId, new stripe.ChargeCaptureOptions() { StatementDescriptor = $"{store.Name.ToStripeDescriptor()}" }); return(charge); }
public static stripe.Refund CreateRefund(this RefundPaymentRequest refundPaymentRequest, StripePaymentSettings stripePaymentSettings, CurrencySettings currencySettings, ICurrencyService currencyService) { var currency = currencyService.GetCurrencyById(currencySettings.PrimaryStoreCurrencyId); if (currency == null) { throw new NopException("Primary store currency cannot be loaded"); } if (!Enum.TryParse(currency.CurrencyCode, out StripeCurrency stripeCurrency)) { throw new NopException($"The {currency.CurrencyCode} currency is not supported by Stripe"); } var refundService = new stripe.RefundService(stripePaymentSettings.GetStripeClient()); stripe.Refund refund = refundService.Create(new stripe.RefundCreateOptions() { Amount = (int)(refundPaymentRequest.AmountToRefund * 100), Charge = refundPaymentRequest.Order.CaptureTransactionId }); return(refund); }