wechatpay-apache-httpclient完全指南:10分钟上手微信支付APIv3开发 wechatpay-apache-httpclient完全指南10分钟上手微信支付APIv3开发【免费下载链接】wechatpay-apache-httpclient微信支付 APIv3 Apache HttpClient装饰器decorator项目地址: https://gitcode.com/gh_mirrors/we/wechatpay-apache-httpclientwechatpay-apache-httpclient是微信支付APIv3的Apache HttpClient扩展实现了请求签名的生成和应答签名的验证。如果你是使用Apache HttpClient的商户开发者可以使用它构造HttpClient自动携带身份认证信息并检查应答的微信支付签名快速实现微信支付功能。 准备工作环境与依赖环境要求Java 1.8快速安装最新版本已在Maven Central发布你可以通过以下方式引入依赖Gradle在build.gradle文件中加入implementation com.github.wechatpay-apiv3:wechatpay-apache-httpclient:0.5.0Maven在pom.xml中加入dependency groupIdcom.github.wechatpay-apiv3/groupId artifactIdwechatpay-apache-httpclient/artifactId version0.5.0/version /dependency 核心概念关键名词解释在开始开发前需要了解以下核心概念商户API证书证实商户身份包含商户号、证书序列号等信息由CA签发。商户API私钥申请商户API证书时生成保存在本地apiclient_key.pem文件中需妥善保管。微信支付平台证书微信支付提供的包含公钥信息的证书用于验证应答签名。证书序列号CA颁发的证书唯一编号。API v3密钥用于AES-256-GCM加密的对称密钥在商户平台设置。 快速上手3步构建HttpClient第1步获取必要参数准备以下参数merchantId商户号merchantSerialNumber商户API证书序列号merchantPrivateKey商户API私钥可通过PemUtil.loadPrivateKey()加载wechatPayCertificates微信支付平台证书列表或使用定时更新功能第2步构建WechatPayHttpClient使用WechatPayHttpClientBuilder替换原生HttpClientBuilderimport com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; // 构建HttpClient WechatPayHttpClientBuilder builder WechatPayHttpClientBuilder.create() .withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey) .withWechatPay(wechatPayCertificates); // 可继续设置其他参数如超时时间、连接池等 CloseableHttpClient httpClient builder.build();第3步发送支付请求使用构建好的httpClient发送请求自动处理签名和验签// 示例JSAPI下单 HttpPost httpPost new HttpPost(https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi); httpPost.addHeader(Accept, application/json); httpPost.addHeader(Content-type, application/json; charsetutf-8); // 构造请求体使用Jackson示例 ObjectMapper objectMapper new ObjectMapper(); ObjectNode rootNode objectMapper.createObjectNode(); rootNode.put(mchid, 1900009191) .put(appid, wxd678efh567hg6787) .put(description, Image形象店-深圳腾大-QQ公仔) .put(notify_url, https://www.weixin.qq.com/wxpay/pay.php) .put(out_trade_no, 1217752501201407033233368018); rootNode.putObject(amount).put(total, 1); rootNode.putObject(payer).put(openid, oUpF8uMuAJO_M2pxb1Q9zNjWeS6o); httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(rootNode), UTF-8)); // 发送请求并获取响应 CloseableHttpResponse response httpClient.execute(httpPost); String bodyAsString EntityUtils.toString(response.getEntity()); System.out.println(bodyAsString);⚡ 高级功能提升开发效率定时更新平台证书版本≥0.4.0支持自动更新平台证书无需手动维护// 获取证书管理器实例 CertificatesManager certificatesManager CertificatesManager.getInstance(); // 添加商户信息 certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8)); // 获取自动更新的验签器 Verifier verifier certificatesManager.getVerifier(merchantId); // 构建HttpClient时使用该验签器 WechatPayHttpClientBuilder builder WechatPayHttpClientBuilder.create() .withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier));回调通知处理版本≥0.4.2提供NotificationHandler处理回调通知的验签与解密// 构建回调请求 NotificationRequest request new NotificationRequest.Builder() .withSerialNumber(wechatPaySerial) .withNonce(nonce) .withTimestamp(timestamp) .withSignature(signature) .withBody(body) .build(); // 处理回调 NotificationHandler handler new NotificationHandler(verifier, apiV3Key.getBytes(StandardCharsets.UTF_8)); Notification notification handler.parse(request); System.out.println(解密后数据 notification.getDecryptData());图片/视频上传使用WechatPayUploadHttpPost简化媒体文件上传String filePath /your/home/hellokitty.png; URI uri new URI(https://api.mch.weixin.qq.com/v3/merchant/media/upload); File file new File(filePath); try (FileInputStream ins1 new FileInputStream(file)) { String sha256 DigestUtils.sha256Hex(ins1); try (InputStream ins2 new FileInputStream(file)) { HttpPost request new WechatPayUploadHttpPost.Builder(uri) .withImage(file.getName(), sha256, ins2) .build(); CloseableHttpResponse response httpClient.execute(request); } }❓ 常见问题解决如何加载商户私钥使用PemUtil.loadPrivateKey()方法// 从文件加载 PrivateKey merchantPrivateKey PemUtil.loadPrivateKey(new FileInputStream(/path/to/apiclient_key.pem)); // 从字符串加载 PrivateKey merchantPrivateKey PemUtil.loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes(utf-8)));如何下载平台证书首次使用时可临时跳过验签下载证书CloseableHttpClient httpClient WechatPayHttpClientBuilder.create() .withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey) .withValidator(response - true) // 仅用于首次下载证书业务请求需验证签名 .build();依赖冲突解决若出现JacksonNoSuchMethodError可引入Jackson BOM统一版本// Gradle implementation(platform(com.fasterxml.jackson:jackson-bom:2.13.2.20220328))!-- Maven -- parent groupIdcom.fasterxml.jackson/groupId artifactIdjackson-bom/artifactId version2.13.2.20220328/version /parent 更多资源完整示例代码AutoUpdateVerifierTest.uploadImageTest回调处理示例NotificationHandlerTest升级指南UPGRADING.md通过wechatpay-apache-httpclient开发者可以快速集成微信支付APIv3减少签名验签等繁琐工作专注于业务逻辑开发。立即尝试10分钟开启微信支付开发之旅【免费下载链接】wechatpay-apache-httpclient微信支付 APIv3 Apache HttpClient装饰器decorator项目地址: https://gitcode.com/gh_mirrors/we/wechatpay-apache-httpclient创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/29 2:52:50
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/29 2:52:51
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/29 1:29:30
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/29 1:39:24
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 17:20:49
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/29 2:52:53

日新闻

周新闻