废话不多说直接上代码  不熟悉的直接私信我

依赖

<dependency><groupId>com.github.wechatpay-apiv3</groupId><artifactId>wechatpay-apache-httpclient</artifactId><version>0.4.2</version>
</dependency>

配置类(填写自己的配置)

/*** 微信支付配置信息*/
@PropertySource(value = {"classpath:wxpayConfig.properties"})
@Component
public class WxPayConfigure {private static final Logger log = LoggerFactory.getLogger(WxPayConfigure.class);@Value("${NOTIFY_URL}")private String NOTIFY_URL;@Value("${MCH_ID}")private String MCH_ID; // 商户号@Value("${MCH_SERIAL_NO}")private String MCH_SERIAL_NO; // 商户证书序列号// 你的商户私钥@Value("${API_V3_KEY}")private String API_V3_KEY;@Value("${APP_ID}")private String APP_ID;@Value("${PACKAGE}")private String PACKAGE; //签名固定字符串(必须)// 商户私钥@Value("${PRIVATE_KEY}")private String PRIVATE_KEY;//你的微信支付平台证书@Value("${CERTIFICATE}")private String certificate;@Value("${wxDomainUrl}")private String wxDomainUrl;public String getWxDomainUrl() {return wxDomainUrl;}public void setWxDomainUrl(String wxDomainUrl) {this.wxDomainUrl = wxDomainUrl;}public String getNOTIFY_URL() {return NOTIFY_URL;}public void setNOTIFY_URL(String NOTIFY_URL) {this.NOTIFY_URL = NOTIFY_URL;}public String getMCH_ID() {return MCH_ID;}public void setMCH_ID(String MCH_ID) {this.MCH_ID = MCH_ID;}public String getMCH_SERIAL_NO() {return MCH_SERIAL_NO;}public void setMCH_SERIAL_NO(String MCH_SERIAL_NO) {this.MCH_SERIAL_NO = MCH_SERIAL_NO;}public String getAPI_V3_KEY() {return API_V3_KEY;}public void setAPI_V3_KEY(String API_V3_KEY) {this.API_V3_KEY = API_V3_KEY;}public String getAPP_ID() {return APP_ID;}public void setAPP_ID(String APP_ID) {this.APP_ID = APP_ID;}public String getPACKAGE() {return PACKAGE;}public void setPACKAGE(String PACKAGE) {this.PACKAGE = PACKAGE;}public String getPRIVATE_KEY() {return PRIVATE_KEY;}public void setPRIVATE_KEY(String PRIVATE_KEY) {this.PRIVATE_KEY = PRIVATE_KEY;}public String getCertificate() {return certificate;}public void setCertificate(String certificate) {this.certificate = certificate;}public WxPayConfigure() {}/*** 获取商户秘钥* @return*/private PrivateKey getPrivateKey(){PrivateKey privateKey = PemUtil.loadPrivateKey(PRIVATE_KEY);return privateKey;}/*** 获取签名验证器 启动加载一次* @return* @throws NotFoundException*/@Beanpublic Verifier getVerifier() throws NotFoundException {//获取私钥PrivateKey privateKey = getPrivateKey();// 获取证书管理器实例CertificatesManager certificatesManager = CertificatesManager.getInstance();// 向证书管理器增加需要自动更新平台证书的商户信息try {certificatesManager.putMerchant(MCH_ID, new WechatPay2Credentials(MCH_ID,new PrivateKeySigner(MCH_SERIAL_NO, privateKey)), API_V3_KEY.getBytes(StandardCharsets.UTF_8));//... 若有多个商户号,可继续调用putMerchant添加商户信息} catch (IOException | HttpCodeException | GeneralSecurityException e) {log.error("微信商户私钥加载失败={}",e.getMessage());}// 从证书管理器中获取verifierreturn certificatesManager.getVerifier(MCH_ID);}/*** 获取微信支付http请求对象并且只加载一次* @param verifier* @return*/@Beanpublic  CloseableHttpClient getWxPayClient(Verifier verifier){//获取商户私钥PrivateKey privateKey = getPrivateKey();// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create().withMerchant(MCH_ID, MCH_SERIAL_NO, privateKey).withValidator(new WechatPay2Validator(verifier));CloseableHttpClient httpClient = builder.build();/*** 使用WechatPayHttpClientBuilder需要调用withWechatPay设置微信支付平台证书,* 而平台证书又只能通过调用获取平台证书接口下载。为了解开"死循环",* 你可以在第一次下载平台证书时,按照下述方法临时"跳过”应答签名的验证。*/
//        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
//                .withMerchant(MCH_ID, MCH_SERIAL_NO, privateKey)
//                .withValidator(response -> true) // NOTE: 设置一个空的应答签名验证器,**不要**用在业务请求
//                .build();return httpClient;}}

二维码工具类:

public class QrUtil {public static String qrBase64Str(String url) throws Exception{String base64Str = null;ByteArrayOutputStream out = null;try {out = QRCode.from(url).to(ImageType.JPG).withSize(250, 250).stream();BASE64Encoder encoder = new BASE64Encoder();// 返回Base64编码过的字节数组字符串base64Str = encoder.encode(out.toByteArray());} finally {if (null != out) {out.close();}}return base64Str;}
@Service
public class WxPayUtil {private static Log log = LogFactory.getLog(WxPayUtil.class);@Autowiredprivate WxPayConfigure wxPayConfigure;@Autowiredprivate MyConfiguration myConfiguration;/*** 商户Native支付下单接口,微信后台系统返回链接参数code_url,商户后台系统将code_url值生成二维码图片,用户使用微信客户端扫码后发起支付。*/public String createOrder(PayParams payParams, HttpServletResponse response){
//        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");HttpPost httpPost = new HttpPost(WxApiType.NATIVE_PAY.getValue());httpPost.addHeader("Accept", "application/json");httpPost.addHeader("Content-type","application/json; charset=utf-8");ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectMapper objectMapper = new ObjectMapper();ObjectNode rootNode = objectMapper.createObjectNode();//元化为分BigDecimal bigDecimal = new BigDecimal(payParams.getTotalAmount());BigDecimal str = new BigDecimal("100");BigDecimal moneyResult = bigDecimal.multiply(str);//appid  微信生成的应用ID,全局唯一。 必填//mchid  直连商户号    必填//description  商品描述        必填//out_trade_no  商户订单号 商户系统内部订单号  必填//  time_expire  交易结束时间   非必填 订单失效时间//attach   附加数据//notify_url 必填   回调url   必填//amount  + 订单金额    total int类型(微信要求的)     必填   currency  货币类型  CNY(人民币)  非必填rootNode.put("mchid", wxPayConfigure.getMCH_ID()).put("appid",wxPayConfigure.getAPP_ID() ).put("description", "日照市中心医院自助缴费").put("notify_url", wxPayConfigure.getNOTIFY_URL())//商户订单号  商户系统内部订单号,只能是数字、大小写字母_-*且在同一个商户号下唯一
//                .put("out_trade_no", payParams.getOutTradeNo());.put("out_trade_no", payParams.getOutTradeNo());rootNode.putObject("amount").put("total", moneyResult.intValue());
//                .put("total", 1);
//        rootNode.putObject("payer")
//                .put("openid", "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o");String codeUrl="";try {objectMapper.writeValue(bos, rootNode);httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));CloseableHttpResponse closeableHttpResponse = null;CloseableHttpClient wxPayClient = wxPayConfigure.getWxPayClient(wxPayConfigure.getVerifier());closeableHttpResponse = wxPayClient.execute(httpPost);String bodyAsString = EntityUtils.toString(closeableHttpResponse.getEntity());ObjectMapper mapper = new ObjectMapper();JsonNode jsonNode = mapper.readTree(bodyAsString);//二维码urlcodeUrl = jsonNode.get("code_url").asText();} catch (IOException | NotFoundException e) {e.printStackTrace();}String qrCodePath = myConfiguration.getQrCode();String  filePath = String.format(qrCodePath+"/qr-%s.jpg",payParams.getOutTradeNo());
//                log.info("filePath:" + filePath);ZxingUtils.getQRCodeImge(codeUrl, 256, filePath);//用流读取显示二维码File file = new File(filePath);ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();ImageIO.write(ImageIO.read(file),"jpg",outputStream);outputStream.flush();} catch (IOException e) {e.printStackTrace();}//最后删除生成的图片FileUtil.deleteFile(filePath);return null;}/*** 微信查询订单   查询订单可通过微信支付订单号和商户订单号两种方式查询,两种查询方式返回结果相同*///  微信支付订单号查询   1217752501201407033233368018 微信支付订单号   1230000109 商户号//  get  请求URL: https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}// https://api.mch.weixin.qq.com/v3/pay/transactions/id/1217752501201407033233368018?mchid=1230000109//商户订单号查询    1217752501201407033233368018 商户订单号    1230000109商户号//  商户订单号查询  get  请求URL: https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}// https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/1217752501201407033233368018?mchid=1230000109public String queryWxOrder( PayParams payParams){URIBuilder uriBuilder = null;try {
//            uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/"+payParams.getOutTradeNo()+"?mchid="+wxPayConfigure.getMCH_ID());uriBuilder = new URIBuilder(WxApiType.ORDER_QUERY_BY_NO.getValue()+payParams.getOutTradeNo()+"?mchid="+wxPayConfigure.getMCH_ID());} catch (URISyntaxException e) {e.printStackTrace();}HttpGet httpGet = null;try {assert uriBuilder != null;httpGet = new HttpGet(uriBuilder.build());} catch (URISyntaxException e) {e.printStackTrace();}assert httpGet != null;httpGet.addHeader("Accept", "application/json");CloseableHttpResponse response = null;CloseableHttpClient wxPayClient = null;try {wxPayClient = wxPayConfigure.getWxPayClient(wxPayConfigure.getVerifier());} catch (NotFoundException e) {e.printStackTrace();}try {assert wxPayClient != null;response = wxPayClient.execute(httpGet);} catch (IOException e) {e.printStackTrace();}String bodyAsString = null;try {assert response != null;bodyAsString = EntityUtils.toString(response.getEntity());} catch (IOException e) {e.printStackTrace();}
//        System.out.println(bodyAsString);ObjectMapper mapper = new ObjectMapper();String tradeStateDesc ="";try {JsonNode jsonNode = mapper.readTree(bodyAsString);
//            Map<String,String> map = mapper.convertValue(bodyAsString, Map.class);tradeStateDesc = jsonNode.get("trade_state_desc").asText();} catch (IOException e) {e.printStackTrace();}return tradeStateDesc;}/*** 关闭订单  待测试* @param payParams*/public void closeWxOrder(PayParams payParams){HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/"+payParams.getOutTradeNo()+"/close");httpPost.addHeader("Accept", "application/json");httpPost.addHeader("Content-type","application/json; charset=utf-8");ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectMapper objectMapper = new ObjectMapper();ObjectNode rootNode = objectMapper.createObjectNode();rootNode.put("mchid",wxPayConfigure.getMCH_ID());CloseableHttpResponse response = null;String bodyAsString = null;try {objectMapper.writeValue(bos, rootNode);httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));CloseableHttpClient wxPayClient = wxPayConfigure.getWxPayClient(wxPayConfigure.getVerifier());response = wxPayClient.execute(httpPost);bodyAsString = EntityUtils.toString(response.getEntity());} catch (IOException | NotFoundException e) {e.printStackTrace();}int statusCode = response.getStatusLine().getStatusCode();//204 正常System.out.println(bodyAsString);}/*** 申请退款* @param payParams* @return*/public String refundOrder(PayParams payParams){HttpPost httpPost = new HttpPost(WxApiType.REFUND_ORDER_BY_NO.getValue());httpPost.addHeader("Accept", "application/json");httpPost.addHeader("Content-type","application/json; charset=utf-8");ByteArrayOutputStream bos = new ByteArrayOutputStream();ObjectMapper objectMapper = new ObjectMapper();ObjectNode rootNode = objectMapper.createObjectNode();//元化为分BigDecimal str = new BigDecimal("100");BigDecimal totalBigDecimal = new BigDecimal(payParams.getTotalAmount());BigDecimal totalMoneyResult = totalBigDecimal.multiply(str);BigDecimal refundBigDecimal = new BigDecimal(payParams.getRefundAmount());BigDecimal refundMoneyResult = refundBigDecimal.multiply(str);//appid  微信生成的应用ID,全局唯一。 必填//mchid  直连商户号    必填//description  商品描述        必填//out_trade_no  商户订单号 商户系统内部订单号  必填//  time_expire  交易结束时间   非必填 订单失效时间//attach   附加数据//notify_url 必填   回调url   必填//amount  + 订单金额    total int类型(微信要求的)     必填   currency  货币类型  CNY(人民币)  非必填//商户订单号  商户系统内部订单号,只能是数字、大小写字母_-*且在同一个商户号下唯一rootNode.put("out_trade_no", payParams.getOutTradeNo()).put("out_refund_no",payParams.getRefundTradeNo()).put("reason", "正常退款");rootNode.putObject("amount").put("total", totalMoneyResult.intValue()).put("refund", refundMoneyResult.intValue()).put("currency","CNY");String status="";
//        String reason="";try {objectMapper.writeValue(bos, rootNode);httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));CloseableHttpResponse response = null;CloseableHttpClient wxPayClient = wxPayConfigure.getWxPayClient(wxPayConfigure.getVerifier());response = wxPayClient.execute(httpPost);String bodyAsString = EntityUtils.toString(response.getEntity());ObjectMapper mapper = new ObjectMapper();JsonNode jsonNode = mapper.readTree(bodyAsString);//SUCCESS:退款成功//CLOSED:退款关闭//PROCESSING:退款处理中//ABNORMAL:退款异常status = jsonNode.get("status").asText();
//            reason = jsonNode.get("reason").asText();} catch (IOException | NotFoundException e) {e.printStackTrace();}//return status;}
}

微信支付API v3 Native支付相关推荐

