ASP微信支付(V3支付)让更多ASP新老项目更加方便做社交电商。很多asp老项目一直运营的比较好,也不愿意去更换技术,今天我用我整理的常用WeixinDLL组件给大家讲解微信支付过程。
准备工作:
1、注册微信服务号(一定要认证)、开通微信支付
2、安装微信支付安全证书
此处介绍微信V3支付,一共是用三个执行文件代码进行讲解!
1、toPay.asp 发起支付
2、pay_ok.asp 支付成功页
3、notify.asp 支付成功过程微信服务器与我们的服务器通信,将支付结果数据推送到我们服务器上
页面:toPay.asp
1 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 2 3 <% 4 On Error Resume Next 5 Dim notify_url,redirect_url,payapiurl,attach 6 notify_url = "https://www.domain.com/pay/notify.asp" '支付完成后微信将在后台发送回调处理信息,由本页面接受是否成功支付的信息 7 redirect_url = "https://www.doman.com/pay/pay_ok.asp" '支付完成后,跳转到本页面,用于展示订单支付提示,本页面可以自己修改 8 payapiurl = "https://api.mch.weixin.qq.com/pay/unifiedorder" 9 body = "购买元森美水果酵素" 'Trim(Request.Form("body")) ' 商品名称 10 total_fee = 0.01*100 ''Trim(Request.Form("total_fee"))*100 ' 以分为单位,入库前需要除以100 11 openid = OpenID 'Trim(Request.Form("openid")) ' "oK1dP1tPcSsh6xog898yXiuGlfaI" //测试openid 12 attach = "OnlinePay___1212" ' 用于标记,方便在notify时进行数据库相关操作 13 14 Dim WxObj,Md5Obj 15 Set Md5Obj = Server.CreateObject("WeixinDLL.Md5Class") 16 Set WxObj = Server.CreateObject("WeixinDLL.WeixinClass") 17 WxObj.SetAppID = C_AppID 18 WxObj.SetAppSecret = C_AppSecret 19 WxObj.SetMchid = C_mch_id 20 WxObj.SetMchKey = C_mch_key 21 WxObj.SetSoftCodeID = C_SoftCodeID 22 WxObj.SetMd5Obj = Md5Obj 23 WxObj.SetPayUrl = payapiurl 24 25 ' 通过函数获取参数 26 Dim create_ip,timeStamp,nonce_str,out_trade_no,body,total_fee,openid,prepay_id,paySign 27 create_ip = WxObj.GetIP ' 当前用户网络IP地址 28 out_trade_no = WxObj.MakeRanNum(15) '唯一订单号,可以自行生成 "462101281206294" 29 timeStamp = WxObj.ToUnixTime(now()) ' 1907-1-1 00:00:00时间戳 30 nonce_str = WxObj.MakeRanNumChar(12) ' 随机字符串 31 prepay_id = WxObj.get_prepay_id(attach, body, nonce_str, notify_url, openid, out_trade_no, total_fee) 32 paySign = WxObj.get_paySign(nonce_str, prepay_id, timeStamp) 33 34 Set Md5Obj = Nothing 35 Set WxObj = Nothing 36 If Err Then Response.Write Err.Description 37 %> 38 <!DOCTYPE html> 39 <html> 40 <head> 41 <title>在线下单</title> 42 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 43 <meta id="viewport" name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1; user-scalable=no;" /> 44 <link href="/wx/fonts/font.css" type="text/css" rel="stylesheet" /> 45 <script Language="javascript"> 46 var prepay_id="<%=prepay_id%>"; 47 var paySign="<%=paySign%>"; 48 function Pay_ok(){ 49 alert ("支付成功"); 50 self.location='<%=redirect_url%>?body=<%=body%>&total_fee=<%=total_fee%>&out_trade_no=<%=out_trade_no%>'; 51 } 52 function callpay(){ 53 if(prepay_id==""){ 54 alert("提示:prepay_id获取失败、本次交易结束!"); 55 }else{ 56 WeixinJSBridge.invoke('getBrandWCPayRequest',{ 57 "appId":"<%=C_AppID%>", 58 "timeStamp":"<%=timeStamp%>", 59 "nonceStr":"<%=nonce_str%>", 60 "package":"prepay_id=<%=prepay_id%>", 61 "signType":"MD5", 62 "paySign":"<%=paySign%>" 63 }, 64 function(res){ 65 if(res.err_msg=="get_brand_wcpay_request:ok"){ 66 Pay_ok(); 67 }else if(res.err_msg=="get_brand_wcpay_request:cancel"){ 68 alert("提示:取消交易成功!"); 69 }else if(res.err_msg=="get_brand_wcpay_request:fail"){ 70 alert("提示:支付失败!"); 71 }else{ 72 alert(res.err_code+res.err_desc+res.err_msg); 73 } 74 } 75 ); 76 } 77 } 78 </script> 79 <style type="text/css"> 80 *{ margin:0;padding:0;list-style:none;word-wrap:break-word; } 81 body { -webkit-user-select:none; -webkit-text-size-adjust:none; font-family:Helvetica; background:#eae9e6; } 82 a,button,input,img{-webkit-touch-callout:none;outline:none;} 83 a{text-decoration:none; color:#666666} 84 a[class*="btn"]{display:block;height:42px;line-height:42px;color:#FFFFFF;text-align:center;border-radius:5px;} 85 .clear { clear:both; width:100%; height:1px; } 86 .btn-blue { background:#3D87C3;border:1px solid #1C5E93;} 87 .btn-green { background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, #43C750), color-stop(1, #31AB40));border:1px solid #2E993C; 88 box-shadow:0 1px 0 0 #69D273 inset; height:42px; line-height:42px; margin-top:15px; } 89 .PayInfo { border-top:1px solid rgba(0, 0, 0, 0.1);border-bottom:1px solid rgba(0, 0, 0, 0.1); padding:15px 10px; background:#fff;margin-bottom:5px; margin-top:5px; } 90 .PayInfo .InfoLine { min-height:42px; line-height:42px; position:relative; display:block; } 91 .PayInfo .InfoLine .txtT { width:25%; min-height:42px; line-height:42px; text-align:right; position:absolute; left:0; top:0; font-weight:bold; color:#333; } 92 .PayInfo .InfoLine .txtS { text-align:left; text-indent:5px; min-height:42px; line-height:42px; float:left; margin-left:25%; color:#555; width:75%; } 93 </style> 94 </head> 95 <body> 96 <div class="top"> 97 在线支付 98 </div> 99 <div class="clear"></div> 100 101 <div class="PayInfo"> 102 <div class="InfoLine"><span class="txtT">单号:</span><span class="txtS"><%=out_trade_no%></span></div> 103 <div class="clear"></div> 104 <div class="InfoLine"><span class="txtT">商品:</span><span class="txtS"><%=body%></span></div> 105 <div class="clear"></div> 106 <div class="InfoLine"><span class="txtT">支付金额:</span><span class="txtS"><%=FormatNumber(total_fee*0.01,2,-1)%>元</span></div> 107 <div class="clear"></div> 108 <div class="InfoLine"><a href="javascript:callpay();" class="btn-green">确认支付</a></div> 109 </div> 110 </body> 111 </html>
页面:pay_ok.asp
1 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 6 <meta id="viewport" name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1; user-scalable=no;" /> 7 <link href="/wx/fonts/font.css" type="text/css" rel="stylesheet" /> 8 <title>支付结果</title> 9 <style type="text/css"> 10 *{ margin:0;padding:0;list-style:none;word-wrap:break-word; } 11 body { -webkit-user-select:none; -webkit-text-size-adjust:none; font-family:Helvetica; background:#eae9e6; } 12 a,button,input,img{-webkit-touch-callout:none;outline:none;} 13 a{text-decoration:none; color:#666666} 14 a[class*="btn"]{display:block;height:42px;line-height:42px;color:#FFFFFF;text-align:center;border-radius:5px;} 15 .clear { clear:both; width:100%; height:1px; } 16 17 .PayInfo { border-top:1px solid rgba(0, 0, 0, 0.1);border-bottom:1px solid rgba(0, 0, 0, 0.1); padding:15px 10px; background:#fff;margin-bottom:5px; margin-top:5px; } 18 .PayInfo .InfoLine { min-height:42px; line-height:42px; position:relative; display:block; } 19 .PayInfo .InfoLine .txtT { width:25%; min-height:42px; line-height:42px; text-align:right; position:absolute; left:0; top:0; font-weight:bold; color:#333; } 20 .PayInfo .InfoLine .txtS { text-align:left; text-indent:5px; min-height:42px; line-height:42px; float:left; margin-left:25%; color:#555; width:75%; } 21 .PayInfo .CashStatus { min-height:42px; line-height:42px; font-size:26px; color:#333; display:block; text-align:center; position:relative; } 22 .PayInfo .CashStatus:before { font-size:30px;color:#2E993C; } 23 .PayInfo .viewBtm { display:block; width:100%; height:52px; line-height:52px; text-align:center; } 24 </style> 25 </head> 26 <body> 27 <div class="clear"></div> 28 <div class="PayInfo"> 29 <div class="InfoLine"><span class="txtT">订单编号:</span><span class="txtS"><%=request("out_trade_no")%></span></div> 30 <div class="clear"></div> 31 <div class="InfoLine"><span class="txtT">商品名称:</span><span class="txtS"><%=request("body")%></span></div> 32 <div class="clear"></div> 33 <div class="InfoLine"><span class="txtT">下单金额:</span><span class="txtS">¥<%=FormatNumber(request("total_fee")*0.01,2,-1)%>元</span></div> 34 <div class="clear"></div> 35 <div class="font_icon font_wxpay CashStatus">下单成功!</div> 36 </div> 37 </body> 38 </html>
页面:notify.asp
1 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 2 <% 3 Response.cachecontrol="no-cache" 4 Response.addHeader "pragma","no-cache" 5 Response.expires=-1 6 Response.expiresAbsolute=now-1 7 Response.CharSet="utf-8" 8 Dim notifyObj 9 Set notifyObj = New NotifyClass 10 Call notifyObj.notify_url_return() 11 Set notifyObj = Nothing 12 13 Class NotifyClass 14 Private Sub Class_Initialize() 15 End Sub 16 ' 清空文件对象内存 17 Private Sub Class_Terminate() 18 End Sub 19 '本接口为用户支付成功后,微信后台通知结果的接口,包括Url上的参数及XML内的参数 20 '可通过产品唯一订单号和支付状态,确定用户支付成功后执行的一系列操作 21 Public Sub notify_url_return() 22 on error resume next 23 dim xml_dom,logStr:logStr = "" '生成log的内容变量 24 set xml_dom = Server.CreateObject("MSXML2.DOMDocument") 25 xml_dom.load Request 26 dim return_code,return_msg,result_code,err_code_des 27 ' 数据正常时的通用变量 28 dim openid,is_subscribe,trade_type,bank_type,total_fee,transaction_id,out_trade_no,time_end,attach 29 30 return_code = xml_dom.getelementsbytagname("return_code").item(0).text 31 if return_code="FAIL" then 32 '协议级错误 33 return_msg = xml_dom.getelementsbytagname("return_msg").item(0).text 34 logStr = "{'error':'401','msg_type':'协议级错误','return_msg':'"&return_msg&"'}" 35 else 36 result_code = xml_dom.getelementsbytagname("result_code").item(0).text 37 if result_code="FAIL" then 38 '业务级错误 39 err_code_des = xml_dom.getelementsbytagname("err_code_des").item(0).text 40 logStr = "{'error':'402','msg_type':'业务级错误','err_code_des':'"&err_code_des&"'}" 41 else 42 if return_code="SUCCESS" and result_code="SUCCESS" then 43 '数据正常 44 openid = xml_dom.getelementsbytagname("openid").item(0).text 45 is_subscribe = xml_dom.getelementsbytagname("is_subscribe").item(0).text 46 trade_type = xml_dom.getelementsbytagname("trade_type").item(0).text 47 bank_type = xml_dom.getelementsbytagname("bank_type").item(0).text 48 total_fee = xml_dom.getelementsbytagname("total_fee").item(0).text 49 transaction_id = xml_dom.getelementsbytagname("transaction_id").item(0).text 50 out_trade_no = xml_dom.getelementsbytagname("out_trade_no").item(0).text 51 time_end = xml_dom.getelementsbytagname("time_end").item(0).Text 52 attach = xml_dom.getelementsbytagname("attach").item(0).Text 53 54 ' 此处可以调用与数据库通信的函数 55 end if 56 end if 57 end if 58 dim returnXml,errString 59 returnXml="<xml>"&_ 60 "<return_code><![CDATA[SUCCESS]]></return_code>"&_ 61 "</xml>" 62 Response.Write returnXml '返回SUCCESS给微信 63 End Sub 64 End Class 65 %>
三个页面均用到了WeixinDLL组件,有需要的可以联系博主!