AcceptorService.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. })
  97. ->selectRaw($field)
  98. ->orderByRaw($order)
  99. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  100. $datas = $datas ? $datas->toArray() : [];
  101. if ($datas) {
  102. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  103. $currency = isset($params['currency']) && $params['currency'] ? strtoupper($params['currency']) : 'CNY';
  104. $tradeType = isset($params['trade_type']) && $params['trade_type'] ? intval($params['trade_type']) : 1;
  105. $cnyPrice = $this->getRealPrice(1, $currency, $tradeType);
  106. foreach ($datas['data'] as &$item) {
  107. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  108. $item['currency_price'] = $this->getRealPrice(1, $currency, 0);
  109. $item['currency_quota'] = $this->getRealPrice(floatval($item['quota']), $currency, 3);
  110. $item['currency_rate'] = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  111. $item['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  112. $item['price'] = $cnyPrice;
  113. $item['time'] = $item['time'] > 60 ? intval($item['time'] / 60) : 20;
  114. }
  115. unset($item);
  116. RedisService::set($cacheKey, $datas, rand(3, 5));
  117. }
  118. return [
  119. 'list' => isset($datas['data']) ? $datas['data'] : [],
  120. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  121. 'pageSize' => $pageSize
  122. ];
  123. }
  124. /**
  125. * @param $price
  126. * @param $urrency
  127. * @param int $tradeType
  128. * @return string
  129. */
  130. public function getRealPrice($money, $currency = 'CNY', $tradeType = 1)
  131. {
  132. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  133. if ($usdtPrice <= 0) {
  134. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  135. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  136. }
  137. // 人民币与其他币的汇率价格
  138. $currencyPrice = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  139. // 买价
  140. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  141. $buyPrice = moneyFormat($money / $xdPrice, 4); // 星豆价格
  142. $cnyPrice = moneyFormat($buyPrice * $usdtPrice, 4); // 币种价格
  143. if ($tradeType == 1) {
  144. $xdBuyPriceRate = ConfigService::make()->getConfigByCode('trade_buy_price_rate', 0);
  145. $xdBuyPriceRate = $xdBuyPriceRate > 0 && $xdBuyPriceRate < 100 ? $xdBuyPriceRate : 0;
  146. $cnyBuyPrice = moneyFormat($cnyPrice + ($cnyPrice * $xdBuyPriceRate / 100), 4); // 上浮价格
  147. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  148. } else if ($tradeType == 2) {
  149. $xdSellPriceRate = ConfigService::make()->getConfigByCode('trade_sell_price_rate', 0);
  150. $xdSellPriceRate = $xdSellPriceRate > 0 && $xdSellPriceRate < 100 ? $xdSellPriceRate : 0;
  151. $cnyBuyPrice = moneyFormat($cnyPrice - ($cnyPrice * $xdSellPriceRate / 100), 4); // 下浮价格
  152. return moneyFormat($cnyBuyPrice * $currencyPrice, 4); // 汇率价格
  153. }
  154. return moneyFormat($cnyPrice * $currencyPrice, 4); // 汇率价格
  155. }
  156. /**
  157. * 获取信息
  158. * @param $where
  159. * @param array $field
  160. * @return array|mixed
  161. */
  162. public function getInfo($id, $field = [], $userId = 0)
  163. {
  164. if ($id) {
  165. $where = ['a.id' => $id, 'a.mark' => 1];
  166. } else if ($userId) {
  167. $where = ['a.user_id' => $userId, 'a.mark' => 1];
  168. } else {
  169. return false;
  170. }
  171. $defaultField = ['a.id', 'a.user_id', 'a.realname', 'a.name', 'a.mobile', 'a.telegram', 'a.quota', 'a.pay_type', 'a.usdt', 'a.status', 'b.nickname', 'b.avatar'];
  172. $field = $field ? $field : $defaultField;
  173. $info = $this->model->from('acceptor as a')
  174. ->leftjoin('member as b', 'b.id', '=', 'a.user_id')
  175. ->where($where)
  176. ->select($field)
  177. ->first();
  178. $info = $info ? $info->toArray() : [];
  179. if ($info) {
  180. $currency = request()->post('currency', 'CNY');
  181. $tradeType = request()->post('trade_type', 1);
  182. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  183. $cnyPrice = $this->getRealPrice(1, $currency, $tradeType);
  184. $info['currency_price'] = $this->getRealPrice(1, $currency, 0);
  185. $info['currency_quota'] = $this->getRealPrice(floatval($info['quota']), $currency, 3);
  186. $info['currency_rate'] = $currency == 'CNY' ? 1 : WalletService::make()->getRateByCNY($currency);
  187. $info['usdt_price'] = moneyFormat(1 / $xdPrice, 2);
  188. $info['price'] = $cnyPrice;
  189. $info['avatar'] = get_image_url($info['avatar']);
  190. }
  191. return $info;
  192. }
  193. /**
  194. * 修改信息
  195. * @param $userId
  196. * @param $params
  197. * @return array|false|int[]
  198. */
  199. public function saveInfo($userId, $params)
  200. {
  201. // 验证是否入驻过和入驻状态
  202. $info = $this->model->where(['user_id' => $userId])->select('id', 'realname', 'trade_status', 'status', 'mark')->first();
  203. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  204. $mark = isset($info['mark']) ? $info['mark'] : 0;
  205. $id = isset($info['id']) ? $info['id'] : 0;
  206. if ($userId && empty($info)) {
  207. $this->error = 2216;
  208. return false;
  209. }
  210. // 是否被冻结
  211. if ($id && $tradeStatus == 4 && $mark) {
  212. $this->error = 2202;
  213. return false;
  214. }
  215. // 验证账户是否正常
  216. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  217. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  218. if (empty($userInfo) || $status != 1) {
  219. $this->error = 2017;
  220. return false;
  221. }
  222. // 入驻数据
  223. $data = [
  224. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  225. 'user_id' => $userId,
  226. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  227. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  228. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  229. 'create_time' => time(),
  230. 'update_time' => time(),
  231. 'mark' => 1,
  232. ];
  233. // 写入数据
  234. if ($id) {
  235. if ($this->model->where(['id' => $id])->update($data)) {
  236. $this->error = 2228;
  237. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  238. return ['id' => $id];
  239. } else {
  240. $this->error = 2229;
  241. return false;
  242. }
  243. } else {
  244. if ($merchId = $this->model->insertGetId($data)) {
  245. $this->error = 2228;
  246. RedisService::keyDel("caches:acceptor:info:temp_{$userId}*");
  247. return ['id' => $id];
  248. } else {
  249. $this->error = 2229;
  250. return false;
  251. }
  252. }
  253. }
  254. /**
  255. * 申请入驻
  256. * @param $userId
  257. * @param $params
  258. * @return array|false|int[]
  259. */
  260. public function apply($userId, $params)
  261. {
  262. // 验证是否入驻过和入驻状态
  263. $info = $this->model->where(['user_id' => $userId])->select('id', 'name', 'trade_status', 'status', 'mark')->first();
  264. $status = isset($info['status']) ? $info['status'] : 0;
  265. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  266. $mark = isset($info['mark']) ? $info['mark'] : 0;
  267. $id = isset($info['id']) ? $info['id'] : 0;
  268. if ($id && $status == 2 && $mark) {
  269. $this->error = 2201;
  270. return false;
  271. }
  272. // 是否被冻结
  273. if ($id && $tradeStatus == 4 && $mark) {
  274. $this->error = 2202;
  275. return false;
  276. }
  277. // 验证账户是否正常
  278. $userInfo = MemberService::make()->getCacheInfo(['id' => $userId], ['id', 'status']);
  279. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  280. if (empty($userInfo) || $status != 1) {
  281. $this->error = 2017;
  282. return false;
  283. }
  284. // 支付方式验证
  285. $payType = isset($params['pay_type']) && $params['pay_type'] ? intval($params['pay_type']) : 1;
  286. $payment = MemberBankModel::where(['user_id' => $userId, 'type' => $payType, 'status' => 1, 'mark' => 1])->value('id');
  287. if ($payment <= 0) {
  288. $this->error = 3011;
  289. return false;
  290. }
  291. // 入驻数据
  292. $data = [
  293. 'name' => isset($params['name']) ? $params['name'] : '',
  294. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  295. 'user_id' => $userId,
  296. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
  297. 'telegram' => isset($params['telegram']) ? $params['telegram'] : '',
  298. 'pay_type' => $payType,
  299. 'price_float' => isset($params['price_float']) ? $params['price_float'] : '',
  300. 'create_time' => time(),
  301. 'update_time' => time(),
  302. 'quota' => 0,
  303. 'trade_status' => 1,
  304. 'status' => 1,
  305. 'mark' => 1,
  306. ];
  307. // 写入数据
  308. if ($id) {
  309. if ($this->model->where(['id' => $id])->update($data)) {
  310. $this->error = 3012;
  311. return ['id' => $id];
  312. } else {
  313. $this->error = 3013;
  314. return false;
  315. }
  316. } else {
  317. if ($id = $this->model->insertGetId($data)) {
  318. $this->error = 3014;
  319. return ['id' => $id];
  320. } else {
  321. $this->error = 3013;
  322. return false;
  323. }
  324. }
  325. }
  326. /**
  327. * 获取入驻信息
  328. * @param $userId
  329. * @return mixed
  330. */
  331. public function getApplyInfo($userId)
  332. {
  333. $info = $this->model->where(['user_id' => $userId, 'mark' => 1])
  334. ->orderBy('id', 'desc')
  335. ->first();
  336. $info = $info ? $info->setHidden(['quota', 'update_time', 'mark'])->toArray() : [];
  337. if ($info) {
  338. $payments = [1 => '微信支付', 2 => '支付宝支付', 3 => '银行卡支付'];
  339. $info['pay_name'] = isset($payments[$info['pay_type']]) ? $payments[$info['pay_type']] : '';
  340. }
  341. return $info;
  342. }
  343. /**
  344. * 充值交易额度
  345. * @param $userId 用户ID
  346. * @param $params
  347. * @return array|false
  348. */
  349. public function rechargeQuota($userId, $params)
  350. {
  351. // $acceptId = isset($params['id'])? $params['id'] : 0;
  352. $usdt = isset($params['usdt']) ? floatval($params['usdt']) : 0;
  353. if ($usdt <= 0) {
  354. $this->error = 2031;
  355. return false;
  356. }
  357. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  358. ->select(['id', 'realname', 'mobile', 'usdt', 'quota', 'trade_status', 'status'])
  359. ->first();
  360. $quota = isset($info['quota']) ? $info['quota'] : 0;
  361. $status = isset($info['status']) ? $info['status'] : 0;
  362. $acceptId = isset($info['id']) ? $info['id'] : 0;
  363. $userInfo = isset($info['member']) ? $info['member'] : [];
  364. if ($userId <= 0 || empty($info) || $status != 2) {
  365. $this->error = 2015;
  366. return false;
  367. }
  368. // 充值订单
  369. DB::beginTransaction();
  370. $orderNo = get_order_num('DP');
  371. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  372. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  373. $money = round($xdPrice * $usdt, 2);
  374. $data = [
  375. 'order_no' => $orderNo,
  376. 'user_id' => $acceptId,
  377. 'type' => 1,
  378. 'user_type' => 3,
  379. 'coin_type' => 6,
  380. 'money' => $usdt,
  381. 'actual_money' => $money,
  382. 'pay_type' => 10,
  383. 'pay_status' => 20,
  384. 'trc_url' => isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '',
  385. 'pay_at' => date('Y-m-d H:i:s'),
  386. 'date' => date('Y-m-d'),
  387. 'create_time' => time(),
  388. 'update_time' => time(),
  389. 'status' => 2,
  390. 'mark' => 1,
  391. ];
  392. if (!$orderId = BalanceLogModel::insertGetId($data)) {
  393. $this->error = 2033;
  394. DB::rollBack();
  395. return false;
  396. }
  397. // 扣除usdt余额
  398. $updateData = ['usdt' => DB::raw("usdt - {$usdt}"), 'update_time' => time()];
  399. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  400. $this->error = 2036;
  401. DB::rollBack();
  402. return false;
  403. }
  404. // 用户明细
  405. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  406. $log = [
  407. 'user_id' => $userId,
  408. 'source_id' => $acceptId,
  409. 'source_order_no' => $data['order_no'],
  410. 'user_type' => 1,
  411. 'type' => 5,
  412. 'coin_type' => 1,
  413. 'money' => -$usdt,
  414. 'actual_money' => -$usdt,
  415. 'balance' => $usdt,
  416. 'date' => date('Y-m-d'),
  417. 'create_time' => time(),
  418. 'remark' => '交易额度充值扣除',
  419. 'status' => 1,
  420. 'mark' => 1
  421. ];
  422. if (!AccountLogModel::insertGetId($log)) {
  423. $this->error = 2029;
  424. DB::rollBack();
  425. return false;
  426. }
  427. // 额度增加
  428. $updateData = ['quota' => DB::raw("quota + {$money}"), 'update_time' => time()];
  429. if (!$this->model->where(['id' => $userId])->update($updateData)) {
  430. $this->error = 2036;
  431. DB::rollBack();
  432. return false;
  433. }
  434. // 额度明细
  435. $log = [
  436. 'user_id' => $acceptId,
  437. 'source_id' => $userId,
  438. 'source_order_no' => $data['order_no'],
  439. 'user_type' => 3,
  440. 'type' => 5,
  441. 'coin_type' => 6,
  442. 'money' => $money,
  443. 'actual_money' => $money,
  444. 'balance' => $quota,
  445. 'date' => date('Y-m-d'),
  446. 'create_time' => time(),
  447. 'remark' => '交易额度充值',
  448. 'status' => 1,
  449. 'mark' => 1
  450. ];
  451. if (!AccountLogModel::insertGetId($log)) {
  452. $this->error = 2029;
  453. DB::rollBack();
  454. return false;
  455. }
  456. // 消息
  457. $dateTime = date('Y-m-d H:i:s');
  458. MessageService::make()->pushMessage($userId, '交易额度充值成功', "您在{$dateTime}(UTC+8)成功支付{$usdt}USDT充值{$money}交易额度", 3);
  459. DB::commit();
  460. $this->error = 2037;
  461. return ['order_id' => $orderId, 'quota' => moneyFormat($quota + $money, 2), 'usdt' => moneyFormat($userUsdt - $usdt, 2)];
  462. }
  463. /**
  464. * 交易额度提现处理
  465. * @param $userId 用户
  466. * @param $params
  467. * @return bool
  468. */
  469. public function withdrawQuota($userId, $params)
  470. {
  471. $money = isset($params['money']) ? $params['money'] : 0;
  472. $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0;
  473. if ($money <= 0) {
  474. $this->error = 2039;
  475. return false;
  476. }
  477. $info = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  478. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status'])
  479. ->first();
  480. $usdt = isset($info['usdt']) ? floatval($info['usdt']) : 0;
  481. $quota = isset($info['quota']) ? floatval($info['quota']) : 0;
  482. $status = isset($info['status']) ? $info['status'] : 0;
  483. $tradeStatus = isset($info['trade_status']) ? $info['trade_status'] : 0;
  484. $acceptId = isset($info['accept_id']) ? $info['accept_id'] : 0;
  485. $userInfo = isset($info['member']) ? $info['member'] : [];
  486. if ($acceptId <= 0 || empty($info) || $status != 2) {
  487. $this->error = 2039;
  488. return false;
  489. }
  490. if (in_array($tradeStatus, [2, 4])) {
  491. $this->error = 2015;
  492. return false;
  493. }
  494. // 提现账户
  495. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  496. if (empty($trcUrl)) {
  497. $this->error = 2403;
  498. return false;
  499. }
  500. DB::beginTransaction();
  501. // USDT 提现
  502. $payUsdt = 0;
  503. $orderNo = get_order_num('DP');
  504. if ($coinType == 1) {
  505. $payUsdt = $money;
  506. if ($money > $usdt) {
  507. $this->error = lang(2040, ['money' => $usdt]);
  508. return false;
  509. }
  510. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  511. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  512. DB::rollBack();
  513. $this->error = 2042;
  514. return false;
  515. }
  516. // 明细
  517. $log = [
  518. 'user_id' => $acceptId,
  519. 'source_id' => $userId,
  520. 'source_order_no' => $orderNo,
  521. 'user_type' => 3,
  522. 'type' => 5,
  523. 'coin_type' => 1,
  524. 'money' => -$usdt,
  525. 'actual_money' => -$usdt,
  526. 'balance' => $usdt,
  527. 'date' => date('Y-m-d'),
  528. 'create_time' => time(),
  529. 'remark' => '交易余额提现',
  530. 'status' => 1,
  531. 'mark' => 1
  532. ];
  533. if (!AccountLogModel::insertGetId($log)) {
  534. $this->error = 2029;
  535. DB::rollBack();
  536. return false;
  537. }
  538. } // 额度提现
  539. else if ($coinType == 6) {
  540. // 时间限制
  541. $limitTime = ConfigService::make()->getConfigByCode('quota_withdraw_time');
  542. $limitTime = $limitTime >= 0 ? $limitTime : 0;
  543. $lockQuota = BalanceLogModel::where(['user_id' => $acceptId, 'user_type' => 3, 'coin_type' => 6, 'status' => 1, 'mark' => 1])
  544. ->where('pay_at', '>=', date('Y-m-d H:i:s', time() - $limitTime * 86400))
  545. ->sum('actual_money');
  546. if ($lockQuota > 0 && $money > ($quota - $lockQuota)) {
  547. $this->error = lang(2040, ['money' => ($quota - $lockQuota)]);
  548. return false;
  549. }
  550. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  551. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  552. $payUsdt = round($money / $xdPrice, 4);
  553. $updateData = ['quota' => DB::raw("quota - {$money}"), 'update_time' => time()];
  554. if (!$this->model->where(['id' => $acceptId])->update($updateData)) {
  555. DB::rollBack();
  556. $this->error = 2042;
  557. return false;
  558. }
  559. // 明细
  560. $log = [
  561. 'user_id' => $acceptId,
  562. 'source_id' => $userId,
  563. 'source_order_no' => $orderNo,
  564. 'user_type' => 3,
  565. 'type' => 5,
  566. 'coin_type' => 6,
  567. 'money' => -$money,
  568. 'actual_money' => -$money,
  569. 'balance' => $quota,
  570. 'date' => date('Y-m-d'),
  571. 'create_time' => time(),
  572. 'remark' => '交易额度提现',
  573. 'status' => 1,
  574. 'mark' => 1
  575. ];
  576. if (!AccountLogModel::insertGetId($log)) {
  577. $this->error = 2029;
  578. DB::rollBack();
  579. return false;
  580. }
  581. }
  582. // 提现记录
  583. $data = [
  584. 'order_no' => $orderNo,
  585. 'user_id' => $acceptId,
  586. 'type' => 2,
  587. 'coin_type' => $coinType,
  588. 'user_type' => 3,
  589. 'money' => $money,
  590. 'actual_money' => $payUsdt,
  591. 'balance' => $coinType == 1 ? $usdt : $quota,
  592. 'date' => date('Y-m-d'),
  593. 'trc_url' => $trcUrl,
  594. 'create_time' => time(),
  595. 'update_time' => time(),
  596. 'status' => 1,
  597. 'mark' => 1,
  598. ];
  599. if (!$id = BalanceLogModel::insertGetId($data)) {
  600. DB::rollBack();
  601. $this->error = 2042;
  602. return false;
  603. }
  604. DB::commit();
  605. // 站内消息
  606. $dateTime = date('Y-m-d H:i:s');
  607. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度成功,请耐心等候审核!!!";
  608. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现申请成功' : '交易额度提现申请成功', $message);
  609. // 提现自动审核,低于该金额自动审核
  610. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  611. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  612. if ($payUsdt <= $autoCheckUsdt) {
  613. // 打款处理
  614. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $payUsdt);
  615. $txID = isset($result['txId']) ? $result['txId'] : '';
  616. $payAddress = isset($result['address']) ? $result['address'] : '';
  617. if ($txID && $payAddress) {
  618. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '自动审核打款', 'status' => 2, 'update_time' => time()];
  619. if (BalanceLogModel::where(['user_id' => $acceptId, 'order_no' => $orderNo])->update($updateData)) {
  620. $message = $coinType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT交易余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}交易额度审核成功,请耐心等候打款到账!!!";
  621. MessageService::make()->pushMessage($userId, $coinType == 1 ? '交易余额提现审核成功' : '交易额度提现审核成功', $message);
  622. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  623. }
  624. }
  625. }
  626. $this->error = 2043;
  627. return true;
  628. }
  629. /**
  630. * 余额提现
  631. * @param $userId
  632. * @param $params
  633. * @return array|false
  634. */
  635. public function withdraw($userId, $params)
  636. {
  637. $money = isset($params['money']) ? floatval($params['money']) : 0;
  638. $payPassword = isset($params['pay_password']) ? trim($params['pay_password']) : '';
  639. $userType = isset($params['user_type']) && $params['user_type'] ? intval($params['user_type']) : 1;
  640. if ($money <= 0) {
  641. $this->error = 2401;
  642. return false;
  643. }
  644. if (!in_array($userType, [1, 2, 3])) {
  645. $this->error = 2404;
  646. return false;
  647. }
  648. $acceptor = $this->model->with(['member'])->where(['user_id' => $userId, 'mark' => 1])
  649. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status'])
  650. ->first();
  651. // 获取承兑商信息
  652. $userInfo = isset($acceptor['member']) ? $acceptor['member'] : [];
  653. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  654. $userUsdt = isset($userInfo['usdt']) ? $userInfo['usdt'] : 0;
  655. if (isset($userInfo['pay_password'])) {
  656. $userPayPassword = $userInfo['pay_password'];
  657. } else {
  658. $userPayPassword = DB::table('member')->where('id', $userId)->value('pay_password');
  659. }
  660. $acceptorUsdt = isset($acceptor['usdt']) ? $acceptor['usdt'] : 0;
  661. $acceptorId = isset($acceptor['id']) ? $acceptor['id'] : 0;
  662. $acceptorTradeStatus = isset($acceptor['trade_status']) ? $acceptor['trade_status'] : 0;
  663. if (empty($userInfo) || $status != 1) {
  664. $this->error = 2024;
  665. return false;
  666. }
  667. if ($userType == 3 && (empty($acceptor) || !in_array($acceptorTradeStatus, [1, 2]))) {
  668. $this->error = 2024;
  669. return false;
  670. }
  671. // 提现金额验证
  672. $accountUsdt = $userType == 1 ? $userUsdt : $acceptorUsdt;
  673. if ($money > $accountUsdt) {
  674. $this->error = web_lang(2402, ['money' => $accountUsdt]);
  675. return false;
  676. }
  677. // 提现账户
  678. $trcUrl = isset($userInfo['trc_url']) ? $userInfo['trc_url'] : '';
  679. if (empty($trcUrl)) {
  680. $this->error = 2403;
  681. return false;
  682. }
  683. // 提现验证usdt钱包余额
  684. $balance = (new \App\Services\WalletService)->getTrcUsdtBalance($userInfo['trc_url']);
  685. if($balance < $money){
  686. $this->error = web_lang(2402, ['trcUsdtBalance' => $balance]);
  687. return false;
  688. }
  689. if ($userPayPassword != get_password($payPassword)) {
  690. $this->error = 2038;
  691. return false;
  692. }
  693. $cacheKey = "caches:member:withdraw:lock_{$userId}";
  694. if (RedisService::get($cacheKey)) {
  695. $this->error = 1034;
  696. return false;
  697. }
  698. DB::beginTransaction();
  699. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  700. // 提现记录
  701. $orderNo = get_order_num('DW');
  702. $feeRate = ConfigService::make()->getConfigByCode('withdraw_fee_rate', 5);
  703. $feeRate = $feeRate > 0 && $feeRate < 100 ? moneyFormat($feeRate / 100, 2) : 0;
  704. $fee = round($money * $feeRate, 2);
  705. $realUsdt = moneyFormat($money - $fee, 2);
  706. $data = [
  707. 'order_no' => $orderNo,
  708. 'user_id' => $userId,
  709. 'type' => 2,
  710. 'user_type' => $userType,
  711. 'coin_type' => 1,
  712. 'money' => $money,
  713. 'actual_money' => $realUsdt,
  714. 'fee' => $fee,
  715. 'trc_url' => $trcUrl,
  716. 'pay_type' => 20,
  717. 'date' => date('Y-m-d'),
  718. 'create_time' => time(),
  719. 'update_time' => time(),
  720. 'status' => 1,
  721. 'mark' => 1,
  722. ];
  723. if (!$id = BalanceLogModel::insertGetId($data)) {
  724. DB::rollBack();
  725. $this->error = 2405;
  726. RedisService::clear($cacheKey);
  727. return false;
  728. }
  729. // 商户扣款
  730. if ($userType == 3) {
  731. $updateData = [
  732. 'usdt' => DB::raw("usdt - {$money}"),
  733. 'update_time' => time()
  734. ];
  735. if (!AcceptorModel::where(['user_id' => $userId, 'mark' => 1])->update($updateData)) {
  736. DB::rollBack();
  737. $this->error = 2406;
  738. RedisService::clear($cacheKey);
  739. return false;
  740. }
  741. // 明细
  742. $log = [
  743. 'user_id' => $acceptorId,
  744. 'source_id' => $userId,
  745. 'source_order_no' => $orderNo,
  746. 'type' => 5,
  747. 'coin_type' => 1,
  748. 'user_type' => 2,
  749. 'money' => -$money,
  750. 'actual_money' => -$money,
  751. 'balance' => $acceptorUsdt,
  752. 'create_time' => time(),
  753. 'update_time' => time(),
  754. 'remark' => "承兑商账户余额提现",
  755. 'status' => 1,
  756. 'mark' => 1,
  757. ];
  758. if (!AccountLogModel::insertGetId($log)) {
  759. $this->error = 2407;
  760. DB::rollBack();
  761. RedisService::clear($cacheKey);
  762. return false;
  763. }
  764. } // 用户扣款
  765. else {
  766. $updateData = ['usdt' => DB::raw("usdt - {$money}"), 'update_time' => time()];
  767. if (!MemberModel::where(['id' => $userId, 'mark' => 1])->update($updateData)) {
  768. DB::rollBack();
  769. $this->error = 2406;
  770. RedisService::clear($cacheKey);
  771. return false;
  772. }
  773. // 明细
  774. $log = [
  775. 'user_id' => $userId,
  776. 'source_id' => 0,
  777. 'source_order_no' => $orderNo,
  778. 'type' => 5,
  779. 'coin_type' => 1,
  780. 'user_type' => 1,
  781. 'money' => -$money,
  782. 'actual_money' => -$money,
  783. 'balance' => $userUsdt,
  784. 'create_time' => time(),
  785. 'update_time' => time(),
  786. 'remark' => "USDT余额提现",
  787. 'status' => 1,
  788. 'mark' => 1,
  789. ];
  790. if (!AccountLogModel::insertGetId($log)) {
  791. $this->error = 2407;
  792. DB::rollBack();
  793. RedisService::clear($cacheKey);
  794. return false;
  795. }
  796. }
  797. DB::commit();
  798. // 站内消息
  799. $dateTime = date('Y-m-d H:i:s');
  800. $title = $userType == 1 ? 'USDT余额提现申请成功' : '承兑商余额提现申请成功';
  801. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额成功,请耐心等候审核!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额成功,请耐心等候审核!!!";
  802. MessageService::make()->pushMessage($userId, $title, $message);
  803. // 提现自动审核,低于该金额自动审核
  804. $autoCheckUsdt = ConfigService::make()->getConfigByCode('withdraw_auto_money', 300);
  805. $autoCheckUsdt = $autoCheckUsdt > 0 ? $autoCheckUsdt : 0;
  806. if ($money <= $autoCheckUsdt) {
  807. // 打款处理
  808. $result = WalletService::make()->usdtTrcTransfer($trcUrl, $realUsdt);
  809. $txID = isset($result['txId']) ? $result['txId'] : '';
  810. $payAddress = isset($result['address']) ? $result['address'] : '';
  811. if ($txID && $payAddress) {
  812. $updateData = ['hash' => $txID, 'wallet_url' => $payAddress, 'audit_remark' => '自动审核打款', 'status' => 2, 'update_time' => time()];
  813. if (BalanceLogModel::where(['order_no' => $orderNo, 'user_type' => $userType])->update($updateData)) {
  814. $title = $userType == 1 ? 'USDT余额提现审核成功' : '承兑商余额提现审核成功';
  815. $message = $userType == 1 ? "您在{$dateTime}(UTC+8)申请提现{$money}USDT余额审核成功,请耐心等候打款到账!!!" : "您在{$dateTime}(UTC+8)申请提现{$money}USDT承兑商余额审核成功,请耐心等候打款到账!!!";
  816. MessageService::make()->pushMessage($userId, $title, $message, 3);
  817. AccountLogModel::where(['source_order_no' => $orderNo])->update(['hash' => $txID, 'update_time' => time()]);
  818. // 平台明细
  819. $log = [
  820. 'user_id' => 0,
  821. 'source_id' => $userId,
  822. 'source_order_no' => $orderNo,
  823. 'type' => 5,
  824. 'coin_type' => 1,
  825. 'user_type' => 4,
  826. 'money' => $fee,
  827. 'actual_money' => $fee,
  828. 'balance' => 0,
  829. 'create_time' => time(),
  830. 'update_time' => time(),
  831. 'hash' => $txID,
  832. 'remark' => "USDT余额提现",
  833. 'status' => 1,
  834. 'mark' => 1,
  835. ];
  836. AccountLogModel::insertGetId($log);
  837. // 平台流水
  838. FinanceService::make()->saveLog(0, $fee, 1);
  839. }
  840. }
  841. }
  842. $this->error = $title;
  843. RedisService::clear($cacheKey);
  844. return [
  845. 'id' => $id,
  846. 'money' => $money,
  847. 'user_type' => $userType,
  848. ];
  849. }
  850. /**
  851. * C2C交易-购买
  852. * 1.当前登录用户购买
  853. * 2.承兑商卖
  854. * 用户买,则扣承兑商额度,用户付款,承兑商收款然后把订单交易的豆给用户
  855. * 记录trade记录
  856. * @param $userId
  857. * @param $params
  858. * @return array|false
  859. */
  860. public function buyxd($userId, $params)
  861. {
  862. $cacheKey = "caches:acceptor:buyxd:lock_{$userId}";
  863. if (RedisService::get($cacheKey)) {
  864. $this->error = 1034;
  865. return false;
  866. }
  867. // 购买用户信息
  868. $userInfo = $pointInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  869. ->select(['id', 'usdt', 'balance', 'member_level', 'wait_score', 'pay_password', 'status'])
  870. ->first();
  871. // 承兑商信息
  872. $acceptor = $this->model->with(['member'])->where(['id' => $params['acceptor_id'], 'mark' => 1])
  873. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status', 'pay_type'])
  874. ->first();
  875. if (empty($userInfo) || $userInfo['status'] != 1) {
  876. $this->error = 2024;
  877. return false;
  878. }
  879. if ($userInfo['pay_password'] != get_password($params['pay_password'])) {
  880. $this->error = 2038;
  881. return false;
  882. }
  883. // C2C交易金豆
  884. $orderNo = get_order_num('C2C');
  885. // 卖家1星豆人民币对比价格
  886. $cnyPrice = $this->getRealPrice(1, 'CNY', 1);
  887. $money = moneyFormat($this->getRealPrice($params['quota'], 'CNY', 1), 2, true);
  888. if (floatval($params['money']) != $money) {
  889. $this->error = 2028;
  890. return false;
  891. }
  892. // 星豆
  893. DB::beginTransaction();
  894. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  895. // 承兑商收款信息
  896. $memberBank = MemberBankModel::where(['user_id' => $acceptor['user_id'], 'type' => $acceptor['pay_type'], 'mark' => 1])->first();
  897. // 兑换usdt费率价格
  898. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  899. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  900. $usdt_rate = round(1 / $xdPrice, 4);
  901. // 承兑商额外佣金金豆转u
  902. $acceptorCommissionRate = ConfigService::make()->getConfigByCode('acceptor_commission_rate', 100);
  903. // 保留4位小数点
  904. $bonus_usdt = round($money * $acceptorCommissionRate * 10000)/1000000;
  905. // 添加交易订单记录
  906. $data = [
  907. 'user_id' => $userId,
  908. 'acceptor_id' => $params['acceptor_id'],
  909. 'acceptor_uid' => $acceptor['user_id'],
  910. 'order_no' => $orderNo,
  911. 'type' => 1,
  912. 'coin_type' => 1,
  913. 'price' => $cnyPrice,
  914. 'num' => $params['quota'],
  915. 'account_id' => isset($memberBank) ? $memberBank['id'] : 0,
  916. 'mobile' => $acceptor['mobile'],
  917. 'real_name' => $acceptor['realname'],
  918. 'total' => $money,
  919. 'usdt_rate' => $usdt_rate,
  920. 'currency' => $params['currency'],
  921. 'pay_type' => $params['pay_type'],
  922. 'pay_money' => $params['money'],
  923. 'pay_status' => 2,
  924. 'pay_time' => now(),
  925. 'bonus_usdt' => $bonus_usdt,// 承兑商佣金金豆转u
  926. 'pay_img' => '',
  927. 'create_time' => now(),
  928. 'update_time' => now(),
  929. 'is_settle' => 2,
  930. 'exception_status' => 0,
  931. 'sell_exception_img' => '',
  932. 'exception_img' => '',
  933. 'exception_remark' => '',
  934. 'remark' => '购买下单成功,待支付',
  935. 'status' => 1,
  936. 'mark' => 1,
  937. ];
  938. if (!$orderId = TradeModel::insertGetId($data)) {
  939. RedisService::clear($cacheKey);
  940. $this->error = 2033;
  941. DB::rollBack();
  942. return false;
  943. }
  944. // 扣除承兑商星豆
  945. $updateData = ['balance' => DB::raw("balance - {$params['quota']}"), 'update_time' => time()];
  946. if (!MemberModel::where(['id' => $acceptor['member']['id']])->update($updateData)) {
  947. $this->error = 2036;
  948. DB::rollBack();
  949. return false;
  950. }
  951. // 增加accountLog记录
  952. $log = [
  953. 'user_id' => $userId,
  954. 'source_id' => $orderId,
  955. 'source_order_no' => $orderNo,
  956. 'type' => 100,
  957. 'coin_type' => 2,
  958. 'user_type' => 1,
  959. 'money' => round($params['quota'] / $xdPrice, 4),
  960. 'actual_money' => $params['quota'],
  961. 'balance' => $userInfo['balance'],
  962. 'create_time' => time(),
  963. 'update_time' => time(),
  964. 'remark' => "C2C交易-购买星豆",
  965. 'status' => 2,
  966. 'mark' => 1,
  967. ];
  968. if (!AccountLogModel::insertGetId($log)) {
  969. $this->error = 2407;
  970. DB::rollBack();
  971. RedisService::clear($cacheKey);
  972. return false;
  973. }
  974. // 消息
  975. $dateTime = date('Y-m-d H:i:s');
  976. MessageService::make()->pushMessage($userId, '星豆购买下单成功', "您在{$dateTime}(UTC+8)成功下单¥{$money}购买{$params['quota']}星豆,请在20分钟内确认支付,否则订单将失效", 3);
  977. DB::commit();
  978. $this->error = 2037;
  979. RedisService::clear($cacheKey);
  980. return ['trade_id' => $orderId, 'money' => $money];
  981. }
  982. /**
  983. * C2C交易-卖出
  984. * 1.当前登录用户购买
  985. * 2.承兑商卖
  986. * 用户买,则扣承兑商额度,用户付款,承兑商收款然后把订单交易的豆给用户
  987. * 记录trade记录
  988. * @param $userId
  989. * @param $params
  990. * @return array|false
  991. */
  992. public function sellxd($userId, $params)
  993. {
  994. $cacheKey = "caches:acceptor:sellxd:lock_{$userId}";
  995. if (RedisService::get($cacheKey)) {
  996. $this->error = 1034;
  997. return false;
  998. }
  999. // 卖出用户信息
  1000. $userInfo = $pointInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  1001. ->select(['id', 'usdt', 'balance', 'member_level', 'wait_score', 'pay_password', 'status'])
  1002. ->first();
  1003. // 承兑商信息
  1004. $acceptor = $this->model->with(['member'])->where(['id' => $params['acceptor_id'], 'mark' => 1])
  1005. ->select(['id', 'user_id', 'realname', 'mobile', 'usdt', 'quota', 'status', 'trade_status'])
  1006. ->first();
  1007. if (empty($userInfo) || $userInfo['status'] != 1) {
  1008. $this->error = 2024;
  1009. return false;
  1010. }
  1011. if ($userInfo['pay_password'] != get_password($params['pay_password'])) {
  1012. $this->error = 2038;
  1013. return false;
  1014. }
  1015. // C2C交易金豆
  1016. $orderNo = get_order_num('C2C');
  1017. // 卖家1星豆人民币对比价格
  1018. $cnyPrice = $this->getRealPrice(1, 'CNY', 1);
  1019. $money = moneyFormat($this->getRealPrice($params['quota'], 'CNY', 1), 2, true);
  1020. // if (floatval($params['money']) != $money) {
  1021. // $this->error = 2028;
  1022. // return false;
  1023. // }
  1024. // 星豆
  1025. DB::beginTransaction();
  1026. RedisService::set($cacheKey, $userInfo, rand(2, 3));
  1027. // 用户收款信息
  1028. $memberBank = MemberBankModel::where(['user_id' => $userId, 'type' => $acceptor['pay_type']])->first();
  1029. // 兑换usdt费率价格
  1030. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  1031. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  1032. $usdt_rate = round(1 / $xdPrice, 4);
  1033. // 承兑商额外佣金金豆转u
  1034. $acceptorCommissionRate = ConfigService::make()->getConfigByCode('acceptor_commission_rate', 100);
  1035. $bonus_usdt = round(($money * ($acceptorCommissionRate/100)) * 10000)/10000;
  1036. // 添加交易订单记录
  1037. $data = [
  1038. 'user_id' => $userId,
  1039. 'acceptor_id' => $params['acceptor_id'],
  1040. 'acceptor_uid' => $acceptor['user_id'],
  1041. 'order_no' => $orderNo,
  1042. 'type' => 2,
  1043. 'coin_type' => 1,
  1044. 'price' => $cnyPrice,
  1045. 'num' => $params['quota'],
  1046. 'account_id' => isset($memberBank) ? $memberBank['id'] : 0,
  1047. 'mobile' => $acceptor['mobile'],
  1048. 'real_name' => $acceptor['realname'],
  1049. 'total' => $money,
  1050. 'usdt_rate' => $usdt_rate,
  1051. 'currency' => $params['currency'],
  1052. 'pay_type' => $params['pay_type'],
  1053. 'pay_money' => $params['money'],
  1054. 'pay_status' => 2,
  1055. 'pay_time' => now(),
  1056. 'bonus_usdt' => $bonus_usdt,// 承兑商佣金金豆转u
  1057. 'pay_img' => '',
  1058. 'create_time' => now(),
  1059. 'update_time' => now(),
  1060. 'is_settle' => 2,
  1061. 'exception_status' => 0,
  1062. 'sell_exception_img' => '',
  1063. 'exception_img' => '',
  1064. 'exception_remark' => '',
  1065. 'remark' => '卖出下单成功,待支付',
  1066. 'status' => 1,
  1067. 'mark' => 1,
  1068. ];
  1069. if (!$orderId = TradeModel::insertGetId($data)) {
  1070. RedisService::clear($cacheKey);
  1071. $this->error = 2033;
  1072. DB::rollBack();
  1073. return false;
  1074. }
  1075. // 扣除用户星豆
  1076. $updateData = ['balance' => DB::raw("balance - {$params['quota']}"), 'update_time' => time()];
  1077. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  1078. $this->error = 2036;
  1079. DB::rollBack();
  1080. return false;
  1081. }
  1082. // 增加accountLog记录
  1083. $log = [
  1084. 'user_id' => $userId,
  1085. 'source_id' => $orderId,
  1086. 'source_order_no' => $orderNo,
  1087. 'type' => 101,
  1088. 'coin_type' => 2,
  1089. 'user_type' => 1,
  1090. 'money' => round($params['quota'] / $xdPrice, 4),
  1091. 'actual_money' => $params['quota'],
  1092. 'balance' => $userInfo['balance'],
  1093. 'create_time' => time(),
  1094. 'update_time' => time(),
  1095. 'remark' => "C2C交易-卖出星豆",
  1096. 'status' => 2,
  1097. 'mark' => 1,
  1098. ];
  1099. if (!AccountLogModel::insertGetId($log)) {
  1100. $this->error = 2407;
  1101. DB::rollBack();
  1102. RedisService::clear($cacheKey);
  1103. return false;
  1104. }
  1105. // 消息
  1106. $dateTime = date('Y-m-d H:i:s');
  1107. MessageService::make()->pushMessage($userId, '星豆购买下单成功', "您在{$dateTime}(UTC+8)成功下单¥{$money}购买{$params['quota']}星豆,请在20分钟内确认支付,否则订单将失效", 3);
  1108. DB::commit();
  1109. $this->error = 2037;
  1110. RedisService::clear($cacheKey);
  1111. return ['trade_id' => $orderId, 'money' => $money];
  1112. }
  1113. }