driver-server/src/main/java/com/jwl/driver/server/controller/PayNoticeLogController.java

90 lines
3.3 KiB
Java
Raw Normal View History

2023-08-11 11:51:09 +08:00
package com.jwl.driver.server.controller;
2023-08-19 17:24:51 +08:00
import com.alibaba.fastjson.JSONObject;
import com.jwl.driver.server.config.WechatPayConfig;
import com.jwl.driver.server.response.BaseResponse;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
2023-08-11 11:51:09 +08:00
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
2023-08-19 17:24:51 +08:00
import org.springframework.web.servlet.resource.HttpResource;
import javax.annotation.Resource;
2023-08-11 11:51:09 +08:00
/**
* <p>
*
* </p>
*
* @author Automated procedures
* @since 2023-08-10
*/
2023-08-19 17:24:51 +08:00
@Api(tags = "支付回调")
2023-08-11 11:51:09 +08:00
@Controller
2023-08-19 17:24:51 +08:00
@RequestMapping("/payNoticeLog")
2023-08-11 11:51:09 +08:00
public class PayNoticeLogController {
2023-08-19 17:24:51 +08:00
@Resource
private WechatPayConfig wechatPayConfig;
/**
*
* @param wechatPayCertificateSerialNumber
* @param signature
* @param timstamp
* @param nonce
* @param requestBody
* @return
*/
@ApiOperation("支付回调接口")
@PostMapping()
public JSONObject payNotice(@RequestHeader("Wechatpay-Serial") String wechatPayCertificateSerialNumber,
@RequestHeader("Wechatpay-Signature") String signature,
@RequestHeader("Wechatpay-Timestamp") String timstamp,
@RequestHeader("Wechatpay-Nonce") String nonce,
@RequestBody String requestBody){
NotificationConfig config = new RSAAutoCertificateConfig.Builder()
.merchantId(wechatPayConfig.getMchId())
.privateKeyFromPath(wechatPayConfig.getPrivateKeyPath())
.merchantSerialNumber(wechatPayConfig.getPrivateKeyPath())
.apiV3Key(wechatPayConfig.getApiV3Key())
.build();
RequestParam requestParam = new RequestParam.Builder()
.serialNumber(wechatPayCertificateSerialNumber)
.nonce(nonce)
.signature(signature)
.timestamp(timstamp)
// 若未设置signType默认值为 WECHATPAY2-SHA256-RSA2048
.body(requestBody)
.build();
// 初始化 NotificationParser
NotificationParser parser = new NotificationParser(config);
// 验签并解密报文
JSONObject decryptObject = parser.parse(requestParam,JSONObject.class);
System.out.println("decryptObject="+decryptObject.toJSONString());
2023-08-11 11:51:09 +08:00
2023-08-19 17:24:51 +08:00
String trade_state=decryptObject.getString("trade_state");
JSONObject jsonResponse = new JSONObject();
if(trade_state.equals("SUCCESS")) {
//各种业务逻辑
}else{
//还是各种业务逻辑
}
jsonResponse.put("code", "SUCCESS");
jsonResponse.put("message", "成功");
return jsonResponse;
}
2023-08-11 11:51:09 +08:00
}