小程序微信支付

本讲涉及到小程序微信支付、以及java服务器两部分。

商户系统和微信支付系统主要交互:

  1. 小程序内调用登录接口,获取到用户的openid,api参见公共api【小程序登录API
  2. 商户server调用支付统一下单,api参见公共api【统一下单API
  3. 商户server调用再次签名,api参见公共api【再次签名
  4. 商户server接收支付通知,api参见公共api【支付结果通知API
  5. 商户server查询支付结果,api参见公共api【查询订单API

 

第一步: 获取客户的openid

小程序端

var that = this;
wx.login({
    success: function(res) {
        that.getOpenId(res.code);
    }
});
//获取openid
getOpenId: function(code){
    var that = this;
    wx.request({ 
        url: 'https://www.see-source.com/weixinpay/GetOpenId', 
        method: 'POST',
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        data: {'code':code},
        success: function(res) {
            var openId = res.data.openid;
            that.xiadan(openId);
        }
    })
}

java后端

String code = request.getParameter("code");
HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+Configure.getAppID()+"&secret="+Configure.getSecret()+"&js_code="+code+"&grant_type=authorization_code");
//设置请求器的配置
HttpClient httpClient = HttpClients.createDefault();
HttpResponse res = httpClient.execute(httpGet);
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
response.getWriter().append(result);

 

第二步:调用微信的统一下单接口,返回预订单prepay_id

小程序端

var that = this;
wx.request({
    url: 'https://www.see-source.com/weixinpay/xiadan', 
    method: 'POST',
    header: {
        'content-type': 'application/x-www-form-urlencoded'
    },
    data: {'openid':openId},
    success: function(res) {
        var prepay_id = res.data.prepay_id;
        console.log("统一下单返回 prepay_id:"+prepay_id);
        that.sign(prepay_id);
    }
})

java后端

String openid = request.getParameter("openid");
OrderInfo order = new OrderInfo();
order.setAppid(Configure.getAppID());
order.setMch_id(Configure.getMch_id());
order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));
order.setBody("dfdfdf");
order.setOut_trade_no(RandomStringGenerator.getRandomStringByLength(32));
order.setTotal_fee(10);
order.setSpbill_create_ip("123.57.218.54");
order.setNotify_url("https://www.see-source.com/weixinpay/PayResult");
order.setTrade_type("JSAPI");
order.setOpenid(openid);
order.setSign_type("MD5");
//生成签名
String sign = Signature.getSign(order);
order.setSign(sign);

String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order);
System.out.println(result);
L.info("---------下单返回:"+result);
XStream xStream = new XStream();
xStream.alias("xml", OrderReturnInfo.class); 
 
OrderReturnInfo returnInfo = (OrderReturnInfo)xStream.fromXML(result);
JSONObject json = new JSONObject();
json.put("prepay_id", returnInfo.getPrepay_id());
response.getWriter().append(json.toJSONString());

notify_url  是支付完成后就收微信的通知的,告诉你用户是否付款了

注意:Total_fee单位是分,必须是整数,不能是小数

Trade_type字段对于小程序来说固定写成JSAPI

 

第三步:再次签名

小程序端

var that = this;
wx.request({
    url: 'https://www.see-source.com/weixinpay/sign', 
    method: 'POST',
    header: {
        'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
        'repay_id':prepay_id
    },
    success: function(res) {
        that.requestPayment(res.data);
    }
})

java后端

String repay_id = request.getParameter("repay_id");
SignInfo signInfo = new SignInfo();
signInfo.setAppId(Configure.getAppID());
long time = System.currentTimeMillis()/1000;
signInfo.setTimeStamp(String.valueOf(time));
signInfo.setNonceStr(RandomStringGenerator.getRandomStringByLength(32));
signInfo.setRepay_id("prepay_id="+repay_id);
signInfo.setSignType("MD5");
//生成签名
String sign = Signature.getSign(signInfo);
 
JSONObject json = new JSONObject();
json.put("timeStamp", signInfo.getTimeStamp());
json.put("nonceStr", signInfo.getNonceStr());
json.put("package", signInfo.getRepay_id());
json.put("signType", signInfo.getSignType());
json.put("paySign", sign);
L.info("-------再签名:"+json.toJSONString());
response.getWriter().append(json.toJSONString());

 

第四步:小程序调起支付

wx.requestPayment({
    'timeStamp': obj.timeStamp,
    'nonceStr': obj.nonceStr,
    'package': obj.package,
    'signType': obj.signType,
    'paySign': obj.paySign,
    'success':function(res){
    },
    'fail':function(res){
    }
})

接着输入支付密码即可。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值