最了寫了一個asp.net的在線銀聯支付的功能,看了網上的半天才弄出來自已的。希望能對大家有所用。
1,先在線銀聯去申請,然后,他們那邊會給你一個包,然后把包名字更改下key,放到你的Web項目的根目錄中,跟其他的目錄結構在同一級別上。
2,如果你是asp.net的項目,可以去網上去找這幾個文件,或者直接像我要也可以的哈,
3,如下分別上傳代碼給大家展示下,
%20%201%20using%20System;%20%202%20using%20System.Data;%20%203%20using%20System.Configuration;%20%204%20using%20System.Web;%20%205%20using%20System.Web.Security;%20%206%20using%20System.Web.UI;%20%207%20using%20System.Web.UI.HtmlControls;%20%208%20using%20System.Web.UI.WebControls;%20%209%20using%20System.Web.UI.WebControls.WebParts;%2010%20using%20ChinaPay_JY;%2011%20%2012%20namespace%20DTcms.Web.api.payment.chinapay%2013%20{%2014%20%20%20%20%20///%20<summary>%2015%20%20%20%20%20///Chinapay%20的摘要說明%2016%20%20%20%20%20///%20</summary>%2017%20%20%20%20%20public%20partial%20class%20Chinapay%20:System.Web.UI.Page%2018%20%20%20%20%20{%2019%20%20%20%20%20%20%20%20%20string%20strUrl%20=%20HttpContext.Current.Request.PhysicalapplicationPath;%20//獲取網站根目錄物理路徑%2020%20%20%20%20%20%20%20%20%20public%20Chinapay()%2021%20%20%20%20%20%20%20%20%20{%2022%20%20%20%20%20%20%20%20%20}%2023%20%20%20%20%20%20%20%20%20/////////////////////////////////////////////////////////////////////////////////////////%2024%20%20%20%20%20%20%20%20%20//%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20二次開發%20Eric%20Chen%202014.12.05%2025%20%20%20%20%20%20%20%20%20/////////////////////////////////////////////////////////////////////////////////////////%20%20%20%20%20%20%20%20%2026%20%2027%20%20%20%20%20%20%20%20%20///%20<summary>%2028%20%20%20%20%20%20%20%20%20///%20訂單簽名函數sign%2029%20%20%20%20%20%20%20%20%20///%20</summary>%2030%20%20%20%20%20%20%20%20%20///%20<param%20name="MerId">商戶號,長度為15個字節的數字串,由ChinaPay或清算銀行分配</param>%2031%20%20%20%20%20%20%20%20%20///%20<param%20name="OrdId">訂單號,長度為16個字節的數字串,由用戶系統/網站生成,失敗的訂單號允許重復支付</param>%2032%20%20%20%20%20%20%20%20%20///%20<param%20name="TransAmt">交易金額,長度為12個字節的數字串,例如:數字串"000000001234"表示12.34元</param>%2033%20%20%20%20%20%20%20%20%20///%20<param%20name="CuryId">貨幣代碼,%20長度為3個字節的數字串,目前只支持人民幣,取值為"156"</param>%2034%20%20%20%20%20%20%20%20%20///%20<param%20name="TransDate">交易日期,長度為8個字節的數字串,表示格式為:YYYYMMDD</param>%2035%20%20%20%20%20%20%20%20%20///%20<param%20name="TransType">交易類型,長度為4個字節的數字串,取值范圍為:"0001"和"0002",%20其中"0001"表示消費交易,"0002"表示退貨交易</param>%2036%20%20%20%20%20%20%20%20%20///%20<returns>string%20CheckValue[256]%20%20即NetPayClient根據上述輸入參數生成的商戶數字簽名,長度為256字節的字符串</returns>%2037%20%20%20%20%20%20%20%20%20public%20string%20getSign(string%20MerId,%20string%20OrdId,%20string%20TransAmt,%20string%20CuryId,%20string%20TransDate,%20string%20TransType)%2038%20%20%20%20%20%20%20%20%20{%2039%20%20%20%20%20%20%20%20%20%20%20%20%20NetPayClientClass%20npc%20=%20new%20NetPayClientClass();%20//實例NetPay簽名%2040%20%20%20%20%20%20%20%20%20%20%20%20%20//npc.setMerKeyFile("Bin/MerView Code1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.WebControls.WebParts; 11 using ChinaPay_JY; 12 using System.Data.SqlClient; 13 using DTcms.Web.api.payment.chinapay; //這里引用app_code的chinapay命名空間 14 using DTcms.Common; 15 using DTcms.Model; 16 public partial class payonline_chinapay_Chinapay_send : System.Web.UI.Page 17 { 18 19 20 public int userid; //登錄用戶id 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 24 //讀取站點配置信息 25 DTcms.Model.siteconfig siteConfig = new DTcms.BLL.siteconfig().loadConfig(); 26 //獲取訂單信息 27 string order_no = DTRequest.GetFormString("pay_order_no").ToUpper();//訂單號 28 decimal order_amount = DTRequest.GetFormDecimal("pay_order_amount", 0);//訂單金額 29 string user_name = DTRequest.GetFormString("pay_user_name");//付款用戶名 30 string subject = DTRequest.GetFormString("pay_user_message");//備注說明或者是留言 31 string trans_type = string.Empty;//交易類型1實物2虛擬 32 if (order_no == "" || order_amount == 0) 33 { 34 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("對不起,你提交的參數有誤"))); 35 return; 36 } 37 if (order_no.StartsWith("R"))//R開頭為在線充值訂單 38 { 39 DTcms.Model.user_amount_log model = new DTcms.BLL.user_amount_log().GetModel(order_no); 40 if (model == null) 41 { 42 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("errro", "?msg=" + Utils.UrlEncode("對不起,你充值的訂單號不存在或已刪除"))); 43 return; 44 } 45 if (model.value != order_amount) 46 { 47 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("對不起,你充值的訂單金額與實際金額不一致!"))); 48 return; 49 } 50 trans_type = "2"; 51 } 52 else//B開頭為商品訂單 53 { 54 DTcms.Model.orders model = new DTcms.BLL.orders().GetModel(order_no); 55 if (model == null) 56 { 57 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("對不起,你支付的訂單號不存在或已刪除!"))); 58 return; 59 } 60 if (model.order_amount != order_amount) 61 { 62 Response.Redirect(new DTcms.Web.UI.BasePage().linkurl("error","?msg="+Utils.UrlEncode("對不起,你支付的訂單金額與實際金額不一致!"))); 63 return; 64 } 65 trans_type = "1"; 66 67 } 68 if (user_name != "") 69 { 70 user_name = "支付會員:" + user_name; 71 } 72 else { 73 user_name = "匿名用戶"; 74 } 75 76 77 78 //if (Request.Cookies[ProductCMS.GetCookieName()] == null) 79 //{ 80 // ChangeHope_fc.Show_Msg("請先登錄", "../index.html"); 81 // Response.End(); 82 //} 83 //else 84 // userid = Convert.ToInt32(Request.Cookies[ProductCMS.GetCookieName()].Values["CH_userid"]); 85 86 //int intOrderid = 0, intProid = 0; //訂單id,商品id 87 //if (Request["oid"] != null && Request["oid"] != "") 88 // intOrderid = Convert.ToInt32(Request["oid"]); 89 //else 90 // //ChangeHope_fc.Show_Msg("參數錯誤,請稍后再試!"); 91 //if (Request["pid"] != null && Request["pid"] != "") 92 // intProid = Convert.ToInt32(Request["pid"]); 93 //else 94 //ChangeHope_fc.Show_Msg("參數錯誤,請稍后再試!"); 95 96 //YX_sql Exsql = new YX_sql(); 97 //ChangeHope_CMS cms = new ChangeHope_CMS(); 98 Chinapay cpy = new Chinapay(); 99 //獲取傳遞給銀聯chinapay的各個參數-----------------------------------------------100 //string cpyUrl = "http://payment-test.chinapay.com/pay/TransGet"; //測試地址,測試的時候用這個地址,應用到網站時用下面那個地址101 string cpyUrl = "https://payment.ChinaPay.com/pay/TransGet"; //支付地址 102 string cpyMerId = "808080201303248"; //ChinaPay統一分配給商戶的商戶號,15位長度,必填 103 string cpyOrdId = "03248" + order_no.Substring(8); //商戶提交給ChinaPay的交易訂單號,訂單號的第五至第九位必須是商戶號的最后五位,即“12345”;16位長度,必填 20141208TU33BK 201412080919280460802 .PadRight(11, '0')104 string cpyTransAmt = (Convert.ToDouble((order_amount)*100).ToString()); //訂單交易金額,12位長度,左補0,必填,單位為分,000000001234 表示 12.34 元105 string cpyCuryId = "156"; //訂單交易幣種,3位長度,固定為人民幣156,必填106 string cpyTransDate = DateTime.Now.ToString("yyyyMMdd"); //訂單交易日期,8位長度,必填,格式yyyyMMdd107 string cpyTransType = "0001"; //交易類型,4位長度,必填,0001表示消費交易,0002表示退貨交易108 string cpyVersion = "20040916"; //支付接入版本號,808080開頭的商戶用此版本,必填,另一版本為"20070129" 20040916109 string cpyBgRetUrl = "/chinapay/Chinapay_Bgreturn.aspx";//cms.Hope_Sitwww + "/chinapay/Chinapay_Bgreturn.aspx"; //后臺交易接收URL,為后臺接受應答地址,用于商戶記錄交易信息和處理,對于使用者是不可見的,長度不要超過80個字節,必填110 string cpyPageRetUrl = "/chinapay/Chinapay_Pgreturn.aspx";// cms.Hope_Sitwww + "/chinapay/Chinapay_Pgreturn.aspx"; //頁面交易接收URL,為頁面接受應答地址,用于引導使用者返回支付后的商戶網站頁面,長度不要超過80個字節,必填111 string cpyGateId = ""; //支付網關號,可選,參看銀聯網關類型,如填寫GateId(支付網關號),則消費者將直接進入支付頁面,否則進入網關選擇頁面112 string cpyPriv1 = "Memo"; //"" 商戶私有域,長度不要超過60個字節,商戶通過此字段向Chinapay發送的信息,Chinapay依原樣填充返回給商戶113 114 string sql = "", strCountMoney = cpyTransAmt, strOrdid = ""; //sql語句,實付款,訂單自增id115 //sql = "select * from yx_orderform where yx_id=" + intOrderid; //查詢用戶訂單116 //SqlDataReader sdr = Exsql.Re_dr(sql);117 //if (sdr.Read())118 //{119 // cpyPriv1 = strOrdid = sdr["YX_ID"].ToString().Trim();120 // strCountMoney = Convert.ToDecimal(Convert.ToDecimal(sdr["YX_SumPrice"]) + Convert.ToDecimal(sdr["YX_DGoodsPrice"]) * Convert.ToDecimal(sdr["YX_Sumcount"]) - Convert.ToDecimal(sdr["YX_SumVouchers"])).ToString("f2");121 //}122 ///訂單時間必須為當前時間,且格式必須為yyyyMMdd123 //cpyTransDate = Convert.ToDateTime(DateTime.Now).GetDateTimeFormats('D')[1].ToString().Trim().Replace("-", "");124 ///訂單號,16位長度,左補0,0000 02449 0000000125 126 int intO = strOrdid.Length;127 for (int i = 0; i < 7 - intO; i++)128 {129 strOrdid = "0" + strOrdid;130 }131 //0000 B14 12 05 1559294 00000000132 //201412051721046647112133 //0000000000000064134 135 cpyOrdId = "0000" + cpyOrdId; //銀聯支付id="0000"+銀聯商戶后五位+補0后的訂單自增id + strOrdid;136 ///訂單交易金額,12位長度,左補0137 strCountMoney = strCountMoney.Replace(".", "");138 int intM = strCountMoney.Length;139 for (int i = 0; i < 12 - intM; i++)140 {141 strCountMoney = "0" + strCountMoney;142 }143 cpyTransAmt = strCountMoney; //獲取傳遞的實付款144 145 //////////////////////////////////////////////////////////////////////146 string strChkValue = ""; //256字節長的ASCII碼,此次交易所提交的關鍵數據的數字簽名,必填147 strChkValue = cpy.getSign(cpyMerId, cpyOrdId, cpyTransAmt, cpyCuryId, cpyTransDate, cpyTransType);148 149 if (strChkValue != "")150 {151 Response.Write("<form name='chinapayForm' method='post' action='" + cpyUrl + "'>"); //支付地址152 Response.Write("<input type='hidden' name='MerId' value='" + cpyMerId + "' />"); //商戶號153 Response.Write("<input type='hidden' name='OrdId' value='" + cpyOrdId + "' />"); //訂單號154 Response.Write("<input type='hidden' name='TransAmt' value='" + cpyTransAmt + "' />"); //支付金額155 Response.Write("<input type='hidden' name='CuryId' value='" + cpyCuryId + "' />"); //交易幣種156 Response.Write("<input type='hidden' name='TransDate' value='" + cpyTransDate + "' />"); //交易日期157 Response.Write("<input type='hidden' name='TransType' value='" + cpyTransType + "' />"); //交易類型158 Response.Write("<input type='hidden' name='Version' value='" + cpyVersion + "' />"); //支付接入版本號159 Response.Write("<input type='hidden' name='BgRetUrl' value='" + cpyBgRetUrl + "' />"); //后臺接受應答地址160 Response.Write("<input type='hidden' name='PageRetUrl' value='" + cpyPageRetUrl + "' />"); //為頁面接受應答地址161 Response.Write("<input type='hidden' name='GateId' value='" + cpyGateId + "' />"); //支付網關號162 Response.Write("<input type='hidden' name='Priv1' value='" + cpyPriv1 + "' />"); //商戶私有域,這里將訂單自增編號放進去了163 Response.Write("<input type='hidden' name='ChkValue' value='" + strChkValue + "' />"); //此次交易所提交的關鍵數據的數字簽名164 Response.Write("<script>");165 Response.Write("document.chinapayForm.submit();");166 Response.Write("</script></form>");167 }168 //else169 // ChangeHope_fc.Show_Msg("參數錯誤,請稍后再試!");170 }171 }

1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls;10 using System.Web.UI.WebControls.WebParts;11 12 using DTcms.Web.api.payment.chinapay;13 14 public partial class payonline_chinapay_Chinapay_Bgreturn : System.Web.UI.Page15 {16 protected void Page_Load(object sender, EventArgs e)17 {18 Chinapay cpy = new Chinapay(); 19 string TransDate = "",MerId = "",OrdId = "",TransType = "",TransAmt = "",CuryId = "",ChkValue = "",OrderStatus = "",GateId = "",Priv1 = "";20 bool bolCheck=false;21 22 TransDate = Request["transdate"].Trim();23 MerId = Request["merid"].Trim();24 OrdId = Request["orderno"].Trim();25 TransType = Request["transtype"].Trim();26 TransAmt = Request["amount"].Trim();27 CuryId = Request["currencycode"].Trim(); //交易幣種28 ChkValue = Request["checkvalue"].Trim();29 OrderStatus = Request["status"].Trim();30 GateId = Request["GateId"].Trim(); //支付網關號31 Priv1 = Request["Priv1"].Trim(); //商戶私有域32 ///檢驗是否是銀聯chinapay返回的交易數據33 bolCheck = cpy.getCheck(MerId,OrdId,TransAmt,CuryId,TransDate,TransType,OrderStatus,ChkValue);34 if (bolCheck)35 Response.Write("<script>alert('后臺接收到應答');</script>");36 else37 Response.Write("<script>alert('后臺沒有接收到應答');</script>");38 }39 }

1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.HtmlControls; 9 using System.Web.UI.WebControls;10 using System.Web.UI.WebControls.WebParts;11 12 using System.Data.SqlClient;13 using DTcms.Web.api.payment.chinapay;14 namespace DTcms.Web.api.payment.balance15 {16 public partial class payonline_chinapay_Chinapay_Pgreturn : System.Web.UI.Page17 {18 protected void Page_Load(object sender, EventArgs e)19 {20 //YX_sql Exsql = new YX_sql();21 //ChangHope_DB db = new ChangHope_DB();22 Chinapay cpy = new Chinapay();23 string TransDate = "", MerId = "", OrdId = "", TransType = "", TransAmt = "", CuryId = "", ChkValue = "", OrderStatus = "", GateId = "", Priv1 = "";24 bool bolCheck = false;25 26 TransDate = Request["transdate"].Trim();27 MerId = Request["merid"].Trim();28 OrdId = Request["orderno"].Trim();29 TransType = Request["transtype"].Trim();30 TransAmt = Request["amount"].Trim();31 CuryId = Request["currencycode"].Trim(); //交易幣種32 ChkValue = Request["checkvalue"].Trim();33 OrderStatus = Request["status"].Trim();34 GateId = Request["GateId"].Trim(); //支付網關號35 Priv1 = Request["Priv1"].Trim(); //商戶私有域,內容是訂單自增編號36 ///檢驗是否是銀聯chinapay返回的交易數據37 bolCheck = cpy.getCheck(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, ChkValue);38 if (bolCheck) //bolCheck=true,檢測返回參數是銀聯發送的,進入處理流程39 {40 try41 {42 ///********************************43 ///這里寫成功接收到銀聯支付成功后你自己要處理的流程,比如修改買、賣家金額等,訂單狀態等44 ///********************************45 46 }47 catch (Exception)48 {49 //ChangeHope_fc.Show_Msg("參數錯誤,請稍后再試!", db.System_Http() + "/vipuser/default_vip.aspx");50 }51 }52 //else53 //ChangeHope_fc.Show_Msg("參數錯誤,請稍后再試!", db.System_Http() + "/vipuser/default_vip.aspx");54 }55 }56 57 }
代碼中有很多注釋啦,我就不再熬熟啦。
新聞熱點
疑難解答