ASP微信支付之扫码支付

一、前提:

1、需要是正规公司;

2、网站不能只是虚拟主机,至少要VPS、云服务器,可以登录服务器管理设置;

3、具备一定熟练程序的ASP编程、IIS配置、调试经验;

4、有足够的耐心和细心,因为这个过程耗时很久、很烦琐,需要做的事情非常多。

二、准备工作:

微信支付在用户开发文档、帮忙文档方面做得不够好,这里一点、那儿一点,让你猜、让你试、让你疯,你需要阅读大量文档、动手实验后才能拼促一个大概的思路,这一点比支付宝差,没有商家服务电话,连常见问题解答也是少得可怜。而如果微信能拿出小米写PPT的功力的百分之一将开发流程列出来,对于开发者来说这个事情就会变得非常简单的。不知道会不会有些用户因为接入微信支付实在太复杂了而放弃了。

写这个的目的就要是为了方便需要的用户少走弯路,但由于时间太久,只能记往以下是大致的流程,

1、免费申请微信公众号“服务号”;通过后,您会获得一个公众号的APPID,这个在后续程序中要用到。

2、花300元/年做微信认证;

3、等微信认证后,申请开通微信支付

4、开通微信支付后,在微信支付开发配置中设置“支付授权目录”、“测试授权目录”、“支付回调URL”

微信支付的程序文件需要3个:

(1)生成二唯码供用户扫描的网页;

(2)支付回调URL,就是当用户扫描二唯码后,微信会调用这个回调用URL;

(3)微信支付异步通知回调地址,当用户在微信上确认支付后,接收微信支付异步通知;

其中第(2)、(3)个文件需要在支付授权的目录下。第(1)的文件不在支付授权目录下也没有影响。

5、微信支付商户资料审核后,会发一封邮件到申请微信支付的邮箱,根据邮件提示,登录商户平台,进行以下操作:

微信支付商户审核通过后,您会获得微信支付商户号 mch_id,这个在后续程序中要用到。

以下操作要在“微信商户平台”上操作,而不是微信公众平台。

(1)验证帐户,微信会往你的对公帐户打入一笔“巨款”,在验证中填写收到款项正确数字。

(2)在“账户设置”中的“API安全”中设置API密钥;API密钥这个就是在后续程序中要用到的key。

(3)在“账户设置”中的“API安全”中下载微信支付API证书;这个证书及证书密码在后续的服务器上配置需要用到。

6、将第5步的(3)中的微信支付证书传到网站服务器上指定一个目录,并解压,解压出来后有微信支付API证书共四份(证书pkcs12格式、证书pem格式、证书密钥pem格式、CA证书),接下来的操作需要在服务器上进行操作,可通过远程桌面连接方式连到服务器上操作:

(1)将证书文件所在的目录的安全中添加everyone用户权限;

(2)运行命令MMC,打开“控制台”,在“文件”->“添加/删除管理单元”,添加“证书”->“我的用户帐户”,再添加“证书”->“计算机帐户”,如下图:

ASP微信支付之扫码支付

然后:
[A]在“控制台根节点” -> “证书 – 当前用户” -> “个人” ->“证书”上右键点击选择“导入”证书,然后根据提示导入到之前从微信商户平台上下载的证书文件。
[B]在“控制台根节点” -> “证书 – 当前用户” -> “中级证书颁发机构” ->“证书”上右键点击选择“导入”证书,然后根据提示导入到之前从微信商户平台上下载的证书文件。
[C]在“控制台根节点” -> “证书(本地计算机)” -> “个人” ->“证书”上右键点击选择“导入”证书,然后根据提示导入到之前从微信商户平台上下载的证书文件。
[D]在“控制台根节点” -> “证书(本地计算机)” -> “中级证书颁发机构” ->“证书”上右键点击选择“导入”证书,然后根据提示导入到之前从微信商户平台上下载的证书文件。

(3)到微软网站上下载并安装winhttpcertcfg.exe文件,下载链接:http://www.microsoft.com/en-us/download/details.aspx?id=19801

(4)安装winhttpcertcfg.exe后,找到其安装目录,一般是:C:\Program Files (x86)\Windows Resource Kits\Tools,将winhttpcertcfg.exe拷贝一份到证书文件所在的目录中;

