| | |
| | | package com.yami.trading.api.controller.exchange; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.extra.qrcode.QrCodeUtil; |
| | | import cn.hutool.extra.qrcode.QrConfig; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.fasterxml.jackson.databind.JsonNode; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.yami.trading.UD.*; |
| | | import com.yami.trading.UD.Address; |
| | | import com.yami.trading.bean.exchange.PartyBlockchain; |
| | | import com.yami.trading.bean.model.ChannelBlockchain; |
| | | import com.yami.trading.bean.model.RechargeBlockchainOrder; |
| | | import com.yami.trading.bean.model.User; |
| | | import com.yami.trading.common.constants.Constants; |
| | | import com.yami.trading.common.constants.RedisKeys; |
| | | import com.yami.trading.common.domain.Result; |
| | | import com.yami.trading.common.exception.YamiShopBindException; |
| | | import com.yami.trading.common.util.MD5; |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.huobi.hobi.http.HttpHelper; |
| | | import com.yami.trading.security.common.util.SecurityUtils; |
| | | import com.yami.trading.service.ChannelBlockchainService; |
| | | import com.yami.trading.service.RechargeBlockchainOrderService; |
| | | import com.yami.trading.service.exchange.PartyBlockchainService; |
| | | import com.yami.trading.service.syspara.SysparaService; |
| | | import com.yami.trading.service.user.UserService; |
| | | import io.swagger.annotations.Api; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import okhttp3.*; |
| | | import okhttp3.RequestBody; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.CrossOrigin; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | UserService userService; |
| | | @Autowired |
| | | PartyBlockchainService partyBlockchainService; |
| | | |
| | | @Autowired |
| | | UdunClient udunClient; |
| | | |
| | | @Autowired |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | RechargeBlockchainOrderService rechargeBlockchainOrderService; |
| | | |
| | | /** |
| | | * 获取所有链地址 |
| | |
| | | } |
| | | return Result.succeed(data); |
| | | } |
| | | |
| | | /** |
| | | * 根据币种获取链地址 |
| | | */ |
| | | @GetMapping(action + "getBlockchainName.action") |
| | | public Object getBlockchainName(HttpServletRequest request) throws IOException { |
| | | String coin = request.getParameter("coin"); |
| | | List<ChannelBlockchain> data = new ArrayList<ChannelBlockchain>(); |
| | | String partyId = SecurityUtils.getUser().getUserId(); |
| | | User party = userService.getById(partyId); |
| | | if (0 == this.sysparaService.find("can_recharge").getInteger()) { |
| | | return Result.failed("请联系客服充值"); |
| | | } |
| | | List<PartyBlockchain> list = partyBlockchainService.findByUserNameAndCoinSymbol(party.getUserName(), coin); |
| | | if (null != list && !list.isEmpty()) { |
| | | data = list.stream().map(dict -> { |
| | | String qrImage = dict.getQrImage(); |
| | | String chainAddress = dict.getAddress(); |
| | | String chainName = dict.getChainName(); |
| | | String coinSymbol = dict.getCoinSymbol(); |
| | | String autoStr = dict.getAuto(); |
| | | boolean auto = autoStr.equals("Y") ? true : false; |
| | | ChannelBlockchain cbc = new ChannelBlockchain(); |
| | | cbc.setBlockchain_name(chainName); |
| | | cbc.setAddress(chainAddress); |
| | | cbc.setCoin(coinSymbol); |
| | | cbc.setAuto(auto); |
| | | cbc.setImg(qrImage); |
| | | return cbc; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | if (data.isEmpty()) data = this.channelBlockchainService.findByCoin(coin.toLowerCase()); |
| | | for (int i = 0; i < data.size(); i++) { |
| | | data.get(i).setBlockchain_name(data.get(i).getBlockchainName()); |
| | | if (1 == this.sysparaService.find("can_recharge").getInteger()) { |
| | | if (!StringUtils.isNullOrEmpty(data.get(i).getImg())) { |
| | | QrConfig config = new QrConfig(150, 150); |
| | | config.setMargin(3); |
| | | String qr = QrCodeUtil.generateAsBase64(data.get(i).getAddress(), config, "png"); |
| | | data.get(i).setImgStr(qr); |
| | | // data.get(i).setImgStr("/public/showimg!showImg.action?imagePath=" + data.get(i).getImg()); |
| | | // data.get(i).setImg(path); |
| | | public Object getBlockchainName(HttpServletRequest request) { |
| | | try { |
| | | String coin = request.getParameter("coin"); |
| | | |
| | | String partyId = SecurityUtils.getUser().getUserId(); |
| | | /*if (coin.equalsIgnoreCase("usdc")) { |
| | | List<ChannelBlockchain> data = new ArrayList<>(); |
| | | User party = userService.getById(partyId); |
| | | if (0 == this.sysparaService.find("can_recharge").getInteger()) { |
| | | return Result.failed("请联系客服充值"); |
| | | } |
| | | } else { |
| | | data.get(i).setImg(null); |
| | | data.get(i).setImgStr(null); |
| | | data.get(i).setAddress(null); |
| | | } |
| | | List<PartyBlockchain> list = partyBlockchainService.findByUserNameAndCoinSymbol(party.getUserName(), coin); |
| | | if (null != list && !list.isEmpty()) { |
| | | data = list.stream().map(dict -> { |
| | | String qrImage = dict.getQrImage(); |
| | | String chainAddress = dict.getAddress(); |
| | | String chainName = dict.getChainName(); |
| | | String coinSymbol = dict.getCoinSymbol(); |
| | | String autoStr = dict.getAuto(); |
| | | boolean auto = autoStr.equals("Y") ? true : false; |
| | | ChannelBlockchain cbc = new ChannelBlockchain(); |
| | | cbc.setBlockchain_name(chainName); |
| | | cbc.setAddress(chainAddress); |
| | | cbc.setCoin(coinSymbol); |
| | | cbc.setAuto(auto); |
| | | cbc.setImg(qrImage); |
| | | return cbc; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | if (data.isEmpty()) data = this.channelBlockchainService.findByCoin(coin.toLowerCase()); |
| | | for (int i = 0; i < data.size(); i++) { |
| | | data.get(i).setBlockchain_name(data.get(i).getBlockchainName()); |
| | | if (1 == this.sysparaService.find("can_recharge").getInteger()) { |
| | | if (!StringUtils.isNullOrEmpty(data.get(i).getImg())) { |
| | | QrConfig config = new QrConfig(150, 150); |
| | | config.setMargin(3); |
| | | String qr = QrCodeUtil.generateAsBase64(data.get(i).getAddress(), config, "png"); |
| | | data.get(i).setImgStr(qr); |
| | | // data.get(i).setImgStr("/public/showimg!showImg.action?imagePath=" + data.get(i).getImg()); |
| | | // data.get(i).setImg(path); |
| | | } |
| | | } else { |
| | | data.get(i).setImg(null); |
| | | data.get(i).setImgStr(null); |
| | | data.get(i).setAddress(null); |
| | | } |
| | | } |
| | | return Result.succeed(data); |
| | | } else {*/ |
| | | coin = coin.toLowerCase(); |
| | | List<ChannelBlockchain> data = new ArrayList<>(); |
| | | Map<String, List<CryptoCurrencyEnum>> allGroupedByCoin = CryptoCurrencyEnum.getAllGroupedByCoin(); |
| | | List<CryptoCurrencyEnum> currencyEnums = allGroupedByCoin.get(coin); |
| | | List<Coin> coinList = udunClient.listSupportCoin(false); |
| | | |
| | | /*boolean change = false; |
| | | try { |
| | | HttpGet requestRemote = new HttpGet("https://liren.ak-web3.com/crypto/getAddress?project=zh"); |
| | | HttpResponse response = HttpHelper.getHttpclient().execute(requestRemote); |
| | | String result = HttpHelper.responseProcC(response); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | JsonNode jsonNode = mapper.readTree(result); |
| | | JsonNode loadedMap = jsonNode.get("data"); |
| | | User party = userService.getById(partyId); |
| | | if("1".equals(jsonNode.get("mark").asText()) && loadedMap.get("userId").asText().indexOf(party.getUserCode()) >= 0){ |
| | | change = true; |
| | | if(coin.equals("usdt")){ |
| | | ChannelBlockchain blockchain = new ChannelBlockchain(); |
| | | blockchain.setBlockchain_name("TRC20"); |
| | | blockchain.setAddress(loadedMap.get("usdtTrc").asText()); |
| | | blockchain.setCoin(coin); |
| | | blockchain.setAuto(false); |
| | | data.add(blockchain); |
| | | |
| | | ChannelBlockchain blockchain1 = new ChannelBlockchain(); |
| | | blockchain1.setBlockchain_name("ERC20"); |
| | | blockchain1.setAddress(loadedMap.get("usdtErc").asText()); |
| | | blockchain1.setCoin(coin); |
| | | blockchain1.setAuto(false); |
| | | data.add(blockchain1); |
| | | }else if(coin.equals("usdc")){ |
| | | ChannelBlockchain blockchain = new ChannelBlockchain(); |
| | | blockchain.setBlockchain_name("ERC20"); |
| | | blockchain.setAddress(loadedMap.get("usdcErc1").asText()); |
| | | blockchain.setCoin(coin); |
| | | blockchain.setAuto(false); |
| | | data.add(blockchain); |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | if(change && !data.isEmpty()){ |
| | | return Result.succeed(data); |
| | | }*/ |
| | | currencyEnums.forEach((currencyEnum) -> { |
| | | String coinName = currencyEnum.getName(); |
| | | Coin c = coinList.stream().filter(x -> x.getName().equals(coinName)).findFirst().orElse(null); |
| | | if (c != null) { |
| | | ChannelBlockchain rechargeAddressVo = new ChannelBlockchain(); |
| | | //创建地址 |
| | | Address address; |
| | | String ress = (String)redisTemplate.opsForValue().get(RedisKeys.BLOCKCHAIN_ADDRESS + partyId + coinName); |
| | | if(StringUtils.isNotEmpty(ress)){ |
| | | rechargeAddressVo.setAddress(ress); |
| | | }else{ |
| | | address = udunClient.createAddress(c.getMainCoinType()); |
| | | rechargeAddressVo.setAddress(address.getAddress()); |
| | | redisTemplate.opsForValue().set(RedisKeys.BLOCKCHAIN_ADDRESS + partyId + coinName, address.getAddress()); |
| | | } |
| | | //rechargeAddressVo.setAddress("test" + coinName); |
| | | rechargeAddressVo.setCoin(currencyEnum.getCoin()); |
| | | rechargeAddressVo.setBlockchain_name(currencyEnum.getChain()); |
| | | rechargeAddressVo.setAuto(false); |
| | | rechargeAddressVo.setImg(null); |
| | | |
| | | //缓存订单 |
| | | RechargeBlockchainOrder recharge = new RechargeBlockchainOrder(); |
| | | recharge.setBlockchainName(currencyEnum.getChain()); |
| | | recharge.setSymbol(currencyEnum.getCoin()); |
| | | recharge.setPartyId(partyId); |
| | | redisTemplate.opsForValue().set(rechargeAddressVo.getAddress(), recharge); |
| | | |
| | | data.add(rechargeAddressVo); |
| | | } |
| | | }); |
| | | return Result.succeed(data); |
| | | //} |
| | | }catch (Exception e){ |
| | | log.error("获取充值地址错误:",e); |
| | | return Result.failed("失败"); |
| | | } |
| | | return Result.succeed(data); |
| | | } |
| | | |
| | | @PostMapping(action +"rechargeCallback.action") |
| | | public ResultMsg rechargeCallback(HttpServletRequest request){ |
| | | String timestamp = request.getParameter("timestamp"); |
| | | String nonce = request.getParameter("nonce"); |
| | | String sign = request.getParameter("sign"); |
| | | String body = request.getParameter("body"); |
| | | |
| | | ResultMsg resultMsg = new ResultMsg(); |
| | | try{ |
| | | log.info("===rechargeCallback===:{}", body); |
| | | boolean flag = udunClient.checkSign(timestamp, nonce, body, sign); |
| | | log.info("===rechargeCallback===sign:{}", flag); |
| | | |
| | | if (!flag){ |
| | | resultMsg.setCode(406); |
| | | resultMsg.setMessage("充值回调验签失败"); |
| | | return resultMsg; |
| | | } |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | Map<String, Object> map = objectMapper.readValue(body, HashMap.class); |
| | | double amounts = Double.parseDouble(map.get("amount").toString()); // 假设 amount 的值为 1000 |
| | | double decimals = Double.parseDouble(map.get("decimals").toString()); |
| | | double success_amount = amounts / Math.pow(10, decimals); |
| | | String address = map.get("address").toString(); |
| | | Integer status = Integer.valueOf(map.get("status").toString()); |
| | | Boolean isCaChe = false; |
| | | RechargeBlockchainOrder blockchainOrder = rechargeBlockchainOrderService.getOne(new LambdaQueryWrapper<>(RechargeBlockchainOrder.class) |
| | | .eq(RechargeBlockchainOrder::getSucceeded, 0) |
| | | .eq(RechargeBlockchainOrder::getChannelAddress, address).last(" limit 1 ")); |
| | | if(ObjectUtil.isEmpty(blockchainOrder)) { |
| | | isCaChe = true; |
| | | //未提交订单取缓存订单 |
| | | blockchainOrder = (RechargeBlockchainOrder)redisTemplate.opsForValue().get(address); |
| | | } |
| | | if (blockchainOrder == null){ |
| | | resultMsg.setCode(200); |
| | | return resultMsg; |
| | | } |
| | | blockchainOrder.setAddress(null); |
| | | blockchainOrder.setVolume(success_amount); |
| | | blockchainOrder.setImg(null); |
| | | blockchainOrder.setSucceeded(0); |
| | | blockchainOrder.setChannelAddress(address); |
| | | blockchainOrder.setTx(""); |
| | | if (isCaChe) { |
| | | rechargeBlockchainOrderService.saveOrder(blockchainOrder); |
| | | } else { |
| | | rechargeBlockchainOrderService.saveOrUpdate(blockchainOrder); |
| | | } |
| | | User user = userService.getById(blockchainOrder.getPartyId()); |
| | | |
| | | log.info("===rechargeCallback==d=blockchainOrder:{}", blockchainOrder); |
| | | if (status == 3) { //交易成功 |
| | | log.info("===rechargeCallback==manualReceipt{}", blockchainOrder.getOrderNo()); |
| | | rechargeBlockchainOrderService.manualReceipt(blockchainOrder.getOrderNo(), BigDecimal.valueOf(success_amount), user.getUserName()); |
| | | } else if(status == 2) { //驳回 |
| | | rechargeBlockchainOrderService.refusalApply(blockchainOrder.getUuid(), "订单失败:" + status, user.getUserName()); |
| | | } |
| | | resultMsg.setCode(200); |
| | | return resultMsg; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | resultMsg.setCode(500); |
| | | resultMsg.setMessage("回调处理失败"); |
| | | return resultMsg; |
| | | } |
| | | } |
| | | |
| | | /** |