use of cn.lili.modules.payment.kit.params.dto.CashierParam in project lilishop by lilishop.
the class WechatPlugin method mpPay.
@Override
public ResultMessage<Object> mpPay(HttpServletRequest request, PayParam payParam) {
try {
Connect connect = connectService.queryConnect(ConnectQueryDTO.builder().userId(UserContext.getCurrentUser().getId()).unionType(ConnectEnum.WECHAT_MP_OPEN_ID.name()).build());
if (connect == null) {
return null;
}
Payer payer = new Payer();
payer.setOpenid(connect.getUnionId());
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
// 支付金额
Integer fen = CurrencyUtil.fen(cashierParam.getPrice());
// 第三方付款订单
String outOrderNo = SnowFlake.getIdStr();
// 过期时间
String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
// 微信小程序,appid 需要单独获取,这里读取了联合登陆配置的appid ,实际场景小程序自动登录,所以这个appid是最为保险的做法
// 如果有2开需求,这里需要调整,修改这个appid的获取途径即可
String appid = wechatPaymentSetting().getMpAppId();
if (StringUtils.isEmpty(appid)) {
throw new ServiceException(ResultCode.WECHAT_PAYMENT_NOT_SETTING);
}
String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
WechatPaymentSetting setting = wechatPaymentSetting();
UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel().setAppid(appid).setMchid(setting.getMchId()).setDescription(cashierParam.getDetail()).setOut_trade_no(outOrderNo).setTime_expire(timeExpire).setAttach(attach).setNotify_url(notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.WECHAT)).setAmount(new Amount().setTotal(fen)).setPayer(payer);
log.info("统一下单参数 {}", JSONUtil.toJsonStr(unifiedOrderModel));
PaymentHttpResponse response = WechatApi.v3(RequestMethodEnums.POST, WechatDomain.CHINA.toString(), WechatApiEnum.JS_API_PAY.toString(), setting.getMchId(), setting.getSerialNumber(), null, setting.getApiclient_key(), JSONUtil.toJsonStr(unifiedOrderModel));
// 根据证书序列号查询对应的证书来验证签名结果
boolean verifySignature = WxPayKit.verifySignature(response, getPlatformCert());
log.info("verifySignature: {}", verifySignature);
log.info("统一下单响应 {}", response);
if (verifySignature) {
String body = response.getBody();
JSONObject jsonObject = JSONUtil.parseObj(body);
String prepayId = jsonObject.getStr("prepay_id");
Map<String, String> map = WxPayKit.jsApiCreateSign(appid, prepayId, setting.getApiclient_key());
log.info("唤起支付参数:{}", map);
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
use of cn.lili.modules.payment.kit.params.dto.CashierParam in project lilishop by lilishop.
the class WechatPlugin method jsApiPay.
@Override
public ResultMessage<Object> jsApiPay(HttpServletRequest request, PayParam payParam) {
try {
Connect connect = connectService.queryConnect(ConnectQueryDTO.builder().userId(UserContext.getCurrentUser().getId()).unionType(ConnectEnum.WECHAT.name()).build());
if (connect == null) {
return null;
}
Payer payer = new Payer();
payer.setOpenid(connect.getUnionId());
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
// 支付金额
Integer fen = CurrencyUtil.fen(cashierParam.getPrice());
// 第三方付款订单
String outOrderNo = SnowFlake.getIdStr();
// 过期时间
String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
WechatPaymentSetting setting = wechatPaymentSetting();
String appid = setting.getServiceAppId();
if (appid == null) {
throw new ServiceException(ResultCode.WECHAT_PAYMENT_NOT_SETTING);
}
UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel().setAppid(appid).setMchid(setting.getMchId()).setDescription(cashierParam.getDetail()).setOut_trade_no(outOrderNo).setTime_expire(timeExpire).setAttach(attach).setNotify_url(notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.WECHAT)).setAmount(new Amount().setTotal(fen)).setPayer(payer);
log.info("统一下单参数 {}", JSONUtil.toJsonStr(unifiedOrderModel));
PaymentHttpResponse response = WechatApi.v3(RequestMethodEnums.POST, WechatDomain.CHINA.toString(), WechatApiEnum.JS_API_PAY.toString(), setting.getMchId(), setting.getSerialNumber(), null, setting.getApiclient_key(), JSONUtil.toJsonStr(unifiedOrderModel));
// 根据证书序列号查询对应的证书来验证签名结果
boolean verifySignature = WxPayKit.verifySignature(response, getPlatformCert());
log.info("verifySignature: {}", verifySignature);
log.info("统一下单响应 {}", response);
if (verifySignature) {
String body = response.getBody();
JSONObject jsonObject = JSONUtil.parseObj(body);
String prepayId = jsonObject.getStr("prepay_id");
Map<String, String> map = WxPayKit.jsApiCreateSign(appid, prepayId, setting.getApiclient_key());
log.info("唤起支付参数:{}", map);
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
use of cn.lili.modules.payment.kit.params.dto.CashierParam in project lilishop by lilishop.
the class WechatPlugin method appPay.
@Override
public ResultMessage<Object> appPay(HttpServletRequest request, PayParam payParam) {
try {
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
// 支付金额
Integer fen = CurrencyUtil.fen(cashierParam.getPrice());
// 第三方付款订单
String outOrderNo = SnowFlake.getIdStr();
// 过期时间
String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
WechatPaymentSetting setting = wechatPaymentSetting();
String appid = setting.getAppId();
if (appid == null) {
throw new ServiceException(ResultCode.WECHAT_PAYMENT_NOT_SETTING);
}
UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel().setAppid(appid).setMchid(setting.getMchId()).setDescription(cashierParam.getDetail()).setOut_trade_no(outOrderNo).setTime_expire(timeExpire).setAttach(attach).setNotify_url(notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.WECHAT)).setAmount(new Amount().setTotal(fen));
log.info("统一下单参数 {}", JSONUtil.toJsonStr(unifiedOrderModel));
PaymentHttpResponse response = WechatApi.v3(RequestMethodEnums.POST, WechatDomain.CHINA.toString(), WechatApiEnum.APP_PAY.toString(), setting.getMchId(), setting.getSerialNumber(), null, setting.getApiclient_key(), JSONUtil.toJsonStr(unifiedOrderModel));
// 根据证书序列号查询对应的证书来验证签名结果
boolean verifySignature = WxPayKit.verifySignature(response, getPlatformCert());
log.info("verifySignature: {}", verifySignature);
log.info("统一下单响应 {}", response);
if (verifySignature) {
JSONObject jsonObject = JSONUtil.parseObj(response.getBody());
String prepayId = jsonObject.getStr("prepay_id");
Map<String, String> map = WxPayKit.appPrepayIdCreateSign(appid, setting.getMchId(), prepayId, setting.getApiclient_key(), SignType.MD5);
log.info("唤起支付参数:{}", map);
return ResultUtil.data(map);
}
log.error("微信支付参数验证错误,请及时处理");
throw new ServiceException(ResultCode.PAY_ERROR);
} catch (Exception e) {
log.error("支付异常", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
use of cn.lili.modules.payment.kit.params.dto.CashierParam in project lilishop by lilishop.
the class AliPayPlugin method nativePay.
@Override
public ResultMessage<Object> nativePay(HttpServletRequest request, PayParam payParam) {
try {
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
AlipayTradePrecreateModel payModel = new AlipayTradePrecreateModel();
// 请求订单编号
String outTradeNo = SnowFlake.getIdStr();
payModel.setBody(cashierParam.getTitle());
payModel.setSubject(cashierParam.getDetail());
payModel.setTotalAmount(cashierParam.getPrice() + "");
// 回传数据
payModel.setPassbackParams(URLEncoder.createAll().encode(BeanUtil.formatKeyValuePair(payParam), StandardCharsets.UTF_8));
payModel.setTimeoutExpress("3m");
payModel.setOutTradeNo(outTradeNo);
log.info("支付宝扫码:{}", payModel);
String resultStr = AliPayRequest.tradePrecreatePayToResponse(payModel, notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.ALIPAY)).getBody();
log.info("支付宝扫码交互返回:{}", resultStr);
JSONObject jsonObject = JSONObject.parseObject(resultStr);
return ResultUtil.data(jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code"));
} catch (Exception e) {
log.error("支付业务异常:", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
use of cn.lili.modules.payment.kit.params.dto.CashierParam in project lilishop by lilishop.
the class AliPayPlugin method appPay.
@Override
public ResultMessage<Object> appPay(HttpServletRequest request, PayParam payParam) {
try {
CashierParam cashierParam = cashierSupport.cashierParam(payParam);
// 请求订单编号
String outTradeNo = SnowFlake.getIdStr();
AlipayTradeAppPayModel payModel = new AlipayTradeAppPayModel();
payModel.setBody(cashierParam.getTitle());
payModel.setSubject(cashierParam.getDetail());
payModel.setTotalAmount(cashierParam.getPrice() + "");
// 3分钟超时
payModel.setTimeoutExpress("3m");
// 回传数据
payModel.setPassbackParams(URLEncoder.createAll().encode(BeanUtil.formatKeyValuePair(payParam), StandardCharsets.UTF_8));
payModel.setOutTradeNo(outTradeNo);
payModel.setProductCode("QUICK_MSECURITY_PAY");
log.info("支付宝APP支付:{}", payModel);
String orderInfo = AliPayRequest.appPayToResponse(payModel, notifyUrl(apiProperties.getBuyer(), PaymentMethodEnum.ALIPAY)).getBody();
log.info("支付宝APP支付返回内容:{}", orderInfo);
return ResultUtil.data(orderInfo);
} catch (AlipayApiException e) {
log.error("支付宝支付异常:", e);
throw new ServiceException(ResultCode.ALIPAY_EXCEPTION);
} catch (Exception e) {
log.error("支付业务异常:", e);
throw new ServiceException(ResultCode.PAY_ERROR);
}
}
Aggregations