| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.sandu.ximon.admin.pay.wx; |
| | | |
| | | import cn.dreampie.encription.EncriptionKit; |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
| | | import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; |
| | | import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; |
| | | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| | | import com.github.binarywang.wxpay.exception.WxPayException; |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.util.IpUtil; |
| | | import com.sandu.ximon.admin.dto.YSY_AccessTokenDto; |
| | | import com.sandu.ximon.admin.dto.wxquery.WxOrderDto; |
| | | import com.sandu.ximon.admin.pay.wxpay.UsrWxPayConfigService; |
| | | import com.sandu.ximon.dao.domain.WxConfigEntity; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | | * @date 2021/8/12 11:26 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @AllArgsConstructor |
| | | public class WxFastPayService { |
| | | |
| | | private final com.github.binarywang.wxpay.service.WxPayService wxPayService; |
| | | //private final WxPayProperties wxPayProperties; |
| | | // public static final String WECHAT_ORDER_PAY_CALLBACK_URL = "http://49.235.239.118:20012/callback/pay/wechatCallback"; |
| | | public static final String WECHAT_ORDER_PAY_CALLBACK_URL = "http://49.235.239.118:20012/callback/pay/wechatCallback"; |
| | | //微信订åç¶ææ¥è¯¢ |
| | | public static final String WECHAT_ORDER_QUETY_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/"; |
| | | public static final String WECHAT_ORDER_QUETY = "?mchid="; |
| | | |
| | | private final UsrWxPayConfigService wxPayConfigService; |
| | | |
| | | |
| | | |
| | | /** |
| | | * appæ¯ä» |
| | | * |
| | | * @param body ååæè¿° |
| | | * @param |
| | | * @param totalFee æ¯ä»éé¢ï¼åï¼ |
| | | * @return |
| | | */ |
| | | public SortedMap<Object, Object> appPay(String body, String orderSn, int totalFee, Long poleId) { |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(poleId); |
| | | WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); |
| | | wxPayUnifiedOrderRequest.setBody(body); |
| | | wxPayUnifiedOrderRequest.setOutTradeNo(orderSn); |
| | | wxPayUnifiedOrderRequest.setTotalFee(totalFee); |
| | | |
| | | //wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSpbillCreateIp(IpUtil.getRealIp()); |
| | | wxPayUnifiedOrderRequest.setNotifyUrl(WECHAT_ORDER_PAY_CALLBACK_URL); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.APP); |
| | | |
| | | try { |
| | | WxPayUnifiedOrderResult wxPayUnifiedOrderResult = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest); |
| | | String time = String.valueOf(System.currentTimeMillis() / 1000); |
| | | if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayUnifiedOrderResult.getResultCode())) { |
| | | SortedMap<Object, Object> parameters = new TreeMap<>(); |
| | | /*hashMap.put("appId", wxPayUnifiedOrderResult.getAppid()); |
| | | hashMap.put("nonceStr", wxPayUnifiedOrderResult.getNonceStr()); |
| | | hashMap.put("prepayId", wxPayUnifiedOrderResult.getPrepayId()); |
| | | // hashMap.put("package", "prepay_id=" + wxPayUnifiedOrderResult.getPrepayId()); |
| | | hashMap.put("signType", WxPayConstants.SignType.MD5); |
| | | hashMap.put("timeStamp", time); |
| | | hashMap.put("partnerid", wxPayProperties.getMchId());*/ |
| | | parameters.put("appid", wxPayUnifiedOrderResult.getAppid()); |
| | | |
| | | parameters.put("noncestr", wxPayUnifiedOrderResult.getNonceStr()); // éæºå符串 |
| | | parameters.put("partnerid", wxConfig.getMchId()); // åæ·id |
| | | |
| | | // parameters.put("out_trade_no", orderId);//åæ·è®¢åå· |
| | | parameters.put("prepayid", wxPayUnifiedOrderResult.getPrepayId()); |
| | | parameters.put("package", "Sign=WXPay"); |
| | | parameters.put("timestamp", time); |
| | | |
| | | String sign = createSign(parameters, wxConfig.getPrivateKey()); |
| | | |
| | | //hashMap.put("paySign", wechatSign); |
| | | parameters.put("Sign", sign); |
| | | return parameters; |
| | | } else { |
| | | log.error("微信æ¯ä»å¤±è´¥ï¼{}", wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | throw new BusinessException("微信æ¯ä»å¤±è´¥ï¼" + wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("微信æ¯ä»å¤±è´¥ï¼{}", e.getErrCodeDes()); |
| | | throw new BusinessException("微信æ¯ä»å¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | /* private String createSign(SortedMap<Object, Object> parameters) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | Set es = parameters.entrySet();// ææåä¸ä¼ åçåæ°æç
§accsiiæåºï¼ååºï¼ |
| | | for (Object e : es) { |
| | | Map.Entry entry = (Map.Entry) e; |
| | | String k = (String) entry.getKey(); |
| | | Object v = entry.getValue(); |
| | | if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { |
| | | sb.append(k + "=" + v + "&"); |
| | | } |
| | | } |
| | | sb.append("key=" + wxPayProperties.getMchKey()); |
| | | System.out.println("åç¬¦ä¸²æ¼æ¥åæ¯:" + sb.toString()); |
| | | return SecureUtil.md5(sb.toString()).toUpperCase(); |
| | | }*/ |
| | | |
| | | |
| | | /** |
| | | * 微信订åç¶ææ¥è¯¢ |
| | | * @param poleId |
| | | * @param outTradeNo |
| | | * @return |
| | | */ |
| | | private Object WxOrderQuery(Long poleId,String outTradeNo){ |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(poleId); |
| | | if(wxConfig==null){ |
| | | throw new BusinessException("è·å微信æ¯ä»é
置失败"); |
| | | } |
| | | String s = HttpUtil.get(WECHAT_ORDER_QUETY_URL + outTradeNo + WECHAT_ORDER_QUETY + wxConfig.getMchId());//WxOrderDto |
| | | WxOrderDto wxOrderDto = JSON.parseObject(s, WxOrderDto.class); |
| | | |
| | | return wxOrderDto.getTrade_state(); |
| | | } |
| | | |
| | | /** |
| | | * @param parameters |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | private String createSign(SortedMap<Object, Object> parameters, String Key) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | Set es = parameters.entrySet();// ææåä¸ä¼ åçåæ°æç
§accsiiæåºï¼ååºï¼ |
| | | Iterator it = es.iterator(); |
| | | while (it.hasNext()) { |
| | | Map.Entry entry = (Map.Entry) it.next(); |
| | | String k = (String) entry.getKey(); |
| | | Object v = entry.getValue(); |
| | | if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { |
| | | sb.append(k + "=" + v + "&"); |
| | | } |
| | | } |
| | | sb.append("key=" + Key); |
| | | System.out.println("åç¬¦ä¸²æ¼æ¥åæ¯:" + sb.toString()); |
| | | String sign = EncriptionKit.encrypt(sb.toString()).toUpperCase(); |
| | | return sign; |
| | | } |
| | | |
| | | public WxPayOrderNotifyResult parseOrderNotifyResult(WxPayService wxPayService, String xmlData) { |
| | | try { |
| | | return wxPayService.parseOrderNotifyResult(xmlData); |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException("è§£æåè°ç»æå¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | public WxPayRefundNotifyResult parseRefundNotifyResult(WxPayService wxPayService, String xmlData) { |
| | | try { |
| | | return wxPayService.parseRefundNotifyResult(xmlData); |
| | | } catch (WxPayException e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException("è§£æåè°ç»æå¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 订å鿬¾ åªè½éæ¬¾ä¸æ¬¡ |
| | | * |
| | | * @param wxPayService |
| | | * @param orderId |
| | | * @param refundFee |
| | | * @param callbackUrl |
| | | * @return |
| | | */ |
| | | public boolean refundOrder(WxPayService wxPayService, String orderId, String refundSn, Integer refundFee, String callbackUrl) { |
| | | WxPayRefundRequest refundRequest = new WxPayRefundRequest(); |
| | | refundRequest.setOutTradeNo(orderId); |
| | | refundRequest.setOutRefundNo(refundSn); |
| | | refundRequest.setRefundFee(refundFee); |
| | | refundRequest.setTotalFee(refundFee); |
| | | refundRequest.setNotifyUrl(callbackUrl); |
| | | try { |
| | | WxPayRefundResult wxPayRefundResult = wxPayService.refund(refundRequest); |
| | | if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayRefundResult.getResultCode())) { |
| | | return true; |
| | | } else { |
| | | log.error("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼{}", wxPayRefundResult.getErrCodeDes()); |
| | | throw new BusinessException("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼" + wxPayRefundResult.getErrCodeDes()); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼{}", e.getErrCodeDes()); |
| | | throw new BusinessException("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * çæäºç»´ç 仿¬¾ |
| | | */ |
| | | public String generateQRCodePay(WxPayService wxPayService, String body, |
| | | String orderId, int totalFee, String callbackUrl) { |
| | | WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); |
| | | wxPayUnifiedOrderRequest.setBody(body); |
| | | wxPayUnifiedOrderRequest.setOutTradeNo(orderId); |
| | | wxPayUnifiedOrderRequest.setTotalFee(totalFee); |
| | | wxPayUnifiedOrderRequest.setProductId(orderId); |
| | | |
| | | wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSpbillCreateIp(IpUtil.getRealIp()); |
| | | wxPayUnifiedOrderRequest.setNotifyUrl(callbackUrl); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.NATIVE); |
| | | |
| | | try { |
| | | WxPayUnifiedOrderResult wxPayUnifiedOrderResult = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest); |
| | | if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayUnifiedOrderResult.getResultCode())) { |
| | | byte[] scanPayQrcodeMode2 = wxPayService.createScanPayQrcodeMode2(wxPayUnifiedOrderResult.getCodeURL(), null, null); |
| | | String encode = Base64.encode(scanPayQrcodeMode2); |
| | | return encode; |
| | | } else { |
| | | log.error("微信æ¯ä»å¤±è´¥ï¼{}", wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | throw new BusinessException("微信æ¯ä»å¤±è´¥ï¼" + wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("微信æ¯ä»å¤±è´¥ï¼{}", e.getErrCodeDes()); |
| | | throw new BusinessException("微信æ¯ä»å¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 鿬¾ |
| | | * |
| | | * @param totalAmount |
| | | * @param refundAmount |
| | | * @param outTradeNo |
| | | * @param poleId |
| | | * @return |
| | | */ |
| | | public boolean refund(Double totalAmount, Double refundAmount, String outTradeNo, Long poleId) { |
| | | WxConfigEntity wxConfig = UsrWxPayConfigService.getBean().getConfigByPoleId(poleId); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String refundNo = sdf.format(new Date()); |
| | | WxPayRefundRequest refundRequest = new WxPayRefundRequest(); |
| | | refundRequest.setOutTradeNo(outTradeNo); |
| | | refundRequest.setOutRefundNo(refundNo); |
| | | refundRequest.setTotalFee(Integer.valueOf(parseWxAmount((totalAmount + "")))); |
| | | refundRequest.setRefundFee(Integer.valueOf(parseWxAmount((refundAmount + "")))); |
| | | try { |
| | | WxPayRefundResult wxPayRefundResult = wxPayService.refund(refundRequest); |
| | | if (WxPayConstants.ResultCode.SUCCESS.equals(wxPayRefundResult.getResultCode())) { |
| | | return true; |
| | | } else { |
| | | log.error("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼{}", wxPayRefundResult.getErrCodeDes()); |
| | | throw new BusinessException("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼" + wxPayRefundResult.getErrCodeDes()); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼{}", e.getErrCodeDes()); |
| | | throw new BusinessException("å¾®ä¿¡éæ¬¾å¤±è´¥ï¼" + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºå¾®ä¿¡éé¢(å) |
| | | * |
| | | * @param normalAmount 0.01å
|
| | | * @return |
| | | */ |
| | | public static String parseWxAmount(String normalAmount) { |
| | | // ä¿ç2ä½å°æ° |
| | | double totalAmountDouble = Double.parseDouble(new DecimalFormat("#.00").format( |
| | | Double.parseDouble(normalAmount))); |
| | | String amount = (totalAmountDouble * 100) + ""; |
| | | amount = amount.substring(0, amount.indexOf(".")); |
| | | return amount; |
| | | } |
| | | |
| | | } |