| | |
| | | package com.yeshi.fanli.service.impl.money.extract;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.math.BigDecimal;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.UUID;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.dom4j.Document;
|
| | | import org.dom4j.DocumentException;
|
| | | import org.dom4j.DocumentHelper;
|
| | | import org.dom4j.Element;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | | import org.yeshi.utils.entity.wx.RedPackParams;
|
| | | import org.yeshi.utils.wx.WXPayUtil;
|
| | |
|
| | | import com.alipay.api.AlipayApiException;
|
| | | import com.alipay.api.AlipayClient;
|
| | |
| | | import com.yeshi.fanli.entity.bus.user.Extract;
|
| | | import com.yeshi.fanli.entity.bus.user.ExtractAuditRecord;
|
| | | import com.yeshi.fanli.entity.bus.user.ExtractRecord;
|
| | | import com.yeshi.fanli.entity.bus.user.ExtractWeiXinRecord;
|
| | | import com.yeshi.fanli.entity.bus.user.HongBaoV2;
|
| | | import com.yeshi.fanli.entity.bus.user.PayInfo;
|
| | | import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory;
|
| | |
| | | import com.yeshi.fanli.service.inter.money.UserMoneyService;
|
| | | import com.yeshi.fanli.service.inter.money.extract.ExtractRecordService;
|
| | | import com.yeshi.fanli.service.inter.money.extract.ExtractService;
|
| | | import com.yeshi.fanli.service.inter.money.extract.ExtractWeiXinRecordService;
|
| | | import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
|
| | | import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
|
| | | import com.yeshi.fanli.service.inter.order.OrderUserStatisticService;
|
| | |
| | | import com.yeshi.fanli.util.Constant;
|
| | | import com.yeshi.fanli.util.GsonUtil;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
| | |
|
| | | @Resource
|
| | | private OrderUserStatisticService orderUserStatisticService;
|
| | | |
| | | @Resource
|
| | | private ExtractWeiXinRecordService extractWeiXinRecordService;
|
| | | |
| | |
|
| | | @Transactional
|
| | | public Integer addExtract(Extract extract) {
|
| | |
| | | * @param extract
|
| | | * @param session
|
| | | */
|
| | | //TODO 转账逻辑需要改变
|
| | | //TODO 转账逻辑需要改变,固定IP
|
| | | @Transactional
|
| | | private void extractByZhiFuBao(Extract extract, AdminUser adminUser) {
|
| | | LogHelper.userErrorInfo("提现:开始通过提现-"+extract.getId());
|
| | |
| | | AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
|
| | | Constant.alipayConfig.getAppId(), privateKey, "json", "gbk", null, "RSA2");
|
| | | AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
|
| | | String uuid = UUID.randomUUID().toString().replace("-", "");
|
| | | String appName = Constant.systemCommonConfig.getProjectChineseName();
|
| | | JSONObject json = new JSONObject();
|
| | | json.put("out_biz_no", uuid);//最长长度64位,支持英文,下划线,数字
|
| | | json.put("out_biz_no", "widthdraw_"+extract.getId());//最长长度64位,支持英文,下划线,数字
|
| | | json.put("payee_type", "ALIPAY_LOGONID");
|
| | | json.put("payee_account", extract.getAccount());
|
| | | json.put("amount", extract.getMoney());
|
| | |
| | | else
|
| | | return money;
|
| | | }
|
| | | |
| | | |
| | | |
| | | @Transactional(rollbackFor = Exception.class)
|
| | | @Override
|
| | | public void autoExtract(UserInfo user) {
|
| | | if (user == null || user.getWxOpenId() == null || user.getWxUnionId() == null ||
|
| | | user.getState() != UserInfo.STATE_NORMAL)
|
| | | return;
|
| | | |
| | | Date date = new Date();
|
| | | BigDecimal money = new BigDecimal(Constant.AUTO_EXTRACT_MONEY);
|
| | | |
| | | // 1、插入记录
|
| | | ExtractWeiXinRecord extractRecord = new ExtractWeiXinRecord();
|
| | | extractRecord.setUid(user.getId());
|
| | | extractRecord.setOpenId(user.getWxOpenId());
|
| | | extractRecord.setUnionId(user.getWxUnionId() );
|
| | | extractRecord.setMoney(money);
|
| | | extractRecord.setCreateTime(date);
|
| | | extractRecord.setUpdateTime(date);
|
| | | extractWeiXinRecordService.insertSelective(extractRecord);
|
| | | |
| | | // 交易号
|
| | | Long recordId = extractRecord.getId();
|
| | | String billno = "BanLiExtract" + recordId;
|
| | | |
| | | // 更新记录
|
| | | ExtractWeiXinRecord updeteRecord = null;
|
| | | |
| | | String mchId = "1520950211";
|
| | | String appId = "wx5c0d167c6e3ad726";
|
| | | String key = "XYJkJ2018FAfaodCCx899mLl138rfGVd";
|
| | | |
| | | // 2、发放微信红包
|
| | | RedPackParams redPackParams = new RedPackParams();
|
| | | redPackParams.setBillno(billno);
|
| | | redPackParams.setMchId(mchId);
|
| | | redPackParams.setWxappId(appId);
|
| | | redPackParams.setSendName("板栗快省");
|
| | | redPackParams.setOpenid(user.getWxOpenId());
|
| | | redPackParams.setMoney(money);
|
| | | redPackParams.setTotalNum(1);
|
| | | redPackParams.setWishing("账户中剩余余额请在本App内完成提现");
|
| | | redPackParams.setClientIp("106.80.118.163");
|
| | | redPackParams.setActName("自动提现");
|
| | | redPackParams.setKey(key);
|
| | | redPackParams.setRemark("微信自动提现");
|
| | | //redPackParams.setSceneId(null);
|
| | | //redPackParams.setRiskInfo(null);
|
| | | |
| | | |
| | | // 发生红包状态
|
| | | boolean stateRedPack = false;
|
| | | |
| | | String pwd = "1520950211";
|
| | | InputStream cert = ExtractServiceImpl.class.getClassLoader().getResourceAsStream("certificate/apiclient_cert.p12"); // 读取.p12文件
|
| | | // 执行发放红包
|
| | | try {
|
| | | String result = WXPayUtil.redPackToOpenId(redPackParams, pwd, cert);
|
| | | if (!StringUtil.isNullOrEmpty(result)) {
|
| | | stateRedPack = parseSendResult(result);
|
| | | }
|
| | | |
| | | if (!stateRedPack) {
|
| | | String redPackRecord = WXPayUtil.getRedPackRecord(billno, mchId, appId, key, pwd, cert);
|
| | | if (!StringUtil.isNullOrEmpty(redPackRecord)) {
|
| | | updeteRecord = parseDTO(redPackRecord);
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | String redPackRecord = WXPayUtil.getRedPackRecord(billno, mchId, appId, key, pwd, cert);
|
| | | if (!StringUtil.isNullOrEmpty(redPackRecord)) {
|
| | | updeteRecord = parseDTO(redPackRecord);
|
| | | }
|
| | | LogHelper.errorDetailInfo(e);
|
| | | }
|
| | | try {
|
| | | cert.close();
|
| | | } catch (IOException e1) {
|
| | | e1.printStackTrace();
|
| | | }
|
| | | |
| | | // 更新信息
|
| | | if (updeteRecord == null) {
|
| | | updeteRecord = new ExtractWeiXinRecord();
|
| | | } else {
|
| | | stateRedPack = true;
|
| | | }
|
| | | |
| | | updeteRecord.setId(recordId);
|
| | | updeteRecord.setStatus(ExtractWeiXinRecord.SENDING);
|
| | | updeteRecord.setBillno(billno);
|
| | | extractWeiXinRecordService.updateByPrimaryKeySelective(updeteRecord);
|
| | | |
| | | // 红包发放失败
|
| | | if (!stateRedPack) |
| | | return; |
| | | |
| | | // 3、加入资金明细
|
| | | UserMoneyDetail detail = new UserMoneyDetail();
|
| | | detail.setCreateTime(new Date());
|
| | | detail.setIdentifyCode(StringUtil.Md5(UserMoneyDetailTypeEnum.extractAutoWX.name() + ":" + recordId));
|
| | | detail.setMoney(money);
|
| | | detail.setTitle(UserMoneyDetailTypeEnum.extractAutoWX.getDesc());
|
| | | detail.setType(UserMoneyDetailTypeEnum.extractAutoWX);
|
| | | detail.setUpdateTime(new Date());
|
| | | detail.setUserInfo(user);
|
| | | try {
|
| | | userMoneyDetailService.addUserMoneyDetail(detail);
|
| | | } catch (UserMoneyDetailException e) {
|
| | | LogHelper.errorDetailInfo(e);
|
| | | }
|
| | | |
| | | // 4、发送消息通知
|
| | | String desc = "为保障你的账户余额安全,自动提现金额将不会高于5元";
|
| | | String beizu = "账户中剩余余额请在本App内完成提现";
|
| | | userMoneyMsgNotificationService.extractAuto(extractRecord, "未领取-提现中", desc, beizu);
|
| | | }
|
| | |
|
| | | |
| | | private boolean parseSendResult(String result) {
|
| | | try {
|
| | | Document document = DocumentHelper.parseText(result);
|
| | | // 获取根节点元素对象
|
| | | Element root = document.getRootElement();
|
| | | Element returnCode = root.element("return_code");
|
| | | |
| | | if (!"SUCCESS".equalsIgnoreCase(returnCode.getText())) {
|
| | | Element returnMsg = root.element("return_msg");
|
| | | System.out.println(returnMsg.getText()); |
| | | return false;
|
| | | } |
| | | |
| | | Element resultCode = root.element("result_code");
|
| | | if (!"SUCCESS".equalsIgnoreCase(resultCode.getText())) {
|
| | | Element errCode = root.element("err_code");
|
| | | Element errCodeDes = root.element("err_code_des");
|
| | | System.out.println(errCode.getText() + ":" + errCodeDes.getText()); |
| | | return false;
|
| | | }
|
| | | return true;
|
| | | } catch (DocumentException e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e);
|
| | | }
|
| | | return false;
|
| | | }
|
| | | |
| | | |
| | | private ExtractWeiXinRecord parseDTO(String result) {
|
| | | try {
|
| | | |
| | | Document document = DocumentHelper.parseText(result);
|
| | | // 获取根节点元素对象
|
| | | Element root = document.getRootElement();
|
| | | Element returnCode = root.element("return_code");
|
| | | |
| | | if (!"SUCCESS".equalsIgnoreCase(returnCode.getText())) {
|
| | | Element returnMsg = root.element("return_msg");
|
| | | System.out.println(returnMsg.getText()); |
| | | return null;
|
| | | } |
| | | |
| | | Element resultCode = root.element("result_code");
|
| | | if (!"SUCCESS".equalsIgnoreCase(resultCode.getText())) {
|
| | | Element errCode = root.element("err_code");
|
| | | Element errCodeDes = root.element("err_code_des");
|
| | | System.out.println(errCode.getText() + ":" + errCodeDes.getText()); |
| | | }
|
| | | |
| | | ExtractWeiXinRecord resultRecord = new ExtractWeiXinRecord();
|
| | | resultRecord.setBillno(root.element("result_code").getText());
|
| | | resultRecord.setDetailno(root.element("detail_id").getText());
|
| | | resultRecord.setStatus(root.element("status").getText());
|
| | | resultRecord.setSendType(root.element("send_type").getText());
|
| | | resultRecord.setHbType(root.element("result_code").getText());
|
| | | |
| | | String totalNum = root.element("total_num").getText();
|
| | | resultRecord.setTotalNum(Integer.parseInt(totalNum));
|
| | | |
| | | String sendTime = root.element("send_time").getText();
|
| | | resultRecord.setSendTime(new Date(TimeUtil.convertDateToTemp2(sendTime)));
|
| | | |
| | | Element element = root.element("hblist");
|
| | | Iterator<?> iterator = element.elementIterator();
|
| | | while (iterator.hasNext()) {
|
| | | Element childElement = (Element)iterator.next();
|
| | | Element rcvTime = childElement.element("rcv_time");
|
| | | resultRecord.setRcvTime(new Date(TimeUtil.convertDateToTemp2(rcvTime.getText())));
|
| | | break;
|
| | | } |
| | | return resultRecord;
|
| | | |
| | | } catch (DocumentException e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e);
|
| | | }
|
| | | return null;
|
| | | }
|
| | | |
| | | |
| | | public void downAutoOpenIdFile() {
|
| | | |
| | | |
| | | |
| | | |
| | | |
| | | }
|
| | | |
| | | }
|