PaymentChannelOptionsTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\PaymentConfiguration;
  4. use App\Http\Controllers\admin\RechargeChannel;
  5. use App\Http\Controllers\admin\PaymentCompany;
  6. use App\Services\PaymentChannelLinkService;
  7. use App\Services\Payment\RechargeChannelConfigurationService;
  8. use App\Models\PaymentGateway;
  9. use App\Models\RechargeChannel as ChannelModel;
  10. use App\Services\Payment\PaymentProviderService;
  11. use App\Services\PaymentOrderService;
  12. use Illuminate\Config\Repository;
  13. use Illuminate\Database\Capsule\Manager;
  14. use Illuminate\Database\Schema\Blueprint;
  15. use Illuminate\Foundation\Application;
  16. use Illuminate\Http\JsonResponse;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Facade;
  20. use Illuminate\Translation\ArrayLoader;
  21. use Illuminate\Translation\Translator;
  22. use Illuminate\Validation\Factory;
  23. use PHPUnit\Framework\TestCase;
  24. class PaymentChannelOptionsTest extends TestCase
  25. {
  26. private Application $app;
  27. protected function setUp(): void
  28. {
  29. Facade::clearResolvedInstances();
  30. $this->app = new Application(dirname(__DIR__, 2));
  31. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
  32. Facade::setFacadeApplication($this->app);
  33. $db = new Manager($this->app);
  34. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  35. $db->setAsGlobal();
  36. $db->bootEloquent();
  37. $this->app->instance('db', $db->getDatabaseManager());
  38. $this->app->bind('db.schema', fn () => $db->schema());
  39. $translator = new Translator(new ArrayLoader(), 'zh');
  40. $this->app->instance('translator', $translator);
  41. $this->app->instance('validator', new Factory($translator, $this->app));
  42. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
  43. public function json($data, $status = 200, $headers = [], $options = 0) {
  44. return new JsonResponse($data, $status, $headers, $options);
  45. }
  46. });
  47. Request::macro('validate', function (array $rules) {
  48. return app('validator')->make($this->all(), $rules)->validate();
  49. });
  50. $db->schema()->create('recharge_channel', function (Blueprint $t) {
  51. $t->id(); $t->integer('from'); $t->integer('data_type'); $t->string('key');
  52. $t->string('name'); $t->string('type'); $t->decimal('rate', 8, 4);
  53. $t->decimal('min')->nullable(); $t->decimal('max')->nullable(); $t->string('fixed')->nullable();
  54. $t->integer('status'); $t->integer('sort'); $t->timestamps();
  55. });
  56. $db->schema()->create('recharge_channel_group', function (Blueprint $t) {
  57. $t->id(); $t->string('name'); $t->text('recharge_type')->nullable();
  58. $t->text('withdraw_type')->nullable(); $t->text('activity_type')->nullable(); $t->timestamps();
  59. });
  60. $fixture = require dirname(__DIR__) . '/fixtures/payment_provider_channels.php';
  61. DB::table('recharge_channel')->insert($fixture['channels']);
  62. DB::table('recharge_channel_group')->insert($fixture['groups']);
  63. (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up();
  64. $db->schema()->create('operation_audits', function (Blueprint $t) {
  65. $t->id(); $t->string('resource_type'); $t->unsignedBigInteger('resource_id')->nullable();
  66. $t->string('action'); $t->json('changes'); $t->unsignedBigInteger('operator_id')->nullable();
  67. $t->string('operator_name'); $t->timestamp('created_at')->useCurrent();
  68. });
  69. }
  70. protected function tearDown(): void
  71. {
  72. DB::disconnect();
  73. Facade::clearResolvedInstances();
  74. parent::tearDown();
  75. }
  76. private function request(string $path, array $params): void
  77. {
  78. $this->app->instance('request', Request::create($path, 'GET', $params));
  79. }
  80. private function channelList(array $params): array
  81. {
  82. $this->request('/admin/rechargeChannel/list', $params);
  83. $response = (new RechargeChannel())->list()->getData(true);
  84. $this->assertSame(0, $response['code']);
  85. return $response['data'];
  86. }
  87. public function test_company_options_use_all_rows_not_first_page(): void
  88. {
  89. $page1 = $this->channelList(['data_type' => '1']);
  90. $this->assertSame(19, $page1['total']);
  91. $this->assertCount(15, $page1['data']);
  92. $this->assertNotContains('jd', array_column($page1['data'], 'payment_company'));
  93. $page2 = $this->channelList(['data_type' => '1', 'page' => 2]);
  94. $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company'));
  95. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  96. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true);
  97. $this->assertSame(0, $data['code']);
  98. $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled'));
  99. }
  100. public function test_channel_list_displays_provider_separately_from_channel_name(): void
  101. {
  102. $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']);
  103. $this->assertSame(1, $data['total']);
  104. $this->assertSame('JD钱包', $data['data'][0]['name']);
  105. $this->assertSame('JD支付', $data['data'][0]['payment_company_label']);
  106. $this->assertSame(1, $data['data'][0]['from']);
  107. $this->assertSame(24, $data['data'][0]['id']);
  108. $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']);
  109. $this->assertSame(25, $withdraw['data'][0]['id']);
  110. $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']);
  111. }
  112. public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void
  113. {
  114. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']);
  115. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  116. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  117. $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled'));
  118. $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code'));
  119. $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type'));
  120. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  121. }
  122. public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void
  123. {
  124. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']);
  125. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  126. $response = (new RechargeChannel())->getChannel()->getData(true);
  127. $this->assertSame(0, $response['code']);
  128. $names = array_column($response['data']['data'], 'name', 'type');
  129. $this->assertSame('JD钱包', $names['JDpay']);
  130. $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']);
  131. $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']);
  132. $this->assertSame('808充值', $names['ZIMUpay']);
  133. $this->assertSame(13, $response['data']['total']);
  134. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]);
  135. $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type');
  136. $this->assertSame('JD钱包', $names['JDpay']);
  137. $this->assertSame('NO快捷提现', $names['NOwithdraw']);
  138. $this->assertSame('808账户提现', $names['ZIMUwithdraw']);
  139. $this->assertArrayNotHasKey('ZIMUcash', $names);
  140. $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
  141. }
  142. private function formOptions(int $dataType = 1, ?int $id = null): array
  143. {
  144. $this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
  145. $response = (new RechargeChannel())->options(new RechargeChannelConfigurationService())->getData(true);
  146. $this->assertSame(0, $response['code']);
  147. return $response['data'];
  148. }
  149. private function saveChannel(array $params): array
  150. {
  151. $this->app->instance('request', Request::create('/admin/rechargeChannel/update', 'POST', $params));
  152. return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true);
  153. }
  154. private function newChannel(array $overrides = []): array
  155. {
  156. return $overrides + ['data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12',
  157. 'rate' => '0.0300', 'name' => '测试通道', 'key' => 'test-key', 'min' => 1, 'max' => 100];
  158. }
  159. public function test_creation_catalog_works_without_any_database_channels_or_groups(): void
  160. {
  161. DB::table('recharge_channel')->delete();
  162. DB::table('recharge_channel_group')->delete();
  163. $data = $this->formOptions();
  164. $this->assertSame(['sanjin', 'jd', 'no', 'zimu'], array_column($data['payment_companies'], 'value'));
  165. $this->assertTrue($data['identity_editable']);
  166. $this->assertSame('', $data['identity_edit_reason']);
  167. $this->assertContains('NOpay12', array_column($data['types'], 'value'));
  168. $this->assertSame(['qianbao', 'jd', 'no', 'zimu'], array_column($this->formOptions(2)['payment_companies'], 'value'));
  169. $this->assertSame([], $this->formOptions(3)['payment_companies']);
  170. }
  171. public function test_create_selected_company_then_switch_company_and_type_on_same_record(): void
  172. {
  173. $result = $this->saveChannel($this->newChannel());
  174. $this->assertSame(0, $result['code']);
  175. $this->assertSame([], $result['data']);
  176. $row = DB::table('recharge_channel')->where('key', 'test-key')->first();
  177. $this->assertSame(1, $row->from);
  178. $this->assertSame(1, $row->status);
  179. $this->assertSame(0, $row->sort);
  180. $result = $this->saveChannel(['id' => $row->id, 'data_type' => 1,
  181. 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']);
  182. $this->assertSame(0, $result['code']);
  183. $data = $this->channelList(['data_type' => '1', 'key' => 'test-key'])['data'][0];
  184. $this->assertSame($row->id, $data['id']);
  185. $this->assertSame('jd', $data['payment_company']);
  186. $this->assertSame('JD支付 / 测试通道', $data['display_name']);
  187. }
  188. public function test_invalid_company_type_or_source_is_rejected_without_writing(): void
  189. {
  190. $before = DB::table('recharge_channel')->count();
  191. foreach ([
  192. ['payment_company' => 'jd', 'type' => 'NOpay12'],
  193. ['payment_company' => 'no', 'type' => '12'],
  194. ['payment_company' => 'no', 'type' => 'usdt'],
  195. ['payment_company' => 'no', 'from' => 2],
  196. ['payment_company' => 'sanjin', 'type' => 'DF001', 'data_type' => 2],
  197. ] as $override) {
  198. $this->assertSame(-3, $this->saveChannel($this->newChannel($override))['code']);
  199. }
  200. $this->assertSame($before, DB::table('recharge_channel')->count());
  201. }
  202. public function test_native_and_activity_channels_do_not_require_or_fabricate_company(): void
  203. {
  204. foreach ([[1, 'usdt', 2], [1, 'rgcz', 3], [2, 'rgtx', 3], [3, 'recharge', 1]] as [$direction, $type, $from]) {
  205. $result = $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => $direction,
  206. 'type' => $type, 'key' => 'local-' . $type]));
  207. $this->assertSame(0, $result['code']);
  208. $row = $this->channelList(['key' => 'local-' . $type])['data'][0];
  209. $this->assertSame($from, $row['from']);
  210. $this->assertNull($row['payment_company']);
  211. $this->assertSame('测试通道', $row['display_name']);
  212. }
  213. }
  214. public function test_existing_valid_clients_can_omit_derived_company_and_from(): void
  215. {
  216. $params = $this->newChannel();
  217. unset($params['payment_company']);
  218. $this->assertSame(0, $this->saveChannel($params)['code']);
  219. $row = $this->channelList(['key' => 'test-key'])['data'][0];
  220. $this->assertSame('no', $row['payment_company']);
  221. $this->assertSame(1, $row['from']);
  222. }
  223. public function test_linked_channel_locks_identity_but_keeps_regular_edit_available(): void
  224. {
  225. DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
  226. $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
  227. });
  228. DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
  229. $options = $this->formOptions(1, 24);
  230. $this->assertFalse($options['identity_editable']);
  231. $this->assertNotSame('', $options['identity_edit_reason']);
  232. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no',
  233. 'type' => 'NOpay12', 'rate' => '0.01']);
  234. $this->assertSame(-3, $result['code']);
  235. $this->assertSame('JDpay', DB::table('recharge_channel')->where('id', 24)->value('type'));
  236. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd',
  237. 'type' => 'JDpay', 'rate' => '0.01', 'name' => 'JD新名称']);
  238. $this->assertSame(0, $result['code']);
  239. DB::table('payment_gateways')->update(['deleted_at' => now()]);
  240. $this->assertTrue($this->formOptions(1, 24)['identity_editable']);
  241. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  242. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  243. }
  244. public function test_switching_company_does_not_rewrite_group_permissions(): void
  245. {
  246. $groups = DB::table('recharge_channel_group')->orderBy('id')->get()->toJson();
  247. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  248. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  249. $this->assertSame($groups, DB::table('recharge_channel_group')->orderBy('id')->get()->toJson());
  250. $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 2,
  251. 'payment_company' => 'no', 'type' => 'NOwithdraw', 'rate' => '0.01'])['code']);
  252. }
  253. public function test_group_option_names_include_company_without_changing_values(): void
  254. {
  255. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  256. $rows = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], null, 'type');
  257. $this->assertSame('JD钱包', $rows['JDpay']['name']);
  258. $this->assertSame('jd', $rows['JDpay']['payment_company']);
  259. $this->assertSame('JD支付 / JD钱包', $rows['JDpay']['display_name']);
  260. $this->assertSame('USDT充值', $rows['usdt']['display_name']);
  261. }
  262. public function test_merchant_save_rechecks_channel_identity_inside_transaction(): void
  263. {
  264. DB::table('recharge_channel')->where('id', 26)->update(['type' => 'JDpay']);
  265. $this->request('/admin/paymentConfig/gateways/save', []);
  266. $controller = new PaymentConfiguration(new PaymentChannelLinkService());
  267. $method = new \ReflectionMethod($controller, 'saveModel');
  268. $method->setAccessible(true);
  269. $this->expectException(\InvalidArgumentException::class);
  270. $this->expectExceptionMessage('支付公司与所选通道不匹配');
  271. $method->invoke($controller, PaymentGateway::class, ['kind' => 'deposit', 'recharge_channel_id' => 26,
  272. 'recharge_channel_group_ids' => [1], 'payment_company' => 'no', 'payment_method' => 'NOpay12'], 'payment_gateway');
  273. }
  274. private function setCompanyStatus(string $code, int $status): array
  275. {
  276. $this->app->instance('request', Request::create('/admin/paymentCompany/status', 'POST', ['payment_company' => $code, 'status' => $status]));
  277. return (new PaymentCompany())->status(new PaymentProviderService())->getData(true);
  278. }
  279. private function deleteChannel(int $id): array
  280. {
  281. $this->app->instance('request', Request::create('/admin/rechargeChannel/delete', 'POST', ['id' => $id]));
  282. return (new RechargeChannel())->delete(new RechargeChannelConfigurationService())->getData(true);
  283. }
  284. public function test_company_management_lists_registered_providers_and_filters_status(): void
  285. {
  286. $this->request('/admin/paymentCompany/list', []);
  287. $result = (new PaymentCompany())->list(new PaymentProviderService())->getData(true);
  288. $this->assertSame(0, $result['code']);
  289. $this->assertSame(5, $result['data']['total']);
  290. $rows = array_column($result['data']['data'], null, 'payment_company');
  291. $this->assertSame('SHA256', $rows['no']['signature_algorithm']);
  292. $this->assertSame([1, 2], $rows['no']['data_types']);
  293. $this->assertSame(3, $rows['no']['channel_count']);
  294. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  295. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  296. $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'payment_company')->count());
  297. $this->request('/admin/paymentCompany/list', ['status' => 0]);
  298. $this->assertSame(1, (new PaymentCompany())->list(new PaymentProviderService())->getData(true)['data']['total']);
  299. $this->assertSame(-3, $this->setCompanyStatus('unknown', 0)['code']);
  300. $this->assertSame(-3, $this->setCompanyStatus('no', 9)['code']);
  301. }
  302. public function test_company_switch_disables_options_and_runtime_without_rewriting_channels(): void
  303. {
  304. $groups = DB::table('recharge_channel_group')->get()->toJson();
  305. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  306. $this->assertFalse(ChannelModel::checkRechargeChannel('NOpay12', 1));
  307. $this->assertFalse(ChannelModel::checkWithdrawChannel('NOwithdraw', 1));
  308. $this->assertNotFalse(ChannelModel::checkRechargeChannel('JDpay', 1));
  309. $this->assertNotFalse(ChannelModel::checkRechargeChannel('usdt', 1));
  310. $this->assertArrayNotHasKey('NOpay12', ChannelModel::product(1));
  311. $this->assertSame(1, DB::table('recharge_channel')->where('id', 26)->value('status'));
  312. $this->assertSame($groups, DB::table('recharge_channel_group')->get()->toJson());
  313. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  314. $options = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  315. $companies = array_column($options['payment_companies'], null, 'value');
  316. $this->assertSame('provider_disabled', $companies['no']['disabled_reason_code']);
  317. $this->assertFalse(array_column($options['recharge_channels'], null, 'id')[26]['selectable']);
  318. $form = $this->formOptions();
  319. $this->assertTrue(array_column($form['payment_companies'], null, 'value')['no']['disabled']);
  320. $row = $this->channelList(['data_type' => '1', 'type' => 'NOpay12'])['data'][0];
  321. $this->assertSame(1, $row['status']);
  322. $this->assertSame(0, $row['company_status']);
  323. $this->assertSame(0, $row['effective_status']);
  324. $this->assertSame(-3, $this->saveChannel($this->newChannel())['code']);
  325. $this->assertSame(0, $this->setCompanyStatus('no', 1)['code']);
  326. $this->assertNotFalse(ChannelModel::checkRechargeChannel('NOpay12', 1));
  327. }
  328. public function test_reenable_company_does_not_enable_individually_disabled_channels(): void
  329. {
  330. $this->setCompanyStatus('zimu', 0);
  331. $this->setCompanyStatus('zimu', 1);
  332. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  333. $this->assertFalse(ChannelModel::checkWithdrawChannel('ZIMUcash', 1));
  334. $this->assertNotFalse(ChannelModel::checkWithdrawChannel('ZIMUwithdraw', 1));
  335. }
  336. public function test_disabled_company_blocks_new_external_requests_before_any_wallet_or_network_work(): void
  337. {
  338. $this->setCompanyStatus('no', 0);
  339. $this->assertSame('支付公司已禁用', PaymentOrderService::createPay('test-user', 100, 'NOpay12')['text']);
  340. $this->assertSame('支付公司已禁用', PaymentOrderService::autoCreatePayout('test-user', 100, 'NOwithdraw', '', '', '')['text']);
  341. $this->assertFalse(PaymentProviderService::requestEnabled('nopay12', 1));
  342. $this->assertTrue(PaymentProviderService::requestEnabled('JDpay', 1));
  343. }
  344. public function test_delete_last_deposit_type_soft_deletes_and_cleans_only_its_group_membership(): void
  345. {
  346. $beforeWithdraw = DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type');
  347. $this->assertSame(0, $this->deleteChannel(24)['code']);
  348. $this->assertNull(ChannelModel::query()->find(24));
  349. $deleted = ChannelModel::withTrashed()->findOrFail(24);
  350. $this->assertTrue($deleted->trashed());
  351. $this->assertSame(0, (int)$deleted->status);
  352. $this->assertSame('JD钱包', $deleted->name);
  353. $types = explode(',', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  354. $this->assertNotContains('JDpay', $types);
  355. $this->assertContains('NOpay12', $types);
  356. $this->assertSame($beforeWithdraw, DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type'));
  357. $this->assertNotFalse(ChannelModel::checkWithdrawChannel('JDpay', 1));
  358. $this->assertFalse(ChannelModel::checkRechargeChannel('JDpay', 1));
  359. $this->assertSame(0, $this->deleteChannel(24)['code']);
  360. $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'recharge_channel')->count());
  361. $this->setCompanyStatus('jd', 0); $this->setCompanyStatus('jd', 1);
  362. $this->assertNull(ChannelModel::query()->find(24));
  363. }
  364. public function test_delete_one_of_several_same_type_channels_preserves_groups_until_last_removed(): void
  365. {
  366. $this->assertSame(0, $this->deleteChannel(6)['code']);
  367. $this->assertStringContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  368. $this->assertSame(3, ChannelModel::query()->where('data_type', 1)->where('type', 'zfbsm')->count());
  369. foreach ([7, 8, 9] as $id) $this->assertSame(0, $this->deleteChannel($id)['code']);
  370. $this->assertStringNotContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  371. }
  372. public function test_delete_is_blocked_by_payment_config_reference_and_does_not_touch_activity(): void
  373. {
  374. DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
  375. $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
  376. });
  377. DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
  378. $this->assertSame(-3, $this->deleteChannel(24)['code']);
  379. $this->assertNotNull(ChannelModel::query()->find(24));
  380. $this->assertSame(-3, $this->deleteChannel(21)['code']);
  381. $this->assertNotNull(ChannelModel::query()->find(21));
  382. DB::table('payment_gateways')->update(['deleted_at' => now()]);
  383. $this->assertSame(0, $this->deleteChannel(24)['code']);
  384. }
  385. public function test_migration_retry_keeps_disabled_company_and_deleted_channel(): void
  386. {
  387. $this->setCompanyStatus('no', 0);
  388. $this->deleteChannel(24);
  389. (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up();
  390. $this->assertSame(0, PaymentProviderService::statuses()['no']);
  391. $this->assertTrue(ChannelModel::withTrashed()->findOrFail(24)->trashed());
  392. $this->assertSame(5, DB::table('payment_providers')->count());
  393. }
  394. private function paymentOrderFixture(int $type, string $channel, int $status): \App\Models\PaymentOrder
  395. {
  396. DB::connection()->getSchemaBuilder()->create('payment_orders', function (Blueprint $t) {
  397. $t->id(); $t->integer('type'); $t->string('channel'); $t->integer('status');
  398. $t->string('order_no'); $t->string('member_id'); $t->decimal('amount', 18, 4);
  399. $t->string('state')->nullable(); $t->text('callback_data')->nullable(); $t->timestamps();
  400. });
  401. $this->app->instance('log', new class {
  402. public function channel($channel) { return $this; }
  403. public function error($message, array $context = []) {}
  404. });
  405. return \App\Models\PaymentOrder::query()->create(['type' => $type, 'channel' => $channel,
  406. 'status' => $status, 'order_no' => 'test-order', 'member_id' => 'test-user', 'amount' => 10]);
  407. }
  408. public function test_pending_payout_cannot_be_sent_after_company_disabled(): void
  409. {
  410. $order = $this->paymentOrderFixture(2, 'NOwithdraw', PaymentOrderService::STATUS_STAY);
  411. $this->setCompanyStatus('no', 0);
  412. $result = PaymentOrderService::createPayout($order->id);
  413. $this->assertSame(-3, $result['code']);
  414. $this->assertSame('支付公司已禁用', $result['msg']);
  415. $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status);
  416. }
  417. public function test_pending_payout_cannot_be_sent_after_last_channel_deleted(): void
  418. {
  419. $order = $this->paymentOrderFixture(2, 'JDpay', PaymentOrderService::STATUS_STAY);
  420. $this->assertSame(0, $this->deleteChannel(25)['code']);
  421. $result = PaymentOrderService::createPayout($order->id);
  422. $this->assertSame(-3, $result['code']);
  423. $this->assertSame('提现通道已停用或删除', $result['msg']);
  424. $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status);
  425. }
  426. public function test_existing_pay_callback_is_not_blocked_by_company_switch(): void
  427. {
  428. $order = $this->paymentOrderFixture(1, 'NOpay12', PaymentOrderService::STATUS_PROCESS);
  429. $this->setCompanyStatus('no', 0);
  430. $method = new \ReflectionMethod(PaymentOrderService::class, 'applyPayCallback');
  431. $method->setAccessible(true);
  432. $this->assertTrue($method->invoke(null, $order, '10', 'failed', 'success', 'failed', ['status' => 'failed']));
  433. $this->assertSame(PaymentOrderService::STATUS_FAIL, $order->fresh()->status);
  434. $this->assertNotNull($order->fresh()->callback_data);
  435. }
  436. public function test_company_disabled_updates_saved_gateway_effective_status(): void
  437. {
  438. $channel = ChannelModel::query()->findOrFail(26);
  439. $gateway = new PaymentGateway(['payment_company' => 'no', 'payment_method' => 'NOpay12',
  440. 'status' => 1, 'recharge_channel_group_ids' => [1]]);
  441. $gateway->setRelation('rechargeChannel', $channel);
  442. $this->setCompanyStatus('no', 0);
  443. $data = (new PaymentChannelLinkService())->formatOne($gateway);
  444. $this->assertSame(1, $data['config_status']);
  445. $this->assertSame(1, $data['channel_status']);
  446. $this->assertSame(0, $data['company_status']);
  447. $this->assertSame(0, $data['effective_status']);
  448. $this->expectException(\RuntimeException::class);
  449. $this->expectExceptionMessage('NO支付已禁用');
  450. (new PaymentChannelLinkService())->validate(26, [1], 1);
  451. }
  452. public function test_empty_group_types_can_be_saved_after_last_channel_removed(): void
  453. {
  454. $this->app->instance('request', Request::create('/admin/rechargeChannel/updateGroup', 'POST',
  455. ['id' => 1, 'name' => '全部充值', 'recharge_type' => [], 'withdraw_type' => []]));
  456. $this->assertSame(0, (new RechargeChannel())->updateGroup()->getData(true)['code']);
  457. $this->assertSame('', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  458. $this->assertSame('yuebao,old_user,recharge', DB::table('recharge_channel_group')->where('id', 1)->value('activity_type'));
  459. }
  460. }