微信扫码支付

来源:互联网 发布:康明斯诊断软件下载 编辑:程序博客网 时间:2024/05/16 16:16

微信扫码支付可以首先到官网下载一个API,我采用的是c#的

首先建一个index.aspx页面,可以输入一些基本的信息,如下图

页面的详细代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WxPayAPI.index" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="js/jquery.multiselect.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-ui.css" />
    <script type="text/javascript" src="js/jquery.1.7.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.multiselect.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style type="text/css">
    body,td,th {
   font-family: 宋体;
   font-size: 14px;
    }
    .aa{ margin-top:10px;}.aa1{ margin-top:15px;}
    .checkbox{width:200px; height:150px; overflow-y:auto; }
    .grv{ font-size:12px; font-family:微软雅黑;border:1px solid #a9a9a9;}
        
</style>
<script type="text/javascript" >
    function payFrm() {
        if (directFrm.DropDownList1.value == "") {
            alert("提醒:请选择产品!");
            directFrm.DropDownList1.focus();
            return false;
        }
        if (directFrm.order_no.value == "") {
            alert("提醒:请填写订单金额!");
            directFrm.order_no.focus();
            return false;
        }
        if (directFrm.Txtdiscount.value == "") {
            alert("提醒:请填写订单折扣!");
            directFrm.Txtdiscount.focus();
            return false;
        }
        if (directFrm.order_price.value == "") {
            alert("提醒:请填写订单的交易金额!");
            directFrm.order_price.focus();
            return false;
        }

        return true;
    }


    $(function () {


        $("#example").multiselect({
            header: false,
            height: 200,
            minWidth: 200,
            selectedList: 10, //预设值最多显示10被选中项
            hide: ["explode", 500],
            noneSelectedText: '',
            close: function () {
                var values = $("#example").val();
                $("#hfexample").val(values);
            }
        });


    });
</script>
</head>
<body style=" margin-top:0px;">
<form id="directFrm" runat="server">
<div align="center">
  <table width="760" style=" height:406; margin-top:50px;"border="0" align="center" cellpadding="0" cellspacing="0">
    <tbody>
    <tr>
      <td align="center" valign="top">
    <table width="760" border="0" cellspacing="0" cellpadding="3" ><tbody>
    <tr>
          <td width="743" colspan="2" height="30" bgcolor="#FF6600"><font style="color:#FFFFFF;font-size:14px; text-align:center;"><b> 微信扫码支付</b></font></td>
        </tr>
      </tbody></table>
   <table width="760" style=" height:42px;"  border="0" align="center" cellpadding="0" cellspacing="0">
    <tbody><tr style=" text-align:left;">
<td height="30"><span class="STYLE2"><img alt="" src="image/arrow_02_01.gif"/> 填写订单信息</span></td>
       </tr>
   </tbody></table>
        <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
            <tbody>
            <tr>
              <td align="left">
                <table width="760" style=" height:30px;" border="0" align="left" cellpadding="0" cellspacing="0" bgcolor="#FFCC00">
                  <tbody>
                      <tr>
                        <td align="center" valign="top" bgcolor="#FFFFEE">
                        <table width="760" style=" height:351px;" border="0" cellpadding="6" cellspacing="0" class="px14">
                            <tbody>
                                <tr>
                                    <td valign="top" style=" width:300px;" >
                                     <asp:ScriptManager runat="server" ID="ScriptManager1" ></asp:ScriptManager>
                                     <asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline">
                                        <ContentTemplate>
                                          <table  style=" height:200px;" border="0" cellpadding="6" cellspacing="0" class="px14">
                                            <tbody>
                                              <tr>
                                        <td width="102" height="26" align="right" valign="top" >产品详情:</td>
                                        <td  valign="top" colspan="2">
                                                 <asp:DropDownList ID="DropDownList1" runat="server"
                                                    onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" Width="300" CssClass="login_wbk" 
                                                    style="height:25px; font-size:12px; line-height:25px " Font-Bold="True" >
                                                     <asp:ListItem Value="">--请选择--</asp:ListItem>
                                                    <asp:ListItem Value="0">产品</asp:ListItem>
                                                    <asp:ListItem Value="1">检查</asp:ListItem>
                                                    <asp:ListItem Value="2">临检</asp:ListItem>
                                                    <asp:ListItem Value="3">生化</asp:ListItem>
                                                    <asp:ListItem Value="4">免疫</asp:ListItem>
                                                    <asp:ListItem Value="5">病毒</asp:ListItem>
                                                    <asp:ListItem Value="6">尿便</asp:ListItem>
                                                    <asp:ListItem Value="7">特检</asp:ListItem>
                                                    <asp:ListItem Value="8">服务</asp:ListItem>
                                                    <asp:ListItem Value="9">其他</asp:ListItem>
                                                </asp:DropDownList>
                                                 <div runat="server" id="div1" style="display:none;  text-align:left; margin-top:15px; width:300px; height:100px; overflow-y:auto; border:1px solid #a9a9a9;  ">
                                                     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                                                     </asp:CheckBoxList>
                                                 </div> 
                                                 <div id="zidingyi" runat="server"><asp:TextBox ID="TxtZiDingyi"  runat="server" CssClass="aa"  Width="300px" 
                                                         ontextchanged="TxtZiDingyi_TextChanged" AutoPostBack="True" ></asp:TextBox></div>
                                                 <div runat="server" id="trbtn" style=" width:300px; display:none;text-align:left; margin-top:15px;">
                                                   <asp:ImageButton ID="BtnNext" runat="server" ImageUrl="image/next.png" onclick="BtnNext_Click" />
                                                   <asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label>
                                                   <asp:Label ID="Label2" runat="server" Text="" Visible="false"></asp:Label>
                                                </div>
                                                 <div>
                                                 <asp:GridView ID="GridView2" runat="server" Width="300px" CssClass="aa1"  AutoGenerateColumns="true">
                                                 </asp:GridView>
                                                </div>
                                         </td>
                                       </tr>
                                              <tr>
                                                    <td align="right" valign="top">订单金额:</td>
                                                    <td valign="top" colspan="2">
                                                       <asp:TextBox ID="order_no" runat="server"  Width="300px"></asp:TextBox>
                                                    <%--      <asp:RequiredFieldValidator ID="Pnameorder" runat="server" 
                                                ControlToValidate="order_no" Display="Dynamic" Font-Size="12px" ErrorMessage="(请填写订单金额)" 
                                                ForeColor="Red"></asp:RequiredFieldValidator>--%>
                                                       </td>
                                              </tr>
                                              <tr>
                                                <td align="right" valign="top">折&nbsp;&nbsp;&nbsp;&nbsp;扣:</td>
                                                <td valign="top" colspan="2">
                                                   <asp:TextBox ID="Txtdiscount" runat="server"  Width="300px" AutoPostBack="True" 
                                                        ontextchanged="Txtdiscount_TextChanged"></asp:TextBox>
                                                      <%--     <asp:RequiredFieldValidator ID="zhekou" runat="server" 
                                                ControlToValidate="Txtdiscount" Display="Dynamic" Font-Size="12px" ErrorMessage="(请填写订单折扣)" 
                                                ForeColor="Red"></asp:RequiredFieldValidator>--%>
                                                        </td>
                                              </tr>
                                              <tr>
                                                <td align="right" valign="top">付款金额:</td>
                                                <td valign="top" colspan="2"><asp:TextBox ID="order_price" Width="300px"  runat="server" ></asp:TextBox> 
                                                </td>
                                            </tr>
                                              <%--<tr>
                                            <td height="99" align="right" valign="top">简要说明:</td>
                                            <td valign="top" colspan="2"> <asp:TextBox ID="product_name" runat="server" Columns="48" Rows="5" TextMode="MultiLine"></asp:TextBox>
                                             <br />
                                              请填写您订单的简要说明(30字以内)</td>
                                         </tr>--%>
                                              <tr> 
                                            <td align="right" valign="top">&nbsp;</td>
                                            <td valign="top" style=" text-align:left;"><b>
                                              <asp:ImageButton ID="ibtn_Next" runat="server" ImageUrl="image/nextx.png" OnClientClick="return payFrm()"  onclick="ibtn_Next_Click" />
                                              <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="image/订单查询.png"  onclick="ImageButton1_Click" />
                                            </b></td>
                                        <td valign="top">&nbsp;</td>
                                      </tr>
                                            </tbody></table>
                                         </ContentTemplate>
                                       </asp:UpdatePanel>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                      </tr>
              </tbody>
              </table>
              </td>
           </tr> 
        </tbody>
        </table>
        </td>
    </tr>
  </tbody></table>
</div><hr width="760" size="1"/>
    </form>
  </body>
</html>

第二步是根据产品id,生成二维码,官方api中有两种扫码方式,我采用的是模式二,详细在内容在NativePayPage.aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NativePayPage.aspx.cs" Inherits="WxPayAPI.NativePayPage" %>


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="content-type" content="text/html;image/gif;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <title>微信扫码支付</title>
</head>
<body>
<div style="margin-left: 10px;color:#00CD00;font-size:30px;font-weight: bolder;">扫码支付模式一</div><br/>
<asp:Image ID="Image1" runat="server" style="width:200px;height:200px;"/>
<br/><br/><br/>
<div style="margin-left: 10px;color:#00CD00;font-size:30px;font-weight: bolder;">扫码支付模式二</div><br/>
<asp:Image ID="Image2" runat="server" style="width:200px;height:200px;"/>

</body>
</html>

.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Text;
using ThoughtWorks;
using ThoughtWorks.QRCode;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;


namespace WxPayAPI
{
    public partial class NativePayPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Log.Info(this.GetType().ToString(), "page load");
            string productId = Request["productId"];
            //string product_name = Request["product_name"];
            string order_price = Request["order_price"];
            string ProName = Request["ProName"]; 
            NativePay nativePay = new NativePay();
            double money = 0;
            if (null == Request["order_price"])
            {
                Response.End();
                return;
            }
            try
            {
                money = Convert.ToDouble(order_price);
            }
            catch
            {
                //Response.Write("支付金额格式错误!");
                //Response.End();
                //return;
            }


            //生成扫码支付模式一url
            //string url1 = nativePay.GetPrePayUrl(productId);


            //生成扫码支付模式二url
            string url2 = nativePay.GetPayUrl(productId, money, ProName);


            //将url生成二维码图片
            Image1.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url1);
            Image2.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url2);
        }
    }
}