(5)运行CMD命令行后,在命令行中转到证书所在的目录,然后运行:

winhttpcertcfg -g -i “apiclient_cert.p12” -c LOCAL_MACHINE\My -a “Network Service” -p password

winhttpcertcfg -g -c LOCAL_MACHINE\My -s “MMPay” -a “Network Service”

winhttpcertcfg -g -c LOCAL_MACHINE\My -s “MMPay” -a “EveryOne”

备注说明:以上命令行中的第1行中password为你的证书密码,密码一般设置为商户号;第2、3行中的MMPay为证书名称

提醒:以上命令行中的减号,双引号都为英文半角状态,如果您发现直接复制以上命令运行后,提示的是使用方法,那么说明复制的不行,请手动输入。为了方便您复制,代码形式的再补充一份:

winhttpcertcfg -g -i "apiclient_cert.p12" -c LOCAL_MACHINE\My -a "Network Service" -p password  
winhttpcertcfg -g -c LOCAL_MACHINE\My -s "MMPay" -a "Network Service"  
winhttpcertcfg -g -c LOCAL_MACHINE\My -s "MMPay" -a "EveryOne"

到此为止,准备工作应该是差不多了,由于时间较久,以上都是凭记忆写的,没有再次验主,如果漏掉了请见谅!

三、写微信支付程序:

微信的程序编码都需要用UTF-8,如果网站原来用GBK、GB2312的也没关系,只要在涉及微信支付程序的文件头中加入编码申请即可,另外,网页文件也要以UTF-8编码存储,验证方法:用记事本打开网页文件,然后选择另存为,在弹出的框中编码一栏如果显示的是UTF-8,则表明该网页文件已经是以UTF-8编码存储的。

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
<%Session.CodePage=65001%>  
<%Response.Charset = "utf-8"%>  
<%Response.CodePage=65001%>

1、MD5,微信签名算法中需要用到

网络上有各种MD5,但在微信支付中有汉字时就不能用了,以下这款可用,复制后到记事本中存为MD5.asp文件,存的时候要记得选择UTF-8编码格式:

<%Private Const BITS_TO_A_BYTE = 8  
Private Const BYTES_TO_A_WORD = 4  
Private Const BITS_TO_A_WORD = 32  
Private m_lOnBits(30)  
Private m_l2Power(30)  
Private Function LShift(lValue, iShiftBits)  
    If iShiftBits = 0 Then  
        LShift = lValue  
        Exit Function  
    ElseIf iShiftBits = 31 Then  
        If lValue And 1 Then  
            LShift = &H80000000  
        Else  
            LShift = 0  
        End If  
        Exit Function  
    ElseIf iShiftBits < 0 Or iShiftBits > 31 Then  
        Err.Raise 6  
    End If  
    If (lValue And m_l2Power(31 - iShiftBits)) Then  
        LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000  
    Else  
        LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))  
    End If  
End Function  
Private Function str2binold(varstr)  
     str2bin=""  
     For i = 1 To Len(varstr)  
         varchar=mid(varstr,i,1)  
         varasc = Asc(varchar)  
         If varasc < 0 Then  
            varasc = varasc + 65535  
         End If  
         If varasc > 255 Then  
            varlow = Left(Hex(Asc(varchar)),2)  
            varhigh = right(Hex(Asc(varchar)),2)  
            str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)  
         Else  
            str2bin = str2bin & chrB(AscB(varchar))  
         End If  
     Next  
End Function  
Private Function str2bin(varstr)  
    Dim varchar, code, codearr, j  
    str2bin = ""  
    For i=1 To Len(varstr)  
        varchar = Mid(varstr,i,1)  
        code = Server.UrlEncode(varchar)  
        If Len(code) = 1 Then  
           str2bin = str2bin & chrB(AscB(code))  
        Else  
           codearr = Split(code,"%")  
           For j = 1 to UBound(codearr)  
              str2bin = str2bin & ChrB("&H" & codearr(j))  
           Next  
         End If  
    Next  
End Function  
Private Function RShift(lValue, iShiftBits)  
    If iShiftBits = 0 Then  
        RShift = lValue  
        Exit Function  
    ElseIf iShiftBits = 31 Then  
        If lValue And &H80000000 Then  
            RShift = 1  
        Else  
            RShift = 0  
        End If  
        Exit Function  
    ElseIf iShiftBits < 0 Or iShiftBits > 31 Then  
        Err.Raise 6  
    End If  
    RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)  
    If (lValue And &H80000000) Then  
        RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))  
    End If  