  1. 微信支付 API V3 JSAPI支付 JAVA下载账单

    下载账单 写这个主要是太气人了,开发文档未写具体的代码示例.网上各种搜索了一天都是V2接口的示例V3的标题党,感觉被欺骗了,太气人了(V2接口有个参数APPID,具体业务使用了多个APPID所以不合适 ...

  2. 微信支付API v3接口使用应用篇

    目录 前言 版本 应用 基础配置 1.申请商户API证书 2.设置接口密钥 3.下载平台证书 接口实测 微信支付API官方客户端 1.客户端 2.支付调起参数签名 3.回调通知 参考资料 前言 最近新 ...

  3. 微信支付API v3签名与验签-APP支付问题

    目录 使用API v3微信支付遇到的问题: 1.微信请求客户端配置 2.生成预付款订单 3.拼接字符串使用API v3签名 4.微信支付成功后通知 使用API v3微信支付遇到的问题: 1.jdk版本 ...

  4. 微信支付API V3版本JAVA开发指南

    微信支付版本V3的Demo,在官方上下载下来,压根就是不能直接用的东西,你要想学会用,你就得一层一层的看源码,看文档,要求你事无巨细的做一个接入者. 如果接入API需要让人看源码来理解,我觉得是一件让 ...

  5. PHP项目集成支付宝PC端扫码支付API(国内支付)

    一.PC端(电脑端)网站扫码支付接口申请流程 1. 注册是支付宝商家账号 -- 注册地址: 2. 注册成功后,找到蚂蚁金服开放平台,点击支付应用: 3. 创建应用 4. 添加应用中的电脑支付功能 5. ...

  6. 2022微信支付v3 - Native

    Native支付介绍 参考文档:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_0.shtml Native支付是指商户系统按 ...

  7. 在线支付系列【14】微信支付实战篇之Native支付下单

    有道无术,术尚可求,有术无道,止于术. 文章目录 Native支付 产品介绍 业务流程图 1. 下单 2. 支付 3. 并行处理 Native下单API 案例演示 1. 环境搭建 2. 创建商户订单 ...

  8. 微信支付,JSAPI支付,APP支付,H5支付,Native支付,小程序支付功能详情以及回调处理

    一.支付相关文档地址 支付wiki:https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml 支付api: https://pay.weixin.qq. ...

  9. 微信支付之Native支付H5支付JSAPI支付退款

    参考文档: https://mp.weixin.qq.com/mp/homepage?__biz=MzI3OTIwNDU0MA==&hid=2&sn=efa76e36c5b580e41 ...

最新文章

  1. JavaWeb手机短信实现前台利用JS获取随机验证码,倒计时效果
  2. Delphi 中自定义异常及异常处理的一般方法
  3. MFC之ComboBox控件用法
  4. WinPcap编程调试解决办法
  5. java drawstring字体大小,JAVA中,drawstring 方法的用法,格式是什么啊
  6. quartz mysql索引_分布式系统中的定时任务全解(二)
  7. 《软件测试》第五次作业
  8. mysql里边字符函数_mysql函数(一.字符函数)
  9. UI实用干货素材|工作管理、日程日历专辑
  10. mysql字符串结束符_mysql常见字符串处理函数结束
  11. 使用EasyRecovery简单修复视频
  12. EPLAN教程——导出CAD如何快捷配置
  13. 2021-01-18
  14. 扫描机一直显示连接服务器,扫描仪通过SMTP中继服务器发送通知邮件失败
  15. JAVA 腾讯企业邮箱发送邮件
  16. mono java 性能_Mono对Java的支持
  17. php编写网页实例,网页实例:怎么详细介绍用PHP来编写网页记数器
  18. fuzzy仿真 MATLAB,基于MATLAB的FUZZY控制器的设计和仿真
  19. 最新瑞芯微四核芯片RK3288开源开发板
  20. 计算机应用基础客观答案,20春国家开放大学计算机应用基础客观题资料参考答案...

热门文章

  1. Reporting Service:纵向合并单元格
  2. Maya更改路径后找引用失败解决方法
  3. Problem C: 判断三角形的性质
  4. csdn写博客时图片插入方法
  5. 解决报错Process finished with exit code -1073741571 (0xC00000FD),修改栈大小
  6. [技巧] 关于photoshop中参考线/标尺的应用11条技巧 [
  7. 【前端调试技巧】webview,企业微信
  8. python中什么是异常_一文教你读懂 Python 中的异常信息
  9. 聊一聊前端中常说的接口
  10. PDA手持终端扫描条码开单打印一体 结合后台电脑系统 数据同步交互解决方案...