| | |
| | | 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.dto.wxquerydto.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 org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.text.SimpleDateFormat; |
| | |
| | | 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; |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | public SortedMap<Object, Object> appPay(String body, String orderSn, int totalFee, Long poleId) { |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(poleId); |
| | | wxConfig.setMchId(wxConfig.getMchId().trim()); |
| | | WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); |
| | | wxPayUnifiedOrderRequest.setBody(body); |
| | | wxPayUnifiedOrderRequest.setOutTradeNo(orderSn); |
| | | wxPayUnifiedOrderRequest.setTotalFee(totalFee); |
| | | wxPayUnifiedOrderRequest.setOpenid("otiM54x_hc_5gbhMY5aZxo7UByNo"); |
| | | |
| | | //wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSpbillCreateIp(IpUtil.getRealIp()); |
| | | wxPayUnifiedOrderRequest.setNotifyUrl(WECHAT_ORDER_PAY_CALLBACK_URL); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.APP); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | |
| | | try { |
| | | WxPayUnifiedOrderResult wxPayUnifiedOrderResult = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest); |
| | | System.out.println("********************************"); |
| | | 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("package", "Sign=WXPay"); |
| | | parameters.put("timestamp", time); |
| | | |
| | | String sign = createSign(parameters, wxConfig.getPrivateKey()); |
| | | String sign = createSign(wxConfig.getPrivateKey(), parameters); |
| | | |
| | | //hashMap.put("paySign", wechatSign); |
| | | parameters.put("Sign", sign); |
| | |
| | | |
| | | /** |
| | | * 微信订单状态查询 |
| | | * |
| | | * @param poleId |
| | | * @param outTradeNo |
| | | * @return |
| | | */ |
| | | private Object WxOrderQuery(Long poleId,String outTradeNo){ |
| | | public boolean WxOrderQuery(Long poleId, String outTradeNo) { |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(poleId); |
| | | if(wxConfig==null){ |
| | | 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); |
| | | if (("SUCCESS").equals(wxOrderDto.getTrade_state())) { |
| | | return true; |
| | | } |
| | | |
| | | return wxOrderDto.getTrade_state(); |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | private String createSign(SortedMap<Object, Object> parameters, String Key) { |
| | | private String createSign(String Key, SortedMap<Object, Object> parameters) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序) |
| | | Iterator it = es.iterator(); |
| | |
| | | return sign; |
| | | } |
| | | |
| | | public WxPayOrderNotifyResult parseOrderNotifyResult(WxPayService wxPayService, String xmlData) { |
| | | public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) { |
| | | try { |
| | | return wxPayService.parseOrderNotifyResult(xmlData); |
| | | } catch (WxPayException e) { |
| | |
| | | } |
| | | } |
| | | |
| | | public WxPayRefundNotifyResult parseRefundNotifyResult(WxPayService wxPayService, String xmlData) { |
| | | public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) { |
| | | try { |
| | | return wxPayService.parseRefundNotifyResult(xmlData); |
| | | } catch (WxPayException e) { |
| | |
| | | /** |
| | | * 订单退款 只能退款一次 |
| | | * |
| | | * @param wxPayService |
| | | * @param orderId |
| | | * @param refundFee |
| | | * @param callbackUrl |
| | | * @return |
| | | */ |
| | | public boolean refundOrder(WxPayService wxPayService, String orderId, String refundSn, Integer refundFee, String callbackUrl) { |
| | | public boolean refundOrder(String orderId, String refundSn, Integer refundFee, String callbackUrl) { |
| | | WxPayRefundRequest refundRequest = new WxPayRefundRequest(); |
| | | refundRequest.setOutTradeNo(orderId); |
| | | refundRequest.setOutRefundNo(refundSn); |
| | |
| | | /** |
| | | * 生成二维码付款 |
| | | */ |
| | | public String generateQRCodePay(WxPayService wxPayService, String body, |
| | | public String generateQRCodePay(String body, |
| | | String orderId, int totalFee, String callbackUrl) { |
| | | WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); |
| | | wxPayUnifiedOrderRequest.setBody(body); |
| | |
| | | return amount; |
| | | } |
| | | |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public SortedMap<Object, Object> miniAppPay(String body, |
| | | String orderId, int totalFee, Long poleId) { |
| | | WxConfigEntity wxConfig = wxPayConfigService.getConfigByPoleId(poleId); |
| | | wxConfig.setMchId(wxConfig.getMchId().trim()); |
| | | WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest(); |
| | | wxPayUnifiedOrderRequest.setBody(body); |
| | | wxPayUnifiedOrderRequest.setOutTradeNo(orderId); |
| | | wxPayUnifiedOrderRequest.setTotalFee(totalFee); |
| | | wxPayUnifiedOrderRequest.setOpenid("otiM54x_hc_5gbhMY5aZxo7UByNo"); |
| | | |
| | | |
| | | wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5); |
| | | wxPayUnifiedOrderRequest.setSpbillCreateIp(IpUtil.getRealIp()); |
| | | wxPayUnifiedOrderRequest.setNotifyUrl(WECHAT_ORDER_PAY_CALLBACK_URL); |
| | | wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | System.out.println("-----------"+wxPayUnifiedOrderRequest); |
| | | 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<>(); |
| | | 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(wxConfig.getPrivateKey(), parameters); |
| | | |
| | | //hashMap.put("paySign", wechatSign); |
| | | parameters.put("Sign", sign); |
| | | return parameters; |
| | | } else { |
| | | log.error("微信支付失败,{}", wxPayUnifiedOrderResult.getErrCodeDes()); |
| | | throw new BusinessException("微信支付失败," + wxPayUnifiedOrderResult); |
| | | } |
| | | } catch (WxPayException e) { |
| | | log.error("微信支付失败,{}", e.getErrCodeDes()); |
| | | throw new BusinessException("微信支付失败," + e.getErrCodeDes()); |
| | | } |
| | | } |
| | | |
| | | } |