End Function  
Private Function RotateLeft(lValue, iShiftBits)  
    RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))  
End Function  
Private Function AddUnsigned(lX, lY)  
    Dim lX4  
    Dim lY4  
    Dim lX8  
    Dim lY8  
    Dim lResult  
    lX8 = lX And &H80000000  
    lY8 = lY And &H80000000  
    lX4 = lX And &H40000000  
    lY4 = lY And &H40000000  
    lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)  
    If lX4 And lY4 Then  
        lResult = lResult Xor &H80000000 Xor lX8 Xor lY8  
    ElseIf lX4 Or lY4 Then  
        If lResult And &H40000000 Then  
            lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8  
        Else  
            lResult = lResult Xor &H40000000 Xor lX8 Xor lY8  
        End If  
    Else  
        lResult = lResult Xor lX8 Xor lY8  
    End If  
    AddUnsigned = lResult  
End Function  
Private Function md5_F(x, y, z)  
    md5_F = (x And y) Or ((Not x) And z)  
End Function  
Private Function md5_G(x, y, z)  
    md5_G = (x And z) Or (y And (Not z))  
End Function  
Private Function md5_H(x, y, z)  
    md5_H = (x Xor y Xor z)  
End Function  
Private Function md5_I(x, y, z)  
    md5_I = (y Xor (x Or (Not z)))  
End Function  
Private Sub md5_FF(a, b, c, d, x, s, ac)  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))  
    a = RotateLeft(a, s)  
    a = AddUnsigned(a, b)  
End Sub  
Private Sub md5_GG(a, b, c, d, x, s, ac)  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))  
    a = RotateLeft(a, s)  
    a = AddUnsigned(a, b)  
End Sub  
Private Sub md5_HH(a, b, c, d, x, s, ac)  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))  
    a = RotateLeft(a, s)  
    a = AddUnsigned(a, b)  
End Sub  
Private Sub md5_II(a, b, c, d, x, s, ac)  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))  
    a = RotateLeft(a, s)  
    a = AddUnsigned(a, b)  
End Sub  
Private Function ConvertToWordArray(sMessage)  
    Dim lMessageLength  
    Dim lNumberOfWords  
    Dim lWordArray()  
    Dim lBytePosition  
    Dim lByteCount  
    Dim lWordCount  
    Const MODULUS_BITS = 512  
    Const CONGRUENT_BITS = 448  
    lMessageLength = LenB(sMessage)  
    lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)  
    ReDim lWordArray(lNumberOfWords - 1)  
    lBytePosition = 0  
    lByteCount = 0  
    Do Until lByteCount >= lMessageLength  
        lWordCount = lByteCount \ BYTES_TO_A_WORD  
        lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE  
        lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(AscB(MidB(sMessage, lByteCount + 1, 1)), lBytePosition)  
        lByteCount = lByteCount + 1  
    Loop  
    lWordCount = lByteCount \ BYTES_TO_A_WORD  
    lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE  
    lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)  
    lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)  
    lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)  
    ConvertToWordArray = lWordArray  
End Function  
Private Function WordToHex(lValue)  
    Dim lByte  
    Dim lCount  
    For lCount = 0 To 3  
        lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)  
        WordToHex = WordToHex & Right("0" & Hex(lByte), 2)  
    Next  
