前言
项目做的小程序,需要用到小程序支付,在网上找了很多资料,官网Api也看了很久,感觉有点坑,网上都说微信api写的不是很清楚,我也有点这种感觉。废话不多说。
首先你必须要有
- mch_id 商户号id ,个人是不能够使用微信支付功能,需要企业才能使用
- key 商户号的密匙,这个是自己在商户平台设置的,自己设置的
- appid,小程序的appid,创建小程序时分配的。我不确定小程序测试号可不可以使用
实现说明
主要的功能实现步骤有4步,一步一步来,基本没有问题的。
- 后台调用微信支付统一下单接口
- 返回数据给前端,前端通过得到的数据在调用微信接口。小程序部分就是 wx.requestPayment,实现,下面有代码。
- 小程序弹出支付页面,支付,有问题微信会有提示,没有就会成功。
- 微信调用我们自己提供的外网接口,推送支付成功的消息给我们。
代码实现
后台代码
小程序调用下面接口,后台调用微信统一下单接口,返回数据给小程序
@RequestMapping(value="/payment",method = RequestMethod.POST)
public Result payment(@RequestBody FormReceive formReceive){
if(formReceive.getOrderId() == null)
return Result.error("订单id不可为空");
try {
Map<String, Object>map = new HashedMap<String, Object>();
Order order = orderService.selectOrderByOrderId1(formReceive.getOrderId());
String nonce_str = AppCustomerUtils.getRandomString(12)+System.currentTimeMillis();
String out_trade_no = order.getOrder().getOrderNumber();//订单编号
BigDecimal total_fee = (order.getOrder().getActualPayment().multiply(new BigDecimal(100)));
String spbill_create_ip=AppCustomerUtils.getIpAddr(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
String body = "购买商品 "+order.getGoods().getName();
String openid = ((String)redisService.get(formReceive.getToken())).split("::")[1];
//上面一部分代码就是构造Orderpay中地参数,需要改,上面部分地代码都需要改成你自己地
//构造成类
OrderPay orderPay = new OrderPay(AppId, MchId, nonce_str, body, out_trade_no, total_fee+"", spbill_create_ip, NotifyUrl, “JSAPI”, openid, “商户密匙”, “https://api.mch.weixin.qq.com/pay/unifiedorder”);
//调用方法,得到统一下单地数据
Map<String, Object> pac = PayUtil.wxPay(orderPay);
//直接返回给前端,前端调用即可
return Result.ok().put("datas", pac);
} catch (SchedulerException e) {
e.printStackTrace();
}
return Result.error("payment处异常");
}
OrderPay类
package WXAppletPayUtils;
public class OrderPay {
private String appid;//正式小程序的appid
private String mch_id;//商户号的id
private String nonce_str;//随机字符串
private String body;//商品主题内容
private String out_trade_no;//交易号
private String total_fee;//总的费用
private String spbill_create_ip;//本地ip,谁调用就是谁的ip
private String notify_url;//微信回调地址
private String trade_type="JSAPI";//类型
private String openid;//用户openid
private String key;//商户号在平台上设置的密匙,自己设置的
private String pay_url;//微信支付的地址
public OrderPay(String appid, String mch_id, String nonce_str, String body, String out_trade_no, String total_fee, String spbill_create_ip, String notify_url, String trade_type, String openid, String key, String pay_url) {
this.appid = appid;
this.mch_id = mch_id;
this.nonce_str = nonce_str;
this.body = body;
this.out_trade_no = out_trade_no;
this.total_fee = total_fee;
this.spbill_create_ip = spbill_create_ip;
this.notify_url = notify_url;
this.trade_type = trade_type;
this.openid = openid;
this.key = key;
this.pay_url = pay_url;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getTotal_fee() {
return total_fee;
}
public void setTotal_fee(String total_fee) {
this.total_fee = total_fee;
}
public String getSpbill_create_ip() {
return spbill_create_ip;
}
public void setSpbill_create_ip(String spbill_create_ip) {
this.spbill_create_ip = spbill_create_ip;
}
public String getNotify_url() {
return notify_url;
}
public void setNotify_url(String notify_url) {
this.notify_url = notify_url;
}
public String getTrade_type() {
return trade_type;
}
public void setTrade_type(String trade_type) {
this.trade_type = trade_type;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getPay_url() {
return pay_url;
}
public void setPay_url(String pay_url) {
this.pay_url = pay_url;
}
}
最关键的工具类,发起http请求微信接口,下面工具代码是没有问题的
wxPay方法用于统一下单,返回包装好的数据,我们直接返回给前端就行
支付成功,微信调用我们的自己写的接口用作微信回调,在接口中我们调用wxNotify方法,得到微信传来的数据,方法直接返回一个map(微信返回的xml格式,以此打包成map)
package WXAppletPayUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
/**
* @ClassName PayUtil.java
* @Description TODO 订单支付
* @Date 2019/8/22 9:20
* @Version 1.0
**/
public class PayUtil {
//调用支付 统一下单 接口,返回Map数据
public static Map<String, Object> wxPay(OrderPay orderPay){
try{
Map<String, String> packageParams = new HashMap<String, String>();
packageParams.put("appid", orderPay.getAppid());
packageParams.put("mch_id", orderPay.getMch_id());
packageParams.put("nonce_str", orderPay.getNonce_str());
packageParams.put("body", orderPay.getBody());
packageParams.put("out_trade_no", orderPay.getOut_trade_no());//商户订单号
packageParams.put("total_fee", orderPay.getTotal_fee());
packageParams.put("spbill_create_ip", orderPay.getSpbill_create_ip());
packageParams.put("notify_url", orderPay.getNotify_url());
packageParams.put("trade_type", orderPay.getTrade_type());
packageParams.put("openid", orderPay.getOpenid());
// 除去数组中的空值和签名参数
packageParams = PayUtil.paraFilter(packageParams);
String prestr = PayUtil.createLinkString(packageParams); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
//MD5运算生成签名,这里是第一次签名,用于调用统一下单接口
String mysign = PayUtil.sign(prestr, orderPay.getKey(), "utf-8").toUpperCase();
//拼接统一下单接口使用的xml数据,要将上一步生成的签名一起拼接进去
String xml = "<xml version='1.0' encoding='utf-8'>" + "<appid>" + orderPay.getAppid() + "</appid>"
+ "<body><![CDATA[" + orderPay.getBody() + "]]></body>"
+ "<mch_id>" + orderPay.getMch_id() + "</mch_id>"
+ "<nonce_str>" + orderPay.getNonce_str() + "</nonce_str>"
+ "<notify_url>" + orderPay.getNotify_url() + "</notify_url>"
+ "<openid>" + orderPay.getOpenid() + "</openid>"
+ "<out_trade_no>" + orderPay.getOut_trade_no() + "</out_trade_no>"
+ "<spbill_create_ip>" + orderPay.getSpbill_create_ip() + "</spbill_create_ip>"
+ "<total_fee>" + orderPay.getTotal_fee() + "</total_fee>"
+ "<trade_type>" +orderPay.getTrade_type() + "</trade_type>"
+ "<sign>" + mysign + "</sign>"
+ "</xml>";
// System.out.println("调试模式_统一下单接口 请求XML数据:" + xml);
//调用统一下单接口,并接受返回的结果
String result = PayUtil.httpRequest(orderPay.getPay_url(), "POST", xml);
// System.out.println("调试模式_统一下单接口 返回XML数据:" + result);
// 将解析结果存储在HashMap中
Map map = PayUtil.doXMLParse(result);
String return_code = (String) map.get("return_code");//返回状态码
//返回给移动端需要的参数
Map<String, Object> response = new HashMap<String, Object>();
if(return_code == "SUCCESS" || return_code.equals(return_code)){
// 业务结果
String prepay_id = (String) map.get("prepay_id");//返回的预付单信息
response.put("nonceStr", orderPay.getNonce_str());
response.put("package", "prepay_id=" + prepay_id);
Long timeStamp = System.currentTimeMillis() / 1000;
response.put("timeStamp", timeStamp + "");//这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
String stringSignTemp = "appId=" + orderPay.getAppid() + "&nonceStr=" + orderPay.getNonce_str() + "&package=prepay_id=" + prepay_id+ "&signType=" + "MD5" + "&timeStamp=" + timeStamp;
//再次签名,这个签名用于小程序端调用wx.requesetPayment方法
String paySign = PayUtil.sign(stringSignTemp, orderPay.getKey(), "utf-8").toUpperCase();
response.put("paySign", paySign);
}
response.put("appid", orderPay.getOpenid());
return response;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static String sign(String text, String key, String input_charset) {
text = text + "&key=" + key;
return DigestUtils.md5Hex(getContentBytes(text, input_charset));
}
public static boolean verify(String text, String sign, String key, String input_charset) {
text = text + key;
String mysign = DigestUtils.md5Hex(getContentBytes(text, input_charset));
if (mysign.equals(sign)) {
return true;
} else {
return false;
}
}
public static byte[] getContentBytes(String content, String charset) {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
}
}
/**
* 生成6位或10位随机数 param codeLength(多少位)
* @return
*/
public static String createCode(int codeLength) {
String code = "";
for (int i = 0; i < codeLength; i++) {
code += (int) (Math.random() * 9);
}
return code;
}
@SuppressWarnings("unused")
private static boolean isValidChar(char ch) {
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
return true;
if ((ch >= 0x4e00 && ch <= 0x7fff) || (ch >= 0x8000 && ch <= 0x952f))
return true;// 简体中文汉字编码
return false;
}
/**
* 除去数组中的空值和签名参数
* @param sArray 签名参数组
* @return 去掉空值与签名参数后的新签名参数组
*/
public static Map<String, String> paraFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<String, String>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
|| key.equalsIgnoreCase("sign_type")) {
continue;
}
result.put(key, value);
}
return result;
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
* @param params 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
public static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
prestr = prestr + key + "=" + value;
} else {
prestr = prestr + key + "=" + value + "&";
}
}
return prestr;
}
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
// 创建SSLContext
StringBuffer buffer = null;
try{
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(requestMethod);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//往服务器端写内容
if(null !=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
// 读取服务器端返回的内容
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
public static String urlEncodeUTF8(String source){
String result=source;
try {
result=java.net.URLEncoder.encode(source, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static Map doXMLParse(String strxml) throws Exception {
if(null == strxml || "".equals(strxml)) {
return null;
}
Map m = new HashMap();
InputStream in = String2Inputstream(strxml);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(in);
Element root = doc.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List children = e.getChildren();
if(children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
}
m.put(k, v);
}
//关闭流
in.close();
return m;
}
/**
* 获取子结点的xml
* @param children
* @return String
*/
public static String getChildrenText(List children) {
StringBuffer sb = new StringBuffer();
if(!children.isEmpty()) {
Iterator it = children.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List list = e.getChildren();
sb.append("<" + name + ">");
if(!list.isEmpty()) {
sb.append(getChildrenText(list));
}
sb.append(value);
sb.append("</" + name + ">");
}
}
return sb.toString();
}
public static InputStream String2Inputstream(String str) throws UnsupportedEncodingException {
return new ByteArrayInputStream(str.getBytes("utf-8"));
}
//支付成功后调用方法中的执行函数,执行后,得到微信推送的消息,返回map
public static Map wxNotify(HttpServletRequest request, HttpServletResponse response, String key) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
br.close();
//sb为微信返回的xml
String notityXml = sb.toString();
String resXml = "";
System.out.println("接收到的报文:" + notityXml);
Map map = PayUtil.doXMLParse(notityXml);
String returnCode = (String) map.get("return_code");
if("SUCCESS".equals(returnCode)){
//验证签名是否正确
if(PayUtil.verify(PayUtil.createLinkString(map), (String)map.get("sign"), key, "utf-8")){
/**此处添加自己的业务逻辑代码start**/
/**此处添加自己的业务逻辑代码end**/
//通知微信服务器已经支付成功
resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>"
+ "<return_msg><![CDATA[OK]]></return_msg></xml> ";
}
}else{
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+ "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
}
System.out.println(resXml);
System.out.println("微信支付回调数据结束");
BufferedOutputStream out = new BufferedOutputStream(
response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
return map;
}
}
支付回调方法
@RequestMapping(value="/payCallBack")
public void callback(){
try {
ServletRequestAttributes s = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
System.out.println(PayUtil.wxNotify(s.getRequest(), s.getResponse(), “商户密匙”));
}
小程序代码
这是我测试支付时候用的小程序代码快,就是一个request请求,是可以地
主要参考下面 wx.requestPayment,这样一个请求后,就会弹出支付界面
wx.request({
url: 'https://www.qinjie.xyz:9998/community/app/user/payment',
data: {
orderId:21,
},
method: 'POST',
header: {
"Content-Type": "application/json",
"token":this.data.token
},
success: function (res) {
console.log(res.data);
console.log(res.data.datas.paySign);
wx.requestPayment({
timeStamp: res.data.datas.timeStamp,
nonceStr: res.data.datas.nonceStr,
package: res.data.datas.package,
signType: 'MD5',
paySign: res.data.datas.paySign,
success:function(e){
console.log(e.data);
},
fail:function(e){
console.log("失败");
}
})
},
})
总结
不知道讲清楚没有,希望下次自己看的时候可以明白,后面还会完善微信小程序支付这块,因为涉及到钱的都必须小心。哈哈。大家一起进步啦!(_)