| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\enums\OrderTypeEnum;
- use addons\AvoidTax\common\enums\StatusEnum;
- use addons\AvoidTax\common\models\LySilentSign;
- use addons\AvoidTax\common\models\TaxAccount;
- use addons\AvoidTax\common\models\TaxAccountBook;
- use addons\AvoidTax\common\models\TaxConfig;
- use addons\AvoidTax\common\models\TaxOrder;
- use addons\AvoidTax\common\models\TaxOrderBook;
- use addons\AvoidTax\common\models\TaxOrderLog;
- use addons\AvoidTax\common\models\TaxUserAuth;
- use addons\AvoidTax\common\models\UserWithdrawLog;
- use common\enums\RabbitMqEnum;
- use common\helpers\FormatHelper;
- use common\helpers\LockHelper;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * OrderService class
- * @package addons\AvoidTax\common\service
- */
- class OrderService extends BaseService
- {
- /**
- * 签约用户信息
- * @deprecated
- * @var array
- */
- protected $sign_users;
- /**
- * 当前订单签约用户信息
- *
- * @var TaxUserAuth|LySilentSign|null
- */
- protected $signUser;
- /**
- * 手续费订单
- *
- * @var TaxOrder
- */
- protected $feeOrder;
- /**
- * 用户订单
- *
- * @var TaxOrder
- */
- protected $userOrder;
- /**
- * 获取订单列表
- *
- * @param array $params
- * @return array
- */
- public function getOrderList($params)
- {
- $params = array_filter($params, function ($val) {
- return ($val === '' || $val === null) ? false : true;
- });
- $limit = $this->getLimit($params);
- $page = $this->getPage($params);
- $data = TaxOrder::getPageList($page, $limit, function($query) use ($params) {
- $query->with(['account', 'withdraw' => function ($q) {
- return $q->select('id, order_no');
- }])
- ->where(['mall_id' => $this->mall_id, 'sub_type' => TaxOrder::SUB_TYPE_USER]);
- // 标题查询
- !empty($params['title']) && $query->andWhere(['like', 'title', $params['title']]);
- // 批次号查询
- !empty($params['batch_num']) && $query->andWhere(['like', 'batch_num', $params['batch_num']]);
- !empty($params['order_sn']) && $query->andWhere(['like', 'order_sn', $params['order_sn']]);
- !empty($params['status']) && $query->andWhere(['=', 'status', $params['status']]);
- !empty($params['mobile']) && $query->andWhere(['=', 'mobile', $params['mobile']]);
- !empty($params['name']) && $query->andWhere(['=', 'name', $params['name']]);
- if (!empty($params['date'])) {
- $query->andWhere(['>=', 'created_at', strtotime($params['date'][0])]);
- $query->andWhere(['<=', 'created_at', strtotime($params['date'][1])]);
- }
- }, 'id desc');
- foreach ($data['list'] as $k => $v) {
- $data['list'][$k]['status_text'] = StatusEnum::getMap()[$v['status']];
- $data['list'][$k]['type_text'] = OrderTypeEnum::getMap()[$v['type']];
- }
- // 统计
- $statistic = TaxOrder::statistic($this->mall_id, $params['date'] ?? []);
- $data['statistic'] = $statistic;
- // 导出列表
- $data['export_list'] = TaxOrder::fieldsList();
- return $data;
- }
- /**
- * 获取提现方法
- *
- * @param array $params
- * @return array
- */
- public static function getWithdrawWay($params)
- {
- $tax_config = (new TaxConfigService(['mall_id' => intval($params['mall_id'])]))->getTaxConfig();
- return ['alipay_tax' => ['name' => "支付宝合规提现", 'taxFee' => $tax_config['taxFee'] ?? 0]];
- }
- /**
- * 批量提现
- *
- * @param array $params
- * [
- * mall_id => int,
- * ids => array,
- * ]
- * @return void
- */
- public static function batchWithdraw(array $params)
- {
- $ids = $params['ids'];
- $mall_id = $params['mall_id'];
- $redis = Yii::$app->redis;
- // 默认记帐本
- $account = TaxAccount::getDefaultAccount($mall_id);
- if (empty($account)) {
- return [false, '支付宝合规未设置默认记帐本'];
- }
- // 是否配置税务方
- $service = new TaxConfigService(['mall_id' => $mall_id]);
- $config = $service->getTaxConfig();
- if (empty($config)) {
- return [false, '税务方未配置'];
- }
- // 支付宝
- if (!isset($config['taxAccountName'])) {
- return [false, '未设置税务方支付宝名称'];
- }
- if (!isset($config['taxAccount'])) {
- return [false, '未设置税务方支付宝账号'];
- }
- if (!isset($config['taxFee'])) {
- return [false, '未设置税务方手续费'];
- }
- foreach ($ids as $id) {
- $redis_key = sprintf('tax_redis_lock_%s', $id);
- if ($redis->get($redis_key)) {
- return [false, sprintf('记录%s执行中, 请勿重复操作', $id)];
- }
- $redis->setex($redis_key, 10, 1);
- $withdraw_log = UserWithdrawLog::getTaxLogByAgree($mall_id, $id);
- if (empty($withdraw_log)) {
- return [false, 'ID:' . $id . ' 提现记录不存在'];
- }
- // 账户信息
- $extra = Json::decode($withdraw_log->extra);
- if (empty($extra)) {
- return [false, '会员ID:' . $withdraw_log['user_id'] . ' 支付宝信息有误'];
- }
- }
- // 添加异步队列
- foreach ($ids as $id) {
- $data = compact('mall_id', 'id');
- Yii::$app->services->rabbitMq->delay(
- 3,
- $data,
- self::class,
- 'handleWithdraw'
- );
- }
- return [true, ''];
- }
- /**
- * 处理提现
- * 异步处理提现业务
- * 1. 创建[提现订单]
- * 2. 生成税务方[手续费订单]
- * 3. 向税务方提交[提现订单],返回成功既可发起打款
- * 5. 调用支付宝 往记账本充值(包含手续费) 先判断资金是否足够,不够才充值
- * 6. 调用支付宝 从记账本出款到用户
- * 7. 调用支付宝 从记账本出款到税务方(手续费)
- * 8. 将结果回传给税务方
- *
- * @param array $data
- * [
- * id => int, 提现记录ID
- * mall_id => int, 商城ID
- * ]
- * @return void
- */
- public function handleWithdraw(array $data)
- {
- $key = 'TaxHandleWithdraw';
- $identification = uniqid();
- LockHelper::lock($key, $identification);
- $id = $data['id'];
- $this->mall_id = $data['mall_id'];
- $alipay_service = $this->getAlipayService();
- Yii::$app->db->close();
- try {
- $withdraw_log = UserWithdrawLog::getTaxLogByAgreeOrWait($this->mall_id, $id);
- if (empty($withdraw_log)) {
- throw new Exception(sprintf('提现记录不存在: %s', $id));
- }
- // 低于2元不能提现(手续费低于0.1元不能转账)
- if ($withdraw_log->num < 2) {
- // 提现记录失败
- UserWithdrawLog::remit_fail($withdraw_log, '合规提现不能低于2元');
- LockHelper::uLock($key, $identification);
- return true;
- }
- // 创建提现订单、生成税务方手续费订单
- $order = $this->createOrder($withdraw_log);
- if ($order === false) {
- // 提现记录失败
- UserWithdrawLog::remit_fail($withdraw_log, '创建订单失败:' . $this->getError());
- LockHelper::uLock($key, $identification);
- return true;
- }
- // 如果是自用版本 检测支付宝余额
- if ($this->getConfigService()->isPersonalType()) {
- if ($this->checkAccountAmount($order) === false) {
- TaxOrderLog::addLog($order->batch_num, $this->getError());
- $this->changeOrderFail($this->getError());
- UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
- LockHelper::uLock($key, $identification);
- return true;
- }
- }
- // 如果是服务商版本 检测用工方余额
- if ($this->getConfigService()->isServiceType()) {
- if ($this->checkBookAmount($order) === false) {
- TaxOrderLog::addLog($order->batch_num, $this->getError());
- $this->changeOrderFail($this->getError());
- UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
- LockHelper::uLock($key, $identification);
- return true;
- }
- }
- // 风险检测
- if ($this->riskCheck($order) === false) {
- TaxOrderLog::addLog($order->batch_num, "向税务方提交风控交易失败:" . $this->getError());
- $this->changeOrderFail($this->getError());
- UserWithdrawLog::remit_fail($withdraw_log, '风险检测未通过' . $this->getError());
- LockHelper::uLock($key, $identification);
- return true;
- }
- // 提交订单
- if ($this->subOrder($order) === false) {
- TaxOrderLog::addLog($order->batch_num, "向税务方提交订单失败:" . $this->getError());
- // TaxOrder::transError($order->batch_num, null, $this->getError());
- $this->changeOrderFail($this->getError());
- // 提现记录失败
- UserWithdrawLog::remit_fail($withdraw_log, '提交订单失败:' . $this->getError());
- LockHelper::uLock($key, $identification);
- return true;
- }
- // 充值(企业支付宝 到 记账本)
- // $response = $alipay_service->bookQuery($order->book->account_book_id, $order->book->agreement_no); // 查询的是税务方的记账本余额
- // $balance = $response->available_amount ?? 0;
- // 余额 大于 打款金额 ,不用充值。否则需要充值
- if (
- // 不再判断余额 默认直接充值到记账本
- true
- ) {
- // 自用版
- if ($this->getConfigService()->isPersonalType()) {
- // 企业支付宝 打款到 记账本
- $res = $this->transBook($order);
- if ($res === false) {
- TaxOrderLog::addLog($order->batch_num, "企业支付宝转账到记账本失败:" . $this->getError());
- // TaxOrder::transError($order->batch_num, null, $this->getError());
- $this->changeOrderFail($this->getError());
- // 提现记录失败
- UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
- // 发送订单失败请求
- $this->orderFail();
- LockHelper::uLock($key, $identification);
- return true;
- }
- }
- // 服务商版
- if ($this->getConfigService()->isServiceType()) {
- // 从用工方记账本转账到税务方记账本
- $res = $this->transBookFromBook($order);
- if ($res === false) {
- TaxOrderLog::addLog($order->batch_num, "记账本转账到税务记账本失败:" . $this->getError());
- // TaxOrder::transError($order->batch_num, null, $this->getError());
- $this->changeOrderFail($this->getError());
- // 提现记录失败
- UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
- // 发送订单失败请求
- $this->orderFail();
- LockHelper::uLock($key, $identification);
- return true;
- }
- }
- }
- // 记账本出款到用户账户
- $res = $this->byAlipayBookToTransUser($order);
- if (!$res) {
- // 打款失败 资金转回支付宝账户
- $service = new AccountService(['mall_id' => $this->mall_id]);
- if ($this->getConfigService()->isPersonalType()) {
- // 个人版使用退款
- $res = $service->refund($order->account_id);
- }
- if ($this->getConfigService()->isServiceType()) {
- // 服务商使用退回
- $res = $service->anarrhea($order->account_id, $order->amount);
- }
- if ($res === false) {
- Yii::error('退款失败:' . $service->getError());
- }
- // 可能支付宝账户有误
- TaxOrderLog::addLog($order->batch_num, "记账本出款到用户账户失败:" . $this->getError());
- // TaxOrder::transError($order->batch_num, null, $this->getError());
- $this->changeOrderFail($this->getError());
- UserWithdrawLog::remit_fail($withdraw_log, $this->getError());
- // 发送订单失败请求
- $this->orderFail();
- LockHelper::uLock($key, $identification);
- return true;
- }
- // 打款成功
- UserWithdrawLog::remit_success($withdraw_log);
- $order->taxOrderSuccess();
- // 自用版手续费打款
- if ($this->getConfigService()->isPersonalType()) {
- $this->transToTax($order->batch_num);
- }
- // 服务商版手续费打款
- if ($this->getConfigService()->isServiceType()) {
- $this->bookTransToTax($order->batch_num);
- }
- LockHelper::uLock($key, $identification);
- return true;
- } catch (Exception $e) {
- $this->error($e->getMessage());
- $exception = FormatHelper::exception($e, '支付宝合规队列提现');
- Yii::error($exception);
- LockHelper::uLock($key, $identification);
- return true;
- }
- LockHelper::uLock($key, $identification);
- return true;
- }
- /**
- * 创建订单
- *
- * @param UserWithdrawLog $withdraw_log
- * @return TaxOrder|false
- */
- protected function createOrder($withdraw_log)
- {
- // 批次号
- $batch_num = TaxOrder::getBatchNum();
- // 默认记帐本
- $book = TaxAccount::getDefaultAccount($this->mall_id);
- if (empty($book)) {
- return $this->error('未设置默认记帐本');
- }
- // 设置签约用户信息
- if ($this->setSignUser($withdraw_log) === false) {
- return $this->error('用户未签约');
- }
- $fee = self::makeFee($withdraw_log->actual_num, $this->getConfigService()->getTaxFee());
- if ($fee <= 0) $fee = 0.1;
- if (($this->userOrder = $model = TaxOrder::addUserOrder($batch_num, $book->id, $fee, $withdraw_log)) == false) {
- return $this->error('创建用户支付宝订单失败');
- }
- if (($this->feeOrder = TaxOrder::addTaxOrder($batch_num, $book->id, $fee, $withdraw_log, $this->config_service)) == false) {
- return $this->error('创建手续费订单失败');
- }
- return $model;
- }
- /**
- * 风险检测
- *
- * @param TaxOrder $order 必须是用户打款的订单
- * @return boolean
- */
- protected function riskCheck(TaxOrder $order)
- {
- if ($this->getConfigService()->isXLApiType()) {
- if (empty($this->signUser)) {
- return $this->error('用户未签约');
- }
- if ($this->getXlService()->getRiskControl($order, $this->signUser) === false) {
- return $this->error('打款批次风控接口检测失败' . $this->getXlService()->getError());
- }
- }
- if ($this->getConfigService()->isLYApiType()) {
- if (empty($this->signUser)) {
- return $this->error('用户未签约');
- }
- if ($this->getLyService()->riskCheck($order, $this->signUser) === false) {
- return $this->error('打款批次风控接口检测失败' . $this->getLyService()->getError());
- }
- }
- return true;
- }
- /**
- * 通过提现信息设置用户签约信息
- *
- * @param UserWithdrawLog $log
- * @return boolean
- */
- public function setSignUser(UserWithdrawLog $log)
- {
- $user_id = $log->user_id;
- list($name, $mobile, $idcard) = $log->getExtra();
- // 如果打款账号是邮箱
- if ($this->getConfigService()->isLYApiType() && strpos($mobile, "@")) {
- $mobile = LySilentSign::find()
- ->where(['user_id' => $user_id, 'account' => $mobile])
- ->select('phone')
- ->scalar();
- }
- if ($this->getConfigService()->isXLApiType()) {
- $sign = $this->signUser
- = TaxUserAuth::getSignByNMI($user_id, $name, $mobile, $idcard);
- }
- if ($this->getConfigService()->isLYApiType()) {
- $sign = $this->signUser
- = LySilentSign::getSignByNMI($user_id, $name, $mobile, $idcard);
- }
- return !empty($sign);
- }
- /**
- * 获取用户签约信息 废弃
- *
- * @deprecated
- * @param integer $user_id
- * @return TaxUserAuth|LySilentSign|null
- */
- protected function getSignUser(int $user_id)
- {
- $type = $this->getConfigService()->getApiType();
- if (!empty($this->sign_users[$type][$user_id])) {
- return $this->sign_users[$type][$user_id];
- }
- if ($this->getConfigService()->isXLApiType()) {
- return $this->sign_users[TaxConfig::XIONLONG][$user_id]
- = TaxUserAuth::getAccountUser($this->mall_id, $user_id, TaxUserAuth::STATUS_OK);
- }
- if ($this->getConfigService()->isLYApiType()) {
- return $this->sign_users[TaxConfig::LUYONG][$user_id]
- = LySilentSign::getSignUser($user_id);
- }
- return null;
- }
- /**
- * 提交订单
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function subOrder(TaxOrder $order)
- {
- if ($this->getConfigService()->isXLApiType()) {
- if ($this->getXlService()->submitOrder(
- $order,
- $this->feeOrder,
- $this->signUser
- ) === false) {
- return $this->error($this->getXlService()->getError());
- }
- }
- if ($this->getConfigService()->isLYApiType()) {
- if ($this->getLyService()->asyncTransferNotify(
- $order,
- $this->feeOrder,
- $this->signUser
- ) == false) {
- return $this->error('支付时异步请求接口请求失败');
- }
- }
- return true;
- }
- /**
- * 检测支付宝余额是否充足
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function checkAccountAmount(TaxOrder $order)
- {
- $alipay_user_id = $this->getConfigService()->getAlipayUserId();
- if (empty($alipay_user_id)) {
- return $this->error('未设置支付宝账号ID');
- }
- $account = $this->getAlipayService()->accountQuery($alipay_user_id);
- if ($account === false) {
- return $this->error('支付宝账号ID设置错误');
- }
- // 余额不足
- if ($account->available_amount < bcadd($order->amount, $order->pay_fee, 2)) {
- return $this->error('支付宝账户余额不足, 请充值');
- }
- return true;
- }
- /**
- * 检测记账本余额是否充足
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function checkBookAmount(TaxOrder $order)
- {
- $employ = TaxAccountBook::getEmploy($this->mall_id);
- $response = $this->getAlipayService()->bookQuery($employ->account_book_id, $employ->agreement_no); // 查询的是税务方的记账本余额
- $balance = $response->available_amount ?? 0;
- // 余额不足
- if ($balance < bcadd($order->amount, $order->pay_fee, 2)) {
- return $this->error('记账本余额不足, 请充值');
- }
- return true;
- }
- public static function makeFee($money, $rate = TaxOrder::TAX_ALI_ACCOUNT_RATE)
- {
- $rate = bcdiv((string)$rate, 100, 4);
- $fee = bcmul((string)$money, $rate, 2);
- return $fee <= 0 ? '0.1' : $fee;
- }
- /**
- * 将企业支付的资金转入记账本
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function transBook(TaxOrder $order)
- {
- // 仅出款不转账到记帐本
- if ($order->account_type == TaxOrder::ORDER_TYPE_MANUAL) {
- return true;
- }
- $service = $this->getAlipayService();
- $book_batch_num = $order->batch_num;
- // 获取转账的总金额,该金额包含 给用户的和给税务方手续费的
- $amount = TaxOrder::find()->where(['order_sn' => $order->order_sn])->sum('amount');
- // 记账本资金专款拨入(商户给人力资源记账本转账)
- $response = $service->transUniTransferBook(
- $book_batch_num,
- $order->book->account_book_id,
- $order->title,
- $order->book->agreement_no,
- $amount,
- $order->remarks ? $order->remarks : $order->title
- );
- if ($response === false) {
- return $this->error($service->getError());
- }
- // 记录转账数据
- $res = TaxOrderBook::addOrder([
- 'mall_id' => $this->mall_id,
- 'out_biz_no' => $book_batch_num,
- 'order_id' => $order->id,
- 'identity' => $order->book->account_book_id,
- 'agreement_no' => $order->book->agreement_no,
- 'amount' => $order->amount,
- ]);
- if ($res === false) {
- Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
- // throw new Exception('添加转账记录失败');
- }
- return true;
- }
- /**
- * 从用工方记账本转账到税务方记账本
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function transBookFromBook(TaxOrder $order)
- {
- // 仅出款不转账到记帐本
- if ($order->account_type == TaxOrder::ORDER_TYPE_MANUAL) {
- return true;
- }
- $service = $this->getAlipayService();
- $book_batch_num = $order->batch_num;
- // 打款金额
- $amount = $order->amount;
- // 获取双方记账本
- $payer = TaxAccountBook::getEmploy($this->mall_id);
- $payee = TaxAccountBook::getTaxation($this->mall_id);
- if ($this->getConfigService()->getPaymentFlowStatus()) {
- // 流水方
- $payment = TaxAccountBook::getPaymentFlow($this->mall_id);
- // 打款到流水记账本
- $response = $service->transBookFromBook(
- $book_batch_num . '_1',
- $amount,
- '转账到记账本',
- $payment->account_book_id,
- $payment->agreement_no,
- $payer->account_book_id,
- $payer->agreement_no,
- );
- if ($response === false) {
- return $this->error($service->error);
- }
- // 记录转账数据
- $res = TaxOrderBook::addOrder([
- 'mall_id' => $this->mall_id,
- 'out_biz_no' => $book_batch_num . '_1',
- 'order_id' => $order->id,
- 'identity' => $payer->account_book_id,
- 'agreement_no' => $payer->agreement_no,
- 'amount' => $order->amount,
- ]);
- if ($res === false) Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
- // 打款到税务方记账本
- $response = $service->transBookFromBook(
- $book_batch_num . '_2',
- $amount,
- '转账到记账本',
- $payee->account_book_id,
- $payee->agreement_no,
- $payment->account_book_id,
- $payment->agreement_no,
- );
- if ($response === false) {
- return $this->error($service->error);
- }
- // 记录转账数据
- $res = TaxOrderBook::addOrder([
- 'mall_id' => $this->mall_id,
- 'out_biz_no' => $book_batch_num . '_2',
- 'order_id' => $order->id,
- 'identity' => $payment->account_book_id,
- 'agreement_no' => $payment->agreement_no,
- 'amount' => $order->amount,
- ]);
- if ($res === false) Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
- } else {
- // 记账本之间相互调拨
- $response = $service->transBookFromBook(
- $book_batch_num,
- $amount,
- '转账到记账本',
- $payee->account_book_id,
- $payee->agreement_no,
- $payer->account_book_id,
- $payer->agreement_no,
- );
- if ($response === false) {
- return $this->error($service->error);
- }
- // 记录转账数据
- $res = TaxOrderBook::addOrder([
- 'mall_id' => $this->mall_id,
- 'out_biz_no' => $book_batch_num,
- 'order_id' => $order->id,
- 'identity' => $payer->account_book_id,
- 'agreement_no' => $payer->agreement_no,
- 'amount' => $order->amount,
- ]);
-
- if ($res === false) {
- Yii::error('支付宝合规 - 添加转账记录失败' . __LINE__);
- }
- }
- return true;
- }
- /**
- * 从记账本将资金转到用户
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function byAlipayBookToTransUser(TaxOrder $order)
- {
- $service = new AlipayService($this->mall_id);
- // 代发到户
- $response = $service->bookTransUniTransferUser(
- $order->order_sn,
- $order->amount,
- '服务费',
- [
- // 用户数据
- 'name' => $order->name,
- 'identity' => $order->mobile,
- ],
- [
- // 记账本数据
- 'account_book_id' => $order->book->account_book_id,
- 'agreement_no' => $order->book->agreement_no
- ],
- '服务费'
- );
- if ($response === false) {
- // TaxOrder::transError(null, $order->order_sn, $service->error);
- return $this->error($service->error);
- }
- $order->addAlipayResponse($response);
- return true;
- }
- /**
- * 异步处理
- * 用户打款成功之后,需要给税务方打手续费
- *
- * @param string $batch_num
- * @return boolean
- */
- public function transToTax(string $batch_num)
- {
- // $tax_order = TaxOrder::findOne(['batch_num' => $batch_num, 'sub_type' => TaxOrder::SUB_TYPE_TAX]);
- $tax_order = $this->feeOrder;
- if (empty($tax_order)) return $this->error('手续费订单不存在');
- // 直接从支付宝打款给记账方
- $res = $this->byAlipayToTransTax($tax_order);
- if (!$res) {
- // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
- TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->error);
- return false;
- }
- $tax_order->taxOrderSuccess();
- // 给税务方发送支付成功通知
- if ($this->orderSuccess($tax_order) === false) {
- TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
- return false;
- }
- return true;
- }
- /**
- * 用户打款成功之后,需要给税务方打手续费
- *
- * @param TaxOrder $tax_order
- * @return boolean
- */
- public function transToTaxByOrder(TaxOrder $tax_order)
- {
- // 直接从支付宝打款给记账方
- $res = $this->byAlipayToTransTax($tax_order);
- if (!$res) {
- // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
- TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->error);
- return false;
- }
- $tax_order->taxOrderSuccess();
- // 给税务方发送支付成功通知
- if ($this->orderSuccess($tax_order) === false) {
- TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
- return false;
- }
- return true;
- }
- /**
- * 手续费打款
- *
- * @param string $batch_num
- * @return boolean
- */
- public function bookTransToTax(string $batch_num)
- {
- // $tax_order = TaxOrder::findOne(['batch_num' => $batch_num, 'sub_type' => TaxOrder::SUB_TYPE_TAX]);
- $tax_order = $this->feeOrder;
- if (empty($tax_order)) return;
- // 创建订单的时候已经从支付宝转入记账本了,就不用重新转,直接出款就行了
- $res = $this->byBookToTransTax($tax_order);
- if (!$res) {
- // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
- TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->getError());
- return false;
- }
- $tax_order->taxOrderSuccess();
- // 给税务方发送支付成功通知
- if ($this->orderSuccess($tax_order) === false) {
- TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
- return false;
- }
- return true;
- }
- /**
- * 手续费打款
- *
- * @param TaxOrder $order
- * @return boolean
- */
- public function bookTransToTaxByOrder(TaxOrder $tax_order)
- {
- // 创建订单的时候已经从支付宝转入记账本了,就不用重新转,直接出款就行了
- $res = $this->byBookToTransTax($tax_order);
- if (!$res) {
- // Yii::error("给税务方转手续费失败2:" . $tax_order->batch_num);
- TaxOrderLog::addLog($tax_order->order_sn, "给税务方转手续费失败:" . $this->getError());
- return false;
- }
- $tax_order->taxOrderSuccess();
- // 给税务方发送支付成功通知
- if ($this->orderSuccess($tax_order) === false) {
- TaxOrderLog::addLog($tax_order->order_sn, "向税务方发送订单完成失败:" . $this->getError());
- return false;
- }
- return true;
- }
- /**
- * 订单完成 通知税务方
- *
- * @param TaxOrder $tax_order
- * @return boolean
- */
- public function orderSuccess(TaxOrder $tax_order)
- {
- if ($this->getConfigService()->isXLApiType()) {
- if ($this->getXlService()->pushPayFinish(
- $this->userOrder,
- $this->feeOrder,
- $this->signUser
- ) === false) {
- return $this->error($this->getXlService()->getError());
- }
- }
- if ($this->getConfigService()->isLYApiType()) {
- // 发送订单成功通知
- if ($this->getLyService()->asyncTransferResultNotifyOk($this->userOrder) === false) {
- return $this->error('支付时异步请求接口请求失败');
- }
- }
- return true;
- }
- /**
- * 订单失败 通知税务方
- *
- * @return boolean
- */
- public function orderFail()
- {
- if ($this->getConfigService()->isXLApiType()) {
- // 目前没有处理方式
- return true;
- }
- if ($this->getConfigService()->isLYApiType()) {
- // 发送订单成功通知
- if ($this->getLyService()->asyncTransferResultNotifyNo($this->userOrder) === false) {
- return $this->error('支付时异步请求接口请求失败');
- }
- }
- return true;
- }
- /**
- * 手续费转账给税务公司
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function byAlipayToTransTax(TaxOrder $order)
- {
- $service = $this->getAlipayService();
- $config = $this->getConfigService()->getConfig();
- $tax_config = $this->getConfigService()->getTaxConfig();
- if ($config['settle'] == 1) {
- $product_code = 'TRANS_ACCOUNT_NO_PWD';
- // 支付宝
- $payee = [
- 'identity_type' => 'ALIPAY_LOGON_ID',
- // 用户id
- 'identity' => $tax_config['taxAccount'],
- 'name' => $tax_config['taxAccountName'],
- ];
- }
- if ($config['settle'] == 2) {
- $product_code = 'TRANS_BANKCARD_NO_PWD';
- // 银行卡
- $payee = [
- 'identity_type' => 'BANKCARD_ACCOUNT',
- // 用户id
- 'identity' => $tax_config['bankCard'],
- 'name' => $tax_config['bankName'],
- 'bankcard_ext_info' => $tax_config['bank'],
- 'account_type' => '2',
- ];
- }
- // 代发到户
- $response = $service->transUniTransferUser(
- $order->order_sn,
- $order->amount,
- '服务费',
- $payee,
- $product_code
- );
- if ($response === false) {
- // TaxOrder::transError(null, $order->order_sn, $service->error);
- $order->transTaxFeeError($service->getError());
- return $this->error($service->getError());
- }
- $order->addAlipayResponse($response);
- return true;
- }
- /**
- * 手续费转账给税务公司(记账本打款)
- *
- * @param TaxOrder $order
- * @return boolean
- */
- protected function byBookToTransTax(TaxOrder $order)
- {
- $service = $this->getAlipayService();
- $config = $this->getConfigService()->getConfig();
- $tax_config = $this->getConfigService()->getTaxConfig();
- if ($config['settle'] == 1) {
- $product_code = 'TRANS_ACCOUNT_NO_PWD';
- // 支付宝
- $payee = [
- 'identity_type' => 'ALIPAY_LOGON_ID',
- // 用户id
- 'identity' => $tax_config['taxAccount'],
- 'name' => $tax_config['taxAccountName'],
- ];
- }
- if ($config['settle'] == 2) { // 目前没有2
- $product_code = 'TRANS_BANKCARD_NO_PWD';
- // 银行卡
- $payee = [
- 'identity_type' => 'BANKCARD_ACCOUNT',
- // 用户id
- 'identity' => $tax_config['bankCard'],
- 'name' => $tax_config['bankName'],
- 'bankcard_ext_info' => $tax_config['bank'],
- 'account_type' => '2',
- ];
- }
- // 用工方记账本打款
- $payer = TaxAccountBook::getEmploy($this->mall_id);
- // 代发到户
- $response = $service->bookTransUniTransferUser(
- $order->order_sn,
- $order->amount,
- '服务费',
- $payee,
- $payer
- );
- if ($response === false) {
- // TaxOrder::transError(null, $order->order_sn, $service->error);
- $order->transTaxFeeError($service->error);
- return $this->error($service->error);
- }
- $order->addAlipayResponse($response);
- return true;
- }
- /**
- * 返回提现内容
- *
- * @param array $params
- * @return array
- */
- public static function withdrawChannel($params)
- {
- $mall_id = $params['mall_id'];
- $config = TaxConfig::read($mall_id);
- $is_install = 0;
- !empty($config) && $is_install = 1;
- return [
- 'avoidtax' => ['is_install' => $is_install]
- ];
- }
- /**
- * 获取一笔手续费详情
- *
- * @param string $num
- * @return void
- */
- public function getFeeDeatil(string $num)
- {
- return TaxOrder::getFeeDeatilByNum($num);
- }
- /**
- * 订单打款失败修改
- *
- * @return void
- */
- public function changeOrderFail(string $msg)
- {
- $this->feeOrder->taxOrderFail($msg);
- $this->userOrder->taxOrderFail($msg);
- }
- /**
- * @param TaxOrder $order
- * @return void
- */
- public function setUserOrder(TaxOrder $order)
- {
- $this->userOrder = $order;
- }
- /**
- * @param TaxOrder $order
- * @return void
- */
- public function setFeeOrder(TaxOrder $order)
- {
- $this->feeOrder = $order;
- }
- }
|