Asp.net MVC集成stripe支付

一、註冊Stripe帳號 https://stripe.comjavascript

初始時帳號是TEST模式,須要激活帳號才能進入LIVE模式;點擊 "Your Account" -> "Account Settings",出現以下彈出框:java

若是是TEST模式,請使用Test Secret Key和Test Publishable Key,不然請使用LIVE相關的兩個Key;具體如何使用,請繼續往下看git

 

二、安裝Stripegithub

使用NuGet安裝Stripe,經過搜索會找到Stripe和Stripe.net兩個,請安裝Stripe。具體過程略過!web

能夠到https://github.com/nberardi/stripe-dotnet下載源碼看看api

 

三、集成支付spa

<form action="/ChargeController/Charge"
    <script src="https://checkout.stripe.com/checkout.js" type="text/javascript"
        data-key="your-test-publishable-key"
        data-image="your-website-image,size 128*128"
        data-name="Demo Site"
        data-description="2 widgets (£20.00)"
        data-currency="GBP"
        data-amount="2500" />
</form>

 將以上Script嵌入到form中,form中會出現一個;固然,你也能夠根據須要使用自定義的按鈕,具體請看Stripe官網文檔。.net

 點擊按鈕,在彈出的支付信息填寫Dialog中,填寫正確後,會產生一個stripeToken,連同form的其它內容一塊兒POST到ChargeController/Chargeorm

 

四、執行支付blog

        [HttpPost]
        public ActionResult Charge(FormCollection form)
        {
            string token = form["stripeToken"];
            string email = form["stripeEmail"];

            string apiKey = "sk_test_xxxxxxxxxxxxxxx";
            var stripeClient = new Stripe.StripeClient(apiKey);

            dynamic response = stripeClient.CreateChargeWithToken(2000, token, "NOK", email);
            if (response.IsError == false && response.Paid)
            {
                // success
                string id = response.Id;//支付Id
                bool livemode = response.LiveMode;//支付模式
                long created = response.Created;//支付時間
                string status = response.Status;//支付狀態
                object source = response.Source;//支付源(信用卡等)
                string source_id = response.Source.Id;//卡Id
                string source_last4 = response.Source.Last4;//卡後四位
                string source_brand = response.Source.Brand;//卡品牌(Visa等)
                string source_funding = response.Source.Funding;//資金(Creadit等)
                int source_exp_month = response.Source.Exp_Month; //卡過時月份
                int source_exp_year = response.Source.Exp_Year;//卡過時年份
                string source_name = response.Source.Name;//支付人郵箱名
                return RedirectToAction("Index", "Home");
            }
            return View();
        }     

 

 到此就完成了一個支付流程,相比paypal,以爲仍是方便的多了!

相關文章
相關標籤/搜索