【愚公系列】《微信小程序开发解析》018-小程序支付
🏆 作者简介,愚公搬代码
🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,CSDN商业化专家,阿里云专家博主,阿里云签约作者,腾讯云优秀博主,腾讯云内容共创官,掘金优秀博主,亚马逊技领云博主,51CTO博客专家等。
🏆《近期荣誉》:2022年度博客之星TOP2,2023年度博客之星TOP2,2022年华为云十佳博主,2023年华为云十佳博主等。
🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。
🏆🎉欢迎 👍点赞✍评论⭐收藏
🚀前言
随着移动支付的兴起,小程序作为一种新型的应用形式,已成为商家和开发者实现在线交易的重要平台。微信小程序支付功能的强大与便捷,不仅提升了用户的购物体验,也为商家创造了更多的商业机会。因此,了解小程序支付相关知识点,对于开发者和商家而言显得尤为重要。
本文将系统介绍小程序支付的基本概念和实现方式,涵盖支付流程、接口使用及注意事项等关键内容。我们将详细讲解如何接入微信支付、如何处理支付结果以及常见的支付异常处理方式,并结合实际案例,帮助您更好地理解和应用这些知识。
无论您是小程序开发的新手,还是希望深入了解支付功能的经验者,这篇文章都将为您提供实用的指导和参考。让我们一起探索小程序支付的相关知识点,掌握安全、高效的支付解决方案,为您的小程序增添商业价值!
🚀一、微信小程序支付
🔎1.微信小程序支付相关知识点
微信支付版本 v2 和 v3 的对比:
功能/特性 | 微信支付 v2 | 微信支付 v3 |
---|---|---|
接口名称 | 统一下单接口(Unified Order API) | 统一下单接口(Unified Order API) |
API 地址 | https://api.mch.weixin.qq.com/pay/unifiedorder |
https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi |
数据格式 | XML | JSON |
签名算法 | HMAC-SHA256 或 MD5 | HMAC-SHA256 |
返回数据格式 | XML | JSON |
异步通知 | 支持异步通知(XML 格式) | 支持异步通知(JSON 格式) |
敏感信息加密 | 无(默认明文传输) | 支持敏感信息加密(RSA 加密) |
API 密钥管理 | 商户平台设置 | 商户平台设置,并支持平台证书管理 |
支付凭证获取 | 通过 prepay_id 获取 | 通过 prepay_id 获取 |
订单查询 | 支持(XML 格式) | 支持(JSON 格式) |
退款处理 | 支持(XML 格式) | 支持(JSON 格式),增加了敏感信息加密 |
报文验签 | 需要自行实现 | SDK 自动处理 |
交易状态 | 通过订单查询接口获取 | 通过订单查询接口获取,并增加了状态推送 |
🔎2.开发步骤
开发微信小程序支付功能通常可以分为以下四个主要步骤:
- 申请微信支付权限
- 注册并认证公众号或小程序: 确保你拥有一个已认证的微信小程序或公众号。
- 申请微信支付: 在微信公众平台或微信开放平台中,提交申请微信支付权限,并根据要求填写相关资料。
- 获取支付密钥和配置
- 登录微信支付商户平台: 进入微信支付商户平台(pay.weixin.qq.com)。
- 获取商户号和密钥: 在商户平台上获取商户号(mch_id)和支付密钥(API Key)。
- 配置支付目录: 在商户平台中配置支付请求的合法域名和回调地址。
- 后端服务器生成预支付订单
- 请求支付统一下单接口: 在服务器端,调用微信支付的统一下单接口,生成预支付交易会话标识(prepay_id)。
- 参数准备: 准备好APPID、商户号、随机字符串、商品描述、订单金额等信息。
- 签名处理: 使用商户密钥对请求参数进行签名(MD5或HMAC-SHA256)。
- 发送请求: 通过HTTPS POST请求发送到微信支付接口。
- 处理响应: 获取微信返回的prepay_id,并保存用于后续支付请求。
- 小程序客户端发起支付请求
- 调用微信支付API: 在小程序前端,通过
wx.requestPayment
接口发起支付请求。- 参数准备: 使用服务器返回的prepay_id,以及其他支付参数(如时间戳、随机字符串、签名等)。
- 支付结果处理: 用户完成支付后,根据回调结果进行相应处理(如更新订单状态、通知用户等)。
🦋2.1 获取openid
step1: function () {
wx.login({
success: function (res) {
console.log(res.code)
if (res.code) {
//发起网络请求
wx.request({
url: 'https://127.0.0.1/PayXCX/Step1',
data: {
code: res.code
},
success: function (res) {
var pages = getCurrentPages();
var page = pages[pages.length - 1];
page.setData({ bean1: res.data });
}
})
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
},
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import bean.BeanStep1;
import util.WX_Util;
@WebServlet("/Step1")
public class Step1 extends HttpServlet {
private String appid = "wx19fabf1a7aa490ba";
private String secret = "0cce53aa0788df4234c58ef29e7b0e02";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String js_code = request.getParameter("code");
System.out.println(js_code);
BeanStep1 beanStep1 = WX_Util.step1(appid, secret, js_code);
response.getWriter().print(new Gson().toJson(beanStep1));
System.out.println("Step1 "+new Gson().toJson(beanStep1));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
🦋2.2 得到prepay_id
step2: function () {
wx.request({
url: 'https://127.0.0.1/wx/Step2',
data: {
openid: this.data.bean1.openid
},
success: function (res) {
var pages = getCurrentPages();
var page = pages[pages.length - 1];
page.setData({ bean2: res.data });
}
})
},
package servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import bean.BeanStep2;
import util.WX_Util;
@WebServlet("/Step2")
public class Step2 extends HttpServlet {
String key = "jiubaojiubaojiubao23263210889999";
String appid = "wx19fabf1a7aa490ba";
String mch_id = "1434507702";
String total_fee = "100";
String notify_url = "http://1.1.1.1";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String openid = request.getParameter("openid");
Map map = new HashMap();
map.put("appid", appid);
map.put("mch_id", mch_id);
map.put("nonce_str", WX_Util.getRandomStr());
map.put("body", "�ű���ѵ");
map.put("out_trade_no", "jiubao" + System.currentTimeMillis());
map.put("total_fee", total_fee);
map.put("spbill_create_ip", getIP(request));
map.put("notify_url", notify_url);
map.put("trade_type", "JSAPI");
map.put("openid", openid);
BeanStep2 beanStep2 = WX_Util.step2(map , key);
response.getWriter().print(new Gson().toJson(beanStep2));
System.out.println("Step2 "+new Gson().toJson(beanStep2));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public String getIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
🦋2.3 再次签名
step3: function () {
wx.request({
url: 'https://127.0.0.1/wx/Step3',
data: {
prepay_id: this.data.bean2.prepay_id,
},
success: function (res) {
var pages = getCurrentPages();
var page = pages[pages.length - 1];
page.setData({ bean3: res.data });
}
})
},
package servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import bean.BeanStep3;
import util.WX_Util;
@WebServlet("/Step3")
public class Step3 extends HttpServlet {
String key = "jiubaojiubaojiubao23263210889999";
String appId = "wx19fabf1a7aa490ba";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String timeStamp = System.currentTimeMillis()+"";
String nonceStr = WX_Util.getRandomStr();
String prepay_id = request.getParameter("prepay_id");
Map map = new HashMap();
map.put("appId", appId);
map.put("timeStamp", timeStamp);
map.put("nonceStr", nonceStr);
map.put("package", "prepay_id="+prepay_id);
map.put("signType", "MD5");
BeanStep3 beanStep3 = new BeanStep3(timeStamp, nonceStr, WX_Util.sign(map, key));
response.getWriter().print(new Gson().toJson(beanStep3));
System.out.println("Step3 "+new Gson().toJson(beanStep3));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
🦋2.4 pay
step4: function () {
wx.requestPayment(
{
'timeStamp': this.data.bean3.timeStamp,
'nonceStr': this.data.bean3.nonceStr,
'package': 'prepay_id=' + this.data.bean2.prepay_id,
'signType': 'MD5',
'paySign': this.data.bean3.paySign,
'success': function (res) {
console.log(res);
},
'fail': function (res) {
console.log(res);
}
})
}
🔎3.其他源码补充
🦋3.1 Java端
package bean;
public class BeanStep1 {
private String openid = "";
private String session_key = "";
private String errcode = "";
private String errmsg = "";
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getSession_key() {
return session_key;
}
public void setSession_key(String session_key) {
this.session_key = session_key;
}
public String getErrcode() {
return errcode;
}
public void setErrcode(String errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
}
package bean;
import util.WX_Util;
public class BeanStep2 {
private String return_code = "";
private String return_msg = "";
private String appid = "";
private String mch_id = "";
private String nonce_str = "";
private String sign = "";
private String result_code = "";
private String prepay_id = "";
private String trade_type = "";
public BeanStep2(String str) {
this.return_code = WX_Util.extract(str,"return_code");
this.return_msg = WX_Util.extract(str,"return_msg");
this.appid = WX_Util.extract(str,"appid");
this.mch_id = WX_Util.extract(str,"mch_id");
this.nonce_str = WX_Util.extract(str,"nonce_str");
this.sign = WX_Util.extract(str,"sign");
this.result_code = WX_Util.extract(str,"result_code");
this.prepay_id = WX_Util.extract(str,"prepay_id");
this.trade_type = WX_Util.extract(str,"trade_type");
}
public String getReturn_code() {
return return_code;
}
public void setReturn_code(String return_code) {
this.return_code = return_code;
}
public String getReturn_msg() {
return return_msg;
}
public void setReturn_msg(String return_msg) {
this.return_msg = return_msg;
}
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 getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getResult_code() {
return result_code;
}
public void setResult_code(String result_code) {
this.result_code = result_code;
}
public String getPrepay_id() {
return prepay_id;
}
public void setPrepay_id(String prepay_id) {
this.prepay_id = prepay_id;
}
public String getTrade_type() {
return trade_type;
}
public void setTrade_type(String trade_type) {
this.trade_type = trade_type;
}
}
package bean;
public class BeanStep3 {
private String timeStamp = "";
private String nonceStr = "";
private String paySign = "";
public BeanStep3(String timeStamp, String nonceStr, String paySign) {
super();
this.timeStamp = timeStamp;
this.nonceStr = nonceStr;
this.paySign = paySign;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public String getNonceStr() {
return nonceStr;
}
public void setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
}
public String getPaySign() {
return paySign;
}
public void setPaySign(String paySign) {
this.paySign = paySign;
}
}
package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import com.google.gson.Gson;
import bean.BeanStep1;
import bean.BeanStep2;
public class WX_Util {
public static BeanStep1 step1(String appid, String secret, String js_code) {
URL url = null;
HttpURLConnection connection = null;
BufferedReader breader = null;
StringBuffer strb = new StringBuffer();
try {
url = new URL("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + js_code + "&grant_type=authorization_code");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
breader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String str;
while ((str = breader.readLine()) != null) {
strb.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
connection.disconnect();
Gson gson = new Gson();
return gson.fromJson(strb.toString(), BeanStep1.class);
}
public static String getRandomStr() {
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 32; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
public static BeanStep2 step2(Map map,String key) {
URL url = null;
HttpURLConnection connection = null;
BufferedReader breader = null;
StringBuffer strb = new StringBuffer();
try {
url = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
strb = new StringBuffer();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(getXML(map,key));
writer.flush();
writer.close();
breader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String str;
while ((str = breader.readLine()) != null) {
strb.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
connection.disconnect();
return new BeanStep2(strb.toString());
}
public static String getXML(Map map,String key) throws Exception{
Iterator iterator = map.keySet().iterator();
StringBuffer strb = new StringBuffer();
StringBuffer _strb = new StringBuffer();
while(iterator.hasNext()){
String _key = iterator.next().toString();
String _val = map.get(_key).toString();
_strb.append("<"+_key+">"+_val+"</"+_key+">");
}
strb.append(" <xml> ");
strb.append("<sign>"+sign(map, key)+"</sign>");
strb.append(_strb);
strb.append(" </xml> ");
return strb.toString();
}
public static String sign(Map map , String key) {
Set set = map.keySet();
Iterator iterator = set.iterator();
List list = new ArrayList();
while(iterator.hasNext()){
String _key = iterator.next().toString();
if(("".equals(map.get(_key)))||(null==map.get(_key))){
continue;
}
if("sign".equals(_key)){
continue;
}
list.add(_key);
}
Object[] objects = list.toArray();
Arrays.sort(objects);
StringBuffer strb = new StringBuffer();
for (int i = 0; i < objects.length; i++) {
strb.append(objects[i]+"="+map.get(objects[i])+"&");
}
strb.append("key="+key);
return md5(strb.toString());
}
public static String md5(String str) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes("UTF-8"));
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
int val = ((int) bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString().toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String extract(String str , String key){
List<String> valList = new ArrayList<String>();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(str);
InputSource is = new InputSource(sr);
Document document = db.parse(is);
Element root = document.getDocumentElement();
return root.getElementsByTagName(key).item(0).getTextContent();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
🚀感谢:给读者的一封信
亲爱的读者,
我在这篇文章中投入了大量的心血和时间,希望为您提供有价值的内容。这篇文章包含了深入的研究和个人经验,我相信这些信息对您非常有帮助。
如果您觉得这篇文章对您有所帮助,我诚恳地请求您考虑赞赏1元钱的支持。这个金额不会对您的财务状况造成负担,但它会对我继续创作高质量的内容产生积极的影响。
我之所以写这篇文章,是因为我热爱分享有用的知识和见解。您的支持将帮助我继续这个使命,也鼓励我花更多的时间和精力创作更多有价值的内容。
如果您愿意支持我的创作,请扫描下面二维码,您的支持将不胜感激。同时,如果您有任何反馈或建议,也欢迎与我分享。
再次感谢您的阅读和支持!
最诚挚的问候, “愚公搬代码”
- 点赞
- 收藏
- 关注作者
评论(0)