将Java代码重写为C#,以便使用条带网关进行付款

时间:2015-05-21 16:09:31

标签: c# asp.net .net stripe-payments

我有这个java代码,我想制作同样的C#代码。

Stripe.apiKey = "sk_test_2uoGykz3oyw8GNHWiBVEQY7N";
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 400);
chargeParams.put("currency", "usd");
chargeParams.put("source", "tok_160DSpIZJBCsfX4YCKJKjg2O"); // obtained with Stripe.js
chargeParams.put("description", "Charge for test@example.com");
Charge.create(chargeParams);

2 个答案:

答案 0 :(得分:1)

使用(C#)WCF休息服务进行条带支付。此代码对于条带网关付款是正确的。你不需要任何条带DLL。这将有助于付款。

PrivateKey = WebConfigurationManager.AppSettings["StripeApiKey"].ToString();
                StringBuilder postUrl = new StringBuilder();
                postUrl.Append("&card=");
                postUrl.Append(paymentDetail.Token);
                postUrl.Append("&currency=USD");
                postUrl.Append("&amount=");
                postUrl.Append(paymentDetail.Amount);                
                byte[] formbytes = System.Text.ASCIIEncoding.Default.GetBytes(postUrl.ToString());
                //Create a new HTTP request object, set the method to POST and write the POST data to it
                var webrequest = (HttpWebRequest)WebRequest.Create("https://api.stripe.com/v1/charges");
                webrequest.Method = "POST";
                webrequest.UserAgent = "Stripe Payment";
                webrequest.ContentType = "application/x-www-form-urlencoded";
                webrequest.Headers.Add("Stripe-Version", "2015-04-07");
                webrequest.Headers.Add("Authorization", String.Concat("Basic ", (Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:", this.PrivateKey))))));

                using (Stream postStream = webrequest.GetRequestStream())
                {
                    postStream.Write(formbytes, 0, formbytes.Length);
                }

                //Make the request, get a response and pull the data out of the response stream

                StreamReader reader = null;
                string stripeResponse;
                try
                {
                    var webresponse = (HttpWebResponse)webrequest.GetResponse();
                    Stream responseStream = webresponse.GetResponseStream();
                    reader = new StreamReader(responseStream);
                    stripeResponse = reader.ReadToEnd();
                }
                catch (WebException exception)
                {
                    using (WebResponse response = exception.Response)
                    {
                        using (Stream data = response.GetResponseStream())
                        using (reader = new StreamReader(data))
                        {
                            stripeResponse = reader.ReadToEnd();                                 
                        }
                    }
                }

答案 1 :(得分:0)

C#和Java具有类似的语法,实际上您需要对发布的代码进行的唯一更改是将HashMap<String, Object>更改为Dictionary<String, Object>

Stripe.apiKey = "sk_test_2uoGykz3oyw8GNHWiBVEQY7N";
Dictionary<String, Object> chargeParams = new Dictionary<String, Object>();
chargeParams.Add("amount", 400);
chargeParams.Add("currency", "usd");
chargeParams.Add("source", "tok_160DSpIZJBCsfX4YCKJKjg2O"); // obtained with Stripe.js
chargeParams.Add("description", "Charge for test@example.com");
Charge.Create(chargeParams);