| | |
| | | package com.sandu.ximon.admin.pay.wx; |
| | | |
| | | import cn.dreampie.encription.EncriptionKit; |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
| | | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; |
| | | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; |
| | |
| | | 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"; |
| | | private final UsrWxPayConfigService wxPayConfigService; |
| | | |
| | | /** |
| | | * app支付 |
| | | * |
| | | * @param openId |
| | | * @param body 商品描述 |
| | | * @param orderId 订单id |
| | | * @param totalFee 支付金额(分) |
| | | * @return |
| | | */ |
| | | public SortedMap<Object, Object> miniAppPay(WxPayService wxPayService, String openId, String body, |
| | | String orderId, int totalFee, String callbackUrl) { |
| | | 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(orderId); |
| | | wxPayUnifiedOrderRequest.setOutTradeNo(orderSn); |
| | | wxPayUnifiedOrderRequest.setTotalFee(totalFee); |
| | | wxPayUnifiedOrderRequest.setOpenid(openId); |
| | | |
| | | wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | //wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSpbillCreateIp(IpUtil.getRealIp()); |
| | | wxPayUnifiedOrderRequest.setNotifyUrl(callbackUrl); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | 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> hashMap = new TreeMap<>(); |
| | | hashMap.put("appId", wxPayUnifiedOrderResult.getAppid()); |
| | | SortedMap<Object, Object> parameters = new TreeMap<>(); |
| | | /*hashMap.put("appId", wxPayUnifiedOrderResult.getAppid()); |
| | | hashMap.put("nonceStr", wxPayUnifiedOrderResult.getNonceStr()); |
| | | hashMap.put("package", "prepay_id=" + wxPayUnifiedOrderResult.getPrepayId()); |
| | | hashMap.put("prepayId", wxPayUnifiedOrderResult.getPrepayId()); |
| | | // hashMap.put("package", "prepay_id=" + wxPayUnifiedOrderResult.getPrepayId()); |
| | | hashMap.put("signType", WxPayConstants.SignType.MD5); |
| | | hashMap.put("timeStamp", time); |
| | | String wechatSign = createSign(wxPayService.getConfig().getMchKey(), hashMap); |
| | | hashMap.put("paySign", wechatSign); |
| | | return hashMap; |
| | | 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); |
| | | throw new BusinessException("微信支付失败," + wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("微信支付失败,{}", e.getErrCodeDes()); |
| | |
| | | } |
| | | } |
| | | |
| | | private String createSign(String mchKey, SortedMap<Object, Object> parameters) { |
| | | /* private String createSign(SortedMap<Object, Object> parameters) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序) |
| | | for (Object e : es) { |
| | |
| | | sb.append(k + "=" + v + "&"); |
| | | } |
| | | } |
| | | sb.append("key=" + mchKey); |
| | | sb.append("key=" + wxPayProperties.getMchKey()); |
| | | System.out.println("字符串拼接后是:" + sb.toString()); |
| | | return SecureUtil.md5(sb.toString()).toUpperCase(); |
| | | }*/ |
| | | |
| | | /** |
| | | * @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) { |
| | |
| | | |
| | | /** |
| | | * 退款 |
| | | * |
| | | * @param totalAmount |
| | | * @param refundAmount |
| | | * @param outTradeNo |