|
|
@@ -9,8 +9,10 @@ import com.izouma.nineth.config.GeneralProperties;
|
|
|
import com.izouma.nineth.config.HmPayProperties;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.utils.DateTimeUtils;
|
|
|
+import com.izouma.nineth.utils.SnowflakeIdWorker;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -31,6 +33,7 @@ import java.util.Map;
|
|
|
public class HMPayService {
|
|
|
private final HmPayProperties hmPayProperties;
|
|
|
private final GeneralProperties generalProperties;
|
|
|
+ private final SnowflakeIdWorker snowflakeIdWorker;
|
|
|
|
|
|
private String getSign(Map<String, String> params) {
|
|
|
try {
|
|
|
@@ -57,6 +60,24 @@ public class HMPayService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public JSONObject requestApi(String method, Map<String, String> bizContent) {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("app_id", hmPayProperties.getMerNo());
|
|
|
+ params.put("method", method);
|
|
|
+ params.put("timestamp", DateTimeUtils.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ params.put("nonce", RandomStringUtils.randomAlphabetic(32));
|
|
|
+
|
|
|
+ params.put("biz_content", JSON.toJSONString(bizContent));
|
|
|
+ params.put("sign", getSign(params));
|
|
|
+ log.info("请求参数{}", params);
|
|
|
+ String body = HttpRequest.post("https://hmpay.sandpay.com.cn/gateway/api")
|
|
|
+ .contentType("application/json")
|
|
|
+ .send(JSON.toJSONString(params)).body();
|
|
|
+ JSONObject res = JSON.parseObject(body);
|
|
|
+ log.info("请求结果{}", JSON.toJSONString(res, true));
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
public String getReqTime() {
|
|
|
return DateTimeUtils.format(LocalDateTime.now(), "yyyyMMddHHmmss");
|
|
|
}
|
|
|
@@ -74,7 +95,7 @@ public class HMPayService {
|
|
|
}
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("version", "10");
|
|
|
- params.put("mer_no", "664403000025502");
|
|
|
+ params.put("mer_no", hmPayProperties.getMerNo());
|
|
|
params.put("mer_order_no", orderId);
|
|
|
params.put("create_time", getReqTime());
|
|
|
params.put("expire_time", getTimeout(timeout));
|
|
|
@@ -112,4 +133,17 @@ public class HMPayService {
|
|
|
throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付");
|
|
|
}
|
|
|
|
|
|
+ public JSONObject queryOrder(String orderId) {
|
|
|
+ Map<String, String> bizContent = new HashMap<>();
|
|
|
+ bizContent.put("out_order_no", orderId);
|
|
|
+ return requestApi("trade.query", bizContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject refund(String orderId, BigDecimal amount) {
|
|
|
+ Map<String, String> bizContent = new HashMap<>();
|
|
|
+ bizContent.put("out_order_no", orderId);
|
|
|
+ bizContent.put("refund_amount", amount.stripTrailingZeros().toPlainString());
|
|
|
+ bizContent.put("refund_request_no", snowflakeIdWorker.nextId() + "");
|
|
|
+ return requestApi("trade.refund", bizContent);
|
|
|
+ }
|
|
|
}
|