package com.sinata.rest.modular.mall.controller;
|
|
import com.jpay.ext.kit.HttpKit;
|
import com.jpay.ext.kit.PaymentKit;
|
import com.sinata.common.enums.EnumPayType;
|
import com.sinata.common.enums.mall.EnumMallOrderPrefix;
|
import com.sinata.rest.common.PayUtils;
|
import com.sinata.rest.common.model.WxPay;
|
import com.sinata.rest.config.properties.PayProperties;
|
import com.sinata.rest.modular.mall.controller.body.BodyOrderNo;
|
import com.sinata.rest.modular.mall.model.MallOrder;
|
import com.sinata.rest.modular.mall.model.MallOrderMain;
|
import com.sinata.rest.modular.mall.service.IMallOrderMainService;
|
import com.sinata.rest.modular.mall.service.IMallOrderService;
|
import com.sinata.rest.modular.member.service.IMyUserCouponOrderService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.util.Assert;
|
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.ResponseBody;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 支付
|
*/
|
@Slf4j
|
@Controller
|
@RequestMapping("/mall/pay")
|
@Api(tags = {"商城-支付相关"}, description = "支付信息,支付回调等")
|
public class MallPayController {
|
|
@Resource
|
private PayProperties payProperties;
|
|
@Autowired
|
private IMallOrderMainService orderMainService;
|
|
@Autowired
|
private IMallOrderService orderService;
|
|
@Autowired
|
IMyUserCouponOrderService couponOrderService;
|
|
@ResponseBody
|
@PostMapping("/wx/payInfo")
|
@ApiOperation(value = "微信支付信息", notes = "返回支付信息,payInfo")
|
public Object wxPayInfo(HttpServletRequest request, @RequestBody BodyOrderNo body) {
|
Assert.notNull(body.getOrderNo(), "订单信息错误");
|
|
BigDecimal orderMoney = new BigDecimal("100");
|
String totalFee = null;
|
|
if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
|
MallOrderMain order = orderMainService.getById(body.getOrderNo());
|
Assert.notNull(order, "订单信息错误!");
|
totalFee = orderMoney.multiply(order.getPayMoney()).intValue() + "";
|
} else {
|
MallOrder order = orderService.getById(body.getOrderNo());
|
Assert.notNull(order, "订单信息错误!");
|
totalFee = orderMoney.multiply(order.getPayMoney()).intValue() + "";
|
}
|
|
// if (!"prod".equals(SpringUtil.getActiveProfile())) {
|
// log.info("开发环境测试支付!");
|
// totalFee = "1";
|
// }
|
WxPay wxPay = new WxPay("1", payProperties.getWxAppId(), payProperties.getWxMchId(), "商品订单" + body.getOrderNo(), body.getOpenId(), body.getOrderNo(), totalFee, payProperties.getWxNotifyUrl() + "/wx", payProperties.getWxPayKey());
|
Map<String, Object> map = PayUtils.wxJsApiPay(wxPay, request);
|
HashMap<String, Object> pm = new HashMap<>(1);
|
pm.put("payInfo", map);
|
return pm;
|
}
|
|
// @ResponseBody
|
// @PostMapping("/ali/payInfo")
|
// @ApiOperation(value = "支付宝支付信息", notes = "返回支付信息,payInfo")
|
// public Object aliPayInfo(HttpServletRequest request, @RequestBody BodyOrderNo body) {
|
// Assert.notNull(body.getOrderNo(), "订单信息错误");
|
// String totalFee = null;
|
//
|
// if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
|
// MallOrderMain order = orderMainService.getById(body.getOrderNo());
|
// Assert.notNull(order, "订单信息错误");
|
// totalFee = order.getPayMoney().toString();
|
// }else if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.GOODS_ORDER.index)) {
|
// MallOrder order = orderService.getById(body.getOrderNo());
|
// Assert.notNull(order, "订单信息错误");
|
// totalFee = order.getPayMoney().toString();
|
// }
|
//
|
// if (!"prod".equals(SpringUtil.getActiveProfile())) {
|
// log.info("开发环境测试支付!");
|
// totalFee = "0.01";
|
// }
|
//
|
// String aliPayAppPay = PayUtils.AliPayAppPay(body.getOrderNo(), totalFee, payProperties.getAliNotifyUrl() + "/ali");
|
// HashMap<String, Object> pm = new HashMap<>(1);
|
// pm.put("payInfo", aliPayAppPay);
|
// return pm;
|
// }
|
|
/**
|
* 第三方微信支付回调
|
* @param request
|
*/
|
@ResponseBody
|
@PostMapping(value = "/notify/wx")
|
public String wxNotify(HttpServletRequest request) {
|
//支付结果通用通知文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
|
String xmlMsg = HttpKit.readData(request);
|
log.debug("微信支付回调通知:{}", xmlMsg);
|
Map<String, String> params = PaymentKit.xmlToMap(xmlMsg);
|
String result_code = params.get("result_code");
|
//校验返回来的支付结果,根据已经配置的密钥
|
if (PaymentKit.verifyNotify(params, payProperties.getWxPayKey())) {
|
if (("SUCCESS").equals(result_code)) {
|
//校验通过. 更改订单状态为已支付 修改库存
|
String totalFee = params.get("total_fee");
|
String orderNo = params.get("out_trade_no");
|
String transactionId = params.get("transaction_id");
|
//根据订单号查询对应的订单,进行状态的修改
|
log.debug("微信支付回调:{}", orderNo);
|
|
if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
|
orderService.payCallbackOrderMain(orderNo, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId);
|
} else if (orderNo.substring(0, 3).contains(EnumMallOrderPrefix.COUPON_ORDER.index)) {
|
couponOrderService.paySuccess(orderNo);
|
} else {
|
MallOrder order = orderService.getById(orderNo);
|
orderService.payCallback(order, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId, orderNo);
|
}
|
|
Map<String, String> returnMap = new HashMap<>(2);
|
returnMap.put("return_code", "<![CDATA[SUCCESS]]>");
|
returnMap.put("return_msg", "<![CDATA[OK]]>");
|
String toXml = PaymentKit.toXml(returnMap);
|
log.debug("微信支付回调成功:{}", toXml);
|
return toXml;
|
}
|
}
|
return "ERR";
|
}
|
|
// @ResponseBody
|
// @PostMapping(value = "/notify/test")
|
// public String wxNotify(String orderNo, String totalFee) {
|
// String transactionId = UUID.randomUUID().toString().replaceAll("-", "");
|
// if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
|
// orderService.payCallbackOrderMain(orderNo, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId);
|
// }else if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.GOODS_ORDER.index)) {
|
// MallOrder order = orderService.getById(orderNo);
|
// orderService.payCallback(order, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId, orderNo);
|
// }else if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.REAL_NAME_ORDER.index)) {
|
// orderAuthService.payCallback(orderNo, EnumPayType.WECHAT, transactionId);
|
// }
|
// return "OK";
|
// }
|
}
|