Skip to content

Commit

Permalink
Sample v6.3.2 完成微信支付V3验签等基本功能
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySu committed Aug 23, 2021
1 parent 8e99086 commit 8412649
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public ActionResult JsApi(int productId, int hc)
var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, body, sp_billno, price, HttpContext.UserHostAddress()?.ToString(), TenPayV3Info.TenPayV3Notify, TenPay.TenPayV3Type.JSAPI, openId, TenPayV3Info.Key, nonceStr);

var result = TenPayOldV3.Unifiedorder(xmlDataInfo);//调用统一订单接口
//JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);
//JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);
var package = string.Format("prepay_id={0}", result.prepay_id);

ViewData["product"] = product;
Expand Down Expand Up @@ -1043,92 +1043,90 @@ public ActionResult ProductPayCode(int productId, int hc)
/// <returns></returns>
public ActionResult H5Pay(int productId, int hc)
{
try
{
try
//获取产品信息
var products = ProductModel.GetFakeProductList();
var product = products.FirstOrDefault(z => z.Id == productId);
if (product == null || product.GetHashCode() != hc)
{
//获取产品信息
var products = ProductModel.GetFakeProductList();
var product = products.FirstOrDefault(z => z.Id == productId);
if (product == null || product.GetHashCode() != hc)
{
return Content("商品信息不存在,或非法进入!1002");
}
return Content("商品信息不存在,或非法进入!1002");
}

string openId = null;//此时在外部浏览器,无法或得到OpenId
string openId = null;//此时在外部浏览器,无法或得到OpenId

string sp_billno = Request.Query["order_no"];
if (string.IsNullOrEmpty(sp_billno))
{
//生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId/*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"),
TenPayV3Util.BuildRandomStr(6));
}
else
{
sp_billno = Request.Query["order_no"];
}
string sp_billno = Request.Query["order_no"];
if (string.IsNullOrEmpty(sp_billno))
{
//生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId/*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"),
TenPayV3Util.BuildRandomStr(6));
}
else
{
sp_billno = Request.Query["order_no"];
}

var timeStamp = TenPayV3Util.GetTimestamp();
var nonceStr = TenPayV3Util.GetNoncestr();
var timeStamp = TenPayV3Util.GetTimestamp();
var nonceStr = TenPayV3Util.GetNoncestr();

var body = product == null ? "test" : product.Name;
var price = product == null ? 100 : (int)(product.Price * 100);
//var ip = Request.Params["REMOTE_ADDR"];
var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, body, sp_billno, price, HttpContext.UserHostAddress()?.ToString(), TenPayV3Info.TenPayV3Notify, TenPay.TenPayV3Type.MWEB/*此处无论传什么,方法内部都会强制变为MWEB*/, openId, TenPayV3Info.Key, nonceStr);
var body = product == null ? "test" : product.Name;
var price = product == null ? 100 : (int)(product.Price * 100);
//var ip = Request.Params["REMOTE_ADDR"];
var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, body, sp_billno, price, HttpContext.UserHostAddress()?.ToString(), TenPayV3Info.TenPayV3Notify, TenPay.TenPayV3Type.MWEB/*此处无论传什么,方法内部都会强制变为MWEB*/, openId, TenPayV3Info.Key, nonceStr);

SenparcTrace.SendCustomLog("H5Pay接口请求", xmlDataInfo.ToJson());
SenparcTrace.SendCustomLog("H5Pay接口请求", xmlDataInfo.ToJson());

var result = TenPayOldV3.Html5Order(xmlDataInfo);//调用统一订单接口
//JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);
var result = TenPayOldV3.Html5Order(xmlDataInfo);//调用统一订单接口
//JsSdkUiPackage jsPackage = new JsSdkUiPackage(TenPayV3Info.AppId, timeStamp, nonceStr,);

/*
* result:{"device_info":"","trade_type":"MWEB","prepay_id":"wx20170810143223420ae5b0dd0537136306","code_url":"","mweb_url":"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20170810143223420ae5b0dd0537136306\u0026package=1505175207","appid":"wx669ef95216eef885","mch_id":"1241385402","sub_appid":"","sub_mch_id":"","nonce_str":"juTchIZyhXvZ2Rfy","sign":"5A37D55A897C854F64CCCC4C94CDAFE3","result_code":"SUCCESS","err_code":"","err_code_des":"","return_code":"SUCCESS","return_msg":null}
*/
//return Json(result, JsonRequestBehavior.AllowGet);
/*
* result:{"device_info":"","trade_type":"MWEB","prepay_id":"wx20170810143223420ae5b0dd0537136306","code_url":"","mweb_url":"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20170810143223420ae5b0dd0537136306\u0026package=1505175207","appid":"wx669ef95216eef885","mch_id":"1241385402","sub_appid":"","sub_mch_id":"","nonce_str":"juTchIZyhXvZ2Rfy","sign":"5A37D55A897C854F64CCCC4C94CDAFE3","result_code":"SUCCESS","err_code":"","err_code_des":"","return_code":"SUCCESS","return_msg":null}
*/
//return Json(result, JsonRequestBehavior.AllowGet);

