| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace addons\AvoidTax\common\service;
- use addons\AvoidTax\common\jdk\xinlong\CoreApi;
- use addons\AvoidTax\common\models\TaxConfig;
- use addons\AvoidTax\common\models\TaxOrder;
- use addons\AvoidTax\common\models\TaxUserAuth;
- use common\models\common\PlatformSetting;
- use yii\helpers\Json;
- use \Exception;
- /**
- * Xinlong服务类
- * XinlongService class
- * @package addons\AvoidTax\common\service
- */
- class XinlongService extends BaseService
- {
- /**
- * @var CoreApi
- */
- protected $taxApi;
- public function __construct($config = [])
- {
- if (!isset($config['mall_id'])) {
- throw new Exception('未设置 MALL_ID');
- }
- parent::__construct($config);
- $tax_conf = $this->getConfigService()->getXLConfig();
- if (empty($tax_conf)) {
- throw new Exception('未定义配置');
- }
- $this->taxApi = new CoreApi($config['mall_id'], $tax_conf);
- }
- /**
- * 创建签约账户
- * @param $uid
- * @param $mobile
- * @param $id_card
- * @param $name
- *
- * @return TaxUserAuth
- */
- public function createAccount($uid, $mobile, $id_card, $name)
- {
- // $model = TaxUserAuth::getAccountUser($this->mall_id,$uid);
- // 如果有任何一个字段不匹配则去重新创建账户
- // if( !empty($model) && $model->id_card_no == $id_card ){
- // $model->mobile = $mobile;
- // $model->name = $name;
- // $model->save();
- // return $model;
- // }
- // 去创建账户
- $data = $this->taxApi->submitCreateUser($mobile, $id_card, $name);
- if (!isset($data['data']['accountId'])) {
- return $this->error(" 创建签约账户失败:" . __LINE__);
- }
- $model = new TaxUserAuth();
- $model->user_id = $uid;
- $model->mall_id = $this->mall_id;
- $model->mobile = $mobile;
- $model->id_card_no = $id_card;
- $model->name = $name;
- $model->default = 0;
- $model->accountId = $data['data']['accountId'];
- $model->status = TaxUserAuth::STATUS_WAIT;
- $model->created_at = $model->updated_at = time();
- if ($model->save() == false) {
- return $this->error(" 创建签约账户失败:" . __LINE__);
- }
- return $model;
- }
- /**
- * 获取签约链接
- * @FormData(key="参数名|中文名", rule="")
- * @FormData(key="参数名|中文名", rule="")
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- * @param TaxUserAuth $acc_user
- *
- * @return string
- */
- public function goToSign(TaxUserAuth $acc_user, string $redirect_url = null)
- {
- if ($acc_user->status == TaxUserAuth::STATUS_OK) {
- return true;
- }
- if ($acc_user->status == TaxUserAuth::STATUS_WAIT) {
- // 如果签约中状态,查询上一次签约是否成功
- $status = $this->signQuery($acc_user);
- if ($status == TaxUserAuth::STATUS_OK) {
- // 签约成功
- return true;
- }
- }
- $type = \Yii::$app->request->headers['x-app-platform'] ?? 'h5';
- // 没有签约成功继续去签约
- // 异步地址
- $notify_url = PlatformSetting::conf('system.api_url', [], true)
- . '/avoid-tax/notify/user-sign?' . http_build_query(['auth_id' => $acc_user->id, 'mall_id' => $this->mall_id]);
- $notify_url = str_replace("https", "http", $notify_url);
- if (empty($redirect_url)) {
- // 同步地址
- $redirect_url = PlatformSetting::conf('system.h5_url', [], true);
- }
- if ($type == 'mp-wx') {
- // $path = http_build_query(['type'=>"wechat",'msg'=>"签约成功",'jump_url'=>"/pages/index/index"]);
- // $redirect_url = trim($redirect_url, "/") . '/#/plugins/pages/schemes/index?'.urldecode($path);
- // 微信小程序返回地址
- $redirect_url = 'wechat://back';
- }
- $data = $this->taxApi->submitSign($acc_user->accountId, $notify_url, $redirect_url);
- if (!isset($data['data']['longUrl'])) {
- return $this->error(" 获取签约链接失败:" . __LINE__);
- }
- // 修改签约状态
- $acc_user->status = TaxUserAuth::STATUS_WAIT;
- $acc_user->flowId = $data['data']['flowId'];
- $acc_user->auth_url = $data['data']['url'] . '|' . $redirect_url . "|" . $notify_url;
- $acc_user->save();
- $url = $data['data']['longUrl'];
- return $url;
- }
- /**
- * 签约异步回调
- * @FormData(key="参数名|中文名", rule="")
- * @FormData(key="参数名|中文名", rule="")
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- * @param array $data
- *
- * @return bool
- */
- public function signNotify($auth_id, $data): bool
- {
- $str = $data['requestBody'];
- $str = $this->taxApi->decrypt($str);
- $str = preg_replace('/[\x00-\x1F\x7F-\x9F]/u', '', $str);
- $data = Json::decode($str);
- $this->_handle_sign($auth_id, $data);
- return true;
- }
- /**
- * 签约查询
- *
- * @param TaxUserAuth $userAuth
- * @return integer
- */
- public function signQuery(TaxUserAuth $userAuth)
- {
- $data = $this->taxApi->getEsign($userAuth->mobile, $userAuth->id_card_no, $userAuth->name);
- return $this->_handle_sign($userAuth->id, $data['data']);
- }
- /**
- * 向税务方提交订单
- *
- * @param TaxOrder $order 用户订单
- * @param TaxOrder $tax_order 手续费订单
- * @param TaxUserAuth $sign 用户签约信息
- * @return boolean
- */
- public function submitOrder(TaxOrder $order, TaxOrder $tax_order, TaxUserAuth $sign) : bool
- {
- if (empty($sign)) {
- return $this->error(" {$order['user_id']} 用户未签约 ");
- }
- $data = [
- 'natchno' => $order['order_sn'],
- 'uid' => $this->taxApi->uid,
- 'inAcctName' => $order['name'],
- 'inAcctNo' => $order['mobile'],
- 'tranAmount' => $order['amount'],
- 'mobile' => $sign['mobile'],
- 'idcard' => $sign['id_card_no'],
- 'idtypename' => '1',
- 'ccyCode' => 'ALIPAY',
- 'cardType' => 'DC',
- 'cardAttribute' => 'C',
- // 新增字段
- 'pay_fee' => $tax_order['amount'],
- 'pay_fee_order' => $tax_order['order_sn'],
- ];
- try {
- // 调风控接口
- $res = $this->taxApi->getRiskControl($data);
- if ($res['code'] != '0000') {
- // 1006 超限额
- return $this->error($res['message']);
- }
- // 提交成功
- return true;
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 签约查看
- *
- * @param int $auth_id
- * @param array $data
- * @return integer
- */
- private function _handle_sign($auth_id, $data)
- {
- if ($data['sign_status'] == 2) {
- // 成功
- TaxUserAuth::signOk($auth_id, [
- 'id_card_no' => $data['id_number'],
- 'sign_data' => Json::encode($data),
- ]);
- return TaxUserAuth::STATUS_OK;
- } else {
- // 失败
- TaxUserAuth::signFail($auth_id, [
- 'remark' => $data['remark'],
- 'sign_data' => Json::encode($data),
- ]);
- return TaxUserAuth::STATUS_NOT;
- }
- }
- /**
- * 支付完成后向税务方推送消息
- *
- * @param TaxOrder $order
- * @param TaxOrder $tax_order
- * @param TaxUserAuth $sign
- * @return boolean
- */
- public function pushPayFinish(TaxOrder $order, TaxOrder $tax_order, TaxUserAuth $sign): bool
- {
- if (empty($sign)) {
- return $this->error(" {$order['user_id']} 用户未签约 ");
- }
- $data = [
- 'natchno' => $order['order_sn'],
- 'outerInstNatchno' => $sign['accountId'],
- 'idcard' => $sign['id_card_no'],
- 'mobile' => $sign['mobile'],
- 'ccyCode' => 'ALIPAY',
- 'tranAmount' => $order['amount'],
- 'inAcctNo' => $order['mobile'],
- 'inAcctName' => $order['name'],
- 'cardType' => 'DC',
- 'cardAttribute' => 'C',
- 'fullname' => $order['name'],
- 'payFee' => $tax_order['amount'],
- 'payFeeOrder' => $tax_order['order_sn'],
- ];
- try {
- // 调支付完成接口
- $res = $this->taxApi->withdrawOrder($data);
- if ($res['code'] != '0000') {
- // 1006 超限额
- return $this->error($res['message']);
- }
- // 提交成功
- return true;
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 查询封控
- *
- * @param TaxOrder $order
- * @param TaxUserAuth $sign
- * @return boolean
- */
- public function getRiskControl(TaxOrder $order, TaxUserAuth $sign)
- {
- $data = [
- 'natchno' => $order['order_sn'],
- 'uid' => $this->getConfigService()->getUid(),
- 'idcard' => $sign->id_card_no,
- 'idtypename' => '1',
- 'inAcctName' => $order['name'],
- 'inAcctNo' => $order['mobile'],
- 'ccyCode' => 'ALIPAY',
- 'cardType' => 'DC',
- 'tranAmount' => $order['amount'],
- 'cardAttribute' => 'C',
- 'mobile' => $order['mobile'],
- ];
- try {
- // 封控接口
- $res = $this->taxApi->getRiskControl($data);
- if ($res['code'] != '0000') {
- // 1006 超限额
- return $this->error($res['message']);
- }
- // 提交成功
- return true;
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|