use of com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult in project waynboot-mall by wayn111.
the class OrderServiceImpl method h5pay.
@Override
@Transactional(rollbackFor = Exception.class)
public R h5pay(String orderSn, Integer payType, HttpServletRequest request) {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
Long userId = order.getUserId();
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!ReturnCodeEnum.SUCCESS.equals(returnCodeEnum)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
// 保存支付方式
boolean update = lambdaUpdate().set(Order::getPayType, payType).eq(Order::getOrderSn, orderSn).update();
if (!update) {
return R.error(ReturnCodeEnum.ORDER_SET_PAY_ERROR);
}
switch(Objects.requireNonNull(PayTypeEnum.of(payType))) {
case WX:
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setTradeType(WxPayConstants.TradeType.MWEB);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
case ALI:
// 初始化
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGateway(), alipayConfig.getAppId(), alipayConfig.getRsaPrivateKey(), alipayConfig.getFormat(), alipayConfig.getCharset(), alipayConfig.getAlipayPublicKey(), alipayConfig.getSigntype());
// 创建API对应的request,使用手机网站支付request
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
// 在公共参数中设置回跳和通知地址
String url = WaynConfig.getMobileUrl() + request.getContextPath();
alipayRequest.setReturnUrl(url + "/returnOrders/" + orderSn + "/" + userId);
alipayRequest.setNotifyUrl(url + "/paySuccess?payType=1&orderSn=" + orderSn);
// 填充业务参数
// 必填
// 商户订单号,需保证在商户端不重复
String out_trade_no = orderSn + new Random().nextInt(9999);
// 销售产品码,与支付宝签约的产品码名称。目前仅支持FAST_INSTANT_TRADE_PAY
String product_code = "FAST_INSTANT_TRADE_PAY";
// 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]。
BigDecimal actualPrice = order.getActualPrice();
String total_amount = actualPrice.toString();
// 订单标题
String subject = "支付宝测试";
// 选填
// 商品描述,可空
String body = "商品描述";
alipayRequest.setBizContent("{" + "\"out_trade_no\":\"" + out_trade_no + "\"," + "\"product_code\":\"" + product_code + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"}");
// 请求
String form;
try {
// 需要自行申请支付宝的沙箱账号、申请appID,并在配置文件中依次配置AppID、密钥、公钥,否则这里会报错。
// 调用SDK生成表单
form = alipayClient.pageExecute(alipayRequest).getBody();
return R.success().add("form", form);
} catch (AlipayApiException e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
}
case ALI_TEST:
// 支付宝test,直接更新支付状态为已支付
order.setPayId("xxxxx0987654321-ali");
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
order.setUpdateTime(new Date());
if (!updateById(order)) {
return R.error(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
}
// 订单支付成功以后,会发送短信给用户,以及发送邮件给管理员
String email = iMemberService.getById(order.getUserId()).getEmail();
if (StringUtils.isNotBlank(email)) {
iMailService.sendEmail("新订单通知", order.toString(), email, WaynConfig.getMobileUrl() + "/message/email");
}
// 删除redis中订单id
redisCache.deleteZsetObject("order_zset", order.getId());
// 取消订单超时未支付任务
taskService.removeTask(new OrderUnpaidTask(order.getId()));
return R.success();
default:
return R.error(ReturnCodeEnum.ORDER_NOT_SUPPORT_PAYWAY_ERROR);
}
}
use of com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult in project waynboot-mall by wayn111.
the class OrderServiceImpl method prepay.
@Override
@Transactional(rollbackFor = Exception.class)
public R prepay(String orderSn, Integer payType, HttpServletRequest request) {
// 获取订单详情
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
ReturnCodeEnum returnCodeEnum = checkOrderOperator(order);
if (!returnCodeEnum.equals(ReturnCodeEnum.SUCCESS)) {
return R.error(returnCodeEnum);
}
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
// 设置支付方式
order.setPayType(payType);
Member member = iMemberService.getById(MobileSecurityUtils.getUserId());
String openid = member.getWeixinOpenid();
if (openid == null) {
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
WxPayMpOrderResult result;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setOpenid(openid);
orderRequest.setBody("订单:" + order.getOrderSn());
// 元转成分
int fee;
BigDecimal actualPrice = order.getActualPrice();
fee = actualPrice.multiply(new BigDecimal(100)).intValue();
orderRequest.setTotalFee(fee);
orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
result = wxPayService.createOrder(orderRequest);
return R.success().add("result", result);
} catch (Exception e) {
log.error(e.getMessage(), e);
return R.error(ReturnCodeEnum.ORDER_CANNOT_PAY_ERROR);
}
}
use of com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult in project yshopmall by guchengwuyue.
the class YxMiniPayService method wxPay.
/**
* 小程序支付
*
* @param orderId
* @param openId 小程序openid
* @param body
* @param totalFee
* @return
* @throws WxPayException
*/
public WxPayMpOrderResult wxPay(String orderId, String openId, String body, Integer totalFee, String attach) throws WxPayException {
String apiUrl = redisHandler.getVal(ShopKeyUtils.getApiUrl());
if (StrUtil.isBlank(apiUrl)) {
throw new ErrorRequestException("请配置api地址");
}
WxPayService wxPayService = WxPayConfiguration.getWxAppPayService();
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setTradeType("JSAPI");
orderRequest.setOpenid(openId);
orderRequest.setBody(body);
orderRequest.setOutTradeNo(orderId);
orderRequest.setTotalFee(totalFee);
orderRequest.setSpbillCreateIp("127.0.0.1");
orderRequest.setNotifyUrl(apiUrl + "/api/wechat/notify");
orderRequest.setAttach(attach);
WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
return orderResult;
}
use of com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult in project yshopmall by guchengwuyue.
the class YxPayService method wxPay.
/**
* 微信公众号支付
*
* @param orderId
* @param openId 公众号openid
* @param body
* @param totalFee
* @return
* @throws WxPayException
*/
public WxPayMpOrderResult wxPay(String orderId, String openId, String body, Integer totalFee, String attach) throws WxPayException {
String apiUrl = redisHandler.getVal(ShopKeyUtils.getApiUrl());
if (StrUtil.isBlank(apiUrl)) {
throw new ErrorRequestException("请配置api地址");
}
WxPayService wxPayService = WxPayConfiguration.getPayService();
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setTradeType("JSAPI");
orderRequest.setOpenid(openId);
orderRequest.setBody(body);
orderRequest.setOutTradeNo(orderId);
orderRequest.setTotalFee(totalFee);
orderRequest.setSpbillCreateIp("127.0.0.1");
orderRequest.setNotifyUrl(apiUrl + "/api/wechat/notify");
orderRequest.setAttach(attach);
WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
return orderResult;
}
use of com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult in project yshopmall by guchengwuyue.
the class YxPayService method routinePay.
/**
* 微信小程序支付
*
* @param orderId
* @param body
* @param totalFee
* @return
* @throws WxPayException
*/
public WxPayMpOrderResult routinePay(String orderId, String body, String openId, Integer totalFee, String attach) throws WxPayException {
String apiUrl = redisHandler.getVal(ShopKeyUtils.getApiUrl());
if (StrUtil.isBlank(apiUrl)) {
throw new ErrorRequestException("请配置api地址");
}
WxPayService wxPayService = WxPayConfiguration.getWxAppPayService();
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOpenid(openId);
orderRequest.setTradeType("JSAPI");
orderRequest.setBody(body);
orderRequest.setOutTradeNo(orderId);
orderRequest.setTotalFee(totalFee);
orderRequest.setSpbillCreateIp("127.0.0.1");
orderRequest.setNotifyUrl(apiUrl + "/api/wechat/notify");
orderRequest.setAttach(attach);
WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
return orderResult;
}
Aggregations