private string BuildEncryptXml <T>(IJdPayRequest <T> request, JdPayDictionary dic) where T : JdPayResponse { var xmldoc = JdPayUtil.SortedDictionary2AllXml(dic); var smlStr = JdPayUtil.ConvertXmlToString(xmldoc); var sha256SourceSignString = Sha256.Encrypt(smlStr); var encyptBytes = JdPaySignature.Encrypt(sha256SourceSignString, Options.RsaPrivateKey); var sign = Convert.ToBase64String(encyptBytes, Base64FormattingOptions.InsertLineBreaks); var data = smlStr.Replace("</jdpay>", "<sign>" + sign + "</sign></jdpay>"); var key = Convert.FromBase64String(Options.DesKey); var encrypt = Des3.Des3EncryptECB(key, data); // 字典排序 var reqdic = new JdPayDictionary { { VERSION, request.GetApiVersion() }, { MERCHANT, Options.Merchant }, { ENCRYPT, Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypt)) } }; return(JdPayUtil.SortedDictionary2XmlStr(reqdic)); }
public string ChargeByJdpay(string productName, decimal amount, string orderNo, string returnUrl) { var config = ConfigInfo; if (!config.IsJdpay) { return(null); } //var callbackUrl = Utils.AddProtocolToUrl(Pay.GetUrl(PageUtility.OuterApiUrl, returnUrl)); var callbackUrl = returnUrl; var orderInfoDic = new SortedDictionary <string, string> { { "version", "V2.0" }, { "merchant", config.JdpayMerchant }, { "device", "111" }, { "tradeNum", orderNo }, { "tradeName", productName }, { "tradeDesc", "交易描述" }, { "tradeTime", DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) }, { "amount", Convert.ToInt32(amount * 100).ToString() }, { "currency", "CNY" }, { "note", "备注" }, { "callbackUrl", callbackUrl }, { "notifyUrl", string.Empty }, { "ip", Utils.GetIpAddress() }, { "specCardNo", string.Empty }, { "specId", string.Empty }, { "specName", string.Empty }, { "userType", string.Empty }, //{"userId", config.JdpayUserId}, { "userId", Utils.GetShortGuid() }, { "expireTime", string.Empty }, { "orderType", "1" }, { "industryCategoryCode", string.Empty } }; var priKey = config.JdpayPrivateKey; var desKey = config.JdpayDesKey; var unSignedKeyList = new List <string> { "sign" }; var signStr = SignUtil.signRemoveSelectedKeys(orderInfoDic, priKey, unSignedKeyList); orderInfoDic.Add("sign", signStr); byte[] key = Convert.FromBase64String(desKey); //当模式为ECB时,IV无用,java默认使用的ECB if (!string.IsNullOrEmpty(orderInfoDic["device"])) { //String desStr = Des3.Des3EncryptECB(key, orderInfoDic["device")); orderInfoDic["device"] = Des3.Des3EncryptECB(key, orderInfoDic["device"]); //String str = Des3.Des3DecryptECB(key, desStr); } orderInfoDic["tradeNum"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeNum"]); if (!string.IsNullOrEmpty(orderInfoDic["tradeName"])) { orderInfoDic["tradeName"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeName"]); } if (!string.IsNullOrEmpty(orderInfoDic["tradeDesc"])) { orderInfoDic["tradeDesc"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeDesc"]); } orderInfoDic["tradeTime"] = Des3.Des3EncryptECB(key, orderInfoDic["tradeTime"]); orderInfoDic["amount"] = Des3.Des3EncryptECB(key, orderInfoDic["amount"]); orderInfoDic["currency"] = Des3.Des3EncryptECB(key, orderInfoDic["currency"]); if (!string.IsNullOrEmpty(orderInfoDic["note"])) { orderInfoDic["note"] = Des3.Des3EncryptECB(key, orderInfoDic["note"]); } orderInfoDic["callbackUrl"] = Des3.Des3EncryptECB(key, orderInfoDic["callbackUrl"]); orderInfoDic["notifyUrl"] = Des3.Des3EncryptECB(key, orderInfoDic["notifyUrl"]); orderInfoDic["ip"] = Des3.Des3EncryptECB(key, orderInfoDic["ip"]); if (!string.IsNullOrEmpty(orderInfoDic["userType"])) { orderInfoDic["userType"] = Des3.Des3EncryptECB(key, orderInfoDic["userType"]); } if (!string.IsNullOrEmpty(orderInfoDic["userId"])) { orderInfoDic["userId"] = Des3.Des3EncryptECB(key, orderInfoDic["userId"]); } if (!string.IsNullOrEmpty(orderInfoDic["expireTime"])) { orderInfoDic["expireTime"] = Des3.Des3EncryptECB(key, orderInfoDic["expireTime"]); } if (!string.IsNullOrEmpty(orderInfoDic["orderType"])) { orderInfoDic["orderType"] = Des3.Des3EncryptECB(key, orderInfoDic["orderType"]); } if (!string.IsNullOrEmpty(orderInfoDic["industryCategoryCode"])) { orderInfoDic["industryCategoryCode"] = Des3.Des3EncryptECB(key, orderInfoDic["industryCategoryCode"]); } if (!string.IsNullOrEmpty(orderInfoDic["specCardNo"])) { orderInfoDic["specCardNo"] = Des3.Des3EncryptECB(key, orderInfoDic["specCardNo"]); } if (!string.IsNullOrEmpty(orderInfoDic["specId"])) { orderInfoDic["specId"] = Des3.Des3EncryptECB(key, orderInfoDic["specId"]); } if (!string.IsNullOrEmpty(orderInfoDic["specName"])) { orderInfoDic["specName"] = Des3.Des3EncryptECB(key, orderInfoDic["specName"]); } StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<form id='jdpaysubmit' name='jdpaysubmit' action='https://wepay.jd.com/jdpay/saveOrder' method='post'>"); foreach (KeyValuePair <string, string> temp in orderInfoDic) { sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>"); } sbHtml.Append("<script>document.forms['jdpaysubmit'].submit();</script>"); return(sbHtml.ToString()); }