WithdrawService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace addons\CloudStock\common\services;
  3. use addons\CloudStock\common\models\CloudStockAgent;
  4. use common\enums\AddonsEnum;
  5. use common\enums\ApiStatusEnum;
  6. use common\enums\RedisKeyEnum;
  7. use common\enums\StatusEnum;
  8. use common\enums\WithdrawWayEnum;
  9. use common\globals\GlobalInvoke;
  10. use common\helpers\LockHelper;
  11. use common\models\common\PlatformSetting;
  12. use common\models\common\UserBankCard;
  13. use common\models\user\UserWithdrawLog;
  14. use services\common\UserWalletService;
  15. use yii\helpers\Json;
  16. use Exception;
  17. use Yii;
  18. class WithdrawService extends BaseService
  19. {
  20. const LOCK_CLOUD_STOCK_AGENT = 'lock_cloud_stock_agent_';
  21. public function withdraw($params)
  22. {
  23. $params['mall_id'] = $this->mall_id;
  24. $params['user_id'] = $this->user_id;
  25. try {
  26. $res = UserWithdrawLog::withdrawCheck($params);
  27. if (!empty($res) && $res['state'] == true) {
  28. return $res['data'];
  29. }
  30. $res = $this->saveRecord($params);
  31. return $res;
  32. } catch (\Exception $e) {
  33. throw new \Exception($e->getMessage());
  34. }
  35. }
  36. public function saveRecord($params, $extra = [])
  37. {
  38. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  39. if (empty($configs['payment_withdraw'])) {
  40. throw new \Exception('暂未开启货款提现功能');
  41. }
  42. if ($params['price'] <= 0) {
  43. throw new \Exception('提现金额必须大于0');
  44. }
  45. $exists = UserWithdrawLog::find()->where(['mall_id' => $params['mall_id'],
  46. 'status' => StatusEnum::DISABLED,
  47. 'user_id' => $params['user_id'],
  48. 'from' => UserWithdrawLog::FROM_PAYMENT_FOR_GOODS
  49. ])->asArray()->one();
  50. if ($exists) {
  51. throw new \Exception('尚有未审核的提现申请');
  52. }
  53. $cloud_stock_agent = CloudStockAgent::getOne([['user_id' => $this->user_id, 'status' => 1]], true, [], false, 'cloud_warehouse_price');
  54. if ($cloud_stock_agent['cloud_warehouse_price'] < $params['price']) {
  55. throw new \Exception('提现金额超出可提现金额');
  56. }
  57. $withdraw_cash_type = json_decode($configs['withdraw_cash_type'], true);
  58. if (!in_array($params['type'], $withdraw_cash_type)) {
  59. throw new \Exception('不支持该提现方式');
  60. }
  61. $poundage_ratio = isset($configs['payment_withdraw_poundage']) && $configs['payment_withdraw_poundage'] > 0 ? $configs['payment_withdraw_poundage'] : 0;
  62. $cash = new UserWithdrawLog();
  63. $cash->mall_id = $params['mall_id'];
  64. $cash->user_id = $params['user_id'];
  65. $cash->num = $params['price'];
  66. $cash->source = 'CloudStock';
  67. $cash->actual_num = $params['price'];
  68. $cash->content = Json::encode(['user_content' => $params['content']]);
  69. $cash->from = UserWithdrawLog::FROM_PAYMENT_FOR_GOODS;
  70. $cash->type = $params['type'];
  71. $cash->extra = Json::encode([
  72. 'name' => !empty($params['name']) ? $params['name'] : '',
  73. 'mobile' => !empty($params['mobile']) ? $params['mobile'] : '',
  74. 'bank_name' => $params['bank_name'],
  75. 'bank_account' => (string)$params['bank_account'],
  76. 'bank_account_type' => $params['bank_account_type'],
  77. 'wechat_qrcode' => $params['wechat_qrcode'],
  78. 'id_card' => $params['id_card'] ?? '',
  79. 'receive_bank_chanel_no' => $params['receive_bank_chanel_no'] ?? '',
  80. 'x-app-platform' => $params['x-app-platform'] ?? '',
  81. 'bank_address' => !empty($params['bank_address']) ? $params['bank_address'] : '',
  82. 'idcard' => !empty($params['idcard']) ? $params['idcard'] : '',
  83. ], JSON_UNESCAPED_UNICODE);
  84. if ($poundage_ratio < 0 || $poundage_ratio > 100) {
  85. throw new \Exception('手续费设置不正确');
  86. }
  87. //手续费
  88. $poundage = 0;
  89. if ($poundage_ratio > 0 && $poundage_ratio < 100) {
  90. //手续费:取小数点2位,截断
  91. $poundage = bcmul($poundage_ratio / 100, $params['price'], 4);
  92. // 四舍五入,保留2位
  93. $poundage = number_format($poundage, 2, '.', '');
  94. $cash->actual_num = bcsub($params['price'], $poundage, 2);
  95. $cash->poundage_num = $poundage;
  96. $cash->poundage_ratio = $poundage_ratio;
  97. }
  98. if (intval(bcmul($cash->actual_num, 100)) < 1) {
  99. throw new \Exception('提现金额扣除费用后金额不足');
  100. }
  101. $t = \Yii::$app->db->beginTransaction();
  102. try {
  103. $cash->order_no = $cash->getCashOrder($params['mall_id']);
  104. if (!$cash->save()) {
  105. throw new \Exception($cash->getErrorMessage());
  106. }
  107. $res_data = [
  108. 'state' => true,
  109. 'data' => ['cash_id' => $cash->id],
  110. 'msg' => ''
  111. ];
  112. $data = [
  113. 'mall_id' => $params['mall_id'],
  114. 'user_id' => $params['user_id'],
  115. 'money' => -abs($cash->num),
  116. ];
  117. self::handle($data);
  118. $t->commit();
  119. } catch (Exception $e) {
  120. $t->rollBack();
  121. \Yii::error($e->getMessage());
  122. throw new \Exception('提现失败!' . $e->getMessage() . $e->getFile() . $e->getLine());
  123. }
  124. return $res_data;
  125. }
  126. public static function handle($data)
  127. {
  128. $user_id = $data['user_id'];
  129. $lock_key = RedisKeyEnum::suffix(self::LOCK_CLOUD_STOCK_AGENT, $user_id);
  130. $identification = uniqid();
  131. if (LockHelper::lock($lock_key, $identification, 100, 100)) {
  132. $t = \Yii::$app->db->beginTransaction();
  133. try {
  134. $cloud_stock_agent = CloudStockAgent::getOne([['user_id' => $user_id, 'status' => 1]], false, [], false);
  135. if(isset($data['is_frozen']) && $data['is_frozen']){
  136. $cloud_stock_agent->cloud_warehouse_frozen_price += $data['money'];
  137. }else{
  138. $cloud_stock_agent->cloud_warehouse_price += $data['money'];
  139. }
  140. if ($cloud_stock_agent->cloud_warehouse_price < 0) {
  141. throw new \Exception('云仓货款不足');
  142. }
  143. $cloud_stock_agent->save();
  144. $t->commit();
  145. LockHelper::uLock($lock_key, $identification);
  146. return true;
  147. } catch (Exception $e) {
  148. $t->rollBack();
  149. LockHelper::uLock($lock_key, $identification);
  150. self::$static_error = $e->getMessage();
  151. return false;
  152. }
  153. } else {
  154. LockHelper::uLock($lock_key, $identification);
  155. //抢不到锁
  156. Yii::error(__CLASS__ . ' handle error : 抢不到锁');
  157. self::$static_error = '操作太频繁';
  158. return false;
  159. }
  160. }
  161. /**
  162. * 获取提现信息
  163. * @return array
  164. * @throws \Exception
  165. */
  166. public function cashSetting()
  167. {
  168. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  169. if (empty($configs['payment_withdraw'])) {
  170. throw new \Exception('暂未开启货款提现功能');
  171. }
  172. $cloud_stock_agent = CloudStockAgent::getOne([['user_id' => $this->user_id, 'status' => 1]], true, [], false, 'cloud_warehouse_price');
  173. $is_need_redirect = 0;
  174. $repurchase_redirect_url = '';
  175. $repurchase_prompt = '';
  176. $is_hidden_money_unit = 0;
  177. $userMoney = $cloud_stock_agent['cloud_warehouse_price'];
  178. $pay_password_status = 0;
  179. $cash__type_name = [];
  180. $withdraw_cash_type = $configs['withdraw_cash_type'];
  181. $get_system_setting = PlatformSetting::getConfByGroup('system', true);
  182. if (!empty($withdraw_cash_type)) {
  183. $cash_type = Json::decode($withdraw_cash_type);
  184. $default_withdraw_way = WithdrawWayEnum::getDefaultWithDrawWayMap(WithdrawWayEnum::WITHDRAW_TYPE_2);
  185. $extension_withdraw_way = GlobalInvoke::exec(GlobalInvoke::INVOKE_GET_WITHDRAW_EXTENSION_WAY, $this->mall_id, ['mall_id' => $this->mall_id]);
  186. $all_withdraw = array_merge($default_withdraw_way, $extension_withdraw_way);
  187. $cash_type_keys = array_keys($all_withdraw);
  188. if (in_array('bank', $cash_type_keys) || in_array('joinpay', $cash_type_keys)) {
  189. $bank_card_list = UserBankCard::getUserCardList($this->user_id, $this->mall_id);
  190. }
  191. foreach ($cash_type as $i => $item) {
  192. if (isset($all_withdraw[$item])) {
  193. $taxFee = 0;
  194. $name = '';
  195. if (isset($all_withdraw[$item])) {
  196. if (is_array($all_withdraw[$item])) {
  197. $name = $all_withdraw[$item]['name'] ?? '';
  198. $taxFee = $all_withdraw[$item]['taxFee'] ?? 0;
  199. } else {
  200. $name = $all_withdraw[$item];
  201. }
  202. }
  203. $cash__type_name[$i]['type'] = $item;
  204. $cash__type_name[$i]['taxFee'] = $taxFee;
  205. $cash__type_name[$i]['name'] = $name;
  206. $cash__type_name[$i]['img'] = $get_system_setting['api_url']['value'] . '/resources/img/payment/' . $item . '.png';
  207. if ('bank' == $item || 'joinpay' == $item || 'yunfast_pay' == $item) {
  208. $cash__type_name[$i]['bank_card_list'] = $bank_card_list;
  209. }
  210. }
  211. }
  212. }
  213. //检测是否需要实名认证
  214. $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'type' => 'cash']);
  215. if ($need_auth === true) {
  216. //需要先进行实名认证
  217. return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
  218. }
  219. $userWithdrawLog = UserWithdrawLog::getOne([
  220. ['user_id' => $this->user_id],
  221. ['type' => ['alipay_tax', 'alipay']]
  222. ], true, [], 'id desc');
  223. $aliAccountInfo = [];
  224. if (!empty($userWithdrawLog)) {
  225. $extra = json_decode($userWithdrawLog['extra'], true);
  226. if (!empty($extra)) {
  227. $aliAccountInfo = [
  228. 'name' => $extra['name'],
  229. 'mobile' => $extra['mobile'],
  230. 'id_card' => $extra['id_card'] ?? '',
  231. ];
  232. }
  233. }
  234. return [
  235. 'addons_auth' => [],
  236. 'notice' => [
  237. 'content' => '',
  238. 'icon' => ''
  239. ],
  240. 'setting' => [
  241. 'cash_type_name' => $cash__type_name,
  242. 'cash_service_fee' => $configs['payment_withdraw_poundage'] ?? 0,
  243. 'cash_income_service_fee' => $configs['payment_withdraw_poundage'] ?? 0,
  244. 'min_money' => 0,
  245. 'day_max_money' => 0,
  246. 'withdrawal_rule' => '',
  247. 'pay_password_status' => intval($pay_password_status),
  248. 'multiple' => 0,
  249. 'withdraws_limit_enable' => 0,
  250. 'withdraws_limit_money' => 0,
  251. 'is_hidden_money_unit' => $is_hidden_money_unit,
  252. ],
  253. 'user_info' => [
  254. 'income' => $userMoney,
  255. 'is_transaction_password' => false
  256. ],
  257. 'ali_account_info' => $aliAccountInfo,
  258. 'repurchase_limit' => [
  259. 'is_need_redirect' => $is_need_redirect,
  260. 'repurchase_redirect_url' => $repurchase_redirect_url,
  261. 'repurchase_prompt' => $repurchase_prompt
  262. ]
  263. ];
  264. }
  265. public function transferBalance($params)
  266. {
  267. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  268. if (empty($configs['payment_to_balance'])) {
  269. throw new \Exception('暂未开启货款转余额功能');
  270. }
  271. $payment_to_balance_poundage = $configs['payment_to_balance_poundage'] ?? 0;
  272. $user_id = $this->user_id;
  273. $money = $params['money'];
  274. $res = self::handle(['user_id' => $user_id, 'money' => -$money]);
  275. if ($res) {
  276. $poundage = bcdiv($money * $payment_to_balance_poundage, 100, 2);
  277. $desc = '云仓货款转余额,手续费:' . $poundage;
  278. $insert_wallet [] = [
  279. 'mall_id' => $this->mall_id,
  280. 'user_id' => $user_id,
  281. 'is_frozen' => false,
  282. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  283. 'money' => bcsub($money, $poundage, 2),
  284. 'desc' => $desc,
  285. 'source_table' => '',
  286. 'source_table_id' => 0,
  287. 'from_type' => 'CloudStock',
  288. 'capital_type' => 'cloud_stock_payment_transfer',
  289. ];
  290. UserWalletService::batchHandleByQueue($insert_wallet);
  291. }else{
  292. throw new \Exception(self::$static_error);
  293. }
  294. return true;
  295. }
  296. }