如何调通微信支付及微信发货通知接口(Js API)

如何调通微信支付及微信发货通知接口(Js API)


微信支付提供了一个支付测试页面,微信支付正式使用需要测通支付、发货通知接口 、告警接口、维权接口。告警接口、维权接口非常简单。支付界面调通也相对简单,主要是发货通知接口稍微复杂一点。调通发货通知接口需要注意以下几点:


(1) 微信支付文档中提到发货通知接口的PostData,这个其实不是一个form里的一项,其实 PostData的提法有点误导,理解为json串就可以了。

(2)以下的写法是错误的:

         

                       

                        







 

       
     也不要写成:
     
    其实在支付成功的回调方法中,只需要写一行代码:
   jQuery.post(url,tmpData); //其中url:  "https://api.weixin.qq.com/pay/delivernotify?access_token=生成的token" ,tmpData是一个json串。
   tmpData的构成:
   
    var tmpData = "{";
tmpData += '"appid":"<%=appId%>",';
tmpData += '"openid":"oN6N9tzhHOMg6qA6DySr4IgkhdI8",';
tmpData += '"transid":"<%=transid%>",';
tmpData += '"out_trade_no":"'+orderNo+'",';
tmpData += '"deliver_timestamp":"'+curTime+'",';
tmpData += '"deliver_status":"1",';
tmpData += '"deliver_msg":"ok",';
tmpData += '"app_signature":"'+sign2+'",';//
tmpData += '"sign_method":"sha1"';
 
tmpData += "}";

   transid和 orderNo 及curTime可自己在页面中生成测试数据,这里主要提到签名,签名的函数:

    function getSign2(appId,appKey,openId,transId,outTradeNo,deliver_timestamp)
    {
                var keyvaluestring  =   "appid="+appId+"&appkey="+appKey+"&deliver_msg=ok"+"&deliver_status=1"+"&deliver_timestamp="
                                                          +deliver_timestamp+"&openid="+openId+"&out_trade_no="+outTradeNo+"&transid="+transId;
                var sign2 = CryptoJS.SHA1(keyvaluestring).toString();
                return sign2;
    }

完整的JSP:

<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="com.openjweb.weixin.pojo.*"%>
<%@ page import="com.openjweb.weixin.util.*"%>
<%@ page import="org.openjweb.core.service.*"%>
<%@ page import="com.openjweb.weixin.entity.*"%>
<%@ page import="net.sf.json.*"%>
<%
//下面是openjweb平台中封装的,用户可自行设置appId和appSecret,对于access_token如果不好获取,可在 url中直接输入获取token的连接,获取到json数据后,将token值填到本jsp的tokenId变量,有2小时的可用时间
IDBSupportService service = (IDBSupportService)ServiceLocator.getBean("IDBSupportService3");
WeixinServiceAccount acctEnt = (WeixinServiceAccount)service.findSingleValueByHql("from WeixinServiceAccount where accountId='iartwall'");
String appId =  acctEnt.getAppId();
String appSecret =  acctEnt.getAppSecret();
String transid = String.valueOf(System.currentTimeMillis()); //模拟一个交易号
String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
AccessToken accessToken = null;
String tokenId = "";
String requestUrl = access_token_url.replace("APPID", appId).replace("APPSECRET", appSecret);
JSONObject jsonObject = com.openjweb.weixin.util.WeixinUtil.httpRequest(requestUrl, "GET", null);
// 如果请求成功
if (null != jsonObject) 
{
try {
accessToken = new AccessToken();
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) 
{
accessToken = null;
}
}


if(accessToken!=null)
{
tokenId = accessToken.getToken();
}


System.out.println("http://api.weixin.qq.com/cgi-bin/pay/delivernotify?access_token="+tokenId);
 
%>


   


       
       
         

        公众号支付测试网页
       
       
        
       
       
       
        
    
        
       
       
   
   
      <%
 String proName = new String("手表".getBytes("GB2312")); 
 %>
       

           
               
                   
                       
                           
           
公众号ID
商户ID
总金额
商品名

       


   
       

           

提交


       

        
       
   

调试发货接口出现 not same appid with appid of access_token;   问题,一般不是 appId和access_token不匹配的问题,因为大部分开发人员是可以获得正确的access_token,主要原因是(1)可能用form表单带各种参数提交(2)非json格式 (3)把PostData理解为一个form里的隐藏域并赋值json数据

按照本例的代码,可解决发货通知接口问题。当然微信账号的各种商户id、key要配置正确。

下面是微信发货通知接口调通后的界面(在微信服务号后台查看):

如何调通微信支付及微信发货通知接口(Js API)_第1张图片 如何调通微信支付及微信发货通知接口(Js API)_第2张图片



如何调通微信支付及微信发货通知接口(Js API)_第3张图片 如何调通微信支付及微信发货通知接口(Js API)_第4张图片

你可能感兴趣的:(Java)