SenparcTrace.SendCustomLog("H5Pay接口返回", result.ToJson());
SenparcTrace.SendCustomLog("H5Pay接口返回", result.ToJson());

var package = string.Format("prepay_id={0}", result.prepay_id);
var package = string.Format("prepay_id={0}", result.prepay_id);

ViewData["product"] = product;
ViewData["product"] = product;

ViewData["appId"] = TenPayV3Info.AppId;
ViewData["timeStamp"] = timeStamp;
ViewData["nonceStr"] = nonceStr;
ViewData["package"] = package;
ViewData["paySign"] = TenPayOldV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, package, TenPayV3Info.Key);
ViewData["appId"] = TenPayV3Info.AppId;
ViewData["timeStamp"] = timeStamp;
ViewData["nonceStr"] = nonceStr;
ViewData["package"] = package;
ViewData["paySign"] = TenPayOldV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, package, TenPayV3Info.Key);

//设置成功页面(也可以不设置,支付成功后默认返回来源地址)
var returnUrl =
string.Format("https://sdk.weixin.senparc.com/TenpayV3/H5PaySuccess?productId={0}&hc={1}",
productId, hc);
//设置成功页面(也可以不设置,支付成功后默认返回来源地址)
var returnUrl =
string.Format("https://sdk.weixin.senparc.com/TenpayV3/H5PaySuccess?productId={0}&hc={1}",
productId, hc);

var mwebUrl = result.mweb_url;
if (!string.IsNullOrEmpty(mwebUrl))
{
mwebUrl += string.Format("&redirect_url={0}", returnUrl.AsUrlData());
}
var mwebUrl = result.mweb_url;
if (!string.IsNullOrEmpty(mwebUrl))
{
mwebUrl += string.Format("&redirect_url={0}", returnUrl.AsUrlData());
}

ViewData["MWebUrl"] = mwebUrl;
ViewData["MWebUrl"] = mwebUrl;

//临时记录订单信息,留给退款申请接口测试使用
HttpContext.Session.SetString("BillNo", sp_billno);
HttpContext.Session.SetString("BillFee", price.ToString());
//临时记录订单信息,留给退款申请接口测试使用
HttpContext.Session.SetString("BillNo", sp_billno);
HttpContext.Session.SetString("BillFee", price.ToString());

return View();
}
catch (Exception ex)
{
var msg = ex.Message;
msg += "<br>" + ex.StackTrace;
msg += "<br>==Source==<br>" + ex.Source;
return View();
}
catch (Exception ex)
{
var msg = ex.Message;
msg += "<br>" + ex.StackTrace;
msg += "<br>==Source==<br>" + ex.Source;

if (ex.InnerException != null)
{
msg += "<br>===InnerException===<br>" + ex.InnerException.Message;
}
return Content(msg);
if (ex.InnerException != null)
{
msg += "<br>===InnerException===<br>" + ex.InnerException.Message;
}
return Content(msg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<ProjectGuid>eb4fcb9f-9670-4b03-9e9e-724aff737fd4</ProjectGuid>
<publishUrl>bin\Release\netcoreapp3.1\publish\</publishUrl>
<publishUrl>bin\net6.0-publish\</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<TargetFramework>net6.0</TargetFramework>
<SelfContained>false</SelfContained>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>6.3.1</Version>
<Version>6.3.2</Version>
<UserSecretsId>35e58786-0820-4cde-b1ff-f4c6198d00f7</UserSecretsId>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"timeStamp": "@ViewData["timeStamp"]", //时间戳
"nonceStr": "@ViewData["nonceStr"]", //随机串
"package": "@Html.Raw(ViewData["package"])",//扩展包
"signType": "MD5", //微信签名方式:MD5
"signType": "RSA", //微信签名方式:MD5
"paySign": "@ViewData["paySign"]" //微信签名
}, function (res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static bool VerifyTenpaySign(string wechatpayTimestamp, string wechatpayN
//AsymmetricAlgorithm对象
var key = x509.PublicKey.Key;

//RSAPKCS1SignatureDeformatter对象
//RSAPKCS1SignatureDeformatter 对象
RSAPKCS1SignatureDeformatter df = new RSAPKCS1SignatureDeformatter(key);
//指定 SHA256
df.SetHashAlgorithm("SHA256");
Expand Down

0 comments on commit 8412649

Please sign in to comment.