第三步是生成二维码的图片在MakeQRCode.aspx页面

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using ThoughtWorks;
using ThoughtWorks.QRCode;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;


namespace WxPayAPI
{
    public partial class MakeQRCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["data"]))
            {
                string str = Request.QueryString["data"];


                //初始化二维码生成工具
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                qrCodeEncoder.QRCodeVersion = 0;
                qrCodeEncoder.QRCodeScale = 4;
                
                //将字符串生成二维码图片
                Bitmap image = qrCodeEncoder.Encode(str, Encoding.Default);


                //保存为PNG到内存流  
                MemoryStream ms = new MemoryStream();
                image.Save(ms, ImageFormat.Png);


                //输出二维码图片
                Response.BinaryWrite(ms.GetBuffer());
                Response.End();
            }
        }
    }
}

如下图


然后扫描二维码,付款即可

注意官方demo的Config.cs文件中需要配置一些东西,例如商户id,公众号id等

using System;
using System.Collections.Generic;
using System.Web;


namespace WxPayAPI
{
    
    //* 配置账号信息
   public class WxPayConfig
    {
        //=======【基本信息设置】=====================================
        /* 微信公众号信息配置
        * APPID:绑定支付的APPID(必须配置)
        * MCHID:商户号(必须配置)
        * KEY:商户支付密钥,参考开户邮件设置(必须配置)
        * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置)
        */
        public const string APPID = "";
        public const string MCHID = "";
        public const string KEY = "";
        public const string APPSECRET = "";


        //=======【证书路径设置】===================================== 
        /* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要)
        */
        public const string SSLCERT_PATH = "cert/apiclient_cert.p12";
        public const string SSLCERT_PASSWORD = "1233410002";






        //=======【支付结果通知url】===================================== 
        /* 支付结果通知回调url,用于商户接收支付结果
        */
       public const string NOTIFY_URL = "http://paysdk.weixin.qq.com/example/ResultNotifyPage.aspx";


        //=======【商户系统后台机器IP】===================================== 
        /* 此参数可手动配置也可在程序中自动获取
        */
        public const string IP = "";




        //=======【代理服务器设置】===================================
        /* 默认IP和端口号分别为0.0.0.0和0,此时不开启代理(如有需要才设置)
        */
        //public const string PROXY_URL = "http://10.152.18.220:8080";
        //public const string PROXY_URL = "http://0.0.0.0:0";


        //=======【上报信息配置】===================================
        /* 测速上报等级,0.关闭上报; 1.仅错误时上报; 2.全量上报
        */
        public const int REPORT_LEVENL = 1;


        //=======【日志级别】===================================
        /* 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息
        */
        public const int LOG_LEVENL = 0;
    }
}

0 0
原创粉丝点击