小程序支付流程交互图:
进入小程序,下单,请求下单支付,调用小程序登录API来获取Openid,生成商户订单
// pages/pay/pay.js
var app = getApp();
Page({
data: {},
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
},
/* 微信支付 */
wxpay: function () {
var that = this;
//登陆获取code
wx.login({
success: function (res) {
//获取openid
that.getOpenId(res.code);
}
});
},
getOpenId: function (code) {
var that = this;
wx.request({
url: "https://api.weixin.qq.com/sns/jscode2session?appid=小程序appid&secret=小程序Secret&js_code=" + code + "&grant_type=authorization_code",
data: {},
method: 'GET',
success: function (res) {
console.log(res.data);
that.generateOrder(res.data.openid);
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
/**生成商户订单 */
generateOrder: function (openid) {
var that = this;
//统一支付
wx.request({
url: 'http://localhost:9090/weixin/payment.do',
method: 'GET',
data: {
total_fee: '666', //金额,注意以分为单位
body: '茅台', //产品简单描述
attach:'广州分店' //附加数据
},
success: function (res) {
var pay = res.data
//发起支付
var timeStamp = pay[0].timeStamp;
var packages = pay[0].package;
var paySign = pay[0].paySign;
var nonceStr = pay[0].nonceStr;
var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };
that.pay(param);
},
})
},
/* 支付 */
pay: function (param) {
wx.requestPayment({
timeStamp: param.timeStamp,
nonceStr: param.nonceStr,
package: param.package,
signType: param.signType,
paySign: param.paySign,
success: function (res) {
wx.navigateBack({
delta: 1, // 回退前 delta(默认为1) 页面
success: function (res) {
wx.showToast({
title: '支付成功',
icon: 'success',
duration: 2000
})
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
fail: function (res) {
// fail
console.log("支付失败");
},
complete: function () {
// complete
console.log("pay complete");
}
})
}
})
调用支付统一下单API来获取prepay_id,并将小程序调起支付数据需要签名的字段appId,timeStamp,nonceStr,package再次签名
后台代码
package com.card.mp.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.card.dto.PaymentDto;
import com.card.framework.utils.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class WeiXinPaymentController extends BaseController {
private final String mch_id = "填写商户号";//商户号
private final String spbill_create_ip = "填写终端IP";//终端IP
private final String notify_url = "域名/weixin/paycallback.do";//通知地址
private final String trade_type = "JSAPI";//交易类型
private final String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//统一下单API接口链接
private final String key = "&key=填写商户支付密钥"; // 商户支付密钥
private final String appid = "填写小程序AppId";
/**
*
* @param openId
* @param total_fee 订单总金额,单位为分。
* @param body 商品简单描述,该字段请按照规范传递。 例:腾讯充值中