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

90 lines
3.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.jwl.driver.server.controller;
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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.resource.HttpResource;
import javax.annotation.Resource;
/**
* <p>
* 支付回调日志表 前端控制器
* </p>
*
* @author Automated procedures
* @since 2023-08-10
*/
@Api(tags = "支付回调")
@Controller
@RequestMapping("/payNoticeLog")
public class PayNoticeLogController {
@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());
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;
}
}