|
@@ -0,0 +1,165 @@
|
|
|
|
+package com.bosshand.virgo.api.workark.service;
|
|
|
|
+
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.bosshand.virgo.api.workark.config.WxPayConfig;
|
|
|
|
+import com.bosshand.virgo.api.workark.dao.ProceDao;
|
|
|
|
+import com.bosshand.virgo.api.workark.dao.TransferInfoDao;
|
|
|
|
+import com.bosshand.virgo.api.workark.model.TransferInfo;
|
|
|
|
+import com.bosshand.virgo.api.workark.util.OrderNoUtils;
|
|
|
|
+import com.bosshand.virgo.core.utils.ContextUtils;
|
|
|
|
+import com.bosshand.virgo.exception.ServiceException;
|
|
|
|
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
|
+import com.wechat.pay.java.core.http.*;
|
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class WxTransferService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WxPayConfig wxPayConfig;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ ProceDao proceDao;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ TransferInfoDao transferInfoDao;
|
|
|
|
+
|
|
|
|
+ public String transfer(String openId, BigDecimal money) {
|
|
|
|
+
|
|
|
|
+ long userId = ContextUtils.getUserContext().getUserId();
|
|
|
|
+
|
|
|
|
+ TransferInfo transferInfo = new TransferInfo();
|
|
|
|
+ transferInfo.setOpenId(openId);
|
|
|
|
+ transferInfo.setUserId(userId);
|
|
|
|
+ transferInfo.setMoney(money);
|
|
|
|
+
|
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
|
+ HttpClient httpClient = new DefaultHttpClientBuilder()
|
|
|
|
+ .config(rsaAutoCertificateConfig())
|
|
|
|
+ .okHttpClient(okHttpClient)
|
|
|
|
+ .build();
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
|
|
|
|
+ headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
|
|
|
|
+ headers.addHeader("Wechatpay-Serial", wxPayConfig.getMchSerialNo());
|
|
|
|
+
|
|
|
|
+ HashMap<Object, Object> map = new HashMap<>();
|
|
|
|
+ //小程序 appId
|
|
|
|
+ map.put("appid", wxPayConfig.getAppid());
|
|
|
|
+ String orderNo = OrderNoUtils.getTransferNo();
|
|
|
|
+
|
|
|
|
+ transferInfo.setTransferBillNo(orderNo);
|
|
|
|
+
|
|
|
|
+ // 订单编号
|
|
|
|
+ map.put("out_bill_no", orderNo);
|
|
|
|
+ // 转账场景ID
|
|
|
|
+ map.put("transfer_scene_id", "1000");
|
|
|
|
+ // openid
|
|
|
|
+ map.put("openid", openId);
|
|
|
|
+
|
|
|
|
+ // 元 转换 分
|
|
|
|
+ BigDecimal multiplier = new BigDecimal("100");
|
|
|
|
+ // 使用multiply方法乘以100
|
|
|
|
+ BigDecimal result = money.multiply(multiplier);
|
|
|
|
+
|
|
|
|
+ // 金额(分)
|
|
|
|
+ map.put("transfer_amount", result.intValue());
|
|
|
|
+ // 转账备注
|
|
|
|
+ map.put("transfer_remark", "佣金提现: " + orderNo);
|
|
|
|
+ //回调地址: 商家转账回调地址
|
|
|
|
+ map.put("notify_url", "");
|
|
|
|
+ // 用户收款感知
|
|
|
|
+ map.put("user_recv_perception", "佣金奖励");
|
|
|
|
+ // 转账场景报备信息
|
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
|
+ jsonArray.add(new JSONObject().fluentPut("info_type", "活动名称").fluentPut("info_content", "邀请新用户"));
|
|
|
|
+ jsonArray.add(new JSONObject().fluentPut("info_type", "奖励说明").fluentPut("info_content", "佣金提现"));
|
|
|
|
+ map.put("transfer_scene_report_infos", jsonArray);
|
|
|
|
+
|
|
|
|
+ JsonRequestBody build = new JsonRequestBody.Builder()
|
|
|
|
+ .body(JSONUtil.toJsonStr(map))
|
|
|
|
+ .build();
|
|
|
|
+ HttpRequest executeSendGetHttpRequest = new HttpRequest.Builder()
|
|
|
|
+ .httpMethod(HttpMethod.POST)
|
|
|
|
+ .url("https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills")
|
|
|
|
+ .headers(headers)
|
|
|
|
+ .body(build)
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ HttpResponse<JSONObject> execute = httpClient.execute(executeSendGetHttpRequest, JSONObject.class);
|
|
|
|
+ JSONObject responseBody = execute.getServiceResponse();
|
|
|
|
+
|
|
|
|
+ String packageInfo = responseBody.getString("package_info");
|
|
|
|
+ String transferBillNo = responseBody.getString("transfer_bill_no");
|
|
|
|
+
|
|
|
|
+ transferInfo.setTransferBillNo(transferBillNo);
|
|
|
|
+ transferInfo.setPackageInfo(packageInfo);
|
|
|
|
+ transferInfoDao.save(transferInfo);
|
|
|
|
+
|
|
|
|
+ return packageInfo;
|
|
|
|
+ } catch (ServiceException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private RSAAutoCertificateConfig rsaAutoCertificateConfig() {
|
|
|
|
+ return new RSAAutoCertificateConfig.Builder()
|
|
|
|
+ .merchantId(wxPayConfig.getMchId())
|
|
|
|
+ .privateKey(wxPayConfig.getFileContent(wxPayConfig.getPrivateKeyPath()))
|
|
|
|
+ .merchantSerialNumber(wxPayConfig.getMchSerialNo())
|
|
|
|
+ .apiV3Key(wxPayConfig.getApiV3Key())
|
|
|
|
+ .build();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String transferOutBillNo(String outBillNo) {
|
|
|
|
+ OkHttpClient okHttpClient = new OkHttpClient();
|
|
|
|
+ HttpClient httpClient = new DefaultHttpClientBuilder()
|
|
|
|
+ .config(rsaAutoCertificateConfig())
|
|
|
|
+ .okHttpClient(okHttpClient)
|
|
|
|
+ .build();
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
|
|
|
|
+ headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
|
|
|
|
+ headers.addHeader("Wechatpay-Serial", wxPayConfig.getMchSerialNo());
|
|
|
|
+
|
|
|
|
+ HttpRequest executeSendGetHttpRequest = new HttpRequest.Builder()
|
|
|
|
+ .httpMethod(HttpMethod.GET)
|
|
|
|
+ .url("https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/" + outBillNo)
|
|
|
|
+ .headers(headers)
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ HttpResponse<JSONObject> execute = httpClient.execute(executeSendGetHttpRequest, JSONObject.class);
|
|
|
|
+ JSONObject responseBody = execute.getServiceResponse();
|
|
|
|
+
|
|
|
|
+ String state = responseBody.getString("state");
|
|
|
|
+
|
|
|
|
+ if("SUCCESS".equals(state)){
|
|
|
|
+ TransferInfo outBillNo1 = transferInfoDao.getOutBillNo(outBillNo);
|
|
|
|
+ outBillNo1.setState(state);
|
|
|
|
+ transferInfoDao.update(outBillNo1);
|
|
|
|
+ }
|
|
|
|
+ return state;
|
|
|
|
+ } catch (ServiceException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void update(TransferInfo transferInfo) {
|
|
|
|
+ transferInfoDao.update(transferInfo);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|