End Function  
Public Function MD5(sMessage)  
    m_lOnBits(0) = CLng(1)  
    m_lOnBits(1) = CLng(3)  
    m_lOnBits(2) = CLng(7)  
    m_lOnBits(3) = CLng(15)  
    m_lOnBits(4) = CLng(31)  
    m_lOnBits(5) = CLng(63)  
    m_lOnBits(6) = CLng(127)  
    m_lOnBits(7) = CLng(255)  
    m_lOnBits(8) = CLng(511)  
    m_lOnBits(9) = CLng(1023)  
    m_lOnBits(10) = CLng(2047)  
    m_lOnBits(11) = CLng(4095)  
    m_lOnBits(12) = CLng(8191)  
    m_lOnBits(13) = CLng(16383)  
    m_lOnBits(14) = CLng(32767)  
    m_lOnBits(15) = CLng(65535)  
    m_lOnBits(16) = CLng(131071)  
    m_lOnBits(17) = CLng(262143)  
    m_lOnBits(18) = CLng(524287)  
    m_lOnBits(19) = CLng(1048575)  
    m_lOnBits(20) = CLng(2097151)  
    m_lOnBits(21) = CLng(4194303)  
    m_lOnBits(22) = CLng(8388607)  
    m_lOnBits(23) = CLng(16777215)  
    m_lOnBits(24) = CLng(33554431)  
    m_lOnBits(25) = CLng(67108863)  
    m_lOnBits(26) = CLng(134217727)  
    m_lOnBits(27) = CLng(268435455)  
    m_lOnBits(28) = CLng(536870911)  
    m_lOnBits(29) = CLng(1073741823)  
    m_lOnBits(30) = CLng(2147483647)  
    m_l2Power(0) = CLng(1)  
    m_l2Power(1) = CLng(2)  
    m_l2Power(2) = CLng(4)  
    m_l2Power(3) = CLng(8)  
    m_l2Power(4) = CLng(16)  
    m_l2Power(5) = CLng(32)  
    m_l2Power(6) = CLng(64)  
    m_l2Power(7) = CLng(128)  
    m_l2Power(8) = CLng(256)  
    m_l2Power(9) = CLng(512)  
    m_l2Power(10) = CLng(1024)  
    m_l2Power(11) = CLng(2048)  
    m_l2Power(12) = CLng(4096)  
    m_l2Power(13) = CLng(8192)  
    m_l2Power(14) = CLng(16384)  
    m_l2Power(15) = CLng(32768)  
    m_l2Power(16) = CLng(65536)  
    m_l2Power(17) = CLng(131072)  
    m_l2Power(18) = CLng(262144)  
    m_l2Power(19) = CLng(524288)  
    m_l2Power(20) = CLng(1048576)  
    m_l2Power(21) = CLng(2097152)  
    m_l2Power(22) = CLng(4194304)  
    m_l2Power(23) = CLng(8388608)  
    m_l2Power(24) = CLng(16777216)  
    m_l2Power(25) = CLng(33554432)  
    m_l2Power(26) = CLng(67108864)  
    m_l2Power(27) = CLng(134217728)  
    m_l2Power(28) = CLng(268435456)  
    m_l2Power(29) = CLng(536870912)  
    m_l2Power(30) = CLng(1073741824)  
    Dim x  
    Dim k  
    Dim AA  
    Dim BB  
    Dim CC  
    Dim DD  
    Dim a  
    Dim b  
    Dim c  
    Dim d  
    Const S11 = 7  
    Const S12 = 12  
    Const S13 = 17  
    Const S14 = 22  
    Const S21 = 5  
    Const S22 = 9  
    Const S23 = 14  
    Const S24 = 20  
    Const S31 = 4  
    Const S32 = 11  
    Const S33 = 16  
    Const S34 = 23  
    Const S41 = 6  
    Const S42 = 10  
    Const S43 = 15  
    Const S44 = 21  
    x = ConvertToWordArray(str2bin(sMessage))  
    a = &H67452301  
    b = &HEFCDAB89  
    c = &H98BADCFE  
    d = &H10325476  
    For k = 0 To UBound(x) Step 16  
        AA = a  
        BB = b  
        CC = c  
        DD = d  
        md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478  
        md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756  
        md5_FF c, d, a, b, x(k + 2), S13, &H242070DB  
        md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE  
        md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF  
        md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A  
        md5_FF c, d, a, b, x(k + 6), S13, &HA8304613  
        md5_FF b, c, d, a, x(k + 7), S14, &HFD469501  
        md5_FF a, b, c, d, x(k + 8), S11, &H698098D8  
        md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF  
        md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1  
        md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE  
        md5_FF a, b, c, d, x(k + 12), S11, &H6B901122  
        md5_FF d, a, b, c, x(k + 13), S12, &HFD987193  
        md5_FF c, d, a, b, x(k + 14), S13, &HA679438E  
        md5_FF b, c, d, a, x(k + 15), S14, &H49B40821  
        md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562  
        md5_GG d, a, b, c, x(k + 6), S22, &HC040B340  
        md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51  
        md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA  
        md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D  
        md5_GG d, a, b, c, x(k + 10), S22, &H2441453  
        md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681  
        md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8  
        md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6  
        md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6  
        md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87  
        md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED  
        md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905  
        md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8  
        md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9  
        md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A  
        md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942  
        md5_HH d, a, b, c, x(k + 8), S32, &H8771F681  
        md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122  
        md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C  
        md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44  
        md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9  
        md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60  
        md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70  
        md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6  
        md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA  
        md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085  
        md5_HH b, c, d, a, x(k + 6), S34, &H4881D05  
        md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039  
        md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5  
        md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8  
        md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665  
        md5_II a, b, c, d, x(k + 0), S41, &HF4292244  
        md5_II d, a, b, c, x(k + 7), S42, &H432AFF97  
        md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7  
        md5_II b, c, d, a, x(k + 5), S44, &HFC93A039  
        md5_II a, b, c, d, x(k + 12), S41, &H655B59C3  
        md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92  
        md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D  
        md5_II b, c, d, a, x(k + 1), S44, &H85845DD1  
        md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F  
        md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0  
        md5_II c, d, a, b, x(k + 6), S43, &HA3014314  
        md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1  
        md5_II a, b, c, d, x(k + 4), S41, &HF7537E82  
        md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235  
        md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB  
        md5_II b, c, d, a, x(k + 9), S44, &HEB86D391  
        a = AddUnsigned(a, AA)  
        b = AddUnsigned(b, BB)  
        c = AddUnsigned(c, CC)  
        d = AddUnsigned(d, DD)  
    Next  
    MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))  
