C#實現(xiàn)支付寶沙箱支付的項目實踐
一,準備沙箱環(huán)境
1,登錄支付寶,進入 應用列表界面 https://openhome.alipay.com/dev/workspace
2,如下圖選擇進入沙箱
進入如下頁面:
1,這里的APPID很有用
2,在這里只測試網(wǎng)頁支付,用系統(tǒng)默認的密鑰
3,查看公鑰有如下界面:
需要用到的是應用私鑰(非JAVA)和支付寶公鑰
二,認識官方提供的Demo示例
1,下載demo示例
進入網(wǎng)頁 https://opendocs.alipay.com/open/270/106291
下載.NET版的demo,如下:
App_Code下有Config.cs文件
關(guān)于參數(shù):
1,app_id 就填寫沙箱界面的APPID
2,需要將gatewayUrl 改為:https://openapi.alipaydev.com/gateway.do 這才是測試版本
3,商戶私鑰 復制沙箱界面系統(tǒng)默認密鑰的 應用私鑰
4,支付寶公鑰 復制沙箱界面的支付寶公鑰
- wappay里是具體的調(diào)用API接口
- wappay是發(fā)送調(diào)用支付接口
- close是關(guān)閉訂單接口
- query是查詢訂單接口
- refund是退款接口
三,編寫一個ASP.NET的程序
1,創(chuàng)建ASP.NET web項目
2,解決方案導入Demo示例中的AopSdk模塊,如下:
3,前臺代碼:
<table> <tr> <td>訂單名稱:</td> <td> <asp:TextBox ID="tbxOrderName" runat="server"></asp:TextBox> </td> </tr> <tr> <td>訂單金額:</td> <td> <asp:TextBox ID="tbxOrderAmount" runat="server"></asp:TextBox> </td> </tr> <tr> <td>訂單描述:</td> <td> <asp:TextBox ID="tbxOrderDesc" runat="server"></asp:TextBox> </td> </tr> <tr> <td colspan="2"> <asp:Button ID="btn" runat="server" Text="支付" OnClick="btn_Click"/> </td> </tr> </table>
4,后臺代碼:
// 應用ID,您的APPID public static string app_id = "20210******"; // 支付寶網(wǎng)關(guān) public static string gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; // 商戶私鑰,您的原始格式RSA私鑰 public static string private_key = "MIIE******"; // 支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。 public static string alipay_public_key = "MIIBI******"; // 簽名方式 public static string sign_type = "RSA2"; // 編碼格式 public static string charset = "UTF-8"; protected void Page_Load(object sender, EventArgs e) { } protected void btn_Click(object sender, EventArgs e) { DefaultAopClient client = new DefaultAopClient(gatewayUrl, app_id, private_key, "json", "1.0", sign_type, alipay_public_key, charset, false); // 外部訂單號,商戶網(wǎng)站訂單系統(tǒng)中唯一的訂單號 string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmss"); // 訂單名稱 string subject = this.tbxOrderName.Text; // 付款金額 string total_amout = this.tbxOrderAmount.Text; // 商品描述 string body = this.tbxOrderDesc.Text; // 支付中途退出返回商戶網(wǎng)站地址 string quit_url = "https://localhost:44334/PayFailed.aspx?tradeNo=" + out_trade_no; // 支付成功返回商戶網(wǎng)站頁面 string return_url = "https://localhost:44334/PaySuccess.aspx?tradeNo=" + out_trade_no; // 設(shè)置支付完成異步通知接收地址 string notify_url = "https://localhost:44334/Notify.aspx"; // 組裝業(yè)務(wù)參數(shù)model AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); model.Body = body; model.Subject = subject; model.TotalAmount = total_amout; model.OutTradeNo = out_trade_no; model.ProductCode = "QUICK_WAP_WAY"; model.QuitUrl = quit_url; AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); // 設(shè)置支付完成同步回調(diào)地址 request.SetReturnUrl(return_url); // 設(shè)置支付完成異步通知接收地址 request.SetNotifyUrl(notify_url); // 將業(yè)務(wù)model載入到request request.SetBizModel(model); AlipayTradeWapPayResponse response = null; try { response = client.pageExecute(request, null, "post"); Response.Write(response.Body); } catch (Exception exp) { throw exp; }
說明:
1,這里的app_id等配置,就是來源于 Demo示例中的config.cs文件 需要從自己的支付寶沙箱中將相應的值復制進來
2,https://localhost:44334 這個地址和端口號是當前自己的項目的端口號
3,分別建立PaySuccess.aspx PayFailed.aspx 兩個個窗體 分別用于支付成功和失敗的跳轉(zhuǎn)頁面
4,Notify.aspx用于支付成功的異步通知用,可以不加
四,開始測試
調(diào)用成功,則進入如下界面
點擊繼續(xù)瀏覽器付款
點擊支付寶賬號登錄
注意!這里不是真實的支付寶賬號,需要用沙箱環(huán)境的支付寶賬號,在沙箱界面中找:
支付成功后,點擊右上角完成,則進入代碼中設(shè)定的PaySuccess.aspx頁面,同時也將訂單編號等信息帶過去。
至此,模擬支付成功,更多相關(guān)C# 支付寶沙箱支付內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中結(jié)構(gòu)體和字節(jié)數(shù)組轉(zhuǎn)換實現(xiàn)
這篇文章主要介紹了C#中結(jié)構(gòu)體和字節(jié)數(shù)組轉(zhuǎn)換實現(xiàn),本文直接給出了字節(jié)數(shù)組與結(jié)構(gòu)體的轉(zhuǎn)換代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
本文給大家分享Extjs4如何處理后臺json數(shù)據(jù)中日期和時間,通過代碼示例給大家剖析,感興趣的朋友快來圍觀2015-08-08C# 實現(xiàn)對PPT文檔加密、解密及重置密碼的操作方法
這篇文章主要介紹了C# 實現(xiàn)對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11