WithdrawalService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace backend\modules\mall\services;
  3. use common\enums\AssetsType;
  4. use common\enums\PayTypeEnum;
  5. use common\enums\RedisKeyEnum;
  6. use common\enums\StatusEnum;
  7. use common\enums\UserWalletEnum;
  8. use common\globals\GlobalInvoke;
  9. use common\helpers\LockHelper;
  10. use common\models\common\PaymentTransfer;
  11. use common\models\user\User;
  12. use common\models\user\UserInfo;
  13. use common\models\user\UserWithdrawLog as Withdraw;
  14. use common\models\user\UserWithdrawLog;
  15. use services\common\PayService;
  16. use services\common\UserWalletService;
  17. use yii\helpers\Json;
  18. class WithdrawalService extends \yii\base\BaseObject
  19. {
  20. public $log_id; // 提现记录id
  21. public $mall_id; // mall_id
  22. public $status; // 提现操作类型 1 审核通过 2 打款 3拒绝
  23. public $content = ''; // 备注
  24. public $topay_way = ''; // 打款渠道
  25. public $error = '';
  26. public function error($msg){
  27. $this->error = $msg;
  28. return false;
  29. }
  30. public function __construct ($config = [])
  31. {
  32. if(empty($config['mall_id'])){
  33. throw new \Exception('WithdrawalService mall_id Must have method property');
  34. }
  35. if(empty($config['log_id'])){
  36. throw new \Exception('WithdrawalService log_id Must have method property');
  37. }
  38. foreach ($config as $index=>$name) {
  39. if(!property_exists($this,$index)){
  40. unset($config[$index]);
  41. };
  42. }
  43. parent::__construct($config);
  44. }
  45. public function aliToPay()
  46. {
  47. $list = Withdraw::find()->where(['mall_id'=>$this->mall_id,'id'=>$this->log_id])->all();
  48. if(empty($list)) return $this->error("记录不存在");
  49. foreach ($list as $cash) {
  50. if ( $cash['status'] != 1) return $this->error("{$cash['order_no']}记录状态不正确");
  51. }
  52. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_USER_CASH , 'alitopay_'.$this->mall_id);
  53. $uniqid = uniqid();
  54. // if (!LockHelper::lock($lock_key, $uniqid, 100, 100)) {
  55. // \Yii::error('cashForm 获取锁(' . $lock_key . ')失败阻塞,提现打款:cash,操作:save,金额:' . $cash->id);
  56. // return $this->error("操作频繁,请稍后重试~");
  57. // }
  58. $ids = array_column($list,'id');
  59. $pay_res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_ALITOPAY_PAY,$cash->mall_id,[
  60. 'ids'=>$ids,
  61. 'mall_id'=>$this->mall_id
  62. ]);
  63. if(!$pay_res['res']){
  64. return $this->error($pay_res['data']);
  65. }
  66. // 创建打款订单成功,修改成打款中状态
  67. foreach ($list as $cash) {
  68. // Withdraw::remit_wait($cash,'支付宝打款中...');
  69. // $res = Withdraw::updateAll(['content'=>Json::encode($content),'status'=>5],['id'=>$cash['id']]);
  70. }
  71. return $pay_res["data"];
  72. }
  73. // 代税宝
  74. public function avoidtaxToPay()
  75. {
  76. $list = Withdraw::find()->where(['mall_id'=>$this->mall_id,'id'=>$this->log_id])->all();
  77. if(empty($list)) return $this->error("记录不存在");
  78. foreach ($list as $cash) {
  79. if ( $cash['status'] != 1) return $this->error("{$cash['order_no']}记录状态不正确");
  80. }
  81. // $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_USER_CASH , 'avoidtax_'.$this->mall_id);
  82. // $uniqid = uniqid();
  83. // if (!LockHelper::lock($lock_key, $uniqid, 100, 100)) {
  84. // return $this->error("操作频繁,请稍后重试~");
  85. // }
  86. /* 代税宝 业务 start */
  87. $ids = array_column($list,'id');
  88. // addons\AvoidTax\common\service\OrderService
  89. $pay_res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_AVOIDTAX_PAY,$cash->mall_id,[
  90. 'ids'=>$ids,
  91. 'mall_id'=>$this->mall_id
  92. ]);
  93. if(!$pay_res['res']){
  94. return $this->error($pay_res['data']);
  95. }
  96. /* 代税宝 业务 end */
  97. // 创建打款订单成功,修改成打款中状态
  98. foreach ($list as $cash) {
  99. Withdraw::remit_wait($cash,'代税宝打款中...');
  100. }
  101. return $pay_res["data"];
  102. }
  103. /**
  104. * 银典代付
  105. *
  106. * @return false|array
  107. */
  108. public function yunfastPay()
  109. {
  110. $list = Withdraw::find()->where(['mall_id' => $this->mall_id, 'id' => $this->log_id])->all();
  111. if(empty($list)) return $this->error('记录不存在');
  112. foreach ($list as $cash) {
  113. if ($cash['status'] != 1) return $this->error("{$cash['order_no']}记录状态不正确");
  114. }
  115. $ids = array_column($list, 'id');
  116. $success_ids = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_YUNFAST_PAY, $cash->mall_id, [
  117. 'ids' => $ids,
  118. 'mall_id' => $this->mall_id
  119. ]);
  120. return $success_ids;
  121. }
  122. /**
  123. * 银企直联
  124. *
  125. * @return void
  126. */
  127. public function banktaxToPay()
  128. {
  129. $list = Withdraw::find()
  130. ->where([
  131. 'mall_id' => $this->mall_id,
  132. 'id' => $this->log_id,
  133. 'type' => 'bank_tax',
  134. 'status' => UserWithdrawLog::STATUS_AGREE,
  135. ])
  136. ->all();
  137. if (empty($list)) {
  138. return $this->error('记录不存在');
  139. }
  140. // 合规提现业务
  141. $ids = array_column($list, 'id');
  142. $pay_res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_BANKTAX_PAY, $this->mall_id, [
  143. 'ids' => $ids,
  144. 'mall_id' => $this->mall_id,
  145. ]);
  146. if (!$pay_res['res']) {
  147. return $this->error($pay_res['data']);
  148. }
  149. // 创建打款订单成功,修改成打款中状态
  150. foreach ($list as $cash) {
  151. Withdraw::remit_wait($cash, '银企直联打款中...');
  152. }
  153. return $pay_res['data'];
  154. }
  155. /**
  156. * 点薪灵工
  157. *
  158. * @return void
  159. */
  160. public function linggongToPay($params)
  161. {
  162. $list = Withdraw::find()
  163. ->where([
  164. 'mall_id' => $this->mall_id,
  165. 'id' => $this->log_id,
  166. 'type' => 'linggong',
  167. 'status' => [UserWithdrawLog::STATUS_AGREE, UserWithdrawLog::STATUS_APPLY],
  168. ])
  169. ->all();
  170. if (empty($list)) {
  171. return $this->error('记录不存在');
  172. }
  173. // 合规提现业务
  174. $ids = array_column($list, 'id');
  175. $pay_res = GlobalInvoke::exec(GlobalInvoke::INVOKE_WITHDRAW_LINGGONG_PAY, $this->mall_id, [
  176. 'ids' => $ids,
  177. 'mall_id' => $this->mall_id,
  178. 'task_id' => $params['task_id'],
  179. ]);
  180. if (!$pay_res['res']) {
  181. return $this->error($pay_res['data']);
  182. }
  183. // 创建打款订单成功,修改成打款中状态
  184. foreach ($list as $cash) {
  185. Withdraw::remit_wait($cash, '点薪灵工打款中...');
  186. }
  187. return $pay_res['data'];
  188. }
  189. }