|
|
@@ -0,0 +1,127 @@
|
|
|
+package io.renren.modules.qmgj.wxpayutil;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
+import com.wechat.pay.java.core.notification.NotificationParser;
|
|
|
+import com.wechat.pay.java.core.notification.RequestParam;
|
|
|
+import com.wechat.pay.java.service.transferbatch.TransferBatchService;
|
|
|
+import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferRequest;
|
|
|
+import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferResponse;
|
|
|
+import io.renren.common.exception.RRException;
|
|
|
+import io.renren.modules.wechat.config.NewWxPayProperties;
|
|
|
+import io.renren.modules.wechat.entity.TransferNotification;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletInputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class NewWxPayUtil {
|
|
|
+
|
|
|
+ @Resource(name = "rsaAutoCertificateConfig")
|
|
|
+ private RSAAutoCertificateConfig rsaAutoCertificateConfig;
|
|
|
+ @Resource
|
|
|
+ private NewWxPayProperties newWxPayProperties;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转账回调
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public TransferNotification transferNotify(HttpServletRequest request) throws Exception {
|
|
|
+ log.info("进入退款回调");
|
|
|
+ //读取请求体的信息
|
|
|
+ ServletInputStream inputStream = request.getInputStream();
|
|
|
+ StringBuffer stringBuffer = new StringBuffer();
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
+ String s;
|
|
|
+ //读取回调请求体
|
|
|
+ while ((s = bufferedReader.readLine()) != null) {
|
|
|
+ stringBuffer.append(s);
|
|
|
+ }
|
|
|
+ String s1 = stringBuffer.toString();
|
|
|
+ String timestamp = request.getHeader(NewWxPayProperties.WECHAT_PAY_TIMESTAMP);
|
|
|
+ String nonce = request.getHeader(NewWxPayProperties.WECHAT_PAY_NONCE);
|
|
|
+ String signType = request.getHeader("Wechatpay-Signature-Type");
|
|
|
+ String serialNo = request.getHeader(NewWxPayProperties.WECHAT_PAY_SERIAL);
|
|
|
+ String signature = request.getHeader(NewWxPayProperties.WECHAT_PAY_SIGNATURE);
|
|
|
+
|
|
|
+ RequestParam requestParam = new RequestParam.Builder()
|
|
|
+ .serialNumber(serialNo)
|
|
|
+ .nonce(nonce)
|
|
|
+ .signature(signature)
|
|
|
+ .timestamp(timestamp)
|
|
|
+ .signType(signType)
|
|
|
+ .body(s1)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ NotificationParser parser = new NotificationParser(rsaAutoCertificateConfig);
|
|
|
+ TransferNotification parse = parser.parse(requestParam, TransferNotification.class);
|
|
|
+ return parse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转账到零钱
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public InitiateBatchTransferResponse transferSingle(InitiateBatchTransferRequest request) {
|
|
|
+ if (StringUtils.isBlank(request.getAppid())) {
|
|
|
+ request.setAppid(newWxPayProperties.getAppId());
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(request.getTransferSceneId())) {
|
|
|
+ request.setTransferSceneId("1000");
|
|
|
+ }
|
|
|
+// if(StringUtils.isBlank(request.getTransferRemark()))
|
|
|
+// {
|
|
|
+// request.setTransferRemark("转账");
|
|
|
+// }
|
|
|
+// if(StringUtils.isBlank(request.getNotifyUrl()))
|
|
|
+// {
|
|
|
+// request.setNotifyUrl(newWxPayProperties.getTransferNotifyUrl());
|
|
|
+// }
|
|
|
+ TransferBatchService service = new TransferBatchService.Builder().config(rsaAutoCertificateConfig).build();
|
|
|
+ try {
|
|
|
+ InitiateBatchTransferResponse initiateBatchTransferResponse = service.initiateBatchTransfer(request);
|
|
|
+ log.error("转账信息,错误:{}", JSONUtil.toJsonStr(initiateBatchTransferResponse));
|
|
|
+// TransferErrorCode transferErrorCode = TransferErrorCode.fromName(initiateSingleTransferResponse.getFailReason());
|
|
|
+// TransferStatus transferStatus = TransferStatus.fromName(initiateSingleTransferResponse.getState());
|
|
|
+// if (TransferStatus.SUCCESS.equals(transferStatus) ||
|
|
|
+// TransferStatus.ACCEPTED.equals(transferStatus) ||
|
|
|
+// TransferStatus.WAIT_USER_CONFIRM.equals(transferStatus) ||
|
|
|
+// TransferStatus.TRANSFERING.equals(transferStatus)
|
|
|
+// ){
|
|
|
+// initiateSingleTransferResponse.setAppId(newWxPayProperties.getAppId());
|
|
|
+// initiateSingleTransferResponse.setMchId(newWxPayProperties.getMchId());
|
|
|
+// return initiateSingleTransferResponse;
|
|
|
+// }
|
|
|
+// else {
|
|
|
+// if (ObjectUtil.isNotNull(transferErrorCode)) {
|
|
|
+// log.error("转账失败{},错误接款{}",transferErrorCode.name(),transferErrorCode.getDescription());
|
|
|
+// throw new ServiceException(transferErrorCode.getDescription());
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+ throw new RRException("转账失败");
|
|
|
+ } catch (com.wechat.pay.java.core.exception.ServiceException e) {
|
|
|
+ log.error("微信转账失败", e);
|
|
|
+ throw new RRException(String.format("微信转账失败,错识码:%s,描述:%s", e.getErrorCode(), e.getErrorMessage()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("转账失败,错误{}", e.getMessage(), e);
|
|
|
+ throw new RRException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|