Stripe支付對接


一、由於文檔丟失原因,我就直接上代碼了。

這個Stripe支付可以支持多個幣種,我下面就采用"HDK"來參照支付先上一個支付效果圖

本文用到的框架是Stripe.net 22.8.0   我嘗試了下升級NUGET,但是后台的C#代碼的方法就更新掉了,所以我采用的這個版本。

 

 

 

 

 

 

 提示:先上代碼,在說明博主自己理解的流程。

一、前端代碼如下:請求的是ashx后台一般處理程序(C# asp.net)


myStripe.pay({ title: 'Soonnet Web Site', currency: 'HKD', amount: <%= Money.Text %> * 100,//這里為什么要 * 100 呢 因為這個錢默認他們打了1折扣,具體忘記為什么了,好像只有港幣這樣。 callback: function (p) { var dt = { TokenID: this.tokenId, //這個是必須的,下面的四個參數根據自身業務確認 //Amount:6300, Email: this.email, "MasterGroupType": getQueryString("GT"), "Mmail": getQueryString("E"), "RunId": getQueryString("R") } $.ajax({ url: "/ReasontoRun/Running.ashx?method=paypal&PayType=1",//支付類型 method: "post", data: dt, async: false, success: function (data) { if (data.success) { location.href = "/ReasontoRun/success.html?W=IsSUC"; } else { layer.msg(data.msg); } }, error: function () { } }) } });
 var myStripe = {
            testKey: '<%=ConfigurationManager.AppSettings["pk_liveConfig"] %>', <!--配置文件中的key 這個從Stripe中取,我就不截圖展示了-->
            logoImg: "https://stripe.com/img/documentation/checkout/marketplace.png", <!--抬頭的Logo-->
            //換卡
            changeHandler: function (f) {
                return StripeButton.configure({
                    key: this.testKey,
                    image: f.logoImg || this.logoImg,
                    name: f.title || 'Update Card Detail',
                    panelLabel: f.button || 'Submit',
                    allowRememberMe: false,
                    locale: 'auto',
                    dataKey: this.testKey,
                    token: function (token) {
                        f.email = token.email;
                        f.tokenId = token.id;
                        f.callback(f);
                    }
                });
            },
            payHandler: function (f) {
                layer.closeAll(0);
                return StripeCheckout.configure({
                    key: this.testKey,
                    name: f.title || 'REASON TO RUN 2.0 報名費用',
                    email: f.Email || '',
                    currency: f.currency || 'twd',
                    amount: f.amount || 0,
                    allowRememberMe: false,
                    image: f.logoImg || this.logoImg,
                    locale: 'auto',
                    token: function (token) {
                        f.tokenId = token.id;
                        f.email = token.email;
                        f.callback(f);
                    }
                });
            },
            changeCard: function (f) {
                this.changeHandler(f).open();
            },
            pay: function (f) {
                this.payHandler(f).open();
            },
            SendMsg: function (uid) {
                var message = {};
                message.action = "noticeMember";
                message.code = 1;
                message.uid = uid;
                message.msg = "<div>已有用戶購買你的相片!</div>";
                socketApi.sendMessage(message);
            }
        }

注:如有遺漏評論區誒特,或者加群聯系。

二、上后台代碼,這里只上部分代碼。(用到的,避免混淆)

非常的簡單

 Stripe.StripeConfiguration.SetApiKey(ConfigurationManager.AppSettings["pk_liveSecretKey"]);
                    var options = new Stripe.ChargeCreateOptions
                    {
                        Amount = Convert.ToInt64(GetMoney(codeModel, money, thisCart)) * 100,
                        Currency = CurrencyCode.HKD.ToString().ToLower(),
                        SourceId = Paymodel.stripeToken,
                        Description = "支付尋寶網-網站-" + Paymodel.Description,
                        ReceiptEmail = Paymodel.stripeEmail,
                    };
                    var service = new Stripe.ChargeService();
                    Stripe.Charge charge = service.Create(options);

                    //var options2 = new PayoutCreateOptions
                    //{
                    //    Amount = Convert.ToInt64(model.PayAmount),
                    //    Currency = "usd",
                    //};
                    //var service2 = new PayoutService();
                    //Payout payout = service2.Create(options2);
                    return new ChargeModel
                    {
                        SourceId = charge.Source?.Id,
                        Status = charge.Status,
                        payEmail = charge.ReceiptEmail,
                        Id = charge.Id,
                        tranPayId = charge.BalanceTransactionId
                    };

  結果得到的是 success 就是支付成功了,這里只做了對接支付,沒對接根據流水去查詢。

三、支付流程就是點擊按鈕就調用myStripe.pay函數去生成token,然后會調用里面的callback方法進行回調在進入到你的后台代碼(此處自身理解,有錯請指點) 

配置文件代碼。Web.config

 <add key="pk_liveConfig" value="pk_test_knG4yR1351351234reeYTnQVHS" />
    <add key="pk_liveSecretKey" value="sk_test_UTasdasasgascH26dERrCqyfI" />

 

歡迎加入群聊,期待更多的小伙伴加入,本群剛創立初期。人少,樓主想結合一下所有人的力量做一個1.0版本的快速開發框架, 比如ABP類似的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM