MPaymentSetting.Class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * 支付设置
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/10/31
  7. * Time: 15:53
  8. */
  9. namespace JinDouYun\Model\System;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\System\DPaymentSetting;
  14. use JinDouYun\Dao\System\DAdminSetting;
  15. use JinDouYun\Dao\System\DEnterpriseBindPayment;
  16. use JinDouYun\Model\Finance\MAccount;
  17. class MPaymentSetting
  18. {
  19. private $objDPaymentSetting;
  20. private $objDAdminSetting;
  21. private $objDEnterpriseBindPayment;
  22. private $onlineUserId;
  23. private $onlineEnterpriseId;
  24. private $objMAccount;
  25. /**
  26. * MPaymentSetting constructor.
  27. * @param $onlineUserId
  28. * @param $onlineEnterpriseId
  29. * @throws \Exception
  30. */
  31. public function __construct($onlineUserId, $onlineEnterpriseId)
  32. {
  33. $this->onlineUserId = $onlineUserId;
  34. $this->onlineEnterpriseId = $onlineEnterpriseId;
  35. $this->objDPaymentSetting = new DPaymentSetting('default');
  36. $this->objDAdminSetting = new DAdminSetting('default');
  37. $this->objDEnterpriseBindPayment = new DEnterpriseBindPayment('default');
  38. $this->objMAccount = new MAccount($onlineEnterpriseId,$onlineUserId);
  39. }
  40. /**
  41. * 获取详情
  42. * @param $id
  43. * @return ResultWrapper
  44. */
  45. public function getPaymentInfoById($id)
  46. {
  47. //查询配置表里有没有这个配置
  48. $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $id, 'enterpriseId' => $this->onlineEnterpriseId]);
  49. if($dbResult === false){
  50. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  51. }
  52. $adminResult = $this->objDAdminSetting->get(['id' => $id]);
  53. if($adminResult === false){
  54. return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
  55. }
  56. $returnData = [
  57. 'id' => $id,
  58. 'defaultStatus' => StatusCode::$delete,
  59. 'enableStatus' => StatusCode::$delete,
  60. 'deleteStatus' => StatusCode::$standard,
  61. 'enterpriseId' => $this->onlineEnterpriseId,
  62. 'paymentData' => (object)[],
  63. 'logo' => null,
  64. 'title' => $adminResult['title'],
  65. ];
  66. if($dbResult){
  67. $returnData = [
  68. 'id' => $id,
  69. 'defaultStatus' => $dbResult['defaultStatus'],
  70. 'enableStatus' => $dbResult['enableStatus'],
  71. 'deleteStatus' => $dbResult['deleteStatus'],
  72. 'enterpriseId' => $dbResult['enterpriseId'],
  73. 'paymentData' => !empty($dbResult['paymentData']) ? json_decode($dbResult['paymentData'], true) : (object)[],
  74. 'logo' => $dbResult['logo'],
  75. 'title' => $adminResult['title'],
  76. ];
  77. }
  78. return ResultWrapper::success($returnData);
  79. }
  80. /**
  81. * 支付方式列表
  82. * @param $selectParams
  83. * @return ResultWrapper
  84. */
  85. public function getAllPayment($selectParams)
  86. {
  87. $limit = $selectParams['limit'];
  88. unset($selectParams['limit']);
  89. $offset = $selectParams['offset'];
  90. unset($selectParams['offset']);
  91. // 获取所有和支付相关的设置项
  92. $dbResult = $this->objDAdminSetting->select(['type' => StatusCode::$adminSettingType['pay']], '*', 'createTime ASC', $limit, $offset);
  93. if ($dbResult === false) {
  94. return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
  95. }
  96. $total = $this->objDAdminSetting->count(['type' => StatusCode::$adminSettingType['pay']]);
  97. $ids = [];
  98. $titles = [];
  99. foreach ($dbResult as $value) {
  100. $ids[] = $value['id'];
  101. $titles[] = [
  102. 'title' => $value['title'],
  103. 'id' => $value['id'],
  104. 'signId' => $value['signId'],
  105. ];
  106. }
  107. unset($dbResult);
  108. if (empty($ids)){
  109. return ResultWrapper::success(['data' =>[],'total' => 0]);
  110. }
  111. // 查询指定企业对设置项设置的内容
  112. $dbResult = $this->objDEnterpriseBindPayment->select(['settingId' => $ids, 'enterpriseId' => $this->onlineEnterpriseId]);
  113. if ($dbResult === false) {
  114. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  115. }
  116. $format = [];
  117. foreach ($dbResult as $value) {
  118. $format[$value['settingId']] = $value;
  119. }
  120. foreach ($titles as &$value) {
  121. $value['defaultStatus'] = StatusCode::$delete;
  122. $value['enableStatus'] = StatusCode::$delete;
  123. if (isset($format[$value['id']])) {
  124. $value['defaultStatus'] = $format[$value['id']]['defaultStatus'];
  125. $value['enableStatus'] = $format[$value['id']]['enableStatus'];
  126. }
  127. }
  128. unset($value);
  129. $returnData = [
  130. 'data' => $titles,
  131. 'total' => ($total) ? intval($total) : 0,
  132. ];
  133. return ResultWrapper::success($returnData);
  134. }
  135. /**
  136. * 支付方式,启用/禁用
  137. * @param $params
  138. * @return ResultWrapper
  139. */
  140. public function updateEnableStatus($params)
  141. {
  142. $finallyResult = false;
  143. if ($params['enableStatus'] == StatusCode::$standard) {
  144. //启用
  145. //如果是要启用 查询配置表 该企业是否有此配置
  146. $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
  147. if ($dbResult === false) {
  148. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  149. }
  150. if ($dbResult) {
  151. //如果有而且是禁用状态 状态改为启用 默认状态改为关闭
  152. if ($dbResult['enableStatus'] == StatusCode::$delete) {
  153. $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'enableStatus' => StatusCode::$standard], ['id' => $dbResult['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
  154. }
  155. } else {
  156. //如果没有 从基础表里 查询该配置 添加到配置表 状态为启用
  157. $adminSettingDbResult = $this->objDAdminSetting->get(['id' => $params['id']]);
  158. if ($adminSettingDbResult === false) {
  159. return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
  160. }
  161. $insertData = [
  162. 'enterpriseId' => $this->onlineEnterpriseId,
  163. 'settingId' => $params['id'],
  164. 'defaultStatus' => StatusCode::$delete,
  165. 'enableStatus' => $params['enableStatus'],
  166. 'deleteStatus' => StatusCode::$standard,
  167. 'createTime' => time(),
  168. 'updateTime' => time(),
  169. 'paymentType' => $adminSettingDbResult['signId']
  170. ];
  171. $finallyResult = $this->objDEnterpriseBindPayment->insert($insertData);
  172. }
  173. } else {
  174. //禁用
  175. //配置表里一定有这个配置 默认状态改为禁用
  176. $finallyResult = $this->objDEnterpriseBindPayment->update(['enableStatus' => StatusCode::$delete, 'defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
  177. }
  178. if ($finallyResult === false) {
  179. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  180. }
  181. return ResultWrapper::success($finallyResult);
  182. }
  183. /**
  184. * 更新支付方式的默认状态
  185. * @param $params
  186. * @return ResultWrapper
  187. */
  188. public function updateDefaultStatus($params)
  189. {
  190. $finallyResult = false;
  191. if ($params['defaultStatus'] == StatusCode::$standard) {
  192. //启用默认
  193. //如果配置是要启用默认 查询配置表 该企业是否有此配置
  194. $dbResult = $this->objDEnterpriseBindPayment->get(['settingId' => $params['id'], 'enterpriseId' => $this->onlineEnterpriseId]);
  195. if ($dbResult === false) {
  196. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  197. }
  198. if ($dbResult) {
  199. //如果有而且是关闭状态 状态改为启用 默认状态改为开启
  200. if ($dbResult['defaultStatus'] == StatusCode::$delete) {
  201. //修改其他配置为不是默认
  202. $upResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['enterpriseId' => $this->onlineEnterpriseId, 'defaultStatus' => StatusCode::$standard]);
  203. if ($upResult === false) {
  204. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  205. }
  206. $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard], ['id' => $dbResult['id']]);
  207. }
  208. } else {
  209. //如果没有 从基础表里 查询该配置 添加到配置表 状态为启用
  210. $adminSettingDbResult = $this->objDAdminSetting->get(['id' => $params['id']]);
  211. if ($adminSettingDbResult === false) {
  212. return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
  213. }
  214. $insertData = [
  215. 'enterpriseId' => $this->onlineEnterpriseId,
  216. 'settingId' => $params['id'],
  217. 'defaultStatus' => $params['defaultStatus'],
  218. 'enableStatus' => StatusCode::$standard,
  219. 'deleteStatus' => StatusCode::$standard,
  220. 'createTime' => time(),
  221. 'updateTime' => time(),
  222. 'paymentType' => $adminSettingDbResult['signId']
  223. ];
  224. $finallyResult = $this->objDEnterpriseBindPayment->insert($insertData);
  225. }
  226. } else {
  227. //关闭默认
  228. //配置表里一定有这个配置 默认状态改为关闭
  229. $finallyResult = $this->objDEnterpriseBindPayment->update(['defaultStatus' => StatusCode::$delete, 'updateTime' => time()], ['settingId' => $params['id'], $this->onlineEnterpriseId]);
  230. }
  231. if ($finallyResult === false) {
  232. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  233. }
  234. return ResultWrapper::success($finallyResult);
  235. }
  236. /**
  237. * 编辑支付方式
  238. * @param $params
  239. * @return ResultWrapper
  240. */
  241. public function savePaySetting($params)
  242. {
  243. $settingId = $params['id'];
  244. unset($params['id']);
  245. $params['updateTime'] = time();
  246. $paymentType = 0;
  247. // 查询配置表里有没有该配置
  248. $paySettingResult = $this->objDEnterpriseBindPayment->get(['settingId' => $settingId, 'enterpriseId' => $this->onlineEnterpriseId]);
  249. if ($paySettingResult === false) {
  250. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  251. }
  252. // 如果企业对应设置表中有数据则更新数据 否则做新增操作
  253. if ($paySettingResult) {
  254. $returnResult = $this->objDEnterpriseBindPayment->update($params, ['settingId' => $settingId, 'enterpriseId' => $this->onlineEnterpriseId]);
  255. $paymentType = $paySettingResult['paymentType'];
  256. } else {
  257. $adminSettingData = $this->objDAdminSetting->get(['id' => $settingId]);
  258. if ($adminSettingData === false) {
  259. return ResultWrapper::fail($this->objDAdminSetting->error(), ErrorCode::$dberror);
  260. }
  261. if(empty($adminSettingData)){
  262. return ResultWrapper::fail('配置信息错误', ErrorCode::$paramError);
  263. }
  264. $params['settingId'] = $settingId;
  265. $params['createTime'] = time();
  266. $params['paymentType'] = $adminSettingData['signId'];
  267. $returnResult = $this->objDEnterpriseBindPayment->insert($params);
  268. $paymentType = $adminSettingData['signId'];
  269. }
  270. if ($returnResult === false) {
  271. return ResultWrapper::fail($this->objDEnterpriseBindPayment->error(), ErrorCode::$dberror);
  272. }
  273. //处理账户
  274. $paymentData = json_decode($params['paymentData'], true);
  275. // 支付宝 微信 余额 支付自动创建对应的财务账号
  276. if (in_array($paymentType,[StatusCode::$payType['wxPay'], StatusCode::$payType['aliPay'], StatusCode::$payType['balance']])) {
  277. $account = $this->objMAccount->getDefaultAccount($paymentType);
  278. if(!$account->isSuccess()) {
  279. return ResultWrapper::fail($account->getData(), $account->getErrorCode());
  280. }
  281. $accountInfo = $account->getData();
  282. if(empty($accountInfo)) {
  283. //新增一个账户
  284. $name = '';
  285. switch ($paymentType) {
  286. case StatusCode::$payType['wxPay']:
  287. $name = '微信支付';
  288. break;
  289. case StatusCode::$payType['aliPay']:
  290. $name = '支付宝支付';
  291. break;
  292. case StatusCode::$payType['balance']:
  293. $name = '余额支付';
  294. break;
  295. default:
  296. break;
  297. }
  298. $accountData = [
  299. 'enterpriseId' => $this->onlineEnterpriseId,
  300. 'name' => $name,
  301. 'accountNumber' => isset($paymentData['appid'])?$paymentData['appid']:$name,
  302. 'beginMoney' => isset($paymentData['beginMoney']) ? $paymentData['beginMoney'] : 0,
  303. 'shopId' => 0,
  304. 'shopName' => '企业',
  305. 'enableStatus' => StatusCode::$standard,
  306. 'isDefault' => StatusCode::$delete,
  307. 'remark' => $name.'收款默认进入该账户',
  308. 'money'=> isset($paymentData['beginMoney']) ? $paymentData['beginMoney'] : 0,
  309. 'createTime'=>time(),
  310. 'updateTime'=>time(),
  311. 'type'=>$paymentType
  312. ];
  313. $result=$this->objMAccount->addAccount($accountData);
  314. if($result->isSuccess() == false) {
  315. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  316. }
  317. } else{
  318. //修改账户的账号
  319. $accountData['id']= $accountInfo['id'];
  320. $accountData['accountNumber']= $paymentData['appid'];
  321. $result=$this->objMAccount->editAccount($accountData);
  322. if($result->isSuccess() == false) {
  323. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  324. }
  325. }
  326. }
  327. return ResultWrapper::success($returnResult);
  328. }
  329. public function getPayData($paymentType)
  330. {
  331. $objDEnterpriseBindPayment = new DEnterpriseBindPayment();
  332. $dbResult = $objDEnterpriseBindPayment->get(['enterpriseId'=>$this->onlineEnterpriseId,'paymentType'=>$paymentType],'paymentData,id');
  333. if ($dbResult===false){
  334. return ResultWrapper::fail($objDEnterpriseBindPayment->error(),ErrorCode::$dberror);
  335. }
  336. $pay = empty($dbResult['paymentData']) ? [] : json_decode($dbResult['paymentData'],true);
  337. return ResultWrapper::success($pay);
  338. }
  339. }