| @@ -15,27 +15,29 @@ import io.swagger.annotations.ApiImplicitParam; | |||
| import io.swagger.annotations.ApiImplicitParams; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import java.util.Map; | |||
| @RestController | |||
| @RequestMapping("wxRefundOrder") | |||
| public class WxRefundOrderController extends BaseController | |||
| { | |||
| @Autowired | |||
| @RequestMapping("/api/refund") | |||
| public class WxRefundOrderController extends BaseController { | |||
| @Autowired | |||
| private WxRefundOrderService wxRefundOrderService; | |||
| private Logger logger = Logger.getLogger(WxRefundOrderController.class); | |||
| @ApiOperation("分页列表接口") | |||
| @ApiOperation("分页列表接口") | |||
| @GetMapping("list") | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true), | |||
| @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)}) | |||
| public ResultData list(@ModelAttribute WxRefundOrder wxRefundOrder,Integer pageNum, Integer pageSize) { | |||
| if (null == wxRefundOrder) wxRefundOrder = new WxRefundOrder(); | |||
| @ApiImplicitParams({ | |||
| @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true), | |||
| @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)}) | |||
| public ResultData list(@ModelAttribute WxRefundOrder wxRefundOrder, Integer pageNum, Integer pageSize) { | |||
| if (null == wxRefundOrder) wxRefundOrder = new WxRefundOrder(); | |||
| final PageInfo<WxRefundOrder> page = wxRefundOrderService.listAsPage(wxRefundOrder, pageNum, pageSize); | |||
| return new ResultData(page); | |||
| } | |||
| @ApiOperation("新增接口") | |||
| @ApiOperation("新增接口") | |||
| @PostMapping("add") | |||
| public ResultData add(@RequestBody WxRefundOrder wxRefundOrder) { | |||
| //Assert.notNull(wxRefundOrder.getName(), "角色名不能为空"); | |||
| @@ -53,19 +55,18 @@ public class WxRefundOrderController extends BaseController | |||
| @ApiOperation("根据id删除接口") | |||
| @GetMapping("/del") | |||
| @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) | |||
| @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) | |||
| public ResultData delete(Long id) { | |||
| wxRefundOrderService.deleteById(id); | |||
| return new ResultData(Result.SUCCESS, "删除成功", null); | |||
| } | |||
| @ApiOperation("根据id查询接口") | |||
| @GetMapping("/findById") | |||
| @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) | |||
| @ApiOperation("根据id查询接口") | |||
| @GetMapping("/findById") | |||
| @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) | |||
| public ResultData findById(Long id) { | |||
| return new ResultData(Result.SUCCESS,"查询成功",wxRefundOrderService.getById(id)); | |||
| return new ResultData(Result.SUCCESS, "查询成功", wxRefundOrderService.getById(id)); | |||
| } | |||
| } | |||
| @@ -27,6 +27,7 @@ import com.simple.service.WxPayOrderService; | |||
| import io.swagger.annotations.ApiImplicitParam; | |||
| import io.swagger.annotations.ApiImplicitParams; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import sun.rmi.runtime.Log; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import java.nio.charset.Charset; | |||
| @@ -41,7 +42,7 @@ public class WxPayOrderController extends BaseController { | |||
| @Autowired | |||
| private WxPayOrderService wxPayOrderService; | |||
| @ApiOperation(value = "发起微信小程序支付订单") | |||
| @ApiOperation(value = "发起微信小程序支付订单", notes = "{\"orderId\":\"string\"}") | |||
| @RequestMapping(value = "/create", method = RequestMethod.POST) | |||
| public ResultData _create(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws Exception { | |||
| logger.info("/api/pay/create" + paramMap.toString()); | |||
| @@ -54,7 +55,13 @@ public class WxPayOrderController extends BaseController { | |||
| WxCUser user = getUser(); | |||
| WxAppinfo appInfo = getAppInfo(user.getAppId()); | |||
| Long orderId = Long.valueOf(orderIdStr); | |||
| Long orderId = 0L; | |||
| try { | |||
| orderId = Long.valueOf(orderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| orderId = 0L; | |||
| return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "orderId参数不正确"); | |||
| } | |||
| WxPayOrder record = new WxPayOrder(); | |||
| record.setOrderId(orderId); | |||
| @@ -70,17 +77,51 @@ public class WxPayOrderController extends BaseController { | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR); | |||
| } | |||
| @ApiOperation("更新支付订单状态") | |||
| @ApiOperation(value = "更新支付订单状态", notes = "{\"id\":\"string\",\"orderId\":\"string\",\"status\":integer,\"reason\":\"string\"}") | |||
| @PostMapping("/updatePayOrder") | |||
| public ResultData updatePayOrder(@RequestBody WxPayOrder wxPayOrder) { | |||
| public ResultData updatePayOrder(@RequestBody Map<String, Object> paramMap) { | |||
| logger.info("/api/pay/updatePayOrder" + paramMap.toString()); | |||
| String payOrderIdStr = (String)paramMap.get("payOrderId"); | |||
| String orderIdStr = (String)paramMap.get("orderId"); | |||
| String reasonStr = (String)paramMap.get("reason"); | |||
| Integer status = (Integer)paramMap.get("status"); | |||
| if (StringUtils.isBlank(payOrderIdStr)) { | |||
| logger.info("payOrderId不能为空: " + paramMap.toString()); | |||
| return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "payOrderId不能为空"); | |||
| } | |||
| if (StringUtils.isBlank(orderIdStr)) { | |||
| logger.info("orderId不能为空: " + paramMap.toString()); | |||
| return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "orderId不能为空"); | |||
| } | |||
| Long payOrderId = 0L, orderId = 0L; | |||
| try { | |||
| payOrderId = Long.valueOf(payOrderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| payOrderId = 0L; | |||
| logger.error("payOrderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "payOrderId参数不正确"); | |||
| } | |||
| try { | |||
| orderId = Long.valueOf(orderIdStr); | |||
| } catch (NumberFormatException e) { | |||
| orderId = 0L; | |||
| logger.error("orderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(ErrorCode.PARAMETER_NOT_NULL.getCode(), "orderId参数不正确"); | |||
| } | |||
| WxPayOrder payOrder = new WxPayOrder(); | |||
| payOrder.setId(payOrderId); | |||
| payOrder.setOrderId(orderId); | |||
| payOrder.setPayOrderStatus(status); | |||
| payOrder.setFailReason(reasonStr); | |||
| try { | |||
| wxPayOrderService.handlePayOrderStatusUpdate(wxPayOrder); | |||
| return new ResultData(); | |||
| wxPayOrderService.handlePayOrderStatusUpdate(payOrder); | |||
| return new ResultData(200, "支付状态更新成功"); | |||
| } catch (MallinkException e) { | |||
| logger.error("payment wechat, order create error, update 2: " + wxPayOrder.toString() + ", e:" + e.getMessage()); | |||
| logger.error("支付状态更新失败2: " + payOrder.toString() + ", e:" + e.getMessage()); | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error("payment wechat, order create error, update 3: " + wxPayOrder.toString() + ", e:" + e.getMessage()); | |||
| logger.error("支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage()); | |||
| } | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR); | |||
| } | |||
| @@ -326,16 +326,17 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| } | |||
| @Override | |||
| @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) | |||
| public void handlePayOrderStatusUpdate(WxPayOrder record) { | |||
| // 判断支付状态 | |||
| if (record.getPayOrderStatus() != EnumPayStatus.PAY_WAY_WAIT.getCode()) { | |||
| if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_WAIT.getCode()) { | |||
| logger.error("pay handle, payOrder " + record.getPayOrderNo() + | |||
| "is complete s , orderId : " + record.getOrderId()); | |||
| "is complete, orderId : " + record.getOrderId()); | |||
| return; | |||
| } | |||
| WxOrder order = wxOrderMapper.selectByPrimaryKey(record.getOrderId()); | |||
| if (order == null) { | |||
| logger.error("pay success handle, order " + record.getOrderId() + " not found , payOrderGid : " + record.getPayOrderNo()); | |||
| logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderGid : " + record.getPayOrderNo()); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); | |||
| } | |||
| @@ -347,6 +348,7 @@ public class WxPayOrderServiceImpl implements WxPayOrderService { | |||
| updateOrder.setOrderId(record.getOrderId()); | |||
| updateOrder.setUpdateTime(currentDate); | |||
| updateOrder.setPayOrderStatus(record.getPayOrderStatus()); | |||
| updateOrder.setFailReason(record.getFailReason()); | |||
| wxPayOrderMapper.updateByPrimaryKeySelective(updateOrder); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| @@ -213,26 +213,37 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| // check 是否有退款订单 | |||
| List<WxRefundOrder> list = wxRefundOrderMapper.findList(record); | |||
| if (list.size() > 0) { | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_EXIST); | |||
| logger.error("退款订单已存在, 无法再提交退款申请"); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_EXIST.getCode(), "退款订单已存在, 无法再提交退款申请"); | |||
| } | |||
| // check 支付 订单 | |||
| WxPayOrder payOrder = null; | |||
| Long payOrderId = 0L; | |||
| try { | |||
| payOrderId = Long.valueOf(record.getPayOrderNo()); | |||
| } catch (NumberFormatException e) { | |||
| logger.error("参数转换异常: payOrderId-" + record.getPayOrderNo()); | |||
| throw new MallinkException(ErrorCode.PARAMETER_CAST_ERROR.getCode(), "参数转换异常: payOrderId-" + record.getPayOrderNo()); | |||
| } | |||
| try { | |||
| WxPayOrder payOrderQ = new WxPayOrder(); | |||
| payOrderQ.setOrderId(record.getOrderId()); | |||
| payOrderQ.setId(Long.valueOf(record.getPayOrderNo())); | |||
| payOrderQ.setId(payOrderId); | |||
| payOrderQ.setPayOrderNo(record.getPayOrderNo()); | |||
| payOrderQ.setPayOrderStatus(EnumPayStatus.PAY_WAY_SUCCESS.getCode()); | |||
| payOrder = wxPayOrderMapper.selectOne(payOrderQ); | |||
| } catch (Exception e) { | |||
| logger.error("数据库获取异常: " + e.getMessage()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| if (payOrder == null) { | |||
| logger.error("支付订单不存在: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.REFUND_PAY_ORDER_IS_NOT_EXIST); | |||
| } | |||
| if (payOrder.getPayAmount() <= 0) { | |||
| throw new MallinkException(ErrorCode.REFUND_PAY_ORDER_IS_NOT_EXIST); | |||
| logger.error("支付订单金额小于等于0: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.REFUND_PAY_ORDER_IS_ZERO); | |||
| } | |||
| // TODO check 可退券???? | |||
| @@ -255,51 +266,73 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService { | |||
| record.setRefundOrderStatus(EnumRefundStatus.REFUND_WAIT.getCode()); | |||
| int sqlRow = wxRefundOrderMapper.insertSelective(record); | |||
| if(sqlRow != 1) { | |||
| logger.error("退款订单数据库插入出错: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| } catch (Exception e) { | |||
| logger.error("退款订单数据库插入出错: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.DB_FAIL); | |||
| } | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); | |||
| // 退款申请 | |||
| try { | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxRefundOrderP wxRefundOrderP = new WxRefundOrderP(); | |||
| wxRefundOrderP.setAppid(appInfo.getAppId()); | |||
| wxRefundOrderP.setMch_id(payAccount.getMchId()); | |||
| wxRefundOrderP.setNonce_str(noncestr); | |||
| String noncestr = Utility.generate32UUID(); | |||
| WxRefundOrderP wxRefundOrderP = new WxRefundOrderP(); | |||
| wxRefundOrderP.setAppid(appInfo.getAppId()); | |||
| wxRefundOrderP.setMch_id(payAccount.getMchId()); | |||
| wxRefundOrderP.setNonce_str(noncestr); | |||
| if (record.getTransactionId() == null) | |||
| wxRefundOrderP.setTransaction_id(record.getTransactionId()); | |||
| wxRefundOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxRefundOrderP.setTotal_fee(record.getTotalFee()); | |||
| wxRefundOrderP.setRefund_fee(record.getRefundFee()); | |||
| wxRefundOrderP.setSign(WxPayment.createSign(BeanUtils.toStringMap(wxRefundOrderP), payAccount.getApiKey())); | |||
| String response = WxPay.orderRefund(BeanUtils.toStringMap(wxRefundOrderP), payAccount.getCertPath(), payAccount.getApiKey()); | |||
| logger.info("微信退款订单:" + wxRefundOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String result_no = returnMap.get("result_code"); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| record.setRefundId(refund_id); | |||
| wxRefundOrderP.setOut_trade_no(record.getPayOrderNo()); | |||
| wxRefundOrderP.setTotal_fee(record.getTotalFee()); | |||
| wxRefundOrderP.setRefund_fee(record.getRefundFee()); | |||
| Map signMap = new HashMap(); | |||
| try { | |||
| signMap = BeanUtils.toStringMap(wxRefundOrderP); | |||
| } catch (Exception e) { | |||
| logger.error("退款签名异常"); | |||
| throw new MallinkException(ErrorCode.PARAMETER_CAST_ERROR.getCode(), "退款签名异常"); | |||
| } | |||
| String signAgent = WxPayment.createSign(signMap, payAccount.getApiKey()); | |||
| signMap.put("sign", signAgent); | |||
| String response = WxPay.orderRefund(signMap, payAccount.getCertPath(), payAccount.getApiKey()); | |||
| logger.info("微信退款订单:" + wxRefundOrderP.toString() + ", response: " + response.toString()); | |||
| Map<String, String> returnMap = WxPayment.xmlToMap(response); | |||
| String result_no = returnMap.get("result_code"); | |||
| String refund_id = returnMap.get("refund_id"); | |||
| // 设置 微信退款订单号 | |||
| record.setRefundId(refund_id); | |||
| if ("SUCCESS".equals(result_no)) { | |||
| logger.error("微信退款订单申请成功: " + returnMap.toString()); | |||
| try { | |||
| record.setRefundOrderStatus(EnumRefundStatus.REFUND_REQ_SUCCESS.getCode()); | |||
| wxRefundOrderMapper.updateByPrimaryKey(record); | |||
| return new ResultData(200, "退款订单申请成功", returnMap); | |||
| } else { | |||
| String refund_id = returnMap.get("refund_id"); | |||
| record.setRefundId(refund_id); | |||
| } catch (Exception e) { | |||
| logger.error("微信退款订单更新入库出错: " + e.getMessage() + ", record: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR); | |||
| } | |||
| } else { | |||
| logger.error("微信退款订单申请失败: " + returnMap.toString()); | |||
| try { | |||
| record.setRefundOrderStatus(EnumRefundStatus.REFUND_REQ_FAIL.getCode()); | |||
| wxRefundOrderMapper.updateByPrimaryKey(record); | |||
| JSONObject errObj = errorRefundReqMap.getJSONObject(result_no); | |||
| return new ResultData(ErrorCode.PAY_ORDER_ERROR.getCode(), errObj.toJSONString(), returnMap); | |||
| } catch(Exception e) { | |||
| logger.error("微信退款订单更新入库出错: " + e.getMessage() + ", record: " + record.toString()); | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR); | |||
| } | |||
| } catch(Exception e) { | |||
| throw new MallinkException(ErrorCode.REFUND_ORDER_ERROR); | |||
| } | |||
| } | |||
| // TODO | |||
| // 微信退款查询 | |||
| /** | |||
| * 微信退款查询 | |||
| * @param appInfo | |||
| * @param record | |||
| */ | |||
| @Override | |||
| public ResultData queryRefundOrder(WxAppinfo appInfo, WxRefundOrder record) { | |||
| WxPayAccount payAccount = wxPayAccountMapper.selectByPrimaryKey(appInfo.getPayId()); | |||