无忧付 - 回调

This commit is contained in:
GYJ
2024-12-04 10:17:58 +08:00
parent 29fce0c40e
commit f601de4e6b
5 changed files with 81 additions and 2 deletions

View File

@@ -0,0 +1,46 @@
package com.sqx.modules.pay.controller.app;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.dynamodbv2.xspec.M;
import com.sqx.modules.pay.wuyou.Encrypt;
import com.sqx.modules.pay.wuyou.NotifyDto;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author GYJ
*/
@Slf4j
@RestController
@Api(value = "无忧支付", tags = {"无忧支付"})
@RequestMapping("/app/wuyou")
public class WuyouController {
@PostMapping("/notify")
public String notify(HttpServletRequest request, @RequestBody NotifyDto notifyDto) {
log.info("无忧支付回调");
Map<String, Object> params = new HashMap<>();
params.put("callbacks", notifyDto.getCallbacks());
params.put("total", notifyDto.getTotal());
params.put("out_trade_no", notifyDto.getOutTradeNo());
params.put("pay_time", notifyDto.getPayTime());
String sign = Encrypt.getParamsSign(params);
if (!sign.equals(notifyDto.getSign())) {
log.error("无忧支付回调签名错误, 参数: {},签名结果:{}", JSONObject.toJSONString(notifyDto), sign);
return "签名错误";
}
log.info("无忧支付回调成功, 参数: {}", JSONObject.toJSONString(notifyDto));
return "success";
}
}

View File

@@ -12,6 +12,8 @@ public class BaseResp {
private Integer status;
private String msg;
private OrderResp data;
// 只有 SUCCESS 是成功的
@JSONField(name = "pay_status")
private String payStatus;
}

View File

@@ -12,4 +12,6 @@ public class Constants {
final static String PAY_URL = BASE_URL + "/api/order";
final static String QUERY_URL = BASE_URL + "/api/queryOrder";
final static String NOTIFY_URL = "https://video.hnsiyao.cn/app/wuyou/notify";
}

View File

@@ -0,0 +1,22 @@
package com.sqx.modules.pay.wuyou;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
/**
* @author GYJ
*/
@Data
public class NotifyDto {
// CODE_SUCCESS 代表成功,其余为失败
private String callbacks;
private Double total;
@JSONField(name = "out_trade_no")
private String outTradeNo;
@JSONField(name = "pay_time")
private String payTime;
private String sign;
}

View File

@@ -2,6 +2,8 @@ package com.sqx.modules.pay.wuyou;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
@@ -10,6 +12,8 @@ import java.util.Map;
* @author GYJ
*/
public class WuyouPay {
private static final Logger logger = LoggerFactory.getLogger(WuyouPay.class);
public static BaseResp payOrder(String orderNo, String amount, String userAgent) {
Map<String, Object> params = getBaseParams();
params.put("out_trade_no", orderNo);
@@ -40,7 +44,7 @@ public class WuyouPay {
params.put("mch_id", Constants.MERCHANT_ID);
params.put("timestamp", System.currentTimeMillis() / 1000);
params.put("type", "6001");
params.put("notify_url", Constants.BASE_URL + "/api/notify");
params.put("notify_url", Constants.NOTIFY_URL);
params.put("is_code", "1");
return params;
}
@@ -53,10 +57,13 @@ public class WuyouPay {
.execute()
.body();
logger.info("request url: {}, params: {}, response: {}", url, params, body);
return body;
}
public static void main(String[] args) {
payOrder("20221118123456791", "0.1", "Mozilla/5.0");
// payOrder("20221118123456791", "0.1", "Mozilla/5.0");
queryOrder("20221118123456791", "0.1", "Mozilla/5.0");
}
}