123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- /**
- * 支付设置
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/10/31
- * Time: 15:53
- */
- namespace JinDouYun\Model\System;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\System\DPaymentSetting;
- use JinDouYun\Dao\System\DAdminSetting;
- use JinDouYun\Dao\System\DEnterpriseBindPayment;
- use JinDouYun\Model\Finance\MAccount;
- class MPaymentSetting
- {
- private $objDPaymentSetting;
- private $objDAdminSetting;
- private $objDEnterpriseBindPayment;
- private $onlineUserId;
- private $onlineEnterpriseId;
- private $objMAccount;
- /**
- * MPaymentSetting constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- * @throws \Exception
- */
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->objDPaymentSetting = new DPaymentSetting('default');
- $this->objDAdminSetting = new DAdminSetting('default');
- $this->objDEnterpriseBindPayment = new DEnterpriseBindPayment('default');
- $this->objMAccount = new MAccount($onlineEnterpriseId,$onlineUserId);
- }
- /**
- * 获取详情
- * @param $id
- * @return ResultWrapper
- */
- public function getPaymentInfoById($id)
- {
- //查询配置表里有没有这个配置
- $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $id, 'enterpriseId' => $this->onlineEnterpriseId]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- $adminResult = $this->objDAdminSetting->get(['id' => $id]);
- if($adminResult === false){
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- $returnData = [
- 'id' => $id,
- 'defaultStatus' => StatusCode::$delete,
- 'enableStatus' => StatusCode::$delete,
- 'deleteStatus' => StatusCode::$standard,
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'paymentData' => (object)[],
- 'logo' => null,
- 'title' => $adminResult['title'],
- ];
- if($dbResult){
- $returnData = [
- 'id' => $id,
- 'defaultStatus' => $dbResult['defaultStatus'],
- 'enableStatus' => $dbResult['enableStatus'],
- 'deleteStatus' => $dbResult['deleteStatus'],
- 'enterpriseId' => $dbResult['enterpriseId'],
- 'paymentData' => !empty($dbResult['paymentData']) ? json_decode($dbResult['paymentData'], true) : (object)[],
- 'logo' => $dbResult['logo'],
- 'title' => $adminResult['title'],
- ];
- }
- return ResultWrapper::success($returnData);
- }
- /**
- * 支付方式列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllPayment($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- // 获取所有和支付相关的设置项
- $dbResult = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['pay']], '*', 'createTime ASC', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- $total = $this->objDAdminSetting->count(['type' => StatusCode::$adminSettingType['pay']]);
- $ids = [];
- $titles = [];
- foreach ($dbResult as $value) {
- $ids[] = $value['id'];
- $titles[] = [
- 'title' => $value['title'],
- 'id' => $value['id'],
- 'signId' => $value['signId'],
- ];
- }
- unset($dbResult);
- if (empty($ids)){
- return ResultWrapper::success(['data' =>[],'total' => 0]);
- }
-
- // 查询指定企业对设置项设置的内容
- $dbResult = $this->objDEnterpriseBindPayment->select(['settingId' => $ids, 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- $format = [];
- foreach ($dbResult as $value) {
- $format[$value['settingId']] = $value;
- }
- foreach ($titles as &$value) {
- $value['defaultStatus'] = StatusCode::$delete;
- $value['enableStatus'] = StatusCode::$delete;
- if (isset($format[$value['id']])) {
- $value['defaultStatus'] = $format[$value['id']]['defaultStatus'];
- $value['enableStatus'] = $format[$value['id']]['enableStatus'];
- }
- }
- unset($value);
- $returnData = [
- 'data' => $titles,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($returnData);
- }
- /**
- * 支付方式,启用/禁用
- * @param $params
- * @return ResultWrapper
- */
- public function updateEnableStatus($params)
- {
- $finallyResult = false;
- if ($params['enableStatus'] == StatusCode::$standard) {
- //启用
- //如果是要启用 查询配置表 该企业是否有此配置
- $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- if ($dbResult) {
- //如果有而且是禁用状态 状态改为启用 默认状态改为关闭
- if ($dbResult['enableStatus'] == StatusCode::$delete) {
- $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'enableStatus' => StatusCode::$standard], ['id' => $dbResult['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- }
- } else {
- //如果没有 从基础表里 查询该配置 添加到配置表 状态为启用
- $adminSettingDbResult = $this->objDAdminSetting->get(['id' => $params['id']]);
- if ($adminSettingDbResult === false) {
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- $insertData = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'settingId' => $params['id'],
- 'defaultStatus' => StatusCode::$delete,
- 'enableStatus' => $params['enableStatus'],
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- 'paymentType' => $adminSettingDbResult['signId']
- ];
- $finallyResult = $this->objDEnterpriseBindPayment->insert($insertData);
- }
- } else {
- //禁用
- //配置表里一定有这个配置 默认状态改为禁用
- $finallyResult = $this->objDEnterpriseBindPayment->update(['enableStatus' => StatusCode::$delete, 'defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- }
- if ($finallyResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($finallyResult);
- }
- /**
- * 更新支付方式的默认状态
- * @param $params
- * @return ResultWrapper
- */
- public function updateDefaultStatus($params)
- {
- $finallyResult = false;
- if ($params['defaultStatus'] == StatusCode::$standard) {
- //启用默认
- //如果配置是要启用默认 查询配置表 该企业是否有此配置
- $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- if ($dbResult) {
- //如果有而且是关闭状态 状态改为启用 默认状态改为开启
- if ($dbResult['defaultStatus'] == StatusCode::$delete) {
- //修改其他配置为不是默认
- $upResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->onlineEnterpriseId, 'defaultStatus' => StatusCode::$standard]);
- if ($upResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard], ['id' => $dbResult['id']]);
- }
- } else {
- //如果没有 从基础表里 查询该配置 添加到配置表 状态为启用
- $adminSettingDbResult = $this->objDAdminSetting->get(['id' => $params['id']]);
- if ($adminSettingDbResult === false) {
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- $insertData = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'settingId' => $params['id'],
- 'defaultStatus' => $params['defaultStatus'],
- 'enableStatus' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- 'paymentType' => $adminSettingDbResult['signId']
- ];
- $finallyResult = $this->objDEnterpriseBindPayment->insert($insertData);
- }
- } else {
- //关闭默认
- //配置表里一定有这个配置 默认状态改为关闭
- $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['settingId' => $params['id'], $this->onlineEnterpriseId]);
- }
- if ($finallyResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($finallyResult);
- }
- /**
- * 编辑支付方式
- * @param $params
- * @return ResultWrapper
- */
- public function savePaySetting($params)
- {
- $settingId = $params['id'];
- unset($params['id']);
- $params['updateTime'] = time();
- $paymentType = 0;
- // 查询配置表里有没有该配置
- $paySettingResult = $this->objDEnterpriseBindPayment->get(['settingId' => $settingId, 'enterpriseId' => $this->onlineEnterpriseId]);
- if ($paySettingResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- // 如果企业对应设置表中有数据则更新数据 否则做新增操作
- if ($paySettingResult) {
- $returnResult = $this->objDEnterpriseBindPayment->update($params, ['settingId' => $settingId, 'enterpriseId' => $this->onlineEnterpriseId]);
- $paymentType = $paySettingResult['paymentType'];
- } else {
- $adminSettingData = $this->objDAdminSetting->get(['id' => $settingId]);
- if ($adminSettingData === false) {
- return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
- }
- if(empty($adminSettingData)){
- return ResultWrapper::fail('配置信息错误', ErrorCode::$paramError);
- }
- $params['settingId'] = $settingId;
- $params['createTime'] = time();
- $params['paymentType'] = $adminSettingData['signId'];
- $returnResult = $this->objDEnterpriseBindPayment->insert($params);
- $paymentType = $adminSettingData['signId'];
- }
- if ($returnResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
- }
- //处理账户
- $paymentData = json_decode($params['paymentData'], true);
- // 支付宝 微信 余额 支付自动创建对应的财务账号
- if (in_array($paymentType,[StatusCode::$payType['wxPay'], StatusCode::$payType['aliPay'], StatusCode::$payType['balance']])) {
- $account = $this->objMAccount->getDefaultAccount($paymentType);
- if(!$account->isSuccess()) {
- return ResultWrapper::fail($account->getData(), $account->getErrorCode());
- }
- $accountInfo = $account->getData();
- if(empty($accountInfo)) {
- //新增一个账户
- $name = '';
- switch ($paymentType) {
- case StatusCode::$payType['wxPay']:
- $name = '微信支付';
- break;
- case StatusCode::$payType['aliPay']:
- $name = '支付宝支付';
- break;
- case StatusCode::$payType['balance']:
- $name = '余额支付';
- break;
- default:
- break;
- }
- $accountData = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'name' => $name,
- 'accountNumber' => isset($paymentData['appid'])?$paymentData['appid']:$name,
- 'beginMoney' => isset($paymentData['beginMoney']) ? $paymentData['beginMoney'] : 0,
- 'shopId' => 0,
- 'shopName' => '企业',
- 'enableStatus' => StatusCode::$standard,
- 'isDefault' => StatusCode::$delete,
- 'remark' => $name.'收款默认进入该账户',
- 'money'=> isset($paymentData['beginMoney']) ? $paymentData['beginMoney'] : 0,
- 'createTime'=>time(),
- 'updateTime'=>time(),
- 'type'=>$paymentType
- ];
- $result=$this->objMAccount->addAccount($accountData);
- if($result->isSuccess() == false) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- } else{
- //修改账户的账号
- $accountData['id']= $accountInfo['id'];
- $accountData['accountNumber']= $paymentData['appid'];
- $result=$this->objMAccount->editAccount($accountData);
- if($result->isSuccess() == false) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- }
- }
- return ResultWrapper::success($returnResult);
- }
- public function getPayData($paymentType)
- {
- $objDEnterpriseBindPayment = new DEnterpriseBindPayment();
- $dbResult = $objDEnterpriseBindPayment->get(['enterpriseId'=>$this->onlineEnterpriseId,'paymentType'=>$paymentType],'paymentData,id');
- if ($dbResult===false){
- return ResultWrapper::fail($objDEnterpriseBindPayment->error(),ErrorCode::$dberror);
- }
- $pay = empty($dbResult['paymentData']) ? [] : json_decode($dbResult['paymentData'],true);
- return ResultWrapper::success($pay);
- }
- }
|