整体原则就是按照官方文档一步一步来
注意:只有服务号才能对接微信支付。每年都需要花300块认证费用。
一、引言
在当今数字化支付盛行的时代,微信支付作为主流支付方式之一,为众多应用提供了便捷的支付解决方案。微信 Native 支付,以其原生性和良好的用户体验,在移动应用支付场景中占据重要地位。本文将详细介绍微信 Native 支付的原理、流程、开发要点以及实际应用中的注意事项,帮助开发者更好地集成这一强大的支付功能到自己的应用中。
二、微信 Native 支付概述
微信 Native 支付是指商户系统按微信支付协议生成支付二维码,用户再使用微信“扫一扫”功能扫描二维码后完成支付的模式。这种支付方式适用于线下实体商店、自动售货机等场景,无需用户手动输入支付金额等信息,大大提高了支付效率和便捷性。
三、支付流程详解
四、开发要点与代码示例
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayUtil;
import java.util.HashMap;
import java.util.Map;
public class NativePayExample {
// 商户号
private static final String MCH_ID = "your_mch_id";
// API 密钥
private static final String API_KEY = "your_api_key";
public static void main(String[] args) throws Exception {
WXPayConfig config = new WXPayConfig() {
@Override
public String getAppID() {
return ""; // 如果有 APP 应用相关,可以设置 APPID,这里主要演示 Native 支付可暂不关注
}
@Override
public String getMchID() {
return MCH_ID;
}
@Override
public String getKey() {
return API_KEY;
}
// 其他方法根据实际需求实现,如获取证书等
};
WXPay wxPay = new WXPay(config);
// 构建统一下单请求参数
Map<String, String> data = new HashMap<>();
data.put("body", "商品描述");
data.put("out_trade_no", "商户订单号");
data.put("total_fee", "支付金额(单位:分)");
data.put("spbill_create_ip", "商户服务器 IP");
data.put("notify_url", "支付结果通知回调地址");
data.put("trade_type", "NATIVE");
// 调用统一下单接口
Map<String, String> result = wxPay.unifiedOrder(data);
System.out.println(result);
}
}
在上述代码中,首先配置了商户号和 API 密钥,然后构建统一下单请求参数,调用 unifiedOrder
方法向微信支付服务器发起下单请求,并打印返回结果。
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
public class QrCodeGenerator {
public static void generateQrCode(String content, String filePath) throws WriterException, IOException {
int width = 300;
int height = 300;
// 设置二维码参数
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
// 生成二维码矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 绘制二维码图像
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y)? 0xFF000000 : 0xFFFFFFFF);
}
}
// 保存二维码图像
File outputFile = new File(filePath);
ImageIO.write(image, "png", outputFile);
}
}
在实际应用中,可以将生成二维码的代码集成到商户后台系统中,根据统一下单接口返回的支付链接生成对应的二维码并展示给用户。
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
@RestController
public class NotifyController {
@PostMapping("/wxpay/notify")
public String notify(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 读取通知数据
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
StringBuilder xml = new StringBuilder();
String line;
while ((line = reader.readLine())!= null) {
xml.append(line);
}
reader.close();
// 解析 XML 数据
Map<String, String> notifyMap = WXPayUtil.xmlToMap(xml.toString());
// 验证签名
boolean signVerified = WXPayUtil.isSignatureValid(notifyMap, "your_api_key", WXPayConstants.SignType.HMACSHA256);
if (signVerified) {
// 处理支付结果
String returnCode = notifyMap.get("return_code");
if ("SUCCESS".equals(returnCode)) {
String outTradeNo = notifyMap.get("out_trade_no");
String tradeState = notifyMap.get("trade_state");
if ("SUCCESS".equals(tradeState)) {
// 支付成功,更新订单状态等业务逻辑处理
System.out.println("订单 " + outTradeNo + " 支付成功");
} else {
// 支付失败,根据 tradeState 处理相应逻辑
System.out.println("订单 " + outTradeNo + " 支付失败,状态:" + tradeState);
}
}
// 返回处理结果给微信支付服务器
return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
} else {
// 签名验证失败,记录日志并返回错误信息给微信支付服务器
System.out.println("签名验证失败");
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名验证失败]]></return_msg></xml>";
}
}
}
五、注意事项与常见问题
六、总结
微信 Native 支付为移动应用和线下实体场景提供了一种高效、便捷的支付解决方案。通过深入理解其支付流程、掌握开发要点并注意相关事项,开发者能够顺利地将微信 Native 支付集成到自己的应用或业务系统中,为用户提供优质的支付体验,同时也为商家的业务运营提供有力的支付支持。在实际开发过程中,不断积累经验,灵活应对各种问题,才能更好地发挥微信 Native 支付的优势,促进业务的发展。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。