LYOrderServic.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. namespace addons\AvoidTax\common\service;
  3. use addons\AvoidTax\common\enums\OrderTypeEnum;
  4. use addons\AvoidTax\common\enums\StatusEnum;
  5. use addons\AvoidTax\common\models\LySilentSign;
  6. use addons\AvoidTax\common\models\TaxAccount;
  7. use addons\AvoidTax\common\models\TaxAccountBook;
  8. use addons\AvoidTax\common\models\TaxConfig;
  9. use addons\AvoidTax\common\models\TaxOrder;
  10. use addons\AvoidTax\common\models\TaxOrderBook;
  11. use addons\AvoidTax\common\models\TaxOrderLog;
  12. use addons\AvoidTax\common\models\TaxUserAuth;
  13. use addons\AvoidTax\common\models\UserWithdrawLog;
  14. use common\enums\RabbitMqEnum;
  15. use common\helpers\FormatHelper;
  16. use Exception;
  17. use Yii;
  18. use yii\helpers\Json;
  19. /**
  20. * LYOrderServic class
  21. * @package addons\AvoidTax\common\service
  22. */
  23. class LYOrderServic extends BaseService
  24. {
  25. /**
  26. * @var TaxConfigService
  27. */
  28. protected $config_service;
  29. /**
  30. * @var LuYongService
  31. */
  32. protected $ly_service;
  33. /**
  34. * @var AlipayService
  35. */
  36. protected $alipay_service;
  37. /**
  38. * @var XinLongService
  39. */
  40. protected $xl_service;
  41. /**
  42. * 签约用户信息
  43. *
  44. * @var array
  45. */
  46. protected $sign_users;
  47. /**
  48. * @return TaxConfigService
  49. */
  50. public function getConfigService()
  51. {
  52. if (empty($this->config_service)) {
  53. return $this->config_service = new TaxConfigService(['mall_id' => $this->mall_id]);
  54. }
  55. return $this->config_service;
  56. }
  57. /**
  58. * @return LuYongService
  59. */
  60. public function getLyService()
  61. {
  62. if (empty($this->ly_service)) {
  63. return $this->ly_service = new LuYongService(['mall_id' => $this->mall_id]);
  64. }
  65. return $this->ly_service;
  66. }
  67. /**
  68. * @return AlipayService
  69. */
  70. public function getAlipayService()
  71. {
  72. if (empty($this->alipay_service)) {
  73. return $this->alipay_service = new AlipayService($this->mall_id);
  74. }
  75. return $this->alipay_service;
  76. }
  77. /**
  78. * @return XinLongService
  79. */
  80. public function getXlService()
  81. {
  82. if (empty($this->xl_service)) {
  83. return $this->xl_service = new XinLongService(['mall_id' => $this->mall_id]);
  84. }
  85. return $this->xl_service;
  86. }
  87. /**
  88. * 获取订单列表
  89. *
  90. * @param array $params
  91. * @return array
  92. */
  93. public function getOrderList($params)
  94. {
  95. $params = array_filter($params, function ($val) {
  96. return ($val === '' || $val === null) ? false : true;
  97. });
  98. $limit = $this->getLimit($params);
  99. $page = $this->getPage($params);
  100. $data = TaxOrder::getPageList($page, $limit, function($query) use ($params) {
  101. $query->with(['account', 'withdraw' => function ($q) {
  102. return $q->select('id, order_no');
  103. }])
  104. ->where(['mall_id' => $this->mall_id, 'sub_type' => TaxOrder::SUB_TYPE_USER]);
  105. // 标题查询
  106. !empty($params['title']) && $query->andWhere(['like', 'title', $params['title']]);
  107. // 批次号查询
  108. !empty($params['batch_num']) && $query->andWhere(['like', 'batch_num', $params['batch_num']]);
  109. !empty($params['order_sn']) && $query->andWhere(['like', 'order_sn', $params['order_sn']]);
  110. !empty($params['status']) && $query->andWhere(['=', 'status', $params['status']]);
  111. !empty($params['mobile']) && $query->andWhere(['=', 'mobile', $params['mobile']]);
  112. !empty($params['name']) && $query->andWhere(['=', 'name', $params['name']]);
  113. if (!empty($params['date'])) {
  114. $query->andWhere(['>=', 'created_at', strtotime($params['date'][0])]);
  115. $query->andWhere(['<=', 'created_at', strtotime($params['date'][1])]);
  116. }
  117. }, 'id desc');
  118. foreach ($data['list'] as $k => $v) {
  119. $data['list'][$k]['status_text'] = StatusEnum::getMap()[$v['status']];
  120. $data['list'][$k]['type_text'] = OrderTypeEnum::getMap()[$v['type']];
  121. }
  122. return $data;
  123. }
  124. /**
  125. * 批量提现
  126. *
  127. * @param array $params
  128. * [
  129. * mall_id => int,
  130. * ids => array,
  131. * ]
  132. * @return void
  133. */
  134. public static function batchWithdraw(array $params)
  135. {
  136. $ids = $params['ids'];
  137. $mall_id = $params['mall_id'];
  138. foreach ($ids as $id) {
  139. $withdraw_log = UserWithdrawLog::getTaxLogByAgree($mall_id, $id);
  140. if (empty($withdraw_log)) {
  141. return [false, 'ID:' . $id . ' 提现记录不存在'];
  142. }
  143. // 账户信息
  144. $extra = Json::decode($withdraw_log->extra);
  145. if (empty($extra)) {
  146. return [false, '会员ID:' . $withdraw_log['user_id'] . ' 支付宝信息有误'];
  147. }
  148. }
  149. // 添加异步队列
  150. foreach ($ids as $id) {
  151. $data = compact('mall_id', 'id');
  152. Yii::$app->services->rabbitMq->push(
  153. RabbitMqEnum::EXCHANGE_TASK,
  154. RabbitMqEnum::TASK_QUEUE,
  155. $data,
  156. self::class,
  157. 'handleWithdraw'
  158. );
  159. }
  160. return [true, ''];
  161. }
  162. /**
  163. * 处理提现
  164. *
  165. * @param array $data
  166. * [
  167. * id => int, 提现记录ID
  168. * mall_id => int, 商城ID
  169. * ]
  170. * @return void
  171. */
  172. public function handleWithdraw(array $data)
  173. {
  174. $id = $data['id'];
  175. $this->mall_id = $data['mall_id'];
  176. $config_service = $this->getConfigService();
  177. $alipay_service = $this->getAlipayService();
  178. Yii::$app->db->close();
  179. try {
  180. $withdraw_log = UserWithdrawLog::getTaxLogByAgree($this->mall_id, $id);
  181. // 查询类型(插件使用的类型)
  182. $type = $config_service->getType();
  183. // 低于2元不能提现(手续费低于0.1元不能转账)
  184. if ($withdraw_log->num < 2) {
  185. // 提现记录失败
  186. // UserWithdrawLog::remit_fail($withdraw_log, '合规提现不能低于2元');
  187. // return true;
  188. }
  189. // 创建提现订单、生成税务方手续费订单
  190. $order = $this->createOrder($withdraw_log);
  191. if ($order === false) {
  192. // 提现记录失败
  193. UserWithdrawLog::remit_fail($withdraw_log, '创建订单失败:' . $this->getError());
  194. return true;
  195. }
  196. // 如果是自用版本 检测支付宝余额
  197. if ($type == TaxConfig::PERSONAL) {
  198. if ($this->checkAccountAmount($order) === false) {
  199. UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
  200. return true;
  201. }
  202. }
  203. // 如果是服务商版本 检测用工方余额
  204. if ($type == TaxConfig::SERVICE) {
  205. if ($this->checkBookAmount($order) === false) {
  206. UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
  207. return true;
  208. }
  209. }
  210. // 提交订单
  211. if ($this->subOrder($order) === false) {
  212. TaxOrderLog::addLog($order->batch_num, "向税务方提交风控交易失败:" . $this->getError());
  213. TaxOrder::transError($order->batch_num, null, $this->getError());
  214. // 提现记录失败
  215. UserWithdrawLog::remit_fail($withdraw_log, '提交订单失败:' . $this->getError());
  216. return true;
  217. }
  218. // 充值(企业支付宝 到 记账本)
  219. $response = $alipay_service->bookQuery($order->book->account_book_id, $order->book->agreement_no); // 查询的是税务方的记账本余额
  220. $balance = $response->available_amount ?? 0;
  221. // 余额 大于 打款金额 ,不用充值。否则需要充值
  222. if ($balance < ($order->amount)) {
  223. // 自用版
  224. if ($type == TaxConfig::PERSONAL) {
  225. // 企业支付宝 打款到 记账本
  226. $res = $this->transBook($order);
  227. if ($res === false) {
  228. TaxOrderLog::addLog($order->batch_num, "企业支付宝转账到记账本失败:" . $this->getError());
  229. TaxOrder::transError($order->batch_num, null, $this->getError());
  230. // 提现记录失败
  231. UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
  232. return true;
  233. }
  234. }
  235. // 服务商版
  236. if ($type == TaxConfig::SERVICE) {
  237. // 从用工方记账本转账到税务方记账本
  238. $res = $this->transBookFromBook($order);
  239. if ($res === false) {
  240. TaxOrderLog::addLog($order->batch_num, "记账本转账到税务记账本失败:" . $this->getError());
  241. TaxOrder::transError($order->batch_num, null, $this->getError());
  242. // 提现记录失败
  243. UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
  244. return true;
  245. }
  246. }
  247. }
  248. // 记账本出款到用户账户
  249. $res = $this->byAlipayBookToTransUser($order);
  250. if (!$res) {
  251. // 可能支付宝账户有误
  252. TaxOrderLog::addLog($order->batch_num, "记账本出款到用户账户失败:" . $this->getError());
  253. TaxOrder::transError($order->batch_num, null, $this->getError());
  254. UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
  255. return true;
  256. }
  257. // 打款成功
  258. UserWithdrawLog::remit_success($withdraw_log);
  259. $res = TaxOrder::transSuccess(null, $order->order_sn);
  260. // 自用版手续费打款
  261. if ($type == TaxConfig::PERSONAL) {
  262. $this->transToTax($order->batch_num);
  263. }
  264. // 服务商版手续费打款
  265. if ($type == TaxConfig::SERVICE) {
  266. $this->bookTransToTax($order->batch_num);
  267. }
  268. return true;
  269. } catch (Exception $e) {
  270. dd($e);
  271. $this->error($e->getMessage());
  272. $exception = FormatHelper::exception($e, '支付宝合规队列提现');
  273. Yii::error($exception);
  274. return true;
  275. }
  276. return true;
  277. }
  278. /**
  279. * 创建订单
  280. *
  281. * @param UserWithdrawLog $withdraw_log
  282. * @return TaxOrder|false
  283. */
  284. protected function createOrder($withdraw_log)
  285. {
  286. // 批次号
  287. $batch_num = TaxOrder::getBatchNum();
  288. // 默认记帐本
  289. $book = TaxAccount::getDefaultAccount($this->mall_id);
  290. if (empty($book)) {
  291. return $this->error('未设置默认记帐本');
  292. }
  293. // 签约查询需要重新写
  294. $sign = $this->getSignUser($withdraw_log->user_id);
  295. if (empty($sign)) {
  296. return $this->error(" {$withdraw_log->user_id} 用户未签约 ");
  297. }
  298. $fee = self::makeFee($withdraw_log->actual_num, $this->getConfigService()->getTaxFee());
  299. if ($fee <= 0) $fee = 0.1;
  300. if (($model = TaxOrder::addUserOrder($batch_num, $book->id, $fee, $withdraw_log)) == false) {
  301. return $this->error('创建用户支付宝订单失败');
  302. }
  303. if (TaxOrder::addTaxOrder($batch_num, $book->id, $fee, $withdraw_log, $this->config_service) == false) {
  304. return $this->error('创建手续费订单失败');
  305. }
  306. // 风险检测
  307. if ($this->riskCheck($model) === false) {
  308. return $this->error('风险检测未通过' . $this->getError());
  309. }
  310. return $model;
  311. }
  312. /**
  313. * 风险检测
  314. *
  315. * @param TaxOrder $order 必须是用户打款的订单
  316. * @return boolean
  317. */
  318. protected function riskCheck(TaxOrder $order)
  319. {
  320. $type = $this->getConfigService()->getApiType();
  321. if ($type == TaxConfig::XIONLONG) {
  322. // xinlong不需要检测?
  323. return true;
  324. }
  325. if ($type == TaxConfig::LUYONG) {
  326. $sign = $this->getSignUser($order->user_id);
  327. if (empty($sign)) {
  328. return $this->error('用户未签约');
  329. }
  330. if ($this->getLyService()->riskCheck($order, $sign) === false) {
  331. return $this->error('打款批次风控接口检测失败' . $this->getError());
  332. }
  333. }
  334. return true;
  335. }
  336. /**
  337. * 获取用户签约信息
  338. *
  339. * @param integer $user_id
  340. * @return TaxUserAuth|LySilentSign|null
  341. */
  342. protected function getSignUser(int $user_id)
  343. {
  344. $type = $this->getConfigService()->getApiType();
  345. if (!empty($this->sign_users[$type][$user_id])) {
  346. return $this->sign_users[$type][$user_id];
  347. }
  348. if ($type == TaxConfig::XIONLONG) {
  349. return $this->sign_users[TaxConfig::XIONLONG][$user_id]
  350. = TaxUserAuth::getAccountUser($this->mall_id, $user_id, TaxUserAuth::STATUS_OK);
  351. }
  352. if ($type == TaxConfig::LUYONG) {
  353. return $this->sign_users[TaxConfig::LUYONG][$user_id]
  354. = LySilentSign::getSignUser($user_id);
  355. }
  356. return null;
  357. }
  358. /**
  359. * 提交订单
  360. *
  361. * @param TaxOrder $order
  362. * @return boolean
  363. */
  364. protected function subOrder(TaxOrder $order)
  365. {
  366. $type = $this->getConfigService()->getApiType();
  367. if ($type == TaxConfig::XIONLONG) {
  368. if ($this->getXlService()->submitOrder($order->batch_num) === false) {
  369. return $this->error($this->getXlService()->getError());
  370. }
  371. }
  372. if ($type == TaxConfig::LUYONG) {
  373. if ($this->getLyService()->asyncTransferNotify($order->batch_num) == false) {
  374. return $this->error('支付时异步请求接口请求失败');
  375. }
  376. }
  377. return true;
  378. }
  379. /**
  380. * 检测支付宝余额是否充足
  381. *
  382. * @param TaxOrder $order
  383. * @return boolean
  384. */
  385. protected function checkAccountAmount(TaxOrder $order)
  386. {
  387. $alipay_user_id = $this->getConfigService()->getAlipayUserId();
  388. if (empty($alipay_user_id)) {
  389. return $this->error('未设置支付宝账号ID');
  390. }
  391. $account = $this->getAlipayService()->accountQuery($alipay_user_id);
  392. if ($account === false) {
  393. return $this->error('支付宝账号ID设置错误');
  394. }
  395. // 余额不足
  396. if ($account->available_amount < bcadd($order->amount, $order->pay_fee, 2)) {
  397. return $this->error('支付宝账户余额不足, 请充值');
  398. }
  399. return true;
  400. }
  401. /**
  402. * 检测记账本余额是否充足
  403. *
  404. * @param TaxOrder $order
  405. * @return boolean
  406. */
  407. protected function checkBookAmount(TaxOrder $order)
  408. {
  409. $employ = TaxAccountBook::getEmploy($this->mall_id);
  410. $response = $this->getAlipayService()->bookQuery($employ->account_book_id, $employ->agreement_no); // 查询的是税务方的记账本余额
  411. $balance = $response->available_amount ?? 0;
  412. // 余额不足
  413. if ($balance < bcadd($order->amount, $order->pay_fee, 2)) {
  414. return $this->error('记账本余额不足, 请充值');
  415. }
  416. return true;
  417. }
  418. public static function makeFee($money, $rate = TaxOrder::TAX_ALI_ACCOUNT_RATE)
  419. {
  420. $rate = bcdiv((string)$rate, 100, 4);
  421. $fee = bcmul((string)$money, $rate, 2);
  422. return $fee <= 0 ? '0.1' : $fee;
  423. }
  424. /**
  425. * 将企业支付的资金转入记账本
  426. *
  427. * @param TaxOrder $order
  428. * @return boolean
  429. */
  430. protected function transBook(TaxOrder $order)
  431. {
  432. // 仅出款不转账到记帐本
  433. if ($order->account_type == TaxOrder::ORDER_TYPE_MANUAL) {
  434. return true;
  435. }
  436. $service = $this->getAlipayService();
  437. $book_batch_num = $order->batch_num;
  438. // 获取转账的总金额,该金额包含 给用户的和给税务方手续费的
  439. $amount = TaxOrder::find()->where(['order_sn' => $order->order_sn])->sum('amount');
  440. // 记账本资金专款拨入(商户给人力资源记账本转账)
  441. $response = $service->transUniTransferBook(
  442. $book_batch_num,
  443. $order->book->account_book_id,
  444. $order->title,
  445. $order->book->agreement_no,
  446. $amount,
  447. $order->remarks ? $order->remarks : $order->title
  448. );
  449. if ($response === false) {
  450. return $this->error($service->getError());
  451. }
  452. // 记录转账数据
  453. $res = TaxOrderBook::addOrder([
  454. 'mall_id' => $this->mall_id,
  455. 'out_biz_no' => $book_batch_num,
  456. 'order_id' => $order->id,
  457. 'identity' => $order->book->account_book_id,
  458. 'agreement_no' => $order->book->agreement_no,
  459. 'amount' => $order->amount,
  460. ]);
  461. if ($res === false) {
  462. Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
  463. // throw new Exception('添加转账记录失败');
  464. }
  465. return true;
  466. }
  467. /**
  468. * 从用工方记账本转账到税务方记账本
  469. *
  470. * @param TaxOrder $order
  471. * @return boolean
  472. */
  473. protected function transBookFromBook(TaxOrder $order)
  474. {
  475. // 仅出款不转账到记帐本
  476. if ($order->account_type == TaxOrder::ORDER_TYPE_MANUAL) {
  477. return true;
  478. }
  479. $service = $this->getAlipayService();
  480. $book_batch_num = $order->batch_num;
  481. // 打款金额
  482. $amount = $order->amount;
  483. // 获取双方记账本
  484. $payer = TaxAccountBook::getEmploy($this->mall_id);
  485. $payee = TaxAccountBook::getTaxation($this->mall_id);
  486. // 记账本之间相互调拨
  487. $response = $service->transBookFromBook(
  488. $book_batch_num,
  489. $amount,
  490. '转账到记账本',
  491. $payee->account_book_id,
  492. $payee->agreement_no,
  493. $payer->account_book_id,
  494. $payer->agreement_no,
  495. );
  496. if ($response === false) {
  497. return $this->error($service->error);
  498. }
  499. // 记录转账数据
  500. $res = TaxOrderBook::addOrder([
  501. 'mall_id' => $this->mall_id,
  502. 'out_biz_no' => $book_batch_num,
  503. 'order_id' => $order->id,
  504. 'identity' => $payer->account_book_id,
  505. 'agreement_no' => $payer->agreement_no,
  506. 'amount' => $order->amount,
  507. ]);
  508. if ($res === false) {
  509. Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
  510. }
  511. return true;
  512. }
  513. /**
  514. * 从记账本将资金转到用户
  515. *
  516. * @param TaxOrder $order
  517. * @return boolean
  518. */
  519. protected function byAlipayBookToTransUser(TaxOrder $order)
  520. {
  521. $service = new AlipayService($this->mall_id);
  522. // 代发到户
  523. $response = $service->bookTransUniTransferUser(
  524. $order->order_sn,
  525. $order->amount,
  526. '服务费',
  527. [
  528. // 用户数据
  529. 'name' => $order->name,
  530. 'identity' => $order->mobile,
  531. ],
  532. [
  533. // 记账本数据
  534. 'account_book_id' => $order->book->account_book_id,
  535. 'agreement_no' => $order->book->agreement_no
  536. ],
  537. '服务费'
  538. );
  539. if ($response === false) {
  540. TaxOrder::transError(null, $order->order_sn, $service->error);
  541. return $this->error($service->error);
  542. }
  543. $order->addAlipayResponse($response);
  544. return true;
  545. }
  546. /**
  547. * 异步处理
  548. * 用户打款成功之后,需要给税务方打手续费
  549. *
  550. * @param string $batch_num
  551. * @return boolean
  552. */
  553. protected function transToTax(string $batch_num)
  554. {
  555. $tax_order = TaxOrder::findOne(['batch_num' => $batch_num, 'sub_type' => TaxOrder::SUB_TYPE_TAX]);
  556. if (empty($tax_order)) return $this->error('手续费订单不存在');
  557. // 创建订单的时候已经从支付宝转入记账本了,就不用重新转,直接出款就行了
  558. $res = $this->byAlipayToTransTax($tax_order);
  559. if (!$res) {
  560. // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
  561. TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->error);
  562. return false;
  563. }
  564. TaxOrder::transSuccess(null, $tax_order->order_sn);
  565. // 给税务方发送支付成功通知
  566. if ($this->orderSuccess($tax_order) === false) {
  567. TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
  568. return false;
  569. }
  570. return true;
  571. }
  572. /**
  573. * 手续费打款
  574. *
  575. * @param string $batch_num
  576. * @return boolean
  577. */
  578. protected function bookTransToTax(string $batch_num)
  579. {
  580. $tax_order = TaxOrder::findOne(['batch_num' => $batch_num, 'sub_type' => TaxOrder::SUB_TYPE_TAX]);
  581. if (empty($tax_order)) return;
  582. // 创建订单的时候已经从支付宝转入记账本了,就不用重新转,直接出款就行了
  583. $res = $this->byBookToTransTax($tax_order);
  584. if (!$res) {
  585. // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
  586. TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->getError());
  587. return false;
  588. }
  589. TaxOrder::transSuccess(null, $tax_order->order_sn);
  590. // 给税务方发送支付成功通知
  591. if ($this->orderSuccess($tax_order) === false) {
  592. TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
  593. return false;
  594. }
  595. return true;
  596. }
  597. /**
  598. * 订单完成 通知税务方
  599. *
  600. * @param TaxOrder $order
  601. * @return boolean
  602. */
  603. protected function orderSuccess(TaxOrder $order)
  604. {
  605. $type = $this->getConfigService()->getApiType();
  606. if ($type == TaxConfig::XIONLONG) {
  607. if ($this->getXlService()->pushPayFinish($order->batch_num) === false) {
  608. return $this->error($this->getXlService()->getError());
  609. }
  610. }
  611. if ($type == TaxConfig::LUYONG) {
  612. if ($this->getLyService()->asyncTransferResultNotify($order) === false) {
  613. return $this->error('支付时异步请求接口请求失败');
  614. }
  615. }
  616. return true;
  617. }
  618. /**
  619. * 手续费转账给税务公司
  620. *
  621. * @param TaxOrder $order
  622. * @return boolean
  623. */
  624. protected function byAlipayToTransTax(TaxOrder $order)
  625. {
  626. $service = $this->getAlipayService();
  627. $config = $this->getConfigService()->getConfig();
  628. if ($config['settle'] == 1) {
  629. $product_code = 'TRANS_ACCOUNT_NO_PWD';
  630. // 支付宝
  631. $payee = [
  632. 'identity_type' => 'ALIPAY_LOGON_ID',
  633. // 用户id
  634. 'identity' => $config['tax_config']['taxAccount'],
  635. 'name' => $config['tax_config']['taxAccountName'],
  636. ];
  637. }
  638. if ($config['settle'] == 2) {
  639. $product_code = 'TRANS_BANKCARD_NO_PWD';
  640. // 银行卡
  641. $payee = [
  642. 'identity_type' => 'BANKCARD_ACCOUNT',
  643. // 用户id
  644. 'identity' => $config['tax_config']['bankCard'],
  645. 'name' => $config['tax_config']['bankName'],
  646. 'bankcard_ext_info' => $config['tax_config']['bank'],
  647. 'account_type' => '2',
  648. ];
  649. }
  650. // 代发到户
  651. $response = $service->transUniTransferUser(
  652. $order->order_sn,
  653. $order->amount,
  654. '服务费',
  655. $payee,
  656. $product_code
  657. );
  658. if ($response === false) {
  659. // TaxOrder::transError(null, $order->order_sn, $service->error);
  660. $order->transTaxFeeError($service->getError());
  661. return $this->error($service->getError());
  662. }
  663. $order->addAlipayResponse($response);
  664. return true;
  665. }
  666. /**
  667. * 手续费转账给税务公司(记账本打款)
  668. *
  669. * @param TaxOrder $order
  670. * @return boolean
  671. */
  672. protected function byBookToTransTax(TaxOrder $order)
  673. {
  674. $service = $this->getAlipayService();
  675. $config = $this->getConfigService()->getConfig();
  676. if ($config['settle'] == 1) {
  677. $product_code = 'TRANS_ACCOUNT_NO_PWD';
  678. // 支付宝
  679. $payee = [
  680. 'identity_type' => 'ALIPAY_LOGON_ID',
  681. // 用户id
  682. 'identity' => $config['tax_config']['taxAccount'],
  683. 'name' => $config['tax_config']['taxAccountName'],
  684. ];
  685. }
  686. if ($config['settle'] == 2) { // 目前没有2
  687. $product_code = 'TRANS_BANKCARD_NO_PWD';
  688. // 银行卡
  689. $payee = [
  690. 'identity_type' => 'BANKCARD_ACCOUNT',
  691. // 用户id
  692. 'identity' => $config['tax_config']['bankCard'],
  693. 'name' => $config['tax_config']['bankName'],
  694. 'bankcard_ext_info' => $config['tax_config']['bank'],
  695. 'account_type' => '2',
  696. ];
  697. }
  698. // 用工方记账本打款
  699. $payer = TaxAccountBook::getEmploy($this->mall_id);
  700. // 代发到户
  701. $response = $service->bookTransUniTransferUser(
  702. $order->order_sn,
  703. $order->amount,
  704. '服务费',
  705. $payee,
  706. $payer
  707. );
  708. if ($response === false) {
  709. // TaxOrder::transError(null, $order->order_sn, $service->error);
  710. $order->transTaxFeeError($service->error);
  711. return $this->error($service->error);
  712. }
  713. $order->addAlipayResponse($response);
  714. return true;
  715. }
  716. /**
  717. * 返回提现内容
  718. *
  719. * @param array $params
  720. * @return array
  721. */
  722. public static function withdrawChannel($params)
  723. {
  724. $mall_id = $params['mall_id'];
  725. $config = TaxConfig::read($mall_id);
  726. $is_install = 0;
  727. !empty($config) && $is_install = 1;
  728. return [
  729. 'avoidtax' => ['is_install' => $is_install]
  730. ];
  731. }
  732. }