Beispiel #1
0
        public void FixtureSetup()
        {
            SqlSyntaxProviderTestHelper.EstablishSqlSyntax();

            var cacheProvider = new Mock<IRuntimeCacheProvider>();

            GatewayProviderService = new GatewayProviderService();

            var providers =
                GatewayProviderService.GetAllGatewayProviders()
                    .Where(x => x.GatewayProviderType == GatewayProviderType.Payment);

            GatewayProviderSettings = providers.FirstOrDefault(x => x.Key == new Guid("15C87B6F-7987-49D9-8444-A2B4406941A8"));

            if (GatewayProviderSettings != null)
            {
                GatewayProviderService.Delete(GatewayProviderSettings);
            }

            var petaPoco = new PetaPocoUnitOfWorkProvider();

            var xLogin = ConfigurationManager.AppSettings["xlogin"];
            var xtrankey = ConfigurationManager.AppSettings["xtrankey"];

            var sql = new Sql();

            var dto = new GatewayProviderSettingsDto()
            {
                Key = new Guid("15C87B6F-7987-49D9-8444-A2B4406941A8"),
                Name = "Stripe",
                Description = "Stripe",
                ExtendedData = "<extendedData />",
                EncryptExtendedData = false,
                ProviderTfKey = Constants.TypeFieldKeys.GatewayProvider.PaymentProviderKey,
                CreateDate = DateTime.Now,
                UpdateDate = DateTime.Now
            };


            petaPoco.GetUnitOfWork().Database.Insert(dto);

            GatewayProviderSettings =
                GatewayProviderService.GetGatewayProviderByKey(new Guid("15C87B6F-7987-49D9-8444-A2B4406941A8"));

            var providerSettings = new StripeProcessorSettings()
            {
                // TODO
                //LoginId = xLogin,
                //TransactionKey = xtrankey
            };

            GatewayProviderSettings.ExtendedData.SaveProcessorSettings(providerSettings);

            Provider = new StripePaymentGatewayProvider(GatewayProviderService, GatewayProviderSettings,
                cacheProvider.Object);
        }
Beispiel #2
0
        public void Init()
        {

            var billTo = new Address()
            {
                Organization = "Flightpath",
                Address1 = "36 West 25th Street",
                Locality = "New York",
                Region = "NY",
                PostalCode = "10010",
                CountryCode = "US",
                Email = "*****@*****.**",
                Phone = "212-555-5555"
            };

            // create an invoice
            var invoiceService = new InvoiceService();

            _invoice = invoiceService.CreateInvoice(Core.Constants.DefaultKeys.InvoiceStatus.Unpaid);

            _invoice.SetBillingAddress(billTo);

            _invoice.Total = 120M;
            var extendedData = new ExtendedDataCollection();

            // this is typically added automatically in the checkout workflow
            extendedData.SetValue(Core.Constants.ExtendedDataKeys.CurrencyCode, "USD");
            
            // make up some line items
            var l1 = new InvoiceLineItem(LineItemType.Product, "Item 1", "I1", 10, 1, extendedData);
            var l2 = new InvoiceLineItem(LineItemType.Product, "Item 2", "I2", 2, 40, extendedData);
            var l3 = new InvoiceLineItem(LineItemType.Shipping, "Shipping", "shipping", 1, 10M, extendedData);
            var l4 = new InvoiceLineItem(LineItemType.Tax, "Tax", "tax", 1, 10M, extendedData);

            _invoice.Items.Add(l1);
            _invoice.Items.Add(l2);
            _invoice.Items.Add(l3);
            _invoice.Items.Add(l4);

            var processorSettings = new StripeProcessorSettings
            {
                ApiKey = ConfigurationManager.AppSettings["stripeApiKey"]
            };

            Provider.GatewayProviderSettings.ExtendedData.SaveProcessorSettings(processorSettings);

            if (Provider.PaymentMethods.Any()) return;
            var resource = Provider.ListResourcesOffered().ToArray();
            Provider.CreatePaymentMethod(resource.First(), "Credit Card", "Credit Card");
        }
        /// <summary>
        /// Gets a single use token that can be used in place of a credit card details. 
        /// The token can be used once for creating a new charge. 
        /// </summary>
        /// <param name="creditCard"></param>
        /// <param name="address"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static string GetCardToken(CreditCardFormData creditCard, IAddress address, StripeProcessorSettings settings)
        {
            var requestParams = new NameValueCollection();
            requestParams.Add("card[number]", creditCard.CardNumber);
            requestParams.Add("card[exp_month]", creditCard.ExpireMonth);
            requestParams.Add("card[exp_year]", creditCard.ExpireYear);
            requestParams.Add("card[cvc]", creditCard.CardCode);
            requestParams.Add("card[name]", creditCard.CardholderName);

            if (address != null)
            {
                requestParams.Add("card[address_line1]", address.Address1);
                requestParams.Add("card[address_line2]", address.Address2);
                requestParams.Add("card[address_city]", address.Locality);
                if (!string.IsNullOrEmpty(address.Region))
                    requestParams.Add("card[address_state]", address.Region);
                requestParams.Add("card[address_zip]", address.PostalCode);
                if (!string.IsNullOrEmpty(address.CountryCode))
                    requestParams.Add("card[address_country]", address.CountryCode);
            }

            string postData =
                requestParams.AllKeys.Aggregate("",
                    (current, key) => current + (key + "=" + HttpUtility.UrlEncode(requestParams[key]) + "&"))
                    .TrimEnd('&');

            // https://stripe.com/docs/api#create_card_token
            var response = StripeHelper.MakeStripeApiRequest("https://api.stripe.com/v1/tokens", "POST", requestParams, settings);
            string apiResponse = null;
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                apiResponse = reader.ReadToEnd();
            }
            JObject responseJson = JObject.Parse(apiResponse);
            switch (response.StatusCode)
            {
                case HttpStatusCode.OK: // 200
                    return (string) responseJson["id"];

                default:

                    throw new Exception("Stripe error");
            }
        }
 public StripePaymentProcessor(StripeProcessorSettings settings)
 {
     _settings = settings;
 }
        public static HttpWebResponse MakeStripeApiRequest(string apiUrl, string httpMethod,
            NameValueCollection requestParameters, StripeProcessorSettings settings)
        {
            string postData = requestParameters == null
                ? ""
                : requestParameters.AllKeys.Aggregate("",
                    (current, key) => current + (key + "=" + HttpUtility.UrlEncode(requestParameters[key]) + "&"))
                    .TrimEnd('&');

            var request = (HttpWebRequest) WebRequest.Create(apiUrl);
            request.Method = httpMethod;
            request.ContentLength = postData.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Authorization", BasicAuthToken(settings.ApiKey));
            request.Headers.Add("Stripe-Version", StripePaymentProcessor.ApiVersion);
            request.UserAgent = "Merchello (https://github.com/Merchello/Merchello)";
            if (requestParameters != null)
            {
                using (var writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(postData);
                }
            }

            return (HttpWebResponse) request.GetResponse();
        }
        /// <summary>
        /// Saves the processor settings to an extended data collection
        /// </summary>
        /// <param name="extendedData">The <see cref="ExtendedDataCollection"/></param>
        /// <param name="processorSettings">The <see cref="StripeProcessorSettings"/> to be serialized and saved</param>
        public static void SaveProcessorSettings(this ExtendedDataCollection extendedData, StripeProcessorSettings processorSettings)
        {
            var settingsJson = JsonConvert.SerializeObject(processorSettings);

            extendedData.SetValue(Constants.ExtendedDataKeys.ProcessorSettings, settingsJson);
        }