PayController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Constants\RedisKeyConstant;
  5. use App\Models\PaymentAttrModel;
  6. use App\Models\PaymentModel;
  7. use App\Models\PaymentTypeModel;
  8. use App\Repositories\Service\CommonService;
  9. use Illuminate\Support\Facades\Cache;
  10. use Modules\ManageApi\Services\MerchantServices;
  11. class PayController extends BaseController
  12. {
  13. /**
  14. * 支付方式列表
  15. *
  16. * @return \Illuminate\Http\JsonResponse|mixed
  17. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  18. */
  19. public function paymentList()
  20. {
  21. $inputData = $this->getJsonData();
  22. $rules = [
  23. 'page' => 'nullable|integer',
  24. 'page_size' => 'nullable|integer'
  25. ];
  26. $validator = app('validator')->make($inputData, $rules);
  27. if ($validator->fails()) {
  28. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  29. return json_fail($validator->errors()->first());
  30. }
  31. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  32. if ($pageSize > 500) {
  33. $pageSize = CommonConstant::PAGE_SIZE;
  34. }
  35. $page = format_page($inputData['page'] ?? 1);
  36. $model = PaymentModel::query();
  37. if (!empty($inputData['name'])) {
  38. $model = $model->where('name', 'like', '%' . $inputData['name'] . '%');
  39. }
  40. if (!empty($inputData['status'])) {
  41. $model = $model->where('status', $inputData['status']);
  42. }
  43. $res = $model->orderByDesc('created_at')->paginate( $pageSize, ["*"], 'page', $page);
  44. return json_success($res);
  45. }
  46. /**
  47. * 支付方式添加
  48. *
  49. * @return \Illuminate\Http\JsonResponse|mixed
  50. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  51. */
  52. public function paymentAdd()
  53. {
  54. $inputData = $this->getJsonData();
  55. $rules = [
  56. 'name' => 'required|string',
  57. 'pay_code' => 'required|string',
  58. 'partner_id' => 'required|string',
  59. ];
  60. $validator = app('validator')->make($inputData, $rules);
  61. if ($validator->fails()) {
  62. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  63. return json_fail($validator->errors()->first());
  64. }
  65. $setarr = [
  66. 'name' => $inputData['name'],
  67. 'pay_code' => $inputData['pay_code'],
  68. 'partner_id' => $inputData['partner_id'],
  69. ];
  70. $res = PaymentModel::query()->create($setarr);
  71. if ($res) {
  72. return json_success([], '添加成功');
  73. } else {
  74. return json_fail('添加失败');
  75. }
  76. }
  77. /**
  78. * 支付方式修改
  79. *
  80. * @return \Illuminate\Http\JsonResponse|mixed
  81. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  82. */
  83. public function paymentEdit()
  84. {
  85. $inputData = $this->getJsonData();
  86. $rules = [
  87. 'name' => 'nullable|string',
  88. 'pay_code' => 'nullable|string',
  89. 'partner_id' => 'nullable|string',
  90. 'status' => 'nullable|in:1,2',
  91. 'uid' => 'required|string',
  92. ];
  93. $validator = app('validator')->make($inputData, $rules);
  94. if ($validator->fails()) {
  95. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  96. return json_fail($validator->errors()->first());
  97. }
  98. $info = PaymentModel::query()->where('uid', $inputData['uid'])->first();
  99. if (!$info) {
  100. return json_fail('操作的支付方式不存在');
  101. }
  102. if (!empty($inputData['name'])) {
  103. $info->name = $inputData['name'];
  104. }
  105. if (!empty($inputData['pay_code'])) {
  106. $info->pay_code = $inputData['pay_code'];
  107. }
  108. if (!empty($inputData['partner_id'])) {
  109. $info->partner_id = $inputData['partner_id'];
  110. }
  111. if (!empty($inputData['status'])) {
  112. $info->status = $inputData['status'];
  113. }
  114. if ($info->save()) {
  115. return json_success([], '修改成功');
  116. } else {
  117. return json_fail('修改失败');
  118. }
  119. }
  120. /**
  121. * 支付通道列表
  122. *
  123. * @return \Illuminate\Http\JsonResponse|mixed
  124. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  125. */
  126. public function paymentAttrList()
  127. {
  128. $inputData = $this->getJsonData();
  129. $rules = [
  130. 'page' => 'required|integer',
  131. 'page_size' => 'required|integer',
  132. 'name' => 'nullable|string',
  133. 'pay_uid' => 'nullable',
  134. 'status' => 'nullable|in:1,2',
  135. ];
  136. $validator = app('validator')->make($inputData, $rules);
  137. if ($validator->fails()) {
  138. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  139. return json_fail($validator->errors()->first());
  140. }
  141. $model = PaymentAttrModel::query();
  142. if (!empty($inputData['pay_uid'])) {
  143. $model = $model->where('pay_uid', $inputData['pay_uid']);
  144. }
  145. if (!empty($inputData['status'])) {
  146. $model = $model->where('status', $inputData['status']);
  147. }
  148. if (!empty($inputData['name'])) {
  149. $model = $model->where('name', 'like', '%' . $inputData['name'] . '%');
  150. }
  151. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  152. if ($pageSize > 500) {
  153. $pageSize = CommonConstant::PAGE_SIZE;
  154. }
  155. $page = format_page($inputData['page'] ?? 0);
  156. $res = $model->orderByDesc('created_at')->paginate( $pageSize, ["*"], 'page', $page);
  157. return json_success($res);
  158. }
  159. /**
  160. * 支付通道添加
  161. *
  162. * @return \Illuminate\Http\JsonResponse|mixed
  163. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  164. */
  165. public function paymentAttrAdd()
  166. {
  167. $inputData = $this->getJsonData();
  168. $rules = [
  169. 'pay_uid' => 'required|string',
  170. 'code' => 'required|string',
  171. 'name' => 'required|string',
  172. ];
  173. $validator = app('validator')->make($inputData, $rules);
  174. if ($validator->fails()) {
  175. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  176. return json_fail($validator->errors()->first());
  177. }
  178. $setarr = [
  179. 'name' => $inputData['name'],
  180. 'pay_uid' => $inputData['pay_uid'],
  181. 'code' => $inputData['code'],
  182. ];
  183. $res = PaymentAttrModel::query()->create($setarr);
  184. if ($res) {
  185. return json_success([], '添加成功');
  186. } else {
  187. return json_fail('添加失败');
  188. }
  189. }
  190. /**
  191. * 支付通道修改
  192. *
  193. * @return \Illuminate\Http\JsonResponse|mixed
  194. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  195. */
  196. public function paymentAttrEdit()
  197. {
  198. $inputData = $this->getJsonData();
  199. $rules = [
  200. 'pay_uid' => 'nullable|string',
  201. 'code' => 'nullable|string',
  202. 'name' => 'nullable|string',
  203. 'uid' => 'required|string',
  204. 'status' => 'nullable|in:1,2'
  205. ];
  206. $validator = app('validator')->make($inputData, $rules);
  207. if ($validator->fails()) {
  208. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  209. return json_fail($validator->errors()->first());
  210. }
  211. $info = PaymentAttrModel::query()->where('uid', $inputData['uid'])->first();
  212. if (!$info) {
  213. return json_fail('操作的支付通道不存在');
  214. }
  215. if (!empty($inputData['name'])) {
  216. $info->name = $inputData['name'];
  217. }
  218. if (!empty($inputData['pay_uid'])) {
  219. $info->pay_uid = $inputData['pay_uid'];
  220. }
  221. if (!empty($inputData['code'])) {
  222. $info->code = $inputData['code'];
  223. }
  224. if (!empty($inputData['status'])) {
  225. $info->status = $inputData['status'];
  226. }
  227. if ($info->save()) {
  228. return json_success([], '修改成功');
  229. } else {
  230. return json_fail('修改失败');
  231. }
  232. }
  233. /**
  234. * 支付类型列表
  235. *
  236. * @return \Illuminate\Http\JsonResponse|mixed
  237. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  238. */
  239. public function paymentTypeList()
  240. {
  241. $inputData = $this->getJsonData();
  242. $rules = [
  243. 'page' => 'nullable|integer',
  244. 'page_size' => 'nullable|integer',
  245. 'payment_uid' => 'nullable|string',
  246. 'status' => 'nullable|in:1,2',
  247. 'pay_type' => 'nullable|in:1,2,3,4,5',
  248. 'belong_device' => 'nullable|in:A,I',
  249. ];
  250. $validator = app('validator')->make($inputData, $rules);
  251. if ($validator->fails()) {
  252. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  253. return json_fail($validator->errors()->first());
  254. }
  255. $model = PaymentTypeModel::query();
  256. if (!empty($inputData['payment_uid'])) {
  257. $model->payment_uid = $model->where('payment_uid', $inputData['payment_uid']);
  258. }
  259. if (!empty($inputData['status'])) {
  260. $model = $model->where('status', $inputData['status']);
  261. }
  262. if (!empty($inputData['pay_type'])) {
  263. $model = $model->where('pay_type', $inputData['pay_type']);
  264. }
  265. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  266. if ($pageSize > 500) {
  267. $pageSize = CommonConstant::PAGE_SIZE;
  268. }
  269. $page = format_page($inputData['page'] ?? 1);
  270. $res = $model->orderByDesc('weights')->paginate( $pageSize, ["*"], 'page', $page);
  271. foreach ($res->items() as &$val) {
  272. $val->payment_attr_code = $val->paymentAttr->code ?? '';
  273. unset($val->paymentAttr);
  274. }
  275. return json_success($res);
  276. }
  277. /**
  278. * 支付类型添加
  279. *
  280. * @return \Illuminate\Http\JsonResponse|mixed
  281. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  282. */
  283. public function paymentTypeAdd()
  284. {
  285. $inputData = $this->getJsonData();
  286. $rules = [
  287. 'name' => 'required|string',
  288. 'payment_uid' => 'required|string',
  289. 'payment_attr_uid' => 'required|string',
  290. 'pay_type' => 'required|in:1,2,3,4,5',
  291. 'limit_type' => 'required|in:1,2',
  292. 'limit' => 'required|string',
  293. 'weights' => 'required|integer',
  294. 'remark' => 'nullable|string',
  295. 'status' => 'required|in:1,2',
  296. 'belong_device' => 'nullable|in:I,A',
  297. ];
  298. $validator = app('validator')->make($inputData, $rules);
  299. if ($validator->fails()) {
  300. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  301. return json_fail($validator->errors()->first());
  302. }
  303. $setarr = [
  304. 'name' => $inputData['name'],
  305. 'payment_uid' => $inputData['payment_uid'],
  306. 'payment_attr_uid' => $inputData['payment_attr_uid'],
  307. 'pay_type' => $inputData['pay_type'],
  308. 'limit_type' => $inputData['limit_type'],
  309. 'limit' => $inputData['limit'],
  310. 'weights' => $inputData['weights'],
  311. 'remark' => $inputData['remark'],
  312. 'status' => $inputData['status'],
  313. 'rate' => $inputData['rate'],
  314. 'belong_device' => $inputData['belong_device'],
  315. ];
  316. $res = PaymentTypeModel::query()->create($setarr);
  317. if ($res) {
  318. return json_success([], '添加成功');
  319. } else {
  320. return json_fail('添加失败');
  321. }
  322. }
  323. /**
  324. * 支付类型修改
  325. *
  326. * @return \Illuminate\Http\JsonResponse|mixed
  327. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  328. */
  329. public function paymentTypeEdit()
  330. {
  331. $inputData = $this->getJsonData();
  332. $rules = [
  333. 'name' => 'nullable|string',
  334. 'payment_uid' => 'nullable|string',
  335. 'payment_attr_uid' => 'nullable|string',
  336. 'pay_type' => 'nullable|in:1,2,3,4,5',
  337. 'limit_type' => 'nullable|in:1,2',
  338. 'limit' => 'nullable|string',
  339. 'weights' => 'nullable|integer',
  340. 'remark' => 'nullable|string',
  341. 'status' => 'nullable|in:1,2',
  342. 'uid' => 'required|string',
  343. 'belong_device' => 'nullable|in:I,A',
  344. ];
  345. $validator = app('validator')->make($inputData, $rules);
  346. if ($validator->fails()) {
  347. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  348. return json_fail($validator->errors()->first());
  349. }
  350. $info = PaymentTypeModel::query()->where('uid', $inputData['uid'])->first();
  351. if (!$info) {
  352. return json_fail('操作的支付类型不存在');
  353. }
  354. if (!empty($inputData['name'])) {
  355. $info->name = $inputData['name'];
  356. }
  357. if (!empty($inputData['payment_uid'])) {
  358. $info->payment_uid = $inputData['payment_uid'];
  359. }
  360. if (!empty($inputData['payment_attr_uid'])) {
  361. $info->payment_attr_uid = $inputData['payment_attr_uid'];
  362. }
  363. if (!empty($inputData['pay_type'])) {
  364. $info->pay_type = $inputData['pay_type'];
  365. }
  366. if (!empty($inputData['limit_type'])) {
  367. $info->limit_type = $inputData['limit_type'];
  368. }
  369. if (!empty($inputData['belong_device'])) {
  370. $info->belong_device = $inputData['belong_device'];
  371. }
  372. if (!empty($inputData['limit'])) {
  373. $info->limit = $inputData['limit'];
  374. }
  375. if (!empty($inputData['weights'])) {
  376. $info->weights = $inputData['weights'];
  377. }
  378. if (!empty($inputData['status'])) {
  379. $info->status = $inputData['status'];
  380. }
  381. if (!empty($inputData['rate'])) {
  382. $info->rate = $inputData['rate'];
  383. }
  384. $info->remark = $inputData['remark'];
  385. if ($info->save()) {
  386. Cache::forget(RedisKeyConstant::PAYMENTTYPE);
  387. app(MerchantServices::class)->clearCache();
  388. app(CommonService::class)->clearAllMerchantCache();
  389. return json_success([], '修改成功');
  390. } else {
  391. return json_fail('修改失败');
  392. }
  393. }
  394. /**
  395. * 支付类型删除
  396. *
  397. * @return \Illuminate\Http\JsonResponse|mixed
  398. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  399. */
  400. public function paymentTypeDel()
  401. {
  402. $inputData = $this->getJsonData();
  403. $rules = [
  404. 'uid' => 'required|string',
  405. ];
  406. $validator = app('validator')->make($inputData, $rules);
  407. if ($validator->fails()) {
  408. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  409. return json_fail($validator->errors()->first());
  410. }
  411. $res = PaymentTypeModel::query()->where('uid', $inputData['uid'])->delete();
  412. if ($res) {
  413. Cache::forget(RedisKeyConstant::PAYMENTTYPE);
  414. app(CommonService::class)->clearAllMerchantCache();
  415. return json_success([], '删除成功');
  416. } else {
  417. return json_fail('删除失败');
  418. }
  419. }
  420. /**
  421. * 下拉
  422. *
  423. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  424. */
  425. public function dropDown()
  426. {
  427. $inputData = $this->getJsonData();
  428. $rules = [
  429. 'down_type' => 'required|in:1,2,3',
  430. 'pay_uid' => 'nullable|string'
  431. ];
  432. $validator = app('validator')->make($inputData, $rules);
  433. if ($validator->fails()) {
  434. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  435. return json_fail($validator->errors()->first());
  436. }
  437. if ($inputData['down_type'] == 1) {
  438. $res = PaymentModel::query()->get(['uid', 'name'])->keyBy('uid');
  439. } elseif ($inputData['down_type'] == 2) {
  440. $model = PaymentAttrModel::query();
  441. if (!empty($inputData['pay_uid'])) {
  442. $model = $model->where('pay_uid', $inputData['pay_uid']);
  443. }
  444. $res = $model->get(['uid', 'name', 'pay_uid'])->keyBy('uid');
  445. } else {
  446. $res = PaymentTypeModel::query()->where('status', 1)->get(['uid', 'name', 'payment_uid'])->keyBy('uid');
  447. if ($res->isNotEmpty()) {
  448. foreach ($res as &$val) {
  449. $val->name = $val->payment->name . '-' . $val->name;
  450. unset($val->payment, $val->payment_uid);
  451. }
  452. }
  453. }
  454. return $res;
  455. }
  456. }