java chinapay_银联支付 Chinapay 在.net下的使用总结

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using ChinaPay_loaf        ;

using System.Web.UI;

//Chinapay 的摘要说明///    public class Chinapay

{

string strUrl =HttpContext.Current.Request.PhysicalApplicationPath; //获取网站根目录物理路径        public Chinapay()

{

}

//订单签名函数sign//商户号,长度为15个字节的数字串,由ChinaPay或清算银行分配///订单号,长度为16个字节的数字串,由用户系统/网站生成,失败的订单号允许重复支付///交易金额,长度为12个字节的数字串,例如:数字串"000000001234"表示12.34元///货币代码, 长度为3个字节的数字串,目前只支持人民币,取值为"156"///交易日期,长度为8个字节的数字串,表示格式为:YYYYMMDD///交易类型,长度为4个字节的数字串,取值范围为:"0001"和"0002", 其中"0001"表示消费交易,"0002"表示退货交易///string CheckValue[256]  即NetPayClient根据上述输入参数生成的商户数字签名,长度为256字节的字符串        public string getSign(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType)

{

NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名            string temp = strUrl + "key\\MerPrK.key";

npc.setMerKeyFile(strUrl + "\\App_Data\\MerPrK.key");

string strChkValue = "";                         //chinapay返回的商户数字签名            strChkValue = npc.sign(MerId, OrdId, TransAmt, CuryId, TransDate, TransType);

return strChkValue.Trim();

}

//对一段字符进行签名 signData//商户号,长度为15个字节的数字串,由ChinaPay分配///用于要签名的字符串///String CheckValue[256]即NetPayClient根据上述输入参数生成的商户数字签名,长度为256字节的字符串        public string signData(string MerId, string SignMsg)

{

NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名            npc.setMerKeyFile(strUrl + "App_Data\\MerPrK.key");

string strChkValueData = "";

strChkValueData = npc.signData(MerId, SignMsg);

return strChkValueData.Trim();

}

//验证交易应答函数check//商户号,长度为15个字节的数字串,由ChinaPay分配///订单号,长度为16个字节的数字串,由商户系统生成,失败的订单号允许重复支付///交易金额,长度为12个字节的数字串,例如:数字串"000000001234"表示12.34元///货币代码, 长度为3个字节的数字串,目前只支持人民币,取值为"156"///交易日期,长度为8个字节的数字串,表示格式为: YYYYMMDD///交易类型,长度为4个字节的数字串,取值范围为:"0001"和"0002", 其中"0001"表示消费交易,"0002"表示退货交易///交易状态,长度为4个字节的数字串。详见交易状态码说明///校验值,即ChinaPay对交易应答的数字签名,长度为256字节的字符串///true 表示成功,即该交易应答为ChinaPay所发送,商户根据“交易状态”进行后续处理;否则表示失败,即无效应答,商户可忽略该应答        public bool getCheck(string MerId, string OrdId, string TransAmt, string CuryId, string TransDate, string TransType, string OrderStatus, string CheckValue)

{

NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名            npc.setPubKeyFile(strUrl + "App_Data\\PgPubk.key");

string strFlag = "";

bool bolFlag = false;

strFlag = npc.check(MerId, OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, CheckValue); //ChkValue 为ChinaPay返回给商户的域段内容            if (strFlag == "0") //“0”表示验签成功                bolFlag = true;

return bolFlag;

}

//对一段字符串进行签名验证 checkData//用于数字签名的字符串///校验值,要验证的字符串的数字签名,长度为256字节的字符串///true 表示验证通过成功;否则表示失败        public bool checkData(string PlainData, string CheckValue)

{

NetPayClientClass npc = new NetPayClientClass(); //实例NetPay签名            npc.setPubKeyFile(strUrl + "App_Data\\PgPubk.key");

string strFlagData = "";

bool bolFlagData = false;

strFlagData = npc.checkData(PlainData, CheckValue);

if (strFlagData == "true")

bolFlagData = true;

return bolFlagData;

}

支付函数        //支付函数//程序 订单编号///交易钱数///产品名称 可选        public void GoToPay(string OrderID, string TransAmt, string proName,string gateid)

{

Chinapay cpy = new Chinapay();

//获取传递给银联chinapay的各个参数-----------------------------------------------//string cpyUrl = "http://payment-test.chinapay.com/pay/TransGet";//测试地址,测试的时候用这个地址,应用到网站时用下面那个地址            string cpyUrl = "http://payment.chinapay.com/pay/TransGet";

string cpyMerId = "808080580112345";            //ChinaPay统一分配给商户的商户号,15位长度,必填            string cpyOrdId = getOrderID(OrderID);           //商户提交给ChinaPay的交易订单号,订单号的第五至第九位必须是商户号的最后五位,即“12345”;16位长度,必填            string cpyTransAmt = getTransAmt(TransAmt); //订单交易金额,12位长度,左补0,必填,单位为分,000000000001 表示 12.34 元            string cpyCuryId = "156";            //订单交易币种,3位长度,固定为人民币156,必填            string cpyTransDate = DateTime.Now.ToString("yyyyMMdd");            //订单交易日期,8位长度,必填,格式yyyyMMdd            string cpyTransType = "0001";        //交易类型,4位长度,必填,0001表示消费交易,0002表示退货交易            string cpyVersion = "20040916";      //支付接入版本号,808080开头的商户用此版本,必填,另一版本为"20070129"            string cpyBgRetUrl = "http://test003.abc.cc/Chinapay_Bgreturn.aspx";   //后台交易接收URL,为后台接受应答地址,用于商户记录交易信息和处理,对于使用者是不可见的,长度不要超过80个字节,必填            string cpyPageRetUrl = "http://test003.abc.cc/Chinapay_Pgreturn.aspx"; //页面交易接收URL,为页面接受应答地址,用于引导使用者返回支付后的商户网站页面,长度不要超过80个字节,必填            string cpyGateId = gateid;  //支付网关号,可选,参看银联网关类型,如填写GateId(支付网关号),则消费者将直接进入支付页面,否则进入网关选择页面,可登陆商户管理平台 查看各个银行的网管号            string cpyPriv1 = proName;  //商户私有域,长度不要超过60个字节,商户通过此字段向Chinapay发送的信息,Chinapay依原样填充返回给商户

string strChkValue = ""; //256字节长的ASCII码,此次交易所提交的关键数据的数字签名,必填            strChkValue = cpy.getSign(cpyMerId, cpyOrdId, cpyTransAmt, cpyCuryId, cpyTransDate, cpyTransType);

if (strChkValue != "")

{

HttpContext.Current.Response.Write("

");         //支付地址             HttpContext.Current.Response.Write("");            //商户号             HttpContext.Current.Response.Write("");            //订单号             HttpContext.Current.Response.Write("");      //支付金额             HttpContext.Current.Response.Write("");          //交易币种             HttpContext.Current.Response.Write("");    //交易日期             HttpContext.Current.Response.Write("");    //交易类型             HttpContext.Current.Response.Write("");        //支付接入版本号             HttpContext.Current.Response.Write("");      //后台接受应答地址             HttpContext.Current.Response.Write("");  //为页面接受应答地址             HttpContext.Current.Response.Write("");          //支付网关号             HttpContext.Current.Response.Write("");            //商户私有域,这里将订单自增编号放进去了             HttpContext.Current.Response.Write("");      //此次交易所提交的关键数据的数字签名             HttpContext.Current.Response.Write("");

}

}

//订单号         private string getOrderID(string orderID)

{

//程序中的订单号 案例12022800001            string orderid = string.Format("{0}{1}{2}", orderID.Substring(0, 4), "12345", orderID.Substring(4)); //订单前四位+商家最后五位+订单后七位            return orderid;

}

//返回交易金额         private string getTransAmt(string count)

{

string moneyCount = count.ToString().Replace(".", "");

return moneyCount.PadLeft(12, '0');

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值