package com.L.Wx.controller;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.h5.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("wechat/h5")
public class H5PayController {
public static H5Service service;
/**
* 商户号
*/
public static final String merchantId = "";
/**
* 商户API私钥路径
*/
public static final String privateKeyPath = "";
/**
* 商户证书序列号
*/
public static final String merchantSerialNumber = "";
/**
* 商户APIV3密钥
*/
public static final String apiV3key = "";
/**
* 下单接口
* 下单以后我们不需要做任何操作了,只需要把微信给的url传给前端,然后等待回调接口接收微信支付接口的响应即可
* 后续操作到WechatPayCallBack这个回调类里面操作即可
* */
@PostMapping("/pay")
public PrepayResponse H5Pay(Integer total, String appid, String mchid, String description, String callback) {
Config config =
new RSAAutoCertificateConfig.Builder()
.merchantId(merchantId)
.privateKeyFromPath(privateKeyPath)
.merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3key)
.build();
service = new H5Service.Builder().config(config).build();
// request.setXxx(val)设置所需参数,具体参数可见Request定义
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
/** 总金额 说明:订单总金额,单位为分 */
amount.setTotal(total);
request.setAmount(amount);
/** 公众号ID 说明:公众号ID */
request.setAppid(appid);
/** 直连商户号 说明:直连商户号 */
request.setMchid(mchid);
/** 商品描述 说明:商品描述 */
request.setDescription(description);
/** 通知地址 说明:有效性:1. HTTPS;2. 不允许携带查询串。 */
//todo 这个通知的地址是在支付以后微信支付后台给我们发消息的地址(或接口),接到消息后再返回结果
request.setNotifyUrl(callback);
/** 商户订单号 说明:商户订单号 */
request.setOutTradeNo("out_trade_no_001");
/**这里是场景信息,有时候会出校验失败的问题,这里为了测试流通就加上了,本不是必填字段*/
SceneInfo sceneInfo = new SceneInfo();
//这里是客户端的IP
sceneInfo.setPayerClientIp("14.23.150.211 ");
request.setSceneInfo(sceneInfo);
//调用支付方法发送给微信支付后台,返回url,这个url需要传给前端进行支付的页面跳转
PrepayResponse response = service.prepay(request);
/** h5Url 说明:支付跳转链接 */
String h5Url = response.getH5Url();
System.out.println("h5Url = " + h5Url);
return response;
}
/**
* 商户订单号查询订单
*/
@PostMapping("/selectByOutTradeNo")
public static Transaction queryOrderByOutTradeNo(String mchid, String transactionId) {
QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
request.setMchid(mchid);
request.setOutTradeNo(transactionId);
Transaction result = null;
try {
result = service.queryOrderByOutTradeNo(request);
} catch (ServiceException e) {
// API返回失败, 例如ORDER_NOT_EXISTS
System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
System.out.printf("reponse body=[%s]\n", e.getResponseBody());
}
return result;
}
/**
* 微信支付订单号查询订单
*/
@PostMapping("/selectById")
public static Transaction queryOrderById() {
QueryOrderByIdRequest request = new QueryOrderByIdRequest();
// 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
// 调用接口
request.setMchid("190000****");
request.setTransactionId("4200001569202208304701234567");
try {
Transaction result = service.queryOrderById(request);
System.out.println(result.getTradeState());
} catch (ServiceException e) {
// API返回失败, 例如ORDER_NOT_EXISTS
System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
System.out.printf("reponse body=[%s]\n", e.getResponseBody());
}
return service.queryOrderById(request);
}
/**
* 关闭订单
*/
@PostMapping("/closeOrder")
public static void CloseOrder(String mchid, String transactionId) {
CloseOrderRequest closeRequest = new CloseOrderRequest();
closeRequest.setMchid("190000****");
closeRequest.setOutTradeNo("out_trade_no_001");
// 方法没有返回值,意味着成功时API返回204 No Content
service.closeOrder(closeRequest);
}
}
引入jar包
<!-- 微信支付-->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.5</version>
</dependency>
这里一定要注意,这个包里的okhttp版本要和springboot版本对应上,否则会报错