End Function%>

2、二维码生成程序:

(1)jQuery.min.js 网络上可下载

(2)jquery.qrcode-0.8.0.min.js,网络上可下载

(3)二维码生成:WxQCode.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
<%Session.CodePage=65001%>  
<%Response.Charset = "utf-8"%>  
<%Response.CodePage=65001%>  
<%Response.Buffer =True  
Response.ExpiresAbsolute =Now() - 1  
Response.Expires=0  
Response.CacheControl="no-cache"%><!DOCTYPE html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
<title>微信支付 - 扫码支付</title>  
<script type="text/javascript" src="jquery.min.js"></script>  
<script type="text/javascript" src="jquery.qrcode-0.8.0.min.js"></script>  
<style type="text/css">  
body {  
    font-family: Arial;  
    margin: 0 auto;  
    padding: 0;  
    text-align: center;  
}  
#container {  
    display: inline-block;  
    margin: 20px auto;  
    box-shadow: 0 0 16px rgba(0,0,0,0.5);  
    filter: progid:DXImageTransform.Microsoft.Shadow(color='#969696', Direction=135, Strength=5);  
    -moz-box-shadow:0px 0px 16px rgba(0,0,0,0.5);   /*firefox*/  
    -webkit-box-shadow:0px 0px 16px rgba(0,0,0,0.5);    /*webkit*/  
}  
  
#container > * {  
    display: block;  
}  
#img-buffer {display: none;}  
#PayTitle{width:400px; float:center; text-align: center;font-size:14pt;font-family:"微软雅黑","仿宋","宋体",Arial;line-height:180%;color:#0000ff}  
</style>  
</head>  
<body>  
<!--#include file="md5.asp" -->  
<%Dim appid, mch_id, product_id, time_stamp, nonce_str, key  
appid = "wx1234567890123456"                    '公众号的APPID,替换成您的APPID  
mch_id = "1234567890"                           '微信支付商户号,替换成您的商户号  
key = "12345678901234567890123456789012"        '32位API密钥,替换成您的API密钥  
  
'生成product_id,您可根据您网站的需要生成,或者调用您网站的订单生成这个product_id  
Dim ShiJian, DingDan  
ShiJian = now()  
DingDan = year(shijian) & right("0" & month(shijian), 2) & right("0" & day(shijian), 2) & right("0" & hour(shijian), 2) & right("0" & minute(shijian), 2) & right("0" & second(shijian), 2)  
product_id = DingDan  
  
'时间戳  
time_stamp = DateDiff("s", "1970-1-1 00:00:00", Now())  
  
