123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package com.fdkankan.sale.pay.wx;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.common.constant.Constant;
- import com.fdkankan.common.util.MD5;
- import com.fdkankan.sale.common.CacheUtil;
- import com.fdkankan.sale.common.ResultCode;
- import com.fdkankan.sale.entity.PriceList;
- import com.fdkankan.sale.entity.Repair;
- import com.fdkankan.sale.entity.RepairPay;
- import com.fdkankan.sale.exception.BusinessException;
- import com.fdkankan.sale.service.IPriceListService;
- import com.fdkankan.sale.service.IRepairLogService;
- import com.fdkankan.sale.service.IRepairPayService;
- import com.fdkankan.sale.service.IRepairService;
- import com.fdkankan.sale.util.DateUtil;
- import com.fdkankan.sale.vo.request.WechatMobileParam;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedOutputStream;
- import java.math.BigDecimal;
- import java.net.URLEncoder;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- @Service
- @Slf4j
- public class WechatPayService {
- @Autowired
- IPriceListService priceListService;
- @Autowired
- IRepairPayService repairPayService;
- @Autowired
- IRepairService repairService;
- @Autowired
- IRepairLogService repairLogService;
- public Object wechatMobilePay(WechatMobileParam param, String ip) throws Exception {
- if(StringUtils.isBlank(param.getRepairId())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- Repair repair = repairService.getById(param.getRepairId());
- if(repair == null){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if(repair.getStatus() != 9 && repair.getStatus() != 4){
- throw new BusinessException(ResultCode.REPAIR_STATUS_NOT_EXITS);
- }
- RepairPay repairPay = repairPayService.getByRepairId(param.getRepairId(),1);
- if(repairPay != null ){
- throw new BusinessException(ResultCode.ORDER_PAY_ERROR);
- }
- String orderSn = null;
- BigDecimal amount = BigDecimal.ZERO;
- String subject = null;
- Integer orderType = null;
- if(repair.getStatus() == 9){
- amount = priceListService.getAmountByRepairId(param.getRepairId());
- orderSn = "se_s"+DateUtil.getDate(DateUtil.repairIdFmt);
- subject = "维修订单";
- orderType = 0;
- }
- if(repair.getStatus() == 3){
- amount = priceListService.getRobAmountByRepairId(param.getRepairId());
- orderSn = "se_c"+DateUtil.getDate(DateUtil.repairIdFmt);
- subject = "取消维修订单";
- orderType = 1;
- }
- repairPay = new RepairPay();
- repairPay.setRepairId(param.getRepairId());
- repairPay.setPayType(1);
- repairPay.setOrderSn(orderSn);
- repairPay.setPayAmount(amount);
- repairPay.setOrderType(orderType);
- repairPayService.save(repairPay);
- BigDecimal totalFee = amount.multiply(new BigDecimal(100));
- String body = subject;
- return this.wechatH5Pay(param.getRepairId(),orderSn, subject, body, totalFee, ip);
- }
- public JSONObject wechatH5Pay(String repairId,String orderSn, String subject, String body, BigDecimal totalFee, String ipAddress) throws Exception {
- log.warn("ipAddress:"+ipAddress);
- Map<String, String> data = new HashMap<String, String>();
- data.put("body", subject);
- data.put("detail", body);
- data.put("out_trade_no", orderSn);
- data.put("device_info", "H5");
- data.put("fee_type", "CNY");
- data.put("total_fee", totalFee.stripTrailingZeros().toPlainString());
- data.put("spbill_create_ip", ipAddress);
- data.put("notify_url", CacheUtil.mainUrl + WXPayDefaultConfig.notifyURL);
- data.put("trade_type", "MWEB"); // 此处指定为扫码支付
- data.put("product_id", repairId); //多个商品,使用订单号
- data.put("nonce_str", MD5.getMessageDigest(String.valueOf(new Random().nextInt(10000)).getBytes()));
- String mweb_url = "";
- Map<String, String> resp = WXPay.unifiedOrder(data);
- String return_code = resp.get("return_code");
- String return_msg = resp.get("return_msg");
- if ("SUCCESS".equals(return_code) && "OK".equals(return_msg)) {
- mweb_url = resp.get("mweb_url");//调微信支付接口地址
- mweb_url += "&redirect_url=" + URLEncoder.encode(CacheUtil.mainUrl + WXPayDefaultConfig.h5RedirectURL, "UTF-8");
- log.warn("mweb_url=" + mweb_url);
- } else {
- log.error("微信统一支付接口获取预支付订单出错",resp);
- }
- JSONObject j = new JSONObject();
- j.put("mweb_url", mweb_url);
- j.put("orderSn", orderSn);
- return j;
- }
- public void callBack(HttpServletRequest request, HttpServletResponse response) {
- log.warn("微信回调接口方法 start");
- String inputLine = "";
- String notifyXml = "";
- try {
- while((inputLine = request.getReader().readLine()) != null){
- notifyXml += inputLine;
- }
- //关闭流
- request.getReader().close();
- log.warn("微信回调内容信息:"+notifyXml);
- //解析成Map
- Map<String,String> map = WXPayUtil.xmlToMap(notifyXml);
- if (WXPayUtil.isSignatureValid(map, WXPayDefaultConfig.key, WXPayConstants.SignType.MD5) || WXPayUtil.isSignatureValid(map, WXPayDefaultConfig.key, WXPayConstants.SignType.HMACSHA256)) {
- //判断 支付是否成功
- if("SUCCESS".equals(map.get("result_code"))){
- log.warn("微信回调返回是否支付成功:是");
- //获得 返回的商户订单号
- String out_trade_no = map.get("out_trade_no");
- String trade_no = map.get("transaction_id");
- String openid = map.get("openid");
- log.warn("微信回调返回商户订单号:" + out_trade_no);
- //修改订单状态
- repairService.paySuccess(out_trade_no,trade_no,openid);
- log.warn("微信支付成功,订单号:" + out_trade_no);
- //通知微信服务器已经支付成功
- notifyXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>"
- + "<return_msg><![CDATA[OK]]></return_msg></xml> ";
- }else{
- notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
- + "<return_msg><![CDATA[报文为空]]></return_msg></xml> ";
- }
- } else{
- log.error("签名验证错误");
- notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
- + "<return_msg><![CDATA[签名验证错误]]></return_msg></xml>";
- }
- BufferedOutputStream out = new BufferedOutputStream(
- response.getOutputStream());
- out.write(notifyXml.getBytes());
- out.flush();
- out.close();
- } catch (Exception e) {
- log.error("微信支付回调数据异常, errorMsg:{}", e.getMessage());
- }
- }
- public Object queryStatus(WechatMobileParam param) {
- if(StringUtils.isBlank(param.getOrderSn())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- RepairPay repairPay = repairPayService.getByOrderSn(param.getOrderSn());
- if(repairPay == null){
- throw new BusinessException(ResultCode.ORDER_PAY_NOT_EXITS);
- }
- return repairPay.getPayStatus();
- }
- }
|