业务流程:
商户后台系统根据微信支付规则链接生成二维码,链接中带固定参数productid(可定义为产品标识或订单号)。用户扫码后,微信支付系统将productid和用户openid(用户标识)回调商户后台系统,商户后台系统根据productid生成支付交易,最后微信支付系统发起用户支付流程。
步骤一:设置回调URL
1. 在微信公众平台设置支付权限,进入栏目微信支付-》开发配置-》修改
2. 进入修改页面后,找到公众号支付-》Native原生支付,勾选Native原生支付开通该权限,在支付回调URL上填写商户支付后台系统的网页地址。
用户扫码之后,微信服务器会将相关的数据Post到这个url。
步骤二:生成二维码
public JsonResult GetQrCode(string no) { string productId = no; string timeStamp = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetTimestamp(); string nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr(); Senparc.Weixin.MP.TenPayLibV3.RequestHandler QrReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null); QrReqHandler.SetParameter("appid", AppId); QrReqHandler.SetParameter("mch_id", MchId); QrReqHandler.SetParameter("time_stamp", timeStamp); QrReqHandler.SetParameter("nonce_str", nonceStr); QrReqHandler.SetParameter("product_id", productId); string sign = QrReqHandler.CreateMd5Sign("key", Key); QrReqHandler.SetParameter("sign", sign); string long_url = Senparc.Weixin.MP.AdvancedAPIs.TenPayV3.NativePay(AppId, timeStamp, MchId, nonceStr, productId, sign); nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr(); Senparc.Weixin.MP.TenPayLibV3.RequestHandler ShortUrlReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null); ShortUrlReqHandler.SetParameter("appid", AppId); ShortUrlReqHandler.SetParameter("mch_id", MchId); ShortUrlReqHandler.SetParameter("long_url", long_url); ShortUrlReqHandler.SetParameter("nonce_str", nonceStr); string ShortSign = ShortUrlReqHandler.CreateMd5Sign("key", Key); ShortUrlReqHandler.SetParameter("sign", ShortSign); string ShortData = ShortUrlReqHandler.ParseXML(); var ShortUrlResult = Senparc.Weixin.MP.AdvancedAPIs.TenPayV3.ShortUrl(ShortData); var res = System.Xml.Linq.XDocument.Parse(ShortUrlResult); string short_url = res.Element("xml").Element("short_url").Value; //ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder(); //encoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) //encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高) //encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) //encoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级) //System.Drawing.Bitmap bp = encoder.Encode(short_url, Encoding.GetEncoding("GB2312")); //bp.Save(@"D:QrCode" + productId + ".jpg"); //url = @"D:QrCode" + productId + ".jpg"; string url = "http://qr.liantu.com/api.php?text=" + short_url; //生成二维码 return Json(url); }
步骤三:回调Url获取数据并生成订单,调用统一下单生成预支付订单,将数据回传给微信服务器。微信服务器调用支付让用户完成支付。
public ActionResult NativeCallBack() { Senparc.Weixin.MP.TenPayLibV3.ResponseHandler NativeCallBackRepHandler = new Senparc.Weixin.MP.TenPayLibV3.ResponseHandler(null); string re_appid = NativeCallBackRepHandler.GetParameter("appid"); string re_openid = NativeCallBackRepHandler.GetParameter("openid"); string re_mchid = NativeCallBackRepHandler.GetParameter("mch_id"); string re_is_subscribe = NativeCallBackRepHandler.GetParameter("is_subscribe"); string re_nonce_str = NativeCallBackRepHandler.GetParameter("nonce_str"); string re_product_id = NativeCallBackRepHandler.GetParameter("product_id"); string re_sign = NativeCallBackRepHandler.GetParameter("sign"); string nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr(); string body = "Test-Native-Model1";//商品或支付单简要描述 string out_trade_no = re_product_id;//商户系统内部的订单号,32个字符内,可包含字母,其他说明见商户订单号 int total_fee = 2;//Convert.ToInt32(trade.TRADE_PAYMENT.Value*100);//订单总金额,只能是整数。 string spbill_create_ip = Request.UserHostAddress;//APP和网页支付提交用户端IP,Native支付填调用微信支付API的机器IP string notify_url = "http://×××/PayNotifyUrl";//接收微信支付异步通知回调地址 string trade_type = "NATIVE";//JSAPI,NATIVE,APP,WAP //创建支付应答对象 Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null); //初始化 packageReqHandler.Init(); //设置package订单参数 packageReqHandler.SetParameter("appid", AppId); packageReqHandler.SetParameter("mch_id", MchId); packageReqHandler.SetParameter("nonce_str", nonceStr); packageReqHandler.SetParameter("body", body); packageReqHandler.SetParameter("out_trade_no", out_trade_no); packageReqHandler.SetParameter("total_fee", total_fee.ToString()); packageReqHandler.SetParameter("spbill_create_ip", spbill_create_ip); packageReqHandler.SetParameter("notify_url", notify_url); packageReqHandler.SetParameter("trade_type", trade_type); packageReqHandler.SetParameter("product_id", re_product_id); string sign = packageReqHandler.CreateMd5Sign("key", Key); packageReqHandler.SetParameter("sign", sign); string data = packageReqHandler.ParseXML(); var UnifiedorderResult = Senparc.Weixin.MP.AdvancedAPIs.TenPayV3.Unifiedorder(data); var res = System.Xml.Linq.XDocument.Parse(UnifiedorderResult); string prepayId = res.Element("xml").Element("prepay_id").Value; Senparc.Weixin.MP.TenPayLibV3.RequestHandler returnreqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null); returnreqHandler.SetParameter("return_code", "SUCCESS"); returnreqHandler.SetParameter("result_code", "SUCCESS"); returnreqHandler.SetParameter("appid", AppId); returnreqHandler.SetParameter("mch_id", MchId); returnreqHandler.SetParameter("nonce_str", re_nonce_str); returnreqHandler.SetParameter("prepay_id", prepayId); string returnsign = returnreqHandler.CreateMd5Sign("key", Key); returnreqHandler.SetParameter("sign", returnsign); string xml = returnreqHandler.ParseXML(); return Content(xml, "text/xml"); }