'随机字符串  
randomize timer  
Dim rdCode  
rdCode = Int((32767)*Rnd +1000)  
nonce_str = MD5(rdCode)  
  
'签名  
Dim stringA  
stringA = "appid=" & appid & "&mch_id=" & mch_id & "&nonce_str=" & nonce_str & "&product_id=" & product_id & "&time_stamp=" & time_stamp   
  
Dim stringSignTemp  
stringSignTemp = stringA & "&key=" & key  
      
sign = UCase(MD5(stringSignTemp))  
  
'二维码链接  
Dim long_url  
long_url = "weixin://wxpay/bizpayurl?sign=" & sign & "&" &  stringA  
  
'生成短网址二维码链接  
Dim short_Url, shortUrl, UrlStr  
shortUrl = "https://api.mch.weixin.qq.com/tools/shorturl"  
  
rdCode = Int((32767)*Rnd +1000)  
nonce_str = MD5(rdCode)  
  
stringA = "appid=" & appid & "&long_url=" & long_url & "&mch_id=" & mch_id & "&nonce_str=" & nonce_str  
stringSignTemp = stringA & "&key=" & key  
  
sign = UCase(MD5(stringSignTemp))  
  
Dim orderStr, ReturnStr  
orderStr = "<xml>" & _  
"<appid>" & appid & "</appid>" & _  
"<long_url><![CDATA[" & long_url & "]]></long_url>" & _  
"<mch_id>" & mch_id & "</mch_id>" & _  
"<nonce_str>" & nonce_str & "</nonce_str>" & _  
"<sign>" & sign & "</sign>" & _  
"</xml>"  
  
ReturnStr =  PostXML(shortUrl, orderStr)  
  
'识别微信消息  
Dim objXml, xmlDoc  
Set objXml = Server.CreateObject("Microsoft.XMLDOM")  
objXml.preserveWhiteSpace = true  
objXml.async = false  
  
'加载微信字符串  
Dim WeiXinStr  
WeiXinStr = ReturnStr  
objXml.loadXML(WeiXinStr)  
Set xmlDoc = objXml.documentElement  
  
Dim NodesLength  
NodesLength = xmlDoc.childNodes.length  
  
