forked from kidgrow-microservices-platform

zhaoxiaohao
2020-11-17 b7265cdd6f3e1fbb0d428c27e5b5e29e34d56953
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.kidgrow.oprationcenter.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kidgrow.common.utils.QRCodeUtil;
import com.kidgrow.common.utils.StringUtils;
import com.kidgrow.oprationcenter.model.SaasClientPay;
import com.kidgrow.oprationcenter.service.ISaasClientPayService;
import com.kidgrow.oprationcenter.service.WxService;
import com.kidgrow.oprationcenter.weixin.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class WxServiceImpl implements WxService {
    @Resource
    WeiXinOfficPayProperties weiXinOfficPayProperties;
    @Autowired
    @Lazy
    ISaasClientPayService iSaasClientPayService;
 
    @Override
    public Map<String,String> getCode(SaasClientPay saasClientPay, HttpServletRequest request) throws Exception {
        Map<String,String> jsonObject=new HashMap<>();
        Map<String,Object> mapto=new HashMap<>();
        jsonObject.put("body",saasClientPay.getChildName());//商品描述
        jsonObject.put("out_trade_no", saasClientPay.getOutTradeNo());//商户订单号
        jsonObject.put("total_fee",saasClientPay.getPayPrice().toString());//标价金额  单位为分
        jsonObject.put("spbill_create_ip",getIpAddress(request));//终端IP
        jsonObject.put("notify_url",weiXinOfficPayProperties.getPayNotifyUrl());//通知地址
        jsonObject.put("trade_type","NATIVE");//交易类型
        MyConfig wxPayConfig= new MyConfig(weiXinOfficPayProperties);
        WXPay wxPay=new WXPay(wxPayConfig);
        Map<String, String> stringStringMap = wxPay.fillRequestData(jsonObject);
        Map<String, String> result = wxPay.unifiedOrder(stringStringMap);
        String resultStr = result.get("code_url");
        log.error("");
        if(result.get("code_url")==null|| StringUtils.isBlank(resultStr)){
            return null;
        }
        String s = QRCodeUtil.creatRrCode(resultStr, 200, 200, 0).replaceAll("\n","").replaceAll("\r","");
        String prepay_id = result.get("prepay_id");
        Map<String,String> map = new HashMap<>();
        map.put("code_url",s);
        map.put("prepay_id",prepay_id);
        return map;
    }
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
 
    @Override
    public String callback(Map<String, String> requestMap) throws Exception {
        Map<String,String> map=new HashMap<>();
        map.put("return_code","FAIL");
        map.put("return_msg","INVALID_REQUEST");
        log.error("eeeeeeeeeeeeeee:"+requestMap.toString());
        boolean signatureValid = WXPayUtil.isSignatureValid(requestMap, weiXinOfficPayProperties.getKey(), WXPayConstants.SignType.HMACSHA256);
        if(signatureValid){
            log.error("wwwwwwwwwwwwweeeeeeeeeeeeeeeeeeeee");
            //支付成功
            if(WXPayConstants.SUCCESS.equals(requestMap.get("result_code"))){
                //更新状态 out_trade_no
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("out_trade_no",requestMap.get("out_trade_no"));
                queryWrapper.eq("pay_status",0);
                List<SaasClientPay> list = iSaasClientPayService.list(queryWrapper);
                if(!list.isEmpty()){
                    SaasClientPay saasClientPay = list.get(0);
                    saasClientPay.setPayTime(new Date());
                    saasClientPay.setPayStatus(2);
                    saasClientPay.setPayMethod(0);
                    saasClientPay.setTradeNo(requestMap.get("transaction_id"));
                    boolean b = iSaasClientPayService.updateById(saasClientPay);
                    map.put("return_code","SUCCESS");
                    map.put("return_msg","OK");
                }
            }
        }
        return WXPayUtil.mapToXml(map);
    }
}