关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

微信扫码支付的操作方法

发布时间:2019-11-20 10:33:06

一、在后台完成微信扫码支付配置

二、新建发起文件,代码如下:

<%@ WebHandler Language="C#" Class="DTcms.Web.dev.wxnetpaysend" %>

//上面这个wxnetpaysend 可以自定义

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.SessionState;

using System.IO;

using System.Text;

using System.Data;

using System.Data.Sql;

using System.Data.SqlClient;

using DTcms.Common;

using DTcms.DBUtility;

using NetWing.Common.Data.SQLServer;

using NetWing.Common.Request;

using DTcms.API.Payment.wxpay;//必须引入微信支付类

using Senparc.Weixin.MP.TenPayLibV3;//必须引入微信支付类用于生产不重复订单号

namespace DTcms.Web.dev

{

    /// <summary> 

    /// 支付展会定金 的摘要说明 

    /// </summary> 

    //wxnetpaysend 这个类可以自定义 必须和上面保持一致

    public class wxnetpaysend : IHttpHandler, IRequiresSessionState

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/html";

            string hname = context.Request["hname"].ToString();//从浏览器得到展会名称

            int hmoney = int.Parse(context.Request["hmoney"].ToString());//从浏览器得到订金

            string porderid = context.Request["hid"].ToString();

            //生成不重复订单号

            string sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28);

            //发起微信扫码支付核心文件

            string sendUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            //支付成功或失败通知文件

            string notify_url="http://" + HttpContext.Current.Request.Url.Authority.ToLower() + "/dev/wxnetpayReceive.ashx";

            NativeConfig nativeConfig = new NativeConfig();

            WxPayData data = new WxPayData();

            data.SetValue("body", hname); //商品描述

            data.SetValue("detail", hname + "-" + "订金"); //商品详情 格式xx展会定金

            data.SetValue("out_trade_no", sp_billno); //商户订单号

            data.SetValue("total_fee", (Convert.ToDouble(hmoney) * 100).ToString()); //订单总金额,以分为单位

            data.SetValue("trade_type", "NATIVE");//交易类型

            data.SetValue("product_id", porderid);//商品ID

            data.SetValue("notify_url", notify_url); //异步通知url

            data.SetValue("spbill_create_ip", DTRequest.GetIP()); //终端IP

            data.SetValue("appid", nativeConfig.AppId); //公众账号ID

            data.SetValue("mch_id", nativeConfig.Partner); //商户号

            data.SetValue("nonce_str", NativePay.GenerateNonceStr()); //随机字符串

            data.SetValue("sign", data.MakeSign(nativeConfig.Key)); //签名

            string xml = data.ToXml(); //转换成XML

            var startTime = DateTime.Now; //开始时间

            string response = HttpService.Post(xml, sendUrl, false, 6); //发送请求

            var endTime = DateTime.Now; //结束时间

            int timeCost = (int)((endTime - startTime).TotalMilliseconds); //计算所用时间

            WxPayData result = new WxPayData();

            result.FromXml(response, nativeConfig.Key);

            NativePay.ReportCostTime(sendUrl, timeCost, result);//测速上报

            string codeUrl = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接

            string ImageUrl = "qrcode.aspx?data=" + HttpUtility.UrlEncode(codeUrl);//得到二维码链接

            context.Response.Write("<html><body>");

            context.Response.Write("<img src='/api/payment/wxnatpay/" + ImageUrl + "'>");

            context.Response.Write("</body></html>");

            //发起微信扫码支付核心文件 

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}



/template/Home/Zkeys/PC/Static