123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package com.ym.mec.thirdparty.adapay;
- import java.math.BigDecimal;
- import java.util.*;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.google.common.collect.Lists;
- import com.huifu.adapay.core.exception.BaseAdaPayException;
- import com.huifu.adapay.core.util.StringUtil;
- import com.huifu.adapay.model.PaymentReverse;
- import com.huifu.adapay.model.Refund;
- import com.ym.mec.thirdparty.adapay.entity.BaseResult;
- import com.ym.mec.thirdparty.adapay.entity.HfMerchantConfig;
- import com.ym.mec.thirdparty.exception.ThirdpartyException;
- import org.apache.commons.lang3.StringUtils;
- import org.joda.time.DateTime;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.DigestUtils;
- public class Payment {
- private final static Logger LOGGER = LoggerFactory.getLogger(Payment.class);
- /**
- * 执行一个支付交易
- *
- * @return 创建的支付对象
- * @throws Exception 异常
- */
- public static Map<String, Object> executePayment(String appId, String merchantKey, Map<String, Object> paymentParams) throws Exception {
- paymentParams.put("app_id", appId);
- LOGGER.info("汇付[创建支付对象] Req:{}", paymentParams);
- //调用sdk方法,创建支付,得到支付对象
- Map<String, Object> payment = new HashMap<>();
- payment = com.huifu.adapay.model.Payment.create(paymentParams, merchantKey);
- LOGGER.info("汇付[创建支付对象] Resp:{}", payment);
- String errorCode = (String) payment.get("error_code");
- if (null != errorCode) {
- String errorMsg = (String) payment.get("error_msg");
- throw new ThirdpartyException(errorMsg);
- }
- return payment;
- }
- /**
- * 创建确认对象
- *
- * @return 查询的支付对象
- * @throws Exception 异常
- */
- public static Map<String, Object> createConfirm(Map<String, Object> confirm, String merchantKey) throws Exception {
- confirm = com.huifu.adapay.model.Payment.createConfirm(confirm, merchantKey);
- String error_code = (String) confirm.get("error_code");
- if (null != error_code) {
- String errorMsg = (String) confirm.get("error_msg");
- throw new ThirdpartyException(errorMsg);
- }
- return confirm;
- }
- /**
- * 查询一个支付交易
- *
- * @param paymentId 要查询的支付id
- * @return 查询的支付对象
- * @throws Exception 异常
- */
- public static Map<String, Object> queryPayment(String paymentId, String merchantKey) throws Exception {
- LOGGER.info("=======query payment begin=======");
- //查询支付对象的参数,全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id7
- //调用sdk方法,查询支付交易,得到支付对象
- Map<String, Object> payment = new HashMap<>();
- try {
- LOGGER.info("queryPayment >>> 支付查询请求参数:{}", paymentId);
- payment = com.huifu.adapay.model.Payment.query(paymentId, merchantKey);
- } catch (ThirdpartyException e) {
- LOGGER.error("queryPayment >>> 支付查询异常", e.getCause());
- }
- LOGGER.info("queryPayment >>> 支付查询返回参数:{}", JSON.toJSONString(payment));
- String error_code = (String) payment.get("error_code");
- if (null != error_code) {
- String error_msg = (String) payment.get("error_msg");
- LOGGER.error("queryPayment >>> error_code:" + error_code + "............." + error_msg);
- }
- LOGGER.info("queryPayment >>> =======query payment end=======");
- return payment;
- }
- public static Map<String, Object> queryList(String appId, String merchantKey, Integer pageIndex, Long createdGte, Long createdLte) throws Exception {
- Map<java.lang.String, java.lang.Object> paymentParams = new HashMap<>();
- paymentParams.put("app_id", appId);
- paymentParams.put("page_index", pageIndex);
- paymentParams.put("page_size", "20");
- paymentParams.put("created_gte", createdGte);
- paymentParams.put("created_lte", createdLte);
- Map<String, Object> payment = com.huifu.adapay.model.Payment.queryList(paymentParams, merchantKey);
- String error_code = (String) payment.get("error_code");
- if (null != error_code) {
- String errorMsg = (String) payment.get("error_msg");
- throw new ThirdpartyException(errorMsg);
- }
- return payment;
- }
- public static Map<String, Object> queryConfirmList(String appId, String merchantKey, String paymentId) throws Exception {
- Map<String, Object> paymentParams = new HashMap<>();
- paymentParams.put("app_id", appId);
- paymentParams.put("payment_id", paymentId);
- Map<String, Object> payment = com.huifu.adapay.model.Payment.queryConfirmList(paymentParams, merchantKey);
- String error_code = (String) payment.get("error_code");
- if (null != error_code) {
- String errorMsg = (String) payment.get("error_msg");
- throw new ThirdpartyException(errorMsg);
- }
- return payment;
- }
- public static Map<String, Object> getPayMap(HfMerchantConfig hfMerchantConfig, BigDecimal amount, String orderNo, String returnUrl, String orderSubject, String orderBody) throws Exception {
- Map<String, Object> paymentParams = new LinkedHashMap<>();
- paymentParams.put("appId", hfMerchantConfig.getAppId());
- paymentParams.put("amount", amount.setScale(2, BigDecimal.ROUND_HALF_UP));
- paymentParams.put("orderNo", orderNo);
- // paymentParams.put("notifyUrl", notifyUrl);
- paymentParams.put("returnUrl", returnUrl);
- paymentParams.put("orderSubject", orderSubject);
- paymentParams.put("orderBody", orderBody);
- paymentParams.put("wxAppId", hfMerchantConfig.getWxAppId());
- paymentParams.put("alipayAppId", hfMerchantConfig.getAlipayAppId());
- String originalStr = JSONObject.toJSONString(paymentParams);
- String sign = DigestUtils.md5DigestAsHex(originalStr.getBytes());
- paymentParams.put("sign", sign);
- paymentParams.put("tenantId", hfMerchantConfig.getTenantId());
- paymentParams.remove("appId");
- return paymentParams;
- }
- public static BaseResult<Map<String ,Object>> refundPayment(Map<String, Object> refundParams) {
- LOGGER.info("退款对象信息:{}",refundParams);
- String transNo = refundParams.get("transNo").toString();
- refundParams.remove("transNo");
- Map<String, Object> res;
- try {
- res = Refund.create(transNo, refundParams);
- } catch (BaseAdaPayException e) {
- return BaseResult.failed(e.getMessage());
- }
- if (CollectionUtils.isEmpty(res)) {
- return BaseResult.failed("请求失败");
- }
- LOGGER.info("汇付[支付退款] Resp:{}", res);
- String errorCode = (String) res.get("error_code");
- if (null != errorCode) {
- String errorMsg = (String) res.get("error_msg");
- return BaseResult.failed(errorMsg);
- }
- return BaseResult.succeed(res);
- }
- // 撤销支付
- public static BaseResult<Map<String ,Object>> reversePayment(String paymentId,String appId,String reverseOrderNo,String reverseAmt,String notifyUrl) {
- LOGGER.info("撤销对象信息:paymentId:{},appId:{},reverseOrderNo:{},reverseAmt:{}",paymentId,appId,reverseOrderNo,reverseAmt);
- Map<String, Object> res;
- Map<String,Object> param = new HashMap<>();
- param.put("app_id",appId);
- param.put("payment_id",paymentId);
- param.put("reverse_amt",reverseAmt);
- param.put("order_no",reverseOrderNo);
- param.put("notify_url",notifyUrl);
- try {
- res = PaymentReverse.create(param);
- } catch (BaseAdaPayException e) {
- return BaseResult.failed(e.getMessage());
- }
- if (CollectionUtils.isEmpty(res)) {
- return BaseResult.failed("请求失败");
- }
- LOGGER.info("汇付[支付退款] Resp:{}", res);
- String errorCode = (String) res.get("error_code");
- if (null != errorCode) {
- String errorMsg = (String) res.get("error_msg");
- return BaseResult.failed(errorMsg);
- }
- return BaseResult.succeed(res);
- }
- /**
- *
- 针对已经创建的 支付对象,您可以调用关单接口进行交易的关闭。调用此接口后,该用户订单将不再能支付成功。 对于关单功能的使用有如下规则:
- 1.存在关单记录,不能再次关单
- 2.交易时间 1分钟 内无法关单成功
- 3.正扫交易时间超过 2小时 无法关单成功
- 4.支付宝正扫接口,如果用户没有扫码,订单不能关闭成功(二维码给用户展示,如果用户没有用手机去扫码,那这笔就不能关单; 如果用户扫过了的话(无需支付成功)就可以关单了)—-微信正扫无此条限制
- 5.网银和快捷类交易都不支持关单操作
- 对于已经成功付款的订单,请使用 退款对象 接口进行退款处理。我们建议您只有针对未支付的订单调用关单接口。
- * @param transNo 交易流水号
- * @param reason 原因
- * @param merchantKey 商户Key
- * @return Map<String, Object>
- */
- public static Map<String ,String> closeWithKey(String transNo, String reason, String merchantKey) {
- Map<String, String> result = new HashMap<>();
- Map<String, Object> params = new HashMap<>();
- params.put("payment_id", transNo);
- params.put("reason", reason);
- //params.put("notify_url", adapayPaymentConfig.getPayNotifyUrl());
- Map<String, Object> paymentClose = new HashMap<>();
- try {
- paymentClose = com.huifu.adapay.model.Payment.close(params, merchantKey);
- } catch (BaseAdaPayException e) {
- LOGGER.info("请求汇付[关单]接口失败:{}", e.getMessage());
- result.put("status", "failed");
- result.put ("msg",e.getMessage());
- return result;
- }
- if (StringUtils.equals(paymentClose.get("status").toString(), "failed")) {
- LOGGER.error("调用汇付[关单]接口同步返回,出现异常:{}", paymentClose);
- // 忽略关单异常
- // channel_close_fail 通道关单失败
- // close_order_exists 关单记录已存在,不允许重复关单
- List<String> errorCodes = Lists.newArrayList("close_order_exists"); // "channel_close_fail", "frequent_request"
- // 关单记录已存在,不允许重复关单,重复关单会不抛出异常,只记录异常信息
- if (errorCodes.contains(paymentClose.get("error_code").toString())) {
- LOGGER.warn("调用汇付[关单]接口同步返回,出现异常:{}", paymentClose);
- result.put("status", "success");
- return result;
- }
- result.put("status", "failed");
- result.put ("msg",paymentClose.get("error_msg").toString());
- return result;
- }
- result.put("status", "success");
- return result;
- }
- }
|