| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\enums\AlipayEnum;
- use addons\AvoidTax\common\jdk\alipay\AopCertClient;
- use addons\AvoidTax\common\models\TaxAccount;
- use addons\AvoidTax\common\models\TaxAccountBook;
- use addons\AvoidTax\common\models\TaxConfig;
- use addons\AvoidTax\common\models\TaxRefund;
- use common\helpers\ArrayHelper;
- use Exception;
- use Yii;
- use yii\data\Pagination;
- use yii\helpers\Json;
- /**
- * 账户相关
- * @package addons\AvoidTax\common\service
- */
- class AccountService extends BaseService
- {
- /**
- * 是否是新签约
- *
- * @var boolean
- */
- protected $isNewSign = false;
- /**
- * 过流水(九尾)记账本
- *
- * @var array
- */
- protected $payment_flow_agreement_nos = [
- [
- 'id' => 1,
- 'title' => '云校招',
- 'no' => '2024090607834',
- ],
- [
- 'id' => 0,
- 'title' => '新增主体',
- 'no' => '',
- ],
- ];
- /**
- * 税务方记账本
- *
- * @var array
- */
- protected $tax_agreement_nos = [
- 'zkby@ishouru.com' => '2023101016938', // 周口百耀信息科技有限公司
- 'liumeng@7yida.com' => '2023071314153', // 湖北必达供应链管理有限公司
- 'yuexin0804@163.com' => '2024070417787', // 淮南市玥昕信息科技有限公司
- 'yxzzfb@jianzhimao.com' => '2024090607834', // 云校招信息科技(广州)有限公司
- 'hunanhuaihua@bangdahr.com' => '2024090622739', // 瀚德
- 'xianglingyun006@163.com' => '2024111316708', // 安徽聘小易科技有限公司
- 'wangxq@xinlongmail.com' => '2024122016471', // 南漳雄锐网络科技有限公司
- '15967150814@163.com' => '2025012210207', // 江西仁泰信息技术有限公司
- 'passengerflyer@163.com' => '2025030615457', // 河南楠韶企业服务有限公司
- ];
- /**
- * 获取签约列表
- *
- * @param array $params
- * @return array
- */
- public function getAccountList($params = [])
- {
- $limit = $params['limit'] ?? 30;
- $page = $params['page'] ?? 1;
- $model = TaxAccount::find();
- $model->with(['book']);
- // 查询条件
- $model->andWhere(['mall_id' => $this->mall_id]);
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
- $list = $model->orderBy('id desc')
- ->asArray()
- ->all();
- foreach ($list as $k => $v) {
- $list[$k]['status_text'] = AlipayEnum::STATUS[$v['status']] ?? '';
- }
- $data['list'] = $list;
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- /**
- * 是否是新签约
- *
- * @param int $book_type
- * @return boolean
- */
- public function getIsNewSign($book_type = TaxAccount::TAXATION)
- {
- $this->getExternalAgreementNo($book_type);
- return $this->isNewSign;
- }
- /**
- * 返回签约二维码 (新签约返回字符串跳转 使用已有签约返回true)
- *
- * @return string|TaxAccount
- */
- public function sign($book_type = TaxAccount::TAXATION)
- {
- $service = new AlipayService($this->mall_id);
- // 按配置获取商户签约号
- $external_agreement_no = $this->getExternalAgreementNo($book_type);
- // 生产签约链接
- $html = $service->agreementPageSign($external_agreement_no);
- if ($model = TaxAccount::addAccount([
- 'mall_id' => $this->mall_id,
- 'book_type' => $book_type,
- 'external_agreement_no' => $external_agreement_no,
- ])) {
- return $this->isNewSign ? $html : $model;
- }
- }
- /**
- * 签约查询 注册记帐本绑定
- *
- * @param integer $id
- * @return boolean
- */
- public function query($id = null, $external_agreement_no = null)
- {
- $account = TaxAccount::find()
- ->where(['mall_id' => $this->mall_id])
- ->andFilterWhere(['id' => $id])
- ->andFilterWhere(['external_agreement_no' => $external_agreement_no])
- ->one();
- if (empty($account)) {
- return $this->error('查询账户不存在');
- }
- if ($account->status == 'NORMAL') {
- return $this->error('该账户已经签约');
- }
- $service = new AlipayService($this->mall_id);
- // api查询签约情况
- if (($query_response = $service->agreementQuery($account->external_agreement_no)) === false) {
- return $this->error($service->error);
- }
- // api创建资金记账本
- $book_id = TaxAccountBook::getMerchantUserId();
- if (($book_response = $service->accountbookCreate(
- $book_id,
- $query_response->agreement_no
- )) === false) {
- return $this->error($service->error);
- }
- $db = Yii::$app->db->beginTransaction();
- try {
- // 更新数据
- TaxAccount::updateAll([
- 'agreement_no' => $query_response->agreement_no,
- 'alipay_logon_id' => $query_response->alipay_logon_id,
- 'personal_product_code' => $query_response->personal_product_code,
- 'valid_time' => strtotime($query_response->valid_time),
- 'sign_time' => strtotime($query_response->sign_time),
- 'status' => $query_response->status,
- 'sign_scene' => $query_response->sign_scene,
- 'third_party_type' => $query_response->third_party_type,
- ], ['id' => $account->id]);
- // 新增记帐本
- TaxAccountBook::addBook([
- 'mall_id' => $account->mall_id,
- 'book_type' => $account->book_type,
- 'account_id' => $account->id,
- 'agreement_no' => $query_response->agreement_no,
- 'merchant_user_id' => $book_id,
- 'account_book_id' => $book_response->account_book_id,
- 'ext_card_info' => Json::encode($book_response->ext_card_info),
- ]);
- $db->commit();
- } catch (Exception $e) {
- $db->rollback();
- return $this->error('查询出错');
- }
- return true;
- }
- /**
- * 记帐本查询
- *
- * @param integer $id
- * @return boolean|object
- */
- public function queryBook($id)
- {
- $where = ['id' => $id, 'mall_id' => $this->mall_id];
- $account = TaxAccount::find()->where($where)->with(['book'])->one();
- if (empty($account) || empty($account->book)) {
- return $this->error('账户不存在');
- }
- $service = new AlipayService($this->mall_id);
- $response = $service->bookQuery($account->book->account_book_id, $account->agreement_no);
- if ($response === false) {
- return $this->error($service->error);
- }
- return $response;
- }
- /**
- * 设置默认记帐本
- *
- * @param integer $id
- * @return boolean
- */
- public function setDefault($id, $book_type = TaxAccount::TAXATION)
- {
- $where = ['id' => $id, 'mall_id' => $this->mall_id, 'book_type' => $book_type];
- $account = TaxAccount::find()->where($where)->with(['book'])->one();
- if (empty($account) || empty($account->book)) {
- return $this->error('账户不存在或者未签约');
- }
-
- $db = Yii::$app->db->beginTransaction();
- try {
- TaxAccount::updateAll(
- ['is_default' => 0],
- ['mall_id' => $this->mall_id, 'book_type' => $book_type]
- );
- TaxAccount::updateAll(
- ['is_default' => 1],
- ['mall_id' => $this->mall_id, 'book_type' => $book_type, 'id' => $id]
- );
- TaxAccountBook::updateAll(
- ['is_default' => 0],
- ['mall_id' => $this->mall_id, 'book_type' => $book_type]
- );
- TaxAccountBook::updateAll(
- ['is_default' => 1],
- ['mall_id' => $this->mall_id, 'book_type' => $book_type, 'account_id' => $id]
- );
- $db->commit();
- } catch (Exception $e) {
- $db->rollback();
- return false;
- }
- return true;
- }
- /**
- * 记账本充值
- *
- * @param integer $id
- * @return void
- */
- public function recharge(int $id, float $amount)
- {
- $condition = ['id' => $id, 'book_type' => TaxAccount::EMPLOY, 'mall_id' => $this->mall_id];
- $account = TaxAccount::find()->where($condition)->with(['book'])->one();
- if (empty($account) || empty($account->book)) {
- return $this->error('账户不存在');
- }
- $service = new AlipayService($this->mall_id);
- $html = $service->recharge(
- date('YmdHis') . rand(100, 999),
- $amount,
- $account->book->account_book_id,
- $account->agreement_no
- );
- return $html;
- }
- /**
- * 记账本列表
- *
- * @return array
- */
- public function getBookList()
- {
- $list = TaxAccount::find()
- // ->joinWith(['book'])
- ->where(['mall_id' => $this->mall_id, 'status' => 'NORMAL'])
- ->select('id, alipay_logon_id, agreement_no')
- ->asArray()
- ->all();
- return $list;
- }
- /**
- * 记账本资金逆流
- *
- * @param integer $id
- * @return boolean
- */
- public function anarrhea(int $id, float $amount = 0)
- {
- $taxation = TaxAccount::getAccountByIdAndType($this->mall_id, $id, [TaxAccount::TAXATION, TaxAccount::PAYMENT_FLOW]);
- if (empty($taxation)) {
- return $this->error('记账本不存在');
- }
- // 用工方记账本
- $employ = TaxAccountBook::getEmploy($this->mall_id);
- if (empty($employ)) {
- return $this->error('用工方记账本不存在');
- }
- // 查询税务方记账本滞留金额
- $service = new AlipayService($this->mall_id);
- // 如果没有传递金额 查询滞留金额
- if ($amount == 0) {
- $response = $service->bookQuery($taxation->book->account_book_id, $taxation->agreement_no);
- $amount = $response->available_amount ?? 0;
- }
- if ($amount == 0) {
- return $this->error('账户无滞留金额, 或者查询失败');
- }
- // 如果记账本是税务方 需要先打款到过流水方
- if ($taxation->isTaxation()) {
- // 先打款到流水方
- $account = TaxAccount::getPaymentFlow($this->mall_id);
- $response = $service->transBookFromBook(
- date('YmdHis') . rand(100, 999),
- $amount,
- '滞留余额逆流',
- $account->book->account_book_id,
- $account->agreement_no,
- $taxation->book->account_book_id,
- $taxation->agreement_no
- );
- if ($response === false) {
- return $this->error(sprintf('逆流到过流水记账本错误: %s', $service->getError()));
- }
- // 打款方变成流水方
- $taxation = $account;
- }
- // 逆流
- $response = $service->transBookFromBook(
- date('YmdHis') . rand(100, 999),
- $amount,
- '滞留余额逆流',
- $employ->account_book_id,
- $employ->agreement_no,
- $taxation->book->account_book_id,
- $taxation->agreement_no
- );
- if ($response === false) {
- return $this->error(sprintf('逆流到用工方记账本错误: %s', $service->getError()));
- }
- return true;
- }
- /**
- * 删除未签约记账本
- *
- * @param integer $id
- * @return boolean
- */
- public function deleteNotSign(int $id)
- {
- $account = TaxAccount::getAccountByIdAndNotSign($this->mall_id, $id);
- if (empty($account)) {
- return $this->error('账户不存在');
- }
- return $account->delete();
- }
- /**
- * 记账本滞留金额退回
- *
- * @param integer $id
- * @return boolean
- */
- public function refund(int $id)
- {
- $taxation = TaxAccount::getAccountByIdAndType($this->mall_id, $id, TaxAccount::TAXATION);
- if (empty($taxation)) {
- return $this->error('记账本不存在');
- }
- // 支付宝ID
- $user_id = $this->getConfigService()->getAlipayUserId();
- // 查询税务方记账本滞留金额
- $service = new AlipayService($this->mall_id);
- $response = $service->bookQuery($taxation->book->account_book_id, $taxation->agreement_no);
- $amount = $response->available_amount ?? 0;
- if ($amount == 0) {
- return $this->error('账户无滞留金额, 或者查询失败');
- }
- // 退款
- $response = $service->transUniTransferUserByUserId(
- date('YmdHis') . rand(100, 999),
- $amount,
- '记账本滞留金额退回',
- $user_id,
- $taxation->book->toArray()
- );
- if ($response === false) {
- return $this->error($service->getError());
- }
- return true;
- }
- /**
- * 退款到用户账号
- *
- * @param integer $id
- * @param array $data
- * @return boolean
- */
- public function refundToAccount(int $id, array $data)
- {
- $taxation = TaxAccount::getAccountByIdAndType($this->mall_id, $id, TaxAccount::EMPLOY);
- if (empty($taxation)) {
- return $this->error('记账本不存在');
- }
- // 查询税务方记账本滞留金额
- $service = new AlipayService($this->mall_id);
- $response = $service->bookQuery($taxation->book->account_book_id, $taxation->agreement_no);
- $amount = $response->available_amount ?? 0;
- if ($amount == 0) {
- return $this->error('账户无滞留金额, 或者查询失败');
- }
- // 退款金额超过余额
- if ($data['amount'] > $amount) {
- return $this->error('记账本金额不足');
- }
- // 添加打款记录
- $refund_log = TaxRefund::addLog(array_merge([
- 'mall_id' => $this->mall_id,
- 'book_id' => $id,
- ], $data));
- if (!$refund_log) return $this->error('添加订单失败');
- // 退款
- $response = $service->bookTransUniTransferUser(
- date('YmdHis') . rand(100, 999),
- $data['amount'],
- '记账本滞留金额退回',
- [
- 'identity_type' => 'ALIPAY_LOGON_ID',
- 'identity' => $data['account_number'],
- 'name' => $data['account_name']
- ],
- $taxation->book
- );
-
- $refund_log->setResponse($response === false ? $service->getError() : $response);
- if ($response === false) {
- return $this->error($service->getError());
- }
- return true;
- }
- /**
- * 获取商户签约号
- *
- * @return string
- */
- protected function getExternalAgreementNo(int $book_type)
- {
- // 用工方直接返回新的签约号
- if ($book_type == TaxAccount::EMPLOY) {
- $this->isNewSign = true;
- return TaxAccount::getAgreementNo();
- }
- // 个人版本直接返回新的签约号
- if ($this->getConfigService()->isPersonalType()) {
- $this->isNewSign = true;
- return TaxAccount::getAgreementNo();
- }
- // 服务商版本
- if ($this->getConfigService()->isServiceType()) {
- // 新龙
- if ($this->getConfigService()->isXLApiType()) {
- $config = $this->getConfigService()->getXLConfig();
- }
- // 鹿用
- if ($this->getConfigService()->isLYApiType()) {
- $config = $this->getConfigService()->getLYConfig();
- }
- }
- // 流水方(九尾)
- if ($book_type == TaxAccount::PAYMENT_FLOW) {
- $payment_flow_id = $config['paymentFlowId'];
- $payment_flow = ArrayHelper::find($this->payment_flow_agreement_nos, function ($item) use ($payment_flow_id) {
- return $item['id'] == $payment_flow_id;
- });
- if (!empty($payment_flow['no'])) return $payment_flow['no'];
- $this->isNewSign = true;
- return TaxAccount::getAgreementNo();
- }
- // 商户名称
- $account = $this->getConfigService()->getPaymentFlowStatus() ? $config['taxBookAccount'] : $config['taxAccount'];;
- // 如果找不到已有签约号 返回新签约号
- if (empty($this->tax_agreement_nos[$account])) {
- $this->isNewSign = true;
- return TaxAccount::getAgreementNo();
- }
- return $this->tax_agreement_nos[$account];
- }
- /**
- * 过流水相关
- *
- * @return array
- */
- public function getPaymentFlow()
- {
- return [
- // 'has_payment_flow' => (int)Yii::$app->params['has_payment_flow'] ?? 0,
- 'payment_flow' => $this->payment_flow_agreement_nos,
- ];
- }
- public function notifyAccount($data)
- {
- $app_id = $data['app_id'] ?? '';
- $external_agreement_no = $data['external_agreement_no'] ?? '';
- $config = TaxConfig::findOne(['app_id'=>$app_id]);
- if(empty($config)){
- return "success";
- }
- $aop = $this->client($app_id,$config['private_key'],$config['alipay_public_key']);
- $res = $aop->rsaCheckV1($data,null,$aop->signType);
- var_dump($res);
- }
- private function client($app_id,$private_key,$public_key)
- {
- $aop = new AopCertClient();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $aop->appId = $app_id;
- $aop->rsaPrivateKey = $private_key;
- $aop->alipayrsaPublicKey= $public_key;
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset='UTF-8';
- $aop->format='json';
- return $aop;
- }
- }
|