Dim i, return_code, return_msg, result_code, err_code  
For i= 0 to NodesLength-1  
    If "#text" <> xmlDoc.childNodes(i).nodeName Then  
        Execute(xmlDoc.childNodes(i).nodeName & "=""" & Replace(Replace(xmlDoc.childNodes(i).text, """", ""), "'", "") & """")  
    End If  
Next  
If result_code = "SUCCESS" Then  
    UrlStr = short_url  
Else  
    UrlStr = long_url  
End If  
  
Function PostXML(pUrl, pXml)  
    PostXML = ""  
    Dim https, ResponseBody  
    Set https = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")  
    https.setOption(3) = "LOCAL_MACHINE\My\MMPay"  
      
    Dim xmlDoc  
    Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.3.0")  
    xmlDoc.async = false  
    xmlDoc.loadXML pXml  
      
    With https   
        .Open "Post", pUrl, False  
        .setRequestHeader "Content-Type","application/xml;charset=UTF-8"  
        .Send xmlDoc    'pXml  
        PostXML = .ResponseText  
    End With   
    Set https = Nothing   
End Function%>  
<div align="center"><div id="PayTitle"><p>请打开手机微信的“扫一扫”功能</p><p>扫描以下二唯码进行支付</p></div>  
<div id="container"></div><script type="text/javascript">  
(function ($) {  
    'use strict';  
    var updateQrCode = function ()  
    {  
        var options =  
        {  
            render: 'canvas',  
            minVersion: 3,  
            maxVersion: 40,   
            ecLevel: 'H',   
            left: 0,  
            top: 0,  
            size: 220,   
            fill: '#000',  
            background: '#FFF',  
            text: '<%=UrlStr%>',   
            radius: 0.2,   
            quiet: 1,   
            mode: 4,   
            mSize: 0.1,  
            mPosX: 0.5,  
            mPosY: 0.5,   
            label: '微信支付',  
            fontname: '微软雅黑',  
            fontcolor: '#FF9818',   //'#60BAE6',   
            image: $("#img-buffer")[0]  
        };  
        $("#container").empty().qrcode(options);  
    },  
    update = function ()  
    {  
        updateQrCode();  
    };  
    $(function ()  
    {  
        $(window).load(update);  
        update();  
    });  
}(jQuery));  
</script>  
</div>  
</body>  
</html>

3、回调URL程序:Wx_Return.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
<%Session.CodePage=65001%>  
<%Response.Charset = "utf-8"%>  
<%Response.CodePage=65001%>  
<!--#include file="md5.asp" -->  
<%Dim WxUrl  
WxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"  
  
'获取微信消息内容  
Dim xmlCount, xmlRequest, xmlResult  
xmlCount = Request.TotalBytes  
If xmlCount>0 Then  
    xmlRequest = Request.BinaryRead(xmlCount)  
    xmlResult = Bytes2bStr(xmlRequest, "utf-8")  
Else  
    Response.End      
End If  
  
'识别微信消息  
Dim objXml, xmlDoc  
Set objXml = Server.CreateObject("Microsoft.XMLDOM")  
'Set objXml = Server.CreateObject("MSXML2.DOMDocument")  
objXml.preserveWhiteSpace = true  
objXml.async = false  
  
'加载微信字符串  
Dim WeiXinStr  
WeiXinStr = xmlResult  
objXml.loadXML(WeiXinStr)  
Set xmlDoc = objXml.documentElement  
  
Dim NodesLength  
NodesLength = xmlDoc.childNodes.length  
  
Dim i, appid, openid, mch_id, is_subscribe, nonce_str, product_id, sign  
For i= 0 to NodesLength-1  
    If "#text" <> xmlDoc.childNodes(i).nodeName Then  
        Execute(xmlDoc.childNodes(i).nodeName & "=""" & Replace(Replace(xmlDoc.childNodes(i).text, """", ""), "'", "") & """")  
    End If  
Next  
  
Dim ShiJian, DingDan, out_trade_no  
ShiJian = now()  
DingDan = year(shijian) & right("0" & month(shijian), 2) & right("0" & day(shijian), 2) & right("0" & hour(shijian), 2) & right("0" & minute(shijian), 2) & right("0" & second(shijian), 2)  
  
Dim wxPayStr  
Dim time_stamp  
out_trade_no = DingDan  
  
time_stamp = DateDiff("s", "1970-1-1 00:00:00", Now())  
  
randomize timer  
Dim rdCode  
rdCode = Int((32767)*Rnd +1000)  
  
nonce_str = MD5(rdCode)  
  
Dim IP  
IP = "127.0.0.1"  
  
Dim key, attach, body, notify_url, total_fee, trade_type  
key = "12345678901234567890123456789012"        '32位API密钥,替换成您的API密钥  
attach = "支付测试"  
body = "扫码支付测试"  
notify_url = "http://127.0.0.1/WxPayTest/Wx_Notify.asp"     '替换成您的网址  
total_fee = 1  
trade_type = "NATIVE"  
  
Dim stringA  
stringA = "appid=" & appid & "&attach=" & attach & "&body=" & body & "&mch_id=" & mch_id & "&nonce_str=" & nonce_str & "¬ify_url=" & notify_url & "&openid=" & openid & "&out_trade_no=" & out_trade_no & "&product_id=" & product_id & "&spbill_create_ip=" & IP & "&total_fee=" & total_fee & "&trade_type=" & trade_type  
  
Dim stringSignTemp  
stringSignTemp = stringA & "&key=" & key  
  
sign = UCase(MD5(stringSignTemp))  
  
Dim orderStr, ReturnStr  
orderStr = "<xml>" & _  
"<appid>" & appid & "</appid>" & _  
"<attach>" & attach & "</attach>" & _  
"<body>" & body & "</body>" & _  
"<mch_id>" & mch_id & "</mch_id>" & _  
"<nonce_str>" & nonce_str & "</nonce_str>" & _  
"<notify_url>" & notify_url & "</notify_url>" & _  
"<openid>" & openid & "</openid>" & _  
"<out_trade_no>" & out_trade_no & "</out_trade_no>" & _  
"<product_id>" & product_id & "</product_id>" & _  
"<spbill_create_ip>" & IP & "</spbill_create_ip>" & _  
"<total_fee>" & total_fee & "</total_fee>" & _  
"<trade_type>" & trade_type & "</trade_type>" & _  
"<sign>" & sign & "</sign>" & _  
"</xml>"  
  
ReturnStr =  PostXML(WxUrl, orderStr)  
Response.write ReturnStr  
  
'----此处根据您的需求加入您的程序对统一下单结果进行处理---------------------------  
  
  
Function PostXML(pUrl, pXml)  
    PostXML = ""  
    Dim https, ResponseBody  
    Set https = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")  
    https.setOption(3) = "LOCAL_MACHINE\My\MMPay"  
      
    Dim xmlDoc  
    Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.3.0")  
    xmlDoc.async = false  
    xmlDoc.loadXML pXml  
      
    With https   
        .Open "Post", pUrl, False  
        .setRequestHeader "Content-Type","application/xml;charset=UTF-8"  
        .Send xmlDoc    'pXml  
        PostXML = .ResponseText  
    End With   
    Set https = Nothing   
End Function%>

此部份中有一个技术点就是PostXML函数中加入证书,https.setOption(3) = “LOCAL_MACHINE\My\MMPay”,这里调用的就是此前在服务器上安装的微信支付证书。

4、微信支付异步通知回调程序:Wx_Notify.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
<%Session.CodePage=65001%>  
<%Response.Charset = "utf-8"%>  
<%Response.CodePage=65001%>  
<%'获取微信消息内容  
Dim xmlCount, xmlRequest, xmlResult  
xmlCount = Request.TotalBytes  
If xmlCount>0 Then  
    xmlRequest = Request.BinaryRead(xmlCount)  
    xmlResult = Bytes2bStr(xmlRequest, "utf-8")  
  
    Dim return_code, return_msg  
    return_code = ""  
    return_msg = ""  
    Dim appid, mch_id, device_info, nonce_str, sign, result_code, err_code, err_code_des, openid, is_subscribe, trade_type, bank_type, total_fee, fee_type, cash_fee, cash_fee_type, coupon_fee, coupon_count, coupon_id_n, coupon_fee_n, transaction_id, out_trade_no, attach, time_end  
    appid = ""  
    mch_id = ""  
    device_info = ""  
    nonce_str = ""  
    sign = ""  
    result_code = ""  
    err_code = ""  
    err_code_des = ""  
    openid = ""  
    is_subscribe = ""  
    trade_type = ""  
    bank_type = ""  
    total_fee = ""  
    fee_type = ""  
    cash_fee = ""  
    cash_fee_type = ""  
    coupon_fee = ""  
    coupon_count = ""  
    coupon_id_n = ""  
    coupon_fee_n = ""  
    transaction_id = ""  
    out_trade_no = ""  
    attach = ""  
    time_end = ""  
      
    '识别微信消息  
    Dim objXml, xmlDoc  
    Set objXml = Server.CreateObject("Microsoft.XMLDOM")  
    objXml.preserveWhiteSpace = true  
    objXml.async = false  
  
    '加载微信字符串  
    Dim WeiXinStr  
    WeiXinStr = xmlResult  
    objXml.loadXML(WeiXinStr)  
    Set xmlDoc = objXml.documentElement  
  
    Dim NodesLength  
    NodesLength = xmlDoc.childNodes.length  
  
    Dim i  
    For i= 0 to NodesLength-1  
        If "#text" <> xmlDoc.childNodes(i).nodeName Then  
            Execute(xmlDoc.childNodes(i).nodeName & "=""" & Replace(Replace(xmlDoc.childNodes(i).text, """", ""), "'", "") & """")  
        End If  
    Next  
      
    If return_code = "SUCCESS" Then  
        '这里写入您对支付成功后的处理程序  
    End If  
      
End If%>

到此为止,微信支付的主体部份已经完成,你还需要做的就是在微信支付的回调结果中加入你自己的处理程序。希望以上部份能帮忙到你。

原创文章,作者:管理员,如若转载,请注明出处:https://www.bajiege.com/soft/other/asp_wxpay.html

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

评论列表(1条)

  • kali
    kali 2018年5月24日 上午12:03

    WxQCode.asp运行后显示如下错误,请问是什么问题?
    Microsoft VBScript 运行时错误 错误 ‘800a01a8’

    缺少对象

    /mmpay/WxQCode.asp,行 109