Beispiel #1
0
        public static stripe.Customer GetOrCreateCustomer(this stripe.CustomerService customerService, Core.Domain.Customers.Customer customer, IGenericAttributeService genericAttributeService, StripePaymentSettings paymentSettings)
        {
            string stripeCustomerId = genericAttributeService.GetAttribute <string>(customer, paymentSettings.GetCustomerIdKey());

            stripe.Customer result = customerService.GetOrCreateCustomer(customer, stripeCustomerId, paymentSettings);

            if (string.IsNullOrEmpty(stripeCustomerId))
            {
                genericAttributeService.SaveAttribute(customer, paymentSettings.GetCustomerIdKey(), result.Id);
            }

            return(result);
        }
Beispiel #2
0
        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);
            }
        }
Beispiel #3
0
        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);
        }