1.小程序中js中写上一句请求代码这里,data的参数只需要“openid”,请成功后,执行success函数,在使用wx.requestPayment的小程序接口,在回到那里取出后台支付接口回调的参数设置到下面的timestamp以下的支付中,就可以成功调用支付的页面。
2.后台c#的代码,注释掉的代码不用看。
private static TenPayV3Info _tenPayV3Info;
public static TenPayV3Info TenPayV3Info
{
get
{
if (_tenPayV3Info == null)
{
WeixinConfigInfo config = Meidie_MallReservationApplet.config.Config.WeixinConfig;
string NotifyUrl = config.CallBackUrl + "/Pay/wxPayResult";
_tenPayV3Info = new TenPayV3Info(config.AppID, config.AppSecret, config.MchId, config.AppKey, NotifyUrl, NotifyUrl);
}
return _tenPayV3Info;
}
}
///
/// 接收支付结果通知
///
///
public ActionResult wxPayResult()
{
ResponseHandler resHandler = new ResponseHandler(null);
string return_code = resHandler.GetParameter("return_code");
string return_msg = resHandler.GetParameter("return_msg");
string res = null;
resHandler.SetKey(TenPayV3Info.Key);
//验证请求是否从微信发过来(安全)
if (resHandler.IsTenpaySign())
{
res = "success";
//正确的订单处理
}
else
{
res = "wrong";
//错误的订单处理
}
string xml = string.Format(@"
", return_code, return_msg);
return Content(xml, "text/xml");
}
try
{
通过,用code换取access_token
//var openIdResult = OAuthApi.GetAccessToken(TenPayV3Info.AppId, TenPayV3Info.AppSecret, code);
//if (openIdResult.errcode != ReturnCode.请求成功)
//{
// return Json(new AjaxResult { data = null, message = "错误:" + openIdResult.errmsg });
//}
//时间戳
string timeStamp = TenPayV3Util.GetTimestamp();
string nonceStr = TenPayV3Util.GetNoncestr();
//微信支付提交的XML Data数据模型
TenPayV3UnifiedorderRequestData tpV3 = new TenPayV3UnifiedorderRequestData(
TenPayV3Info.AppId,//小程序/公众号ID
TenPayV3Info.MchId,//商户号
weixinconfig.WebTitle + "-" + productname,//商品描述
ordercode,//商户订单号
totalamount,//商品金额,以分为单位(money * 100)
Request.UserHostAddress,//用户的公网ip,不是商户服务器IP
TenPayV3Info.TenPayV3_WxOpenNotify,//接收财付通通知的回调URL
TenPayV3Type.JSAPI,//交易类型
//openIdResult.openid,//用户的openId
openid,//用户的openId
TenPayV3Info.Key,//商户支付密钥key,用于生成签名sign
nonceStr//随机字符串
);
//调用微信支付[统一下单]接口
var result = TenPayV3.Unifiedorder(tpV3);
//接收返回的预支付订单ID
string prepayId = result.prepay_id;
//获取UI调用的支付签名
string paySign = TenPayV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, "prepay_id=" + prepayId, TenPayV3Info.Key);
//返回json提供给小程序调用
var paydata = new
{
timeStamp = timeStamp,
nonceStr = nonceStr,
prepay_id = prepayId,
paySign = paySign,
};
return Json(new AjaxResult { data = new { paydata }, message = "ok" + result.return_msg, state = tpV3 });
}
catch (Exception e)
{
return Json(new AjaxResult { data = null, message = e.Message });
}
//微信退款提交的XML Data数据模型
TenPayV3RefundRequestData tpV3 = new TenPayV3RefundRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, TenPayV3Info.Key, "", Noncestr, "", ordercode, ordercode, totalamount, totalamount, TenPayV3Info.MchId, "");
var result = TenPayV3.Refund(tpV3, @weixinconfig.CretAbsolutePath, TenPayV3Info.MchId);
return Json(new AjaxResult { data = new { result }, message = "ok" });
3.注意事项:1.要有微信商户号和key秘钥,2.小程序要有申请开通好的支付接口,3.要到公众商户平台上绑定小程序的appid以及回调路径,4.所有的完成后就可以实现小程序的支付功能了。
4.成功图片展示事例: