New file |
| | |
| | | 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); |
| | | } |
| | | } |