AcceptorService.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AcceptorModel;
  13. use App\Models\AccountLogModel;
  14. use App\Models\BalanceLogModel;
  15. use App\Models\MemberBankModel;
  16. use App\Models\MemberModel;
  17. use App\Models\TradeModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use App\Services\WalletService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 承兑商服务管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Api
  28. */
  29. class AcceptorService extends BaseService
  30. {
  31. // 静态对象
  32. protected static $instance = null;
  33. /**
  34. * 构造函数
  35. * @author laravel开发员
  36. * @since 2020/11/11
  37. * MerchantService constructor.
  38. */
  39. public function __construct()
  40. {
  41. $this->model = new AcceptorModel();
  42. }
  43. /**
  44. * 静态入口
  45. * @return static|null
  46. */
  47. public static function make()
  48. {
  49. if (!self::$instance) {
  50. self::$instance = (new static());
  51. }
  52. return self::$instance;
  53. }
  54. /**
  55. * 获取缓存列表
  56. * @param $position
  57. * @param int $num
  58. * @return array|mixed
  59. */
  60. public function getDataList($params, $pageSize = 15, $refresh = false, $field = '')
  61. {
  62. $page = request()->post('page', 1);
  63. $cacheKey = "caches:acceptor:page_{$page}_" . md5(json_encode($params) . $pageSize);
  64. $datas = RedisService::get($cacheKey);
  65. $data = isset($datas['data']) ? $datas['data'] : [];
  66. if ($datas && $data && !$refresh) {
  67. return [
  68. 'list' => $data,
  69. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  70. 'pageSize' => $pageSize
  71. ];
  72. }
  73. $field = $field ? $field : 'lev_a.*,lev_b.nickname,lev_b.username,lev_b.avatar';
  74. $order = 'lev_a.id desc';
  75. $datas = $this->model->from('acceptor as a')
  76. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  77. ->where(['a.mark' => 1, 'b.mark' => 1])
  78. ->where('a.quota', '>', 0)
  79. ->whereIn('a.trade_status', [1, 2])
  80. ->where(function ($query) use ($params) {
  81. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  82. if ($kw) {
  83. $query->where('a.name', 'like', "%{$kw}%");
  84. }
  85. })
  86. ->where(function ($query) use ($params) {
  87. // 状态
  88. $status = isset($params['status']) && $params['status'] >= 0 ? intval($params['status']) : 2;
  89. if ($status > 0) {
  90. $query->where('a.status', $status);
  91. }
  92. $payType = isset($params['pay_type']) && $params['pay_type'] > 0 ? intval($params['pay_type']) : 0;
  93. if ($payType) {
  94. $query->where('a.pay_type', $payType);
  95. }
  96. $userId = isset($params['user_id']) && $params['user_id'] > 0 ? intval($params['user_id']) : 0;
  97. if ($userId) {
  98. $query->whereNotIn('a.user_id', [$userId]);
  99. }
  100. })
  101. ->selectRaw($field)
  102. ->orderByRaw($order)
  103. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  104. $datas = $datas ? $datas->toArray() : [];
  105. if ($datas) {
  106. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  107. $currency = isset($params['currency']) && $params['currency'] ? strtoupper($params['currency']) : 'CNY';
  108. $tradeType = isset($params['trade_type']) && $params['trade_type'] ? intval($params['trade_type']) : 1;
  109. $cnyPrice = $this->getRealPrice(1, $currency, $tradeType);
  110. foreach ($datas['data'] as &$item) {
  111. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  112. $item['currency_price'] = $this->getRealPrice(1, $currency, 0);
  113. $item['currency_quota'] = $this->getRealPrice(floatval($item['quota']), $currency, 3);
  114. $item['currency_rate'] = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  115. $item['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  116. $item['price'] = $cnyPrice;
  117. $item['time'] = $item['time'] > 60 ? intval($item['time'] / 60) : 20;
  118. }
  119. unset($item);
  120. RedisService::set($cacheKey, $datas, rand(3, 5));
  121. }
  122. return [
  123. 'list' => isset($datas['data']) ? $datas['data'] : [],
  124. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  125. 'pageSize' => $pageSize
  126. ];
  127. }
  128. /**
  129. * @param $price
  130. * @param $urrency 币种标志
  131. * @param int $tradeType 1-买,2-卖
  132. * @return string
  133. */
  134. public function getRealPrice($money, $currency = 'CNY', $tradeType = 1)
  135. {
  136. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  137. if ($usdtPrice <= 0) {
  138. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  139. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  140. }
  141. // 人民币与其他币的汇率价格
  142. $currencyPrice = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  143. // 买价
  144. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  145. $buyPrice = moneyFormat($money / $xdPrice, 4); // 星豆价格
  146. $cnyPrice = moneyFormat($buyPrice * $usdtPrice, 4); // 币种价格
  147. if ($tradeType == 1) {
  148. $xdBuyPriceRate = ConfigService::make()->getConfigByCode('trade_buy_price_rate', 0);
  149. $xdBuyPriceRate = $xdBuyPriceRate > 0 && $xdBuyPriceRate < 100 ? $xdBuyPriceRate : 0;
  150. $cnyBuyPrice = moneyFormat($cnyPrice + ($cnyPrice * $xdBuyPriceRate / 100), 4); // 上浮价格
  151. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  152. } else if ($tradeType == 2) {
  153. $xdSellPriceRate = ConfigService::make()->getConfigByCode('trade_sell_price_rate', 0);
  154. $xdSellPriceRate = $xdSellPriceRate > 0 && $xdSellPriceRate < 100 ? $xdSellPriceRate : 0;
  155. $cnyBuyPrice = moneyFormat($cnyPrice - ($cnyPrice * $xdSellPriceRate / 100), 4); // 下浮价格
  156. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  157. }
  158. return moneyFormat($cnyPrice * $currencyPrice, 4); // 汇率价格
  159. }
  160. /**
  161. * 获取信息
  162. * @param $where
  163. * @param array $field
  164. * @return array|mixed
  165. */
  166. public function getInfo($id, $field = [], $userId = 0)
  167. {
  168. if ($id) {
  169. $where = ['a.id' => $id, 'a.mark' => 1];
  170. } else if ($userId) {
  171. $where = ['a.user_id' => $userId, 'a.mark' => 1];
  172. } else {
  173. return false;
  174. }
  175. $defaultField = ['a.id', 'a.user_id', 'a.realname', 'a.name', 'a.mobile', 'a.telegram', 'a.quota', 'a.pay_type', 'a.usdt', 'a.status','b.usdt as usdt_balance','b.trc_url', 'b.nickname', 'b.avatar'];
  176. $field = $field ? $field : $defaultField;
  177. $info = $this->model->from('acceptor as a')
  178. ->leftjoin('member as b', 'b.id', '=', 'a.user_id')
  179. ->where($where)
  180. ->select($field)
  181. ->first();
  182. $info = $info ? $info->toArray() : [];
  183. if ($info) {
  184. $currency = request()->post('currency', 'CNY');
  185. $tradeType = request()->post('trade_type', 1);
  186. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  187. $cnyPrice = $this->getRealPrice(1, $currency, $tradeType);
  188. $info['currency_price'] = $this->getRealPrice(1, $currency, 0);
  189. $info['currency_quota'] = $this->getRealPrice(floatval($info['quota']), $currency, 3);
  190. $info['currency_rate'] = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  191. $info['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  192. $info['price'] = $cnyPrice;
  193. $info['avatar'] = get_image_url($info['avatar']);
  194. }
  195. return $info;
  196. }
  197. /**
  198. * 修改信息
  199. * @param $userId
  200. * @param $params
  201. * @return array|false|int[]
  202. */
  203. public function saveInfo($userId, $params)
  204. {
  205. // 验证是否入驻过和入驻状态
  206. $info = $this->model->where(['user_id' => $userId])->select('id', 'realname', 'trade_status', 'status', 'mark')->first();
  207. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  208. $mark = isset($info['mark']) ? $info['mark'] : 0;
  209. $id = isset($info['id']) ? $info['id'] : 0;
  210. if ($userId && empty($info)) {
  211. $this->error = 2216;
  212. return false;
  213. }
  214. // 是否被冻结
  215. if ($id && $tradeStatus == 4 && $mark) {
  216. $this->error = 2202;
  217. return false;
  218. }
  219. // 验证账户是否正常
  220. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  221. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  222. if (empty($userInfo) || $status != 1) {
  223. $this->error = 2017;
  224. return false;
  225. }
  226. // 入驻数据
  227. $data = [
  228. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  229. 'user_id' => $userId,
  230. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  231. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  232. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  233. 'create_time' => time(),
  234. 'update_time' => time(),
  235. 'mark' => 1,
  236. ];
  237. // 写入数据
  238. if ($id) {
  239. if ($this->model->where(['id' => $id])->update($data)) {
  240. $this->error = 2228;
  241. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  242. return ['id' => $id];
  243. } else {
  244. $this->error = 2229;
  245. return false;
  246. }
  247. } else {
  248. if ($merchId = $this->model->insertGetId($data)) {
  249. $this->error = 2228;
  250. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  251. return ['id' => $id];
  252. } else {
  253. $this->error = 2229;
  254. return false;
  255. }
  256. }
  257. }
  258. /**
  259. * 申请入驻
  260. * @param $userId
  261. * @param $params
  262. * @return array|false|int[]
  263. */
  264. public function apply($userId, $params)
  265. {
  266. // 验证是否入驻过和入驻状态
  267. $info = $this->model->where(['user_id' => $userId])->select('id', 'name', 'trade_status', 'status', 'mark')->first();
  268. $status = isset($info['status']) ? $info['status'] : 0;
  269. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  270. $mark = isset($info['mark']) ? $info['mark'] : 0;
  271. $id = isset($info['id']) ? $info['id'] : 0;
  272. if ($id && $status == 2 && $mark) {
  273. $this->error = 2201;
  274. return false;
  275. }
  276. // 是否被冻结
  277. if ($id && $tradeStatus == 4 && $mark) {
  278. $this->error = 2202;
  279. return false;
  280. }
  281. // 验证账户是否正常
  282. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  283. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  284. if (empty($userInfo) || $status != 1) {
  285. $this->error = 2017;
  286. return false;
  287. }
  288. // 支付方式验证
  289. $payType = isset($params['pay_type']) && $params['pay_type'] ? intval($params['pay_type']) : 1;
  290. $payment = MemberBankModel::where(['user_id' => $userId, 'type' => $payType, 'status' => 1, 'mark' => 1])->value('id');
  291. if ($payment <= 0) {
  292. $this->error = 3011;
  293. return false;
  294. }
  295. // 入驻数据
  296. $data = [
  297. 'name' => isset($params['name']) ? $params['name'] : '',
  298. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  299. 'user_id' => $userId,
  300. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  301. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  302. 'pay_type' => $payType,
  303. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  304. 'create_time' => time(),
  305. 'update_time' => time(),
  306. 'quota' => 0,
  307. 'trade_status' => 1,
  308. 'status' => 1,
  309. 'mark' => 1,
  310. ];
  311. // 写入数据
  312. if ($id) {
  313. if ($this->model->where(['id' => $id])->update($data)) {
  314. $this->error = 3012;
  315. return ['id' => $id];
  316. } else {
  317. $this->error = 3013;
  318. return false;
  319. }
  320. } else {
  321. if ($id = $this->model->insertGetId($data)) {
  322. $this->error = 3014;
  323. return ['id' => $id];
  324. } else {
  325. $this->error = 3013;
  326. return false;
  327. }
  328. }
  329. }
  330. /**
  331. * 获取入驻信息
  332. * @param $userId
  333. * @return mixed
  334. */
  335. public function getApplyInfo($userId)
  336. {
  337. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  338. ->orderBy('id', 'desc')
  339. ->first();
  340. $info = $info ? $info->setHidden(['quota', 'update_time', 'mark'])->toArray() : [];
  341. if ($info) {
  342. $payments = [1 => '微信支付', 2 => '支付宝支付', 3 => '银行卡支付'];
  343. $info['pay_name'] = isset($payments[$info['pay_type']]) ? $payments[$info['pay_type']] : '';
  344. }
  345. return $info;
  346. }
  347. /**
  348. * 充值交易额度
  349. * @param $userId 用户ID
  350. * @param $params
  351. * @return array|false
  352. */
  353. public function recharge($userId, $params)
  354. {
  355. $usdt = isset($params['usdt']) ? floatval($params['usdt']) : 0;
  356. $payPassword = isset($params['pay_password']) ? trim($params['pay_password']) : '';
  357. if ($usdt <= 0) {
  358. $this->error = 2031;
  359. return false;
  360. }
  361. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  362. ->select(['id','user_id', 'realname', 'mobile', 'usdt', 'quota', 'trade_status', 'status'])
  363. ->first();
  364. $info = $info? $info->toArray() :[];
  365. $quota = isset($info['quota']) ? $info['quota'] : 0;
  366. $status = isset($info['status']) ? $info['status'] : 0;
  367. $acceptorId = isset($info['id']) ? $info['id'] : 0;
  368. $userInfo = isset($info['member']) ? $info['member'] : [];
  369. if ($userId <= 0 || empty($info) || $status != 2) {
  370. $this->error = 2015;
  371. return false;
  372. }
  373. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  374. if(empty($userInfo)){
  375. $this->error = 2024;
  376. return false;
  377. }
  378. if($userPayPassword != get_password($payPassword)){
  379. $this->error = 2038;
  380. return false;
  381. }
  382. $userUsdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
  383. if($usdt> $userUsdt){
  384. $this->error = 2035;
  385. return false;
  386. }
  387. $cacheKey = "caches:acceptor:recharge:lock_{$userId}";
  388. if(RedisService::get($cacheKey)){
  389. $this->error = 1034;
  390. return false;
  391. }
  392. // 充值订单
  393. DB::beginTransaction();
  394. $orderNo = get_order_num('DP');
  395. RedisService::set($cacheKey, $info, rand(2,3));
  396. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  397. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  398. $money = round($xdPrice * $usdt, 2);
  399. $data = [
  400. 'order_no' => $orderNo,
  401. 'user_id' => $acceptorId,
  402. 'type' => 1,
  403. 'user_type' => 3,
  404. 'coin_type' => 6,
  405. 'money' => $usdt,
  406. 'actual_money' => $money,
  407. 'pay_type' => 10,
  408. 'pay_status' => 20,
  409. 'trc_url' => isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '',
  410. 'pay_at' => date('Y-m-d H:i:s'),
  411. 'date' => date('Y-m-d'),
  412. 'create_time' => time(),
  413. 'update_time' => time(),
  414. 'status' => 2,
  415. 'mark' => 1,
  416. ];
  417. if (!$orderId = BalanceLogModel::insertGetId($data)) {
  418. $this->error = 2033;
  419. DB::rollBack();
  420. RedisService::clear($cacheKey);
  421. return false;
  422. }
  423. // 扣除usdt余额
  424. $updateData = ['usdt' => DB::raw("usdt - {$usdt}"), 'update_time' => time()];
  425. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  426. $this->error = 2036;
  427. DB::rollBack();
  428. RedisService::clear($cacheKey);
  429. return false;
  430. }
  431. // 用户明细
  432. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  433. $log = [
  434. 'user_id' => $userId,
  435. 'source_id' => $acceptorId,
  436. 'source_order_no' => $data['order_no'],
  437. 'user_type' => 1,
  438. 'type' => 5,
  439. 'coin_type' => 1,
  440. 'money' => -$usdt,
  441. 'actual_money' => -$usdt,
  442. 'balance' => $usdt,
  443. 'date' => date('Y-m-d'),
  444. 'create_time' => time(),
  445. 'remark' => '交易额度充值扣除',
  446. 'status' => 1,
  447. 'mark' => 1
  448. ];
  449. if (!AccountLogModel::insertGetId($log)) {
  450. $this->error = 2029;
  451. DB::rollBack();
  452. RedisService::clear($cacheKey);
  453. return false;
  454. }
  455. // 额度增加
  456. $updateData = ['quota' => DB::raw("quota + {$money}"), 'update_time' => time()];
  457. if (!$this->model->where(['id' => $acceptorId])->update($updateData)) {
  458. $this->error = 2036;
  459. DB::rollBack();
  460. RedisService::clear($cacheKey);
  461. return false;
  462. }
  463. // 额度明细
  464. $log = [
  465. 'user_id' => $acceptorId,
  466. 'source_id' => $userId,
  467. 'source_order_no' => $data['order_no'],
  468. 'user_type' => 3,
  469. 'type' => 5,
  470. 'coin_type' => 6,
  471. 'money' => $money,
  472. 'actual_money' => $money,
  473. 'balance' => $quota,
  474. 'date' => date('Y-m-d'),
  475. 'create_time' => time(),
  476. 'remark' => '交易额度充值',
  477. 'status' => 1,
  478. 'mark' => 1
  479. ];
  480. if (!AccountLogModel::insertGetId($log)) {
  481. $this->error = 2029;
  482. DB::rollBack();
  483. RedisService::clear($cacheKey);
  484. return false;
  485. }
  486. // 消息
  487. $dateTime = date('Y-m-d H:i:s');
  488. MessageService::make()->pushMessage($userId, '交易额度充值成功', "您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}交易额度", 3);
  489. DB::commit();
  490. $this->error = 2037;
  491. return ['order_id' => $orderId, 'quota' => moneyFormat($quota + $money, 2), 'usdt' => moneyFormat($userUsdt - $usdt, 2)];
  492. }
  493. /**
  494. * 余额提现
  495. * @param $userId
  496. * @param $params
  497. * @return array|false
  498. */
  499. public function withdraw($userId, $params)
  500. {
  501. $money = isset($params['money']) ? floatval($params['money']) : 0;
  502. $payPassword = isset($params['pay_password']) ? trim($params['pay_password']) : '';
  503. $payType = isset($params['pay_type']) && $params['pay_type']? intval($params['pay_type']) : 20;
  504. $paymentId = isset($params['payment_id']) && $params['payment_id']? intval($params['payment_id']) : 0;
  505. $coinType = isset($params['coin_type']) && $params['coin_type'] ? intval($params['coin_type']) : 1;
  506. if ($money <= 0) {
  507. $this->error = 2401;
  508. return false;
  509. }
  510. if($paymentId<=0 && $payType == 30){
  511. $this->error = 2409;
  512. return false;
  513. }
  514. if (!in_array($coinType, [1,6])) {
  515. $this->error = 2404;
  516. return false;
  517. }
  518. $acceptor = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  519. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status'])
  520. ->first();
  521. // 获取承兑商信息
  522. $userInfo = isset($acceptor['member']) ? $acceptor['member'] : [];
  523. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  524. if (isset($userInfo['pay_password'])) {
  525. $userPayPassword = $userInfo['pay_password'];
  526. } else {
  527. $userPayPassword = DB::table('member')->where('id', $userId)->value('pay_password');
  528. }
  529. $acceptorUsdt = isset($acceptor['usdt']) ? $acceptor['usdt'] : 0;
  530. $acceptorQuota = isset($acceptor['quota']) ? $acceptor['quota'] : 0;
  531. $acceptorId = isset($acceptor['id']) ? $acceptor['id'] : 0;
  532. $acceptorTradeStatus = isset($acceptor['trade_status']) ? $acceptor['trade_status'] : 0;
  533. if (empty($userInfo) || $status != 1) {
  534. $this->error = 2024;
  535. return false;
  536. }
  537. if (empty($acceptor) || !in_array($acceptorTradeStatus,[1,2])) {
  538. $this->error = 2024;
  539. return false;
  540. }
  541. // 提现金额验证
  542. if($coinType == 1){
  543. if($money>$acceptorUsdt){
  544. $this->error = web_lang(2402, ['money' => $acceptorUsdt]);
  545. return false;
  546. }
  547. }else if($coinType==6){
  548. if($money>$acceptorQuota){
  549. $this->error = web_lang(2408, ['money' => $acceptorQuota]);
  550. return false;
  551. }
  552. }
  553. // 提现账户
  554. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  555. if (empty($trcUrl)) {
  556. $this->error = 2403;
  557. return false;
  558. }
  559. if ($userPayPassword != get_password($payPassword)) {
  560. $this->error = 2038;
  561. return false;
  562. }
  563. $cacheKey = "caches:acceptor:withdraw:lock_{$userId}_{$coinType}";
  564. if (RedisService::get($cacheKey)) {
  565. $this->error = 1034;
  566. return false;
  567. }
  568. DB::beginTransaction();
  569. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  570. // 提现记录
  571. $orderNo = get_order_num('DW');
  572. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  573. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate', 5);
  574. $feeRate = $feeRate > 0 && $feeRate < 100 ? moneyFormat($feeRate / 100, 3) : 0;
  575. if($coinType == 1){
  576. $fee = round($money * $feeRate, 3);
  577. $realAmount = moneyFormat($money - $fee, 4);
  578. }else{
  579. $realUsdt = round($money/$xdPrice,4);
  580. $fee = round($realUsdt * $feeRate, 4);
  581. $realAmount = moneyFormat($realUsdt - $fee, 4);
  582. }
  583. if($payType != 20){
  584. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  585. if ($usdtPrice <= 0) {
  586. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  587. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  588. }
  589. $fee = round($fee * $usdtPrice, 2);
  590. $realAmount = round($realAmount * $usdtPrice, 2);
  591. }
  592. $data = [
  593. 'order_no' => $orderNo,
  594. 'user_id' => $acceptorId,
  595. 'type' => 2,
  596. 'user_type' => 3,
  597. 'coin_type' => $coinType,
  598. 'money' => $money,
  599. 'actual_money' => $realAmount,
  600. 'fee' => $fee,
  601. 'usdt_price' => $payType!=20? $usdtPrice : 0,
  602. 'trc_url' => $trcUrl,
  603. 'pay_type' => $payType,
  604. 'payment_id' => $paymentId,
  605. 'date' => date('Y-m-d'),
  606. 'create_time' => time(),
  607. 'update_time' => time(),
  608. 'status' => 1,
  609. 'mark' => 1,
  610. ];
  611. if (!$id = BalanceLogModel::insertGetId($data)) {
  612. DB::rollBack();
  613. $this->error = 2405;
  614. RedisService::clear($cacheKey);
  615. return false;
  616. }
  617. // 商户扣款
  618. $updateData = [
  619. 'update_time' => time()
  620. ];
  621. if($coinType == 1) {
  622. $updateData['usdt'] = DB::raw("usdt - {$money}");
  623. }else {
  624. $updateData['quota'] = DB::raw("quota - {$money}");
  625. }
  626. if (!AcceptorModel::where(['user_id' => $userId, 'mark' => 1])->update($updateData)) {
  627. DB::rollBack();
  628. $this->error = 2406;
  629. RedisService::clear($cacheKey);
  630. return false;
  631. }
  632. // 明细
  633. $log = [
  634. 'user_id' => $acceptorId,
  635. 'source_id' => $userId,
  636. 'source_order_no' => $orderNo,
  637. 'type' => 5,
  638. 'coin_type' => $coinType,
  639. 'user_type' => 3,
  640. 'money' => -$money,
  641. 'actual_money' => -$money,
  642. 'balance' => $acceptorUsdt,
  643. 'create_time' => time(),
  644. 'update_time' => time(),
  645. 'remark' => $coinType==6? "承兑商额度提现":"承兑商佣金提现",
  646. 'status' => 1,
  647. 'mark' => 1,
  648. ];
  649. if (!AccountLogModel::insertGetId($log)) {
  650. $this->error = 2407;
  651. DB::rollBack();
  652. RedisService::clear($cacheKey);
  653. return false;
  654. }
  655. DB::commit();
  656. // 站内消息
  657. $dateTime = date('Y-m-d H:i:s');
  658. $coinTypeName = $coinType==1?'佣金':'交易额度';
  659. $title = $coinType==1?'承兑商佣金提现申请成功':"承兑商交易额度提现申请成功";
  660. $message = $coinType==1?"您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商佣金成功,请耐心等候审核!!!":"您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商交易额度成功,请耐心等候审核!!!";
  661. MessageService::make()->pushMessage($userId, $title, $message, 3);
  662. // 提现自动审核,低于该金额自动审核
  663. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  664. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  665. if ($money <= $autoCheckUsdt && $payType==20) {
  666. // 打款处理
  667. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  668. $txID = isset($result['txId']) ? $result['txId'] : '';
  669. $payAddress = isset($result['address']) ? $result['address'] : '';
  670. if ($txID && $payAddress) {
  671. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '自动审核打款', 'status' => 2, 'update_time' => time()];
  672. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => 3])->update($updateData)) {
  673. $title = "承兑商{$coinTypeName}提现审核成功";
  674. $message = "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商{$coinTypeName}审核成功,请耐心等候打款到账!!!";
  675. MessageService::make()->pushMessage($userId, $title, $message, 3);
  676. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  677. // 平台明细
  678. $log = [
  679. 'user_id' => 0,
  680. 'source_id' => $userId,
  681. 'source_order_no' => $orderNo,
  682. 'type' => 5,
  683. 'coin_type' => 1,
  684. 'user_type' => 4,
  685. 'money' => $fee,
  686. 'actual_money' => $fee,
  687. 'balance' => 0,
  688. 'create_time' => time(),
  689. 'update_time' => time(),
  690. 'hash' => $txID,
  691. 'remark' => "承兑商{$coinTypeName}提现",
  692. 'status' => 1,
  693. 'mark' => 1,
  694. ];
  695. AccountLogModel::insertGetId($log);
  696. // 平台流水
  697. FinanceService::make()->saveLog(0, $fee, 1);
  698. }
  699. }
  700. }
  701. $this->error = $title;
  702. RedisService::clear($cacheKey);
  703. return [
  704. 'id' => $id,
  705. 'money' => $money,
  706. 'coin_type' => $coinType,
  707. ];
  708. }
  709. /**
  710. * C2C交易-购买
  711. * 1.当前登录用户购买
  712. * 2.承兑商卖
  713. * 用户买,则扣承兑商额度,用户付款,承兑商收款然后把订单交易的豆给用户
  714. * 记录trade记录
  715. * @param $userId
  716. * @param $params
  717. * @return array|false
  718. */
  719. public function buyxd($userId, $params)
  720. {
  721. $cacheKey = "caches:acceptor:buyxd:lock_{$userId}";
  722. if (RedisService::get($cacheKey)) {
  723. $this->error = 1034;
  724. return false;
  725. }
  726. // 购买用户信息
  727. $userInfo = $pointInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  728. ->select(['id', 'usdt', 'balance', 'member_level', 'wait_score', 'pay_password', 'status'])
  729. ->first();
  730. // 承兑商信息
  731. $acceptor = $this->model->with(['member'])->where(['id' => $params['acceptor_id'], 'mark' => 1])
  732. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status', 'pay_type'])
  733. ->first();
  734. $acceptorUid = isset($acceptor['user_id'])? $acceptor['user_id'] : 0;
  735. if (empty($userInfo) || $userInfo['status'] != 1 || $acceptorUid<=0) {
  736. $this->error = 2024;
  737. return false;
  738. }
  739. if($userId == $acceptorUid){
  740. $this->error = 3015;
  741. return false;
  742. }
  743. if ($userInfo['pay_password'] != get_password($params['pay_password'])) {
  744. $this->error = 2038;
  745. return false;
  746. }
  747. if($params['quota'] > $acceptor['quota']){
  748. $this->error = 3205;
  749. return false;
  750. }
  751. // C2C交易金豆
  752. $orderNo = get_order_num('C2C');
  753. // 卖家1星豆人民币对比价格
  754. $currency = isset($params['currency'])? $params['currency'] : 'CNY';
  755. $cnyPrice = $this->getRealPrice(1, $currency, 1);
  756. $money = moneyFormat($this->getRealPrice($params['quota'], $currency, 1), 2, true);
  757. if (floatval($params['money']) != $money) {
  758. $this->error = 2028;
  759. return false;
  760. }
  761. // 星豆
  762. DB::beginTransaction();
  763. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  764. // 承兑商收款信息
  765. $memberBank = MemberBankModel::where(['user_id' => $acceptor['user_id'], 'type' => $acceptor['pay_type'], 'mark' => 1])->first();
  766. // 兑换usdt费率价格
  767. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  768. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  769. $usdt_rate = round(1 / $xdPrice, 4);
  770. // 承兑商额外佣金金豆转u
  771. $acceptorCommissionRate = ConfigService::make()->getConfigByCode('acceptor_commission_rate', 0);
  772. // 保留4位小数点
  773. $bonus_usdt = round($params['quota']/$xdPrice * $acceptorCommissionRate/100, 4);
  774. // 添加交易订单记录
  775. $data = [
  776. 'user_id' => $userId,
  777. 'acceptor_id' => $params['acceptor_id'],
  778. 'acceptor_uid' => $acceptor['user_id'],
  779. 'order_no' => $orderNo,
  780. 'type' => 1,
  781. 'coin_type' => 1,
  782. 'price' => $cnyPrice,
  783. 'num' => $params['quota'],
  784. 'account_id' => isset($memberBank) ? $memberBank['id'] : 0,
  785. 'mobile' => $acceptor['mobile'],
  786. 'real_name' => $acceptor['realname'],
  787. 'total' => $money,
  788. 'usdt_rate' => $usdt_rate,
  789. 'currency' => $params['currency'],
  790. 'pay_type' => $params['pay_type'],
  791. 'pay_money' => $params['money'],
  792. 'pay_status' => 2,
  793. 'pay_time' => now(),
  794. 'bonus_usdt' => $bonus_usdt,// 承兑商佣金金豆转u
  795. 'pay_img' => '',
  796. 'create_time' => now(),
  797. 'update_time' => now(),
  798. 'is_settle' => 2,
  799. 'exception_status' => 0,
  800. 'sell_exception_img' => '',
  801. 'exception_img' => '',
  802. 'exception_remark' => '',
  803. 'remark' => '购买下单成功,待支付',
  804. 'status' => 1,
  805. 'mark' => 1,
  806. ];
  807. if (!$orderId = TradeModel::insertGetId($data)) {
  808. RedisService::clear($cacheKey);
  809. $this->error = 2033;
  810. DB::rollBack();
  811. return false;
  812. }
  813. // 扣除商户额度
  814. $updateData = ['quota' => DB::raw("quota - {$params['quota']}"), 'update_time' => time()];
  815. if (!AcceptorModel::where(['id' => $acceptor['id']])->update($updateData)) {
  816. $this->error = 2036;
  817. DB::rollBack();
  818. return false;
  819. }
  820. // 增加accountLog记录
  821. $log = [
  822. 'user_id' => $acceptor['id'],
  823. 'source_id' => $userId,
  824. 'source_order_no' => $orderNo,
  825. 'type' => 101,
  826. 'coin_type' => 6,
  827. 'user_type' => 3,
  828. 'money' => -$params['quota'],
  829. 'actual_money' => -$params['quota'],
  830. 'balance' => $acceptor['quota'],
  831. 'create_time' => time(),
  832. 'update_time' => time(),
  833. 'remark' => "C2C交易-额度扣除",
  834. 'status' => 1,
  835. 'mark' => 1,
  836. ];
  837. if (!AccountLogModel::insertGetId($log)) {
  838. $this->error = 2407;
  839. DB::rollBack();
  840. RedisService::clear($cacheKey);
  841. return false;
  842. }
  843. // 消息
  844. $dateTime = date('Y-m-d H:i:s');
  845. MessageService::make()->pushMessage($userId, '星豆购买下单成功', "您在{$dateTime}(UTC+8)成功下单¥{$money}购买{$params['quota']}星豆,请在20分钟内确认支付,否则订单将失效", 3);
  846. DB::commit();
  847. $this->error = 2037;
  848. RedisService::clear($cacheKey);
  849. return ['trade_id' => $orderId, 'money' => $money];
  850. }
  851. /**
  852. * C2C交易-卖出
  853. * 1.当前登录用户购买
  854. * 2.承兑商卖
  855. * 用户买,则扣承兑商额度,用户付款,承兑商收款然后把订单交易的豆给用户
  856. * 记录trade记录
  857. * @param $userId
  858. * @param $params
  859. * @return array|false
  860. */
  861. public function sellxd($userId, $params)
  862. {
  863. $cacheKey = "caches:acceptor:sellxd:lock_{$userId}";
  864. if (RedisService::get($cacheKey)) {
  865. $this->error = 1034;
  866. return false;
  867. }
  868. // 卖出用户信息
  869. $userInfo = $pointInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  870. ->select(['id', 'usdt', 'balance', 'member_level', 'wait_score', 'pay_password', 'status'])
  871. ->first();
  872. // 承兑商信息
  873. $acceptor = $this->model->with(['member'])->where(['id' => $params['acceptor_id'], 'mark' => 1])
  874. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status'])
  875. ->first();
  876. $acceptorUid = isset($acceptor['user_id'])? $acceptor['user_id'] : 0;
  877. if (empty($userInfo) || $userInfo['status'] != 1 || $acceptorUid<=0) {
  878. $this->error = 2024;
  879. return false;
  880. }
  881. if($userId == $acceptorUid){
  882. $this->error = 3015;
  883. return false;
  884. }
  885. if ($userInfo['pay_password'] != get_password($params['pay_password'])) {
  886. $this->error = 2038;
  887. return false;
  888. }
  889. if($params['quota'] > $acceptor['quota']){
  890. $this->error = 3205;
  891. return false;
  892. }
  893. // C2C交易金豆
  894. $orderNo = get_order_num('C2C');
  895. // 卖家1星豆人民币对比价格
  896. $currency = isset($params['currency'])? $params['currency'] : 'CNY';
  897. $cnyPrice = $this->getRealPrice(1, $currency, 2);
  898. $money = moneyFormat($this->getRealPrice($params['quota'], $currency, 2), 2, true);
  899. // 星豆
  900. DB::beginTransaction();
  901. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  902. // 用户收款信息
  903. $memberBank = MemberBankModel::where(['user_id' => $userId, 'type' => $acceptor['pay_type']])->first();
  904. // 兑换usdt费率价格
  905. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  906. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  907. $usdt_rate = round(1 / $xdPrice, 4);
  908. // 承兑商额外佣金金豆转u
  909. $acceptorCommissionRate = ConfigService::make()->getConfigByCode('acceptor_commission_rate', 100);
  910. $bonus_usdt = round(($params['quota']/$xdPrice * ($acceptorCommissionRate/100)), 4);
  911. // 添加交易订单记录
  912. $data = [
  913. 'user_id' => $userId,
  914. 'acceptor_id' => $params['acceptor_id'],
  915. 'acceptor_uid' => $acceptor['user_id'],
  916. 'order_no' => $orderNo,
  917. 'type' => 2,
  918. 'coin_type' => 1,
  919. 'price' => $cnyPrice,
  920. 'num' => $params['quota'],
  921. 'account_id' => isset($memberBank) ? $memberBank['id'] : 0,
  922. 'mobile' => $acceptor['mobile'],
  923. 'real_name' => $acceptor['realname'],
  924. 'total' => $money,
  925. 'usdt_rate' => $usdt_rate,
  926. 'currency' => $params['currency'],
  927. 'pay_type' => $params['pay_type'],
  928. 'pay_money' => $params['money'],
  929. 'pay_status' => 2,
  930. 'pay_time' => now(),
  931. 'bonus_usdt' => $bonus_usdt,// 承兑商佣金金豆转u
  932. 'pay_img' => '',
  933. 'create_time' => now(),
  934. 'update_time' => now(),
  935. 'is_settle' => 2,
  936. 'exception_status' => 0,
  937. 'sell_exception_img' => '',
  938. 'exception_img' => '',
  939. 'exception_remark' => '',
  940. 'remark' => '卖出下单成功,待支付',
  941. 'status' => 1,
  942. 'mark' => 1,
  943. ];
  944. if (!$orderId = TradeModel::insertGetId($data)) {
  945. RedisService::clear($cacheKey);
  946. $this->error = 2033;
  947. DB::rollBack();
  948. return false;
  949. }
  950. // 扣除用户星豆
  951. $updateData = ['balance' => DB::raw("balance - {$params['quota']}"), 'update_time' => time()];
  952. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  953. $this->error = 2036;
  954. DB::rollBack();
  955. return false;
  956. }
  957. // 增加accountLog记录
  958. $log = [
  959. 'user_id' => $userId,
  960. 'source_id' => $acceptor['id'],
  961. 'source_order_no' => $orderNo,
  962. 'type' => 101,
  963. 'coin_type' => 2,
  964. 'user_type' => 1,
  965. 'money' => -$params['quota'],
  966. 'actual_money' => -$params['quota'],
  967. 'balance' => $userInfo['balance'],
  968. 'create_time' => time(),
  969. 'update_time' => time(),
  970. 'remark' => "C2C交易-卖出星豆",
  971. 'status' => 1,
  972. 'mark' => 1,
  973. ];
  974. if (!AccountLogModel::insertGetId($log)) {
  975. $this->error = 2407;
  976. DB::rollBack();
  977. RedisService::clear($cacheKey);
  978. return false;
  979. }
  980. // 消息
  981. $dateTime = date('Y-m-d H:i:s');
  982. MessageService::make()->pushMessage($userId, '星豆购买下单成功', "您在{$dateTime}(UTC+8)成功下单¥{$money}购买{$params['quota']}星豆,请在20分钟内确认支付,否则订单将失效", 3);
  983. DB::commit();
  984. $this->error = 2037;
  985. RedisService::clear($cacheKey);
  986. return ['trade_id' => $orderId, 'money' => $money];
  987. }
  988. }