微信支付之扫码、APP、小程序支付接入详解( 二 )


Controller接口层:
@RestController@RequestMapping(value = https://www.isolves.com/it/cxkf/ydd/xcx/2019-09-04/"/api/payment/")public class PaymentController { private static Logger logger = LoggerFactory.getLogger(PaymentController.class); @Autowired private PaymentService paymentService;/** * App支付接口 * 微信和支付宝统一下单入口 ** @param request * @return * @throws Exception */ @ResponseBody @RequestMapping(value="toPay", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) public JSONObject toPay(HttpServletRequest request) throws Exception { String requestStr = RequestStr.getRequestStr(request); if (StringUtils.isEmpty(requestStr)) { throw new ParamException(); } JSONObject jsonObj = JSONObject.parseobject(requestStr); if(StringUtils.isEmpty(jsonObj.getString("orderNo")) || StringUtils.isEmpty(jsonObj.getString("payAmount"))){ throw new ParamException(); } //验证订单是否存在 String orderNo = jsonObj.getString("orderNo"); double payAmount = jsonObj.getDouble("payAmount"); if(payAmount < 0.01){ return AjaxUtil.renderFailMsg("订单有误 , 请确认!"); } else { //微信支付 Map resMap = paymentService.wxAppPayment(orderInfo.getOrderNo(),orderInfo.getPayPrice(),null); //判断微信统一下单是否成功 if("SUCCESS".equals(resMap.get("returnCode")) && "OK".equals(resMap.get("returnMsg"))){ //统一下单成功 resMap.remove("returnCode"); resMap.remove("returnMsg"); logger.info("【App支付服务】微信支付下单成功!"); return AjaxUtil.renderSuccessMsg(resMap); }else{ logger.info("【App支付服务】微信支付下单失败!原因:"+resMap.get("returnMsg")); return AjaxUtil.renderFailMsg(resMap.get("returnMsg")); } }}PaymentService接口方法:

微信支付之扫码、APP、小程序支付接入详解

文章插图
 
PaymentService实现类部分代码(微信App支付):
@Service(value = https://www.isolves.com/it/cxkf/ydd/xcx/2019-09-04/"paymentService")public class PaymentServiceImpl implements PaymentService {private static Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class); @Value("${spring.profiles.active}") private String PROJECT_ENV; @Value("${hcc.pay.domain}") private String payDomain; @Autowired private PaymentRecordMapper paymentRecordMapper;@Override @Transactional(readOnly=false,rollbackFor={Exception.class}) public Map wxAppPayment(String orderId, double money,Long customerId) throws Exception { LOGGER.info("【微信App支付】 统一下单开始, 订单编号="+orderId); SortedMap resultMap = new TreeMap(); //生成支付金额 double payAmount = PayUtils.getPayAmountByEnv(PROJECT_ENV, money); //TODO 操作数据库 , 添加或更新支付记录 //this.addOrUpdatePaymentRecord(......); //微信统一下单 Map resMap = this.wxUnifieldOrder(orderId, PayConfig.TRADE_TYPE_APP, payAmount, null); if(PayConstant.SUCCESS.equals(resMap.get("return_code")) && PayConstant.OK.equals(resMap.get("return_msg"))){ //封装参数返回 resultMap.put("appid", PayConfig.WX_APP_ID); resultMap.put("partnerid", PayConfig.WX_MCH_ID); resultMap.put("prepayid", resMap.get("prepay_id")); resultMap.put("package", "Sign=WXPay"); resultMap.put("noncestr", PayUtils.makeUUID(32)); resultMap.put("timestamp", PayUtils.getCurrentTimeStamp()); resultMap.put("sign", PayUtils.createSign(resultMap,PayConfig.WX_KEY)); resultMap.put("returnCode", "SUCCESS"); resultMap.put("returnMsg", "OK"); LOGGER.info("【微信App支付】统一下单成功 , 返回参数:"+resultMap); }else{ resultMap.put("returnCode", resMap.get("return_code")); resultMap.put("returnMsg", resMap.get("return_msg")); LOGGER.info("【微信App支付】统一下单失败 , 失败原因:"+resMap.get("return_msg")); } return resultMap;} 统一下单方法(在PaymentService实现类里):
  •  
/** * <p>微信支付统一下单</p> * * @param orderId 订单编号 * @param tradeType 支付类型 * @param payAmount 支付金额 * @param openid * @return * @throws Exception */ private Map<String,String> wxUnifieldOrder(String orderId, String tradeType, double payAmount, String openid) throws Exception{ //封装参数 SortedMap<String,String> paramMap = new TreeMap<String,String>(); String appid = PayConfig.WX_APP_ID; String mchid = PayConfig.WX_MCH_ID; if(PayConstant.WX_TRADE_TYPE_JSAPI.equals(tradeType)){ appid = PayConfig.XCX_APP_ID; mchid = PayConfig.XCX_MCH_ID; } paramMap.put("appid", appid); paramMap.put("mch_id", mchid); paramMap.put("nonce_str", PayUtils.makeUUID(32)); paramMap.put("body", BaseConstants.PLATFORM_COMPANY_NAME); paramMap.put("out_trade_no", orderId); paramMap.put("total_fee", PayUtils.moneyToIntegerStr(payAmount)); paramMap.put("spbill_create_ip", PayUtils.getLocalIp()); paramMap.put("notify_url", this.getNotifyUrl(PayConstant.PAY_TYPE_WX)); paramMap.put("trade_type", tradeType); if(PayConstant.WX_TRADE_TYPE_JSAPI.equals(tradeType)){ paramMap.put("openid",openid); } paramMap.put("sign", PayUtils.createSign(paramMap,PayConfig.WX_KEY)); //转换为xml String xmlData = https://www.isolves.com/it/cxkf/ydd/xcx/2019-09-04/PayUtils.mapToXml(paramMap); //请求微信后台 String resXml = HttpUtils.postData(PayConfig.WX_PAY_UNIFIED_ORDER, xmlData); LOGGER.info("【微信支付】 统一下单响应:n"+resXml); return PayUtils.xmlStrToMap(resXml); }


推荐阅读