PaymentChannelOptionsTest.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 \Tests\Support\JsonResponseFactory());
  43. Request::macro('validate', function (array $rules) {
  44. return app('validator')->make($this->all(), $rules)->validate();
  45. });
  46. $db->schema()->create('recharge_channel', function (Blueprint $t) {
  47. $t->id(); $t->integer('from'); $t->integer('data_type'); $t->string('key');
  48. $t->string('name'); $t->string('type'); $t->decimal('rate', 8, 4);
  49. $t->decimal('min')->nullable(); $t->decimal('max')->nullable(); $t->string('fixed')->nullable();
  50. $t->integer('status'); $t->integer('sort'); $t->timestamps();
  51. });
  52. $db->schema()->create('recharge_channel_group', function (Blueprint $t) {
  53. $t->id(); $t->string('name'); $t->text('recharge_type')->nullable();
  54. $t->text('withdraw_type')->nullable(); $t->text('activity_type')->nullable(); $t->timestamps();
  55. });
  56. $fixture = require dirname(__DIR__) . '/fixtures/payment_provider_channels.php';
  57. DB::table('recharge_channel')->insert($fixture['channels']);
  58. DB::table('recharge_channel_group')->insert($fixture['groups']);
  59. (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up();
  60. (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_170000_add_soft_deletes_to_recharge_channel_groups.php')->up();
  61. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_150000_add_recharge_channel_logo.php')->up();
  62. (require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_160000_create_recharge_method_logos.php')->up();
  63. (require dirname(__DIR__, 2) . '/database/migrations/2026_11_09_130000_add_fee_fixed_to_recharge_channel.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_sanjin_fixed_amounts_accept_list_roundtrip_without_any_minimum_or_maximum(): void
  88. {
  89. $params = ['id' => 10, 'data_type' => 1, 'payment_company' => 'sanjin', 'type' => 'zfbge', 'rate' => '0.027'];
  90. $result = $this->saveChannel($params + ['fixed' => [945, '988.00000000', 990, 945], 'min' => 'not applicable', 'max' => ['unused']]);
  91. $this->assertSame(0, $result['code'], $result['msg']);
  92. $row = ChannelModel::find(10);
  93. $this->assertSame(['945', '988', '990'], $row->fixed);
  94. $this->assertNull($row->min); $this->assertNull($row->max);
  95. $result = $this->saveChannel($params + ['fixed' => $row->fixed]);
  96. $this->assertSame(0, $result['code'], $result['msg']);
  97. $this->assertSame(0, $this->saveChannel($params + ['fixed' => '100, 200.50'])['code']);
  98. $this->assertSame(['100', '200.50'], ChannelModel::find(10)->fixed);
  99. $this->assertSame(0, $this->saveChannel($params)['code']);
  100. $this->assertSame(['100', '200.50'], ChannelModel::find(10)->fixed);
  101. $created = $this->saveChannel($this->newChannel(['payment_company' => 'sanjin', 'type' => 'zfbge', 'key' => 'fixed-test', 'fixed' => ['300', '500']]));
  102. $this->assertSame(0, $created['code'], $created['msg']);
  103. $new = ChannelModel::where('key', 'fixed-test')->first();
  104. $this->assertNull($new->min); $this->assertNull($new->max);
  105. $this->assertSame(['300', '500'], $new->fixed);
  106. }
  107. public function test_fixed_amount_validation_and_other_channel_range_validation_remain_independent(): void
  108. {
  109. $params = ['id' => 10, 'data_type' => 1, 'payment_company' => 'sanjin', 'type' => 'zfbge', 'rate' => '0.027'];
  110. $before = DB::table('recharge_channel')->where('id', 10)->first();
  111. foreach ([null, [], '', [0], [-1], ['1.234'], [['invalid']], ['key' => 100], 'abc'] as $fixed) {
  112. $this->assertSame(-3, $this->saveChannel($params + ['fixed' => $fixed])['code']);
  113. $this->assertEquals($before, DB::table('recharge_channel')->where('id', 10)->first());
  114. }
  115. $badRange = $this->saveChannel(['id' => 2, 'data_type' => 1, 'payment_company' => 'sanjin', 'type' => 'wxsm', 'rate' => '0.01', 'min' => 100, 'max' => 50]);
  116. $this->assertSame(-3, $badRange['code']);
  117. try {
  118. (new RechargeChannelConfigurationService())->save($params + ['fixed' => [], 'min' => 1, 'max' => 2]);
  119. $this->fail('Invalid direct-service fixed amounts accepted');
  120. } catch (\Illuminate\Validation\ValidationException $e) { $this->assertArrayHasKey('fixed', $e->errors()); }
  121. }
  122. public function test_logo_options_include_every_payment_method_even_without_enabled_or_existing_channels(): void
  123. {
  124. $this->request('/admin/rechargeChannel/saveLogo', []);
  125. $service = new \App\Services\Payment\RechargeChannelLogoService();
  126. DB::table('recharge_channel')->where('data_type', 1)->update(['status' => 0]);
  127. DB::table('payment_providers')->update(['status' => 0]);
  128. $options = $service->options();
  129. $this->assertSame(13, $options['total']);
  130. $rows = array_column($options['data'], null, 'channel_type');
  131. foreach (['wxsm','ylsm','szrmb','zfbsm','zfbge','zfbzk','sdjt','JDpay','NOpay12','NOpay13','ZIMUpay','usdt','rgcz'] as $type) {
  132. $this->assertArrayHasKey($type, $rows);
  133. }
  134. $this->assertSame('三斤支付 / 微信扫码', $rows['wxsm']['channel_name']);
  135. DB::table('recharge_channel')->delete(); DB::table('recharge_channel_group')->delete();
  136. $this->assertSame(13, $service->options()['total']);
  137. $result = $service->save(['channel_type' => 'wxsm', 'logo' => 'https://cdn.example.test/wx.png']);
  138. $this->assertSame('https://cdn.example.test/wx.png', $result['logo']);
  139. $this->assertSame(0, DB::table('recharge_channel')->count());
  140. $this->assertSame('https://cdn.example.test/wx.png', array_column($service->options()['data'], null, 'channel_type')['wxsm']['logo']);
  141. $this->assertSame('', $service->save(['channel_type' => 'wxsm', 'logo' => null])['logo']);
  142. $this->request('/admin/rechargeChannel/logoOptions', []);
  143. $this->assertSame(13, (new RechargeChannel())->logoOptions($service)->getData(true)['data']['total']);
  144. $router = new \Illuminate\Routing\Router(new \Illuminate\Events\Dispatcher($this->app), $this->app);
  145. $this->app->instance('router', $router);
  146. $router->group(['prefix' => 'admin'], function () { require dirname(__DIR__, 2) . '/routes/admin.php'; });
  147. foreach (['logoOptions' => ['GET', 'admin/rechargeChannel/list'], 'saveLogo' => ['POST', 'admin/rechargeChannel/update']] as $path => [$method, $permission]) {
  148. $route = $router->getRoutes()->match(Request::create('/admin/rechargeChannel/' . $path, $method));
  149. $this->assertSame($permission, $route->defaults['permission_uri']);
  150. $this->assertContains('admin.jwt', $route->middleware()); $this->assertContains('check.button.uri', $route->middleware());
  151. }
  152. try { $service->save(['channel_type' => 'DF001', 'logo' => '/a.png']); $this->fail('Withdrawal-only method accepted'); }
  153. catch (\Illuminate\Validation\ValidationException $e) { $this->assertArrayHasKey('channel_type', $e->errors()); }
  154. }
  155. public function test_logo_preconfiguration_is_inherited_by_later_channels_and_migration_retains_old_uploads(): void
  156. {
  157. $this->request('/admin/rechargeChannel/saveLogo', []);
  158. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_160000_create_recharge_method_logos.php';
  159. $migration->down();
  160. DB::table('recharge_channel')->where('id', 2)->update(['logo' => '/storage/wx.png']);
  161. $migration->up();
  162. $this->assertSame('/storage/wx.png', DB::table('recharge_method_logos')->where('channel_type', 'wxsm')->value('logo'));
  163. DB::table('recharge_channel')->where('type', 'JDpay')->where('data_type', 1)->delete();
  164. $service = new \App\Services\Payment\RechargeChannelLogoService();
  165. $service->save(['channel_type' => 'JDpay', 'logo' => 'https://cdn.example.test/jd.png']);
  166. $body = $this->saveChannel($this->newChannel(['payment_company' => 'jd', 'type' => 'JDpay']));
  167. $this->assertSame(0, $body['code'], $body['msg']);
  168. $this->assertSame('https://cdn.example.test/jd.png', \App\Models\RechargeChannel::where('data_type', 1)->where('type', 'JDpay')->first()->logo);
  169. }
  170. public function test_deposit_logos_upload_save_share_by_method_and_can_be_cleared(): void
  171. {
  172. $this->app['config']->set('app.url', 'https://admin.example.test');
  173. $before = DB::table('recharge_channel')->where('id', 24)->first();
  174. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => '/storage/images/jd.png'])['code']);
  175. $row = \App\Models\RechargeChannel::find(24);
  176. $this->assertSame('https://admin.example.test/storage/images/jd.png', $row->logo);
  177. $this->assertSame((string)$before->type, $row->type);
  178. $this->assertSame(0, $this->saveChannel($this->newChannel(['data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']))['code']);
  179. $peers = \App\Models\RechargeChannel::where('data_type', 1)->where('type', 'JDpay')->get();
  180. $this->assertCount(2, $peers);
  181. foreach ($peers as $peer) $this->assertSame($row->logo, $peer->logo);
  182. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => 'https://cdn.example.test/jd.webp'])['code']);
  183. foreach (\App\Models\RechargeChannel::where('data_type', 1)->where('type', 'JDpay')->get() as $peer) $this->assertSame('https://cdn.example.test/jd.webp', $peer->logo);
  184. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => null])['code']);
  185. foreach (\App\Models\RechargeChannel::where('data_type', 1)->where('type', 'JDpay')->get() as $peer) $this->assertSame('', $peer->logo);
  186. foreach (['javascript:alert(1)', '//evil.test/a.png', ['bad']] as $logo) {
  187. $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => $logo])['code']);
  188. }
  189. }
  190. public function test_withdraw_logos_upload_save_share_by_method_and_list_returns_logo(): void
  191. {
  192. $this->app['config']->set('app.url', 'https://admin.example.test');
  193. $this->assertSame(0, $this->saveChannel(['id' => 25, 'data_type' => 2, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => '/storage/images/jd-withdraw.png'])['code']);
  194. $row = \App\Models\RechargeChannel::find(25);
  195. $this->assertSame('https://admin.example.test/storage/images/jd-withdraw.png', $row->logo);
  196. $deposit = \App\Models\RechargeChannel::find(24);
  197. $this->assertSame('', $deposit->logo);
  198. $this->assertSame(0, $this->saveChannel($this->newChannel(['data_type' => 2, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02', 'key' => 'withdraw-peer']))['code']);
  199. foreach (\App\Models\RechargeChannel::where('data_type', 2)->where('type', 'JDpay')->get() as $peer) {
  200. $this->assertSame($row->logo, $peer->logo);
  201. }
  202. $listRow = $this->channelList(['data_type' => '2', 'payment_company' => 'jd'])['data'][0];
  203. $this->assertArrayHasKey('logo', $listRow);
  204. $this->assertSame('https://admin.example.test/storage/images/jd-withdraw.png', $listRow['logo']);
  205. $formatted = array_column(\App\Models\RechargeChannel::getFormatChannel(2, 1), null, 'value');
  206. $this->assertSame('https://admin.example.test/storage/images/jd-withdraw.png', $formatted['JDpay']['logo'] ?? '');
  207. $this->assertSame('', array_column(\App\Models\RechargeChannel::getFormatChannel(1, 1), null, 'value')['JDpay']['logo'] ?? 'x');
  208. $this->assertSame(0, $this->saveChannel(['id' => 25, 'data_type' => 2, 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.01', 'logo' => null])['code']);
  209. foreach (\App\Models\RechargeChannel::where('data_type', 2)->where('type', 'JDpay')->get() as $peer) {
  210. $this->assertSame('', $peer->logo);
  211. }
  212. }
  213. public function test_company_options_use_all_rows_not_first_page(): void
  214. {
  215. $page1 = $this->channelList(['data_type' => '1']);
  216. $this->assertSame(19, $page1['total']);
  217. $this->assertCount(15, $page1['data']);
  218. $this->assertNotContains('jd', array_column($page1['data'], 'payment_company'));
  219. $page2 = $this->channelList(['data_type' => '1', 'page' => 2]);
  220. $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company'));
  221. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  222. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true);
  223. $this->assertSame(0, $data['code']);
  224. $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled'));
  225. }
  226. public function test_channel_list_displays_provider_separately_from_channel_name(): void
  227. {
  228. $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']);
  229. $this->assertSame(1, $data['total']);
  230. $this->assertSame('JD钱包', $data['data'][0]['name']);
  231. $this->assertSame('JD支付', $data['data'][0]['payment_company_label']);
  232. $this->assertSame(1, $data['data'][0]['from']);
  233. $this->assertSame(24, $data['data'][0]['id']);
  234. $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']);
  235. $this->assertSame(25, $withdraw['data'][0]['id']);
  236. $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']);
  237. }
  238. public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void
  239. {
  240. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']);
  241. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  242. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  243. $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled'));
  244. $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code'));
  245. $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type'));
  246. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  247. }
  248. public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void
  249. {
  250. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']);
  251. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  252. $response = (new RechargeChannel())->getChannel()->getData(true);
  253. $this->assertSame(0, $response['code']);
  254. $names = array_column($response['data']['data'], 'name', 'type');
  255. $this->assertSame('JD钱包', $names['JDpay']);
  256. $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']);
  257. $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']);
  258. $this->assertSame('808充值', $names['ZIMUpay']);
  259. $this->assertSame(13, $response['data']['total']);
  260. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]);
  261. $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type');
  262. $this->assertSame('JD钱包', $names['JDpay']);
  263. $this->assertSame('NO快捷提现', $names['NOwithdraw']);
  264. $this->assertSame('808账户提现', $names['ZIMUwithdraw']);
  265. $this->assertArrayNotHasKey('ZIMUcash', $names);
  266. $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
  267. }
  268. private function formOptions(int $dataType = 1, ?int $id = null): array
  269. {
  270. $this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
  271. $response = (new RechargeChannel())->options(new RechargeChannelConfigurationService())->getData(true);
  272. $this->assertSame(0, $response['code']);
  273. return $response['data'];
  274. }
  275. private function saveChannel(array $params): array
  276. {
  277. $this->app->instance('request', Request::create('/admin/rechargeChannel/update', 'POST', $params));
  278. return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true);
  279. }
  280. private function newChannel(array $overrides = []): array
  281. {
  282. return $overrides + ['data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12',
  283. 'rate' => '0.0300', 'name' => '测试通道', 'key' => 'test-key', 'min' => 1, 'max' => 100];
  284. }
  285. private function channelLogs(array $params): array
  286. {
  287. $this->request('/admin/rechargeChannel/logs', $params);
  288. $response = (new RechargeChannel())->logs(new RechargeChannelConfigurationService())->getData(true);
  289. $this->assertSame(0, $response['code'], $response['msg']);
  290. return $response['data'];
  291. }
  292. public function test_channel_fee_can_be_rate_fixed_or_both_and_is_listed_and_logged(): void
  293. {
  294. // 迁移把原来写死的每笔 2 元写入三方提现通道,USDT / 人工提现不收。
  295. $withdraw = DB::table('recharge_channel')->where('data_type', 2)->pluck('fee_fixed', 'type')->map(fn ($v) => (float)$v);
  296. $this->assertSame([2.0, 0.0, 0.0], [$withdraw['JDpay'], $withdraw['rgtx'], $withdraw['usdt']]);
  297. $this->assertSame(0, $this->saveChannel($this->newChannel(['fee_mode' => 'fixed', 'rate' => '0.05', 'fee_fixed' => '3']))['code']);
  298. $id = (int)DB::table('recharge_channel')->where('key', 'test-key')->value('id');
  299. $this->assertSame(['rate' => 0.0, 'fee_fixed' => 3.0], array_map('floatval', (array)DB::table('recharge_channel')->where('id', $id)->first(['rate', 'fee_fixed'])),
  300. 'fixed mode drops the percentage');
  301. $edit = ['id' => $id, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12'];
  302. $this->assertSame(0, $this->saveChannel($edit + ['fee_mode' => 'rate_fixed', 'rate' => '0.025', 'fee_fixed' => '1.5'])['code']);
  303. $row = collect($this->channelList(['data_type' => 1, 'key' => 'test-key'])['data'])->first();
  304. $this->assertSame(['rate_fixed', '百分比+每笔固定', '2.5', '2.5% + 每笔1.5元'],
  305. [$row['fee_mode'], $row['fee_mode_text'], $row['fee_rate_percent'], $row['fee_text']]);
  306. $this->assertSame(0, $this->saveChannel($edit + ['fee_mode' => 'rate', 'rate' => '0.02', 'fee_fixed' => '9'])['code']);
  307. $row = collect($this->channelList(['data_type' => 1, 'key' => 'test-key'])['data'])->first();
  308. $this->assertSame(['rate', '2%', 0.0], [$row['fee_mode'], $row['fee_text'], (float)$row['fee_fixed']], 'rate mode drops the fixed fee');
  309. $missing = $this->saveChannel($edit + ['fee_mode' => 'rate_fixed', 'rate' => '0.02', 'fee_fixed' => '0']);
  310. $this->assertNotSame(0, $missing['code']);
  311. $this->assertSame('请填写每笔固定手续费', $missing['msg']);
  312. $this->assertNotSame(0, $this->saveChannel($edit + ['fee_mode' => 'fixed'])['code'], 'fixed mode needs an amount');
  313. $this->assertNotSame(0, $this->saveChannel($edit + ['rate' => '1.5'])['code'], 'rate is a fraction, at most 1');
  314. $this->assertSame(0, $this->saveChannel($edit + ['rate' => '0.02', 'fee_fixed' => '4'])['code'], 'without fee_mode both fields save as sent');
  315. $this->assertSame('2% + 每笔4元', collect($this->channelList(['data_type' => 1, 'key' => 'test-key'])['data'])->first()['fee_text']);
  316. $logs = $this->channelLogs(['data_type' => 1, 'channel_id' => $id]);
  317. $change = array_column($logs['data'][0]['changes'], null, 'field')['fee_fixed'];
  318. $this->assertSame(['每笔固定手续费', '0', '4'], [$change['label'], $change['before'], $change['after']]);
  319. }
  320. public function test_channel_logs_record_who_when_and_what_changed_per_direction(): void
  321. {
  322. $saveAs = function (array $params, string $admin) {
  323. $request = Request::create('/admin/rechargeChannel/update', 'POST', $params);
  324. $request->user = (object)['id' => 7, 'username' => $admin];
  325. $this->app->instance('request', $request);
  326. return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true);
  327. };
  328. $this->assertSame(0, $saveAs($this->newChannel(), 'alice')['code']);
  329. $id = (int)DB::table('recharge_channel')->where('key', 'test-key')->value('id');
  330. $this->assertSame(0, $saveAs(['id' => $id, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12',
  331. 'rate' => '0.05', 'name' => '改名通道', 'status' => 0], 'bob')['code']);
  332. // 未改动任何字段不产生日志
  333. $this->assertSame(0, $saveAs(['id' => $id, 'data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.0500'], 'bob')['code']);
  334. $withdraw = DB::table('recharge_channel')->where('data_type', 2)->where('type', 'rgtx')->first();
  335. $withdrawId = (int)$withdraw->id;
  336. $result = $saveAs(['id' => $withdrawId, 'data_type' => 2, 'type' => 'rgtx', 'rate' => '0.07'], 'carol');
  337. $this->assertSame(0, $result['code'], $result['msg']);
  338. $deposit = $this->channelLogs(['data_type' => 1]);
  339. $this->assertSame(2, $deposit['total']);
  340. [$update, $create] = $deposit['data'];
  341. $this->assertSame(['update', '修改', 'bob', $id], [$update['action'], $update['action_label'], $update['operator_name'], $update['channel_id']]);
  342. $this->assertSame('改名通道', $update['channel_name']);
  343. $this->assertMatchesRegularExpression('/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/', $update['created_at']);
  344. $byField = array_column($update['changes'], null, 'field');
  345. $this->assertSame(['name', 'rate', 'status'], array_keys($byField));
  346. $this->assertSame(['测试通道', '改名通道'], [$byField['name']['before'], $byField['name']['after']]);
  347. $this->assertSame(['0.03', '0.05'], [$byField['rate']['before'], $byField['rate']['after']]);
  348. $this->assertSame(['启用', '停用'], [$byField['status']['before_text'], $byField['status']['after_text']]);
  349. $this->assertStringContainsString('费率:0.03 → 0.05', $update['summary']);
  350. $this->assertSame(['create', 'alice'], [$create['action'], $create['operator_name']]);
  351. $this->assertSame('空', array_column($create['changes'], null, 'field')['name']['before_text']);
  352. $this->assertSame(1, $this->channelLogs(['data_type' => 1, 'operator_name' => 'ali'])['total']);
  353. $this->assertSame(0, $this->channelLogs(['data_type' => 1, 'channel_id' => $withdrawId])['total']);
  354. $withdrawLogs = $this->channelLogs(['data_type' => 2]);
  355. $this->assertSame(1, $withdrawLogs['total']);
  356. $this->assertSame(['carol', $withdrawId, ['rate']], [$withdrawLogs['data'][0]['operator_name'], $withdrawLogs['data'][0]['channel_id'],
  357. array_column($withdrawLogs['data'][0]['changes'], 'field')]);
  358. $this->assertSame(0, $this->deleteChannel($id)['code']);
  359. $deleted = $this->channelLogs(['data_type' => 1, 'channel_id' => $id])['data'][0];
  360. $this->assertSame(['delete', '删除'], [$deleted['action'], $deleted['action_label']]);
  361. $this->assertSame('改名通道', $deleted['channel_name']);
  362. }
  363. public function test_activity_direction_is_no_longer_listed_or_editable(): void
  364. {
  365. DB::table('recharge_channel')->insert(['data_type' => 3, 'from' => 1, 'type' => 'recharge', 'name' => '即充即送', 'key' => 'act', 'rate' => 0, 'status' => 1, 'sort' => 0]);
  366. $this->assertSame([], array_values(array_filter($this->channelList([])['data'], fn ($r) => $r['data_type'] === 3)));
  367. $this->request('/admin/rechargeChannel/list', ['data_type' => 3]);
  368. $this->assertSame(-3, (new RechargeChannel())->list()->getData(true)['code']);
  369. $this->assertSame(-3, $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => 3, 'type' => 'recharge']))['code']);
  370. $this->request('/admin/rechargeChannel/groupList', []);
  371. $group = (new RechargeChannel())->groupList()->getData(true)['data']['data'][0];
  372. $this->assertArrayNotHasKey('activityTypes', $group);
  373. $this->assertArrayNotHasKey('activity_type', $group);
  374. }
  375. public function test_creation_catalog_works_without_any_database_channels_or_groups(): void
  376. {
  377. DB::table('recharge_channel')->delete();
  378. DB::table('recharge_channel_group')->delete();
  379. $data = $this->formOptions();
  380. $this->assertSame(['sanjin', 'jd', 'no', 'zimu'], array_column($data['payment_companies'], 'value'));
  381. $this->assertTrue($data['identity_editable']);
  382. $this->assertSame('', $data['identity_edit_reason']);
  383. $this->assertContains('NOpay12', array_column($data['types'], 'value'));
  384. $this->assertSame(['qianbao', 'jd', 'no', 'zimu'], array_column($this->formOptions(2)['payment_companies'], 'value'));
  385. }
  386. public function test_create_selected_company_then_switch_company_and_type_on_same_record(): void
  387. {
  388. $result = $this->saveChannel($this->newChannel());
  389. $this->assertSame(0, $result['code']);
  390. $this->assertSame([], $result['data']);
  391. $row = DB::table('recharge_channel')->where('key', 'test-key')->first();
  392. $this->assertSame(1, $row->from);
  393. $this->assertSame(1, $row->status);
  394. $this->assertSame(0, $row->sort);
  395. $result = $this->saveChannel(['id' => $row->id, 'data_type' => 1,
  396. 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']);
  397. $this->assertSame(0, $result['code']);
  398. $data = $this->channelList(['data_type' => '1', 'key' => 'test-key'])['data'][0];
  399. $this->assertSame($row->id, $data['id']);
  400. $this->assertSame('jd', $data['payment_company']);
  401. $this->assertSame('JD支付 / 测试通道', $data['display_name']);
  402. }
  403. public function test_invalid_company_type_or_source_is_rejected_without_writing(): void
  404. {
  405. $before = DB::table('recharge_channel')->count();
  406. foreach ([
  407. ['payment_company' => 'jd', 'type' => 'NOpay12'],
  408. ['payment_company' => 'no', 'type' => '12'],
  409. ['payment_company' => 'no', 'type' => 'usdt'],
  410. ['payment_company' => 'no', 'from' => 2],
  411. ['payment_company' => 'sanjin', 'type' => 'DF001', 'data_type' => 2],
  412. ] as $override) {
  413. $this->assertSame(-3, $this->saveChannel($this->newChannel($override))['code']);
  414. }
  415. $this->assertSame($before, DB::table('recharge_channel')->count());
  416. }
  417. public function test_native_and_activity_channels_do_not_require_or_fabricate_company(): void
  418. {
  419. foreach ([[1, 'usdt', 2], [1, 'rgcz', 3], [2, 'rgtx', 3]] as [$direction, $type, $from]) {
  420. $result = $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => $direction,
  421. 'type' => $type, 'key' => 'local-' . $type]));
  422. $this->assertSame(0, $result['code']);
  423. $row = $this->channelList(['key' => 'local-' . $type])['data'][0];
  424. $this->assertSame($from, $row['from']);
  425. $this->assertNull($row['payment_company']);
  426. $this->assertSame('测试通道', $row['display_name']);
  427. }
  428. }
  429. public function test_existing_valid_clients_can_omit_derived_company_and_from(): void
  430. {
  431. $params = $this->newChannel();
  432. unset($params['payment_company']);
  433. $this->assertSame(0, $this->saveChannel($params)['code']);
  434. $row = $this->channelList(['key' => 'test-key'])['data'][0];
  435. $this->assertSame('no', $row['payment_company']);
  436. $this->assertSame(1, $row['from']);
  437. }
  438. public function test_linked_channel_locks_identity_but_keeps_regular_edit_available(): void
  439. {
  440. DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
  441. $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
  442. });
  443. DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
  444. $options = $this->formOptions(1, 24);
  445. $this->assertFalse($options['identity_editable']);
  446. $this->assertNotSame('', $options['identity_edit_reason']);
  447. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no',
  448. 'type' => 'NOpay12', 'rate' => '0.01']);
  449. $this->assertSame(-3, $result['code']);
  450. $this->assertSame('JDpay', DB::table('recharge_channel')->where('id', 24)->value('type'));
  451. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd',
  452. 'type' => 'JDpay', 'rate' => '0.01', 'name' => 'JD新名称']);
  453. $this->assertSame(0, $result['code']);
  454. DB::table('payment_gateways')->update(['deleted_at' => now()]);
  455. $this->assertTrue($this->formOptions(1, 24)['identity_editable']);
  456. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  457. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  458. }
  459. public function test_switching_company_does_not_rewrite_group_permissions(): void
  460. {
  461. $groups = DB::table('recharge_channel_group')->orderBy('id')->get()->toJson();
  462. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  463. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  464. $this->assertSame($groups, DB::table('recharge_channel_group')->orderBy('id')->get()->toJson());
  465. $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 2,
  466. 'payment_company' => 'no', 'type' => 'NOwithdraw', 'rate' => '0.01'])['code']);
  467. }
  468. public function test_group_option_names_include_company_without_changing_values(): void
  469. {
  470. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  471. $rows = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], null, 'type');
  472. $this->assertSame('JD钱包', $rows['JDpay']['name']);
  473. $this->assertSame('jd', $rows['JDpay']['payment_company']);
  474. $this->assertSame('JD支付 / JD钱包', $rows['JDpay']['display_name']);
  475. $this->assertSame('USDT充值', $rows['usdt']['display_name']);
  476. }
  477. public function test_merchant_save_rechecks_channel_identity_inside_transaction(): void
  478. {
  479. DB::table('recharge_channel')->where('id', 26)->update(['type' => 'JDpay']);
  480. $this->request('/admin/paymentConfig/gateways/save', []);
  481. $controller = new PaymentConfiguration(new PaymentChannelLinkService());
  482. $method = new \ReflectionMethod($controller, 'saveModel');
  483. $method->setAccessible(true);
  484. $this->expectException(\InvalidArgumentException::class);
  485. $this->expectExceptionMessage('支付公司与所选通道不匹配');
  486. $method->invoke($controller, PaymentGateway::class, ['kind' => 'deposit', 'recharge_channel_id' => 26,
  487. 'recharge_channel_group_ids' => [1], 'payment_company' => 'no', 'payment_method' => 'NOpay12'], 'payment_gateway');
  488. }
  489. private function setCompanyStatus(string $code, int $status): array
  490. {
  491. $this->app->instance('request', Request::create('/admin/paymentCompany/status', 'POST', ['payment_company' => $code, 'status' => $status]));
  492. return (new PaymentCompany())->status(new PaymentProviderService())->getData(true);
  493. }
  494. private function deleteChannel(int $id): array
  495. {
  496. $this->app->instance('request', Request::create('/admin/rechargeChannel/delete', 'POST', ['id' => $id]));
  497. return (new RechargeChannel())->delete(new RechargeChannelConfigurationService())->getData(true);
  498. }
  499. public function test_company_management_lists_registered_providers_and_filters_status(): void
  500. {
  501. $this->request('/admin/paymentCompany/list', []);
  502. $result = (new PaymentCompany())->list(new PaymentProviderService())->getData(true);
  503. $this->assertSame(0, $result['code']);
  504. $this->assertSame(5, $result['data']['total']);
  505. $rows = array_column($result['data']['data'], null, 'payment_company');
  506. $this->assertSame('SHA256', $rows['no']['signature_algorithm']);
  507. $this->assertSame([1, 2], $rows['no']['data_types']);
  508. $this->assertSame(3, $rows['no']['channel_count']);
  509. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  510. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  511. $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'payment_company')->count());
  512. $this->request('/admin/paymentCompany/list', ['status' => 0]);
  513. $this->assertSame(1, (new PaymentCompany())->list(new PaymentProviderService())->getData(true)['data']['total']);
  514. $this->assertSame(-3, $this->setCompanyStatus('unknown', 0)['code']);
  515. $this->assertSame(-3, $this->setCompanyStatus('no', 9)['code']);
  516. }
  517. public function test_company_switch_disables_options_and_runtime_without_rewriting_channels(): void
  518. {
  519. $groups = DB::table('recharge_channel_group')->get()->toJson();
  520. $this->assertSame(0, $this->setCompanyStatus('no', 0)['code']);
  521. $this->assertFalse(ChannelModel::checkRechargeChannel('NOpay12', 1));
  522. $this->assertFalse(ChannelModel::checkWithdrawChannel('NOwithdraw', 1));
  523. $this->assertNotFalse(ChannelModel::checkRechargeChannel('JDpay', 1));
  524. $this->assertNotFalse(ChannelModel::checkRechargeChannel('usdt', 1));
  525. $this->assertArrayNotHasKey('NOpay12', ChannelModel::product(1));
  526. $this->assertSame(1, DB::table('recharge_channel')->where('id', 26)->value('status'));
  527. $this->assertSame($groups, DB::table('recharge_channel_group')->get()->toJson());
  528. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  529. $options = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  530. $companies = array_column($options['payment_companies'], null, 'value');
  531. $this->assertSame('provider_disabled', $companies['no']['disabled_reason_code']);
  532. $this->assertFalse(array_column($options['recharge_channels'], null, 'id')[26]['selectable']);
  533. $form = $this->formOptions();
  534. $this->assertTrue(array_column($form['payment_companies'], null, 'value')['no']['disabled']);
  535. $row = $this->channelList(['data_type' => '1', 'type' => 'NOpay12'])['data'][0];
  536. $this->assertSame(1, $row['status']);
  537. $this->assertSame(0, $row['company_status']);
  538. $this->assertSame(0, $row['effective_status']);
  539. $this->assertSame(-3, $this->saveChannel($this->newChannel())['code']);
  540. $this->assertSame(0, $this->setCompanyStatus('no', 1)['code']);
  541. $this->assertNotFalse(ChannelModel::checkRechargeChannel('NOpay12', 1));
  542. }
  543. public function test_reenable_company_does_not_enable_individually_disabled_channels(): void
  544. {
  545. $this->setCompanyStatus('zimu', 0);
  546. $this->setCompanyStatus('zimu', 1);
  547. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  548. $this->assertFalse(ChannelModel::checkWithdrawChannel('ZIMUcash', 1));
  549. $this->assertNotFalse(ChannelModel::checkWithdrawChannel('ZIMUwithdraw', 1));
  550. }
  551. public function test_disabled_company_blocks_new_external_requests_before_any_wallet_or_network_work(): void
  552. {
  553. $this->setCompanyStatus('no', 0);
  554. $this->assertSame('支付公司已禁用', PaymentOrderService::createPay('test-user', 100, 'NOpay12')['text']);
  555. $this->assertSame('支付公司已禁用', PaymentOrderService::autoCreatePayout('test-user', 100, 'NOwithdraw', '', '')['text']);
  556. $this->assertFalse(PaymentProviderService::requestEnabled('nopay12', 1));
  557. $this->assertTrue(PaymentProviderService::requestEnabled('JDpay', 1));
  558. }
  559. public function test_delete_last_deposit_type_soft_deletes_and_cleans_only_its_group_membership(): void
  560. {
  561. $beforeWithdraw = DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type');
  562. $this->assertSame(0, $this->deleteChannel(24)['code']);
  563. $this->assertNull(ChannelModel::query()->find(24));
  564. $deleted = ChannelModel::withTrashed()->findOrFail(24);
  565. $this->assertTrue($deleted->trashed());
  566. $this->assertSame(0, (int)$deleted->status);
  567. $this->assertSame('JD钱包', $deleted->name);
  568. $types = explode(',', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  569. $this->assertNotContains('JDpay', $types);
  570. $this->assertContains('NOpay12', $types);
  571. $this->assertSame($beforeWithdraw, DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type'));
  572. $this->assertNotFalse(ChannelModel::checkWithdrawChannel('JDpay', 1));
  573. $this->assertFalse(ChannelModel::checkRechargeChannel('JDpay', 1));
  574. $this->assertSame(0, $this->deleteChannel(24)['code']);
  575. $this->assertSame(1, DB::table('operation_audits')->where('resource_type', 'recharge_channel')->count());
  576. $this->setCompanyStatus('jd', 0); $this->setCompanyStatus('jd', 1);
  577. $this->assertNull(ChannelModel::query()->find(24));
  578. }
  579. public function test_delete_one_of_several_same_type_channels_preserves_groups_until_last_removed(): void
  580. {
  581. $this->assertSame(0, $this->deleteChannel(6)['code']);
  582. $this->assertStringContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  583. $this->assertSame(3, ChannelModel::query()->where('data_type', 1)->where('type', 'zfbsm')->count());
  584. foreach ([7, 8, 9] as $id) $this->assertSame(0, $this->deleteChannel($id)['code']);
  585. $this->assertStringNotContainsString('zfbsm', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  586. }
  587. public function test_delete_is_blocked_by_payment_config_reference_and_does_not_touch_activity(): void
  588. {
  589. DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
  590. $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
  591. });
  592. DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
  593. $this->assertSame(-3, $this->deleteChannel(24)['code']);
  594. $this->assertNotNull(ChannelModel::query()->find(24));
  595. $this->assertSame(-3, $this->deleteChannel(21)['code']);
  596. $this->assertNotNull(ChannelModel::query()->find(21));
  597. DB::table('payment_gateways')->update(['deleted_at' => now()]);
  598. $this->assertSame(0, $this->deleteChannel(24)['code']);
  599. }
  600. public function test_migration_retry_keeps_disabled_company_and_deleted_channel(): void
  601. {
  602. $this->setCompanyStatus('no', 0);
  603. $this->deleteChannel(24);
  604. (require dirname(__DIR__, 2) . '/database/migrations/2026_09_08_160000_add_payment_provider_status_and_channel_soft_deletes.php')->up();
  605. $this->assertSame(0, PaymentProviderService::statuses()['no']);
  606. $this->assertTrue(ChannelModel::withTrashed()->findOrFail(24)->trashed());
  607. $this->assertSame(5, DB::table('payment_providers')->count());
  608. }
  609. private function paymentOrderFixture(int $type, string $channel, int $status): \App\Models\PaymentOrder
  610. {
  611. DB::connection()->getSchemaBuilder()->create('payment_orders', function (Blueprint $t) {
  612. $t->id(); $t->integer('type'); $t->string('channel'); $t->integer('status');
  613. $t->string('order_no'); $t->string('member_id'); $t->decimal('amount', 18, 4);
  614. $t->string('state')->nullable(); $t->text('callback_data')->nullable(); $t->timestamps();
  615. });
  616. $this->app->instance('log', new class {
  617. public function channel($channel) { return $this; }
  618. public function error($message, array $context = []) {}
  619. });
  620. return \App\Models\PaymentOrder::query()->create(['type' => $type, 'channel' => $channel,
  621. 'status' => $status, 'order_no' => 'test-order', 'member_id' => 'test-user', 'amount' => 10]);
  622. }
  623. public function test_pending_payout_cannot_be_sent_after_company_disabled(): void
  624. {
  625. $order = $this->paymentOrderFixture(2, 'NOwithdraw', PaymentOrderService::STATUS_STAY);
  626. $this->setCompanyStatus('no', 0);
  627. $result = PaymentOrderService::createPayout($order->id);
  628. $this->assertSame(-3, $result['code']);
  629. $this->assertSame('支付公司已禁用', $result['msg']);
  630. $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status);
  631. }
  632. public function test_pending_payout_cannot_be_sent_after_last_channel_deleted(): void
  633. {
  634. $order = $this->paymentOrderFixture(2, 'JDpay', PaymentOrderService::STATUS_STAY);
  635. $this->assertSame(0, $this->deleteChannel(25)['code']);
  636. $result = PaymentOrderService::createPayout($order->id);
  637. $this->assertSame(-3, $result['code']);
  638. $this->assertSame('提现通道已停用或删除', $result['msg']);
  639. $this->assertSame(PaymentOrderService::STATUS_STAY, $order->fresh()->status);
  640. }
  641. public function test_existing_pay_callback_is_not_blocked_by_company_switch(): void
  642. {
  643. $order = $this->paymentOrderFixture(1, 'NOpay12', PaymentOrderService::STATUS_PROCESS);
  644. $this->setCompanyStatus('no', 0);
  645. $method = new \ReflectionMethod(PaymentOrderService::class, 'applyPayCallback');
  646. $method->setAccessible(true);
  647. $this->assertTrue($method->invoke(null, $order, '10', 'failed', 'success', 'failed', ['status' => 'failed']));
  648. $this->assertSame(PaymentOrderService::STATUS_FAIL, $order->fresh()->status);
  649. $this->assertNotNull($order->fresh()->callback_data);
  650. }
  651. public function test_company_disabled_updates_saved_gateway_effective_status(): void
  652. {
  653. $channel = ChannelModel::query()->findOrFail(26);
  654. $gateway = new PaymentGateway(['payment_company' => 'no', 'payment_method' => 'NOpay12',
  655. 'status' => 1, 'recharge_channel_group_ids' => [1]]);
  656. $gateway->setRelation('rechargeChannel', $channel);
  657. $this->setCompanyStatus('no', 0);
  658. $data = (new PaymentChannelLinkService())->formatOne($gateway);
  659. $this->assertSame(1, $data['config_status']);
  660. $this->assertSame(1, $data['channel_status']);
  661. $this->assertSame(0, $data['company_status']);
  662. $this->assertSame(0, $data['effective_status']);
  663. $this->expectException(\RuntimeException::class);
  664. $this->expectExceptionMessage('NO支付已禁用');
  665. (new PaymentChannelLinkService())->validate(26, [1], 1);
  666. }
  667. public function test_empty_group_types_can_be_saved_after_last_channel_removed(): void
  668. {
  669. $this->app->instance('request', Request::create('/admin/rechargeChannel/updateGroup', 'POST',
  670. ['id' => 1, 'name' => '全部充值', 'recharge_type' => [], 'withdraw_type' => []]));
  671. $this->assertSame(0, (new RechargeChannel())->updateGroup()->getData(true)['code']);
  672. $this->assertSame('', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  673. $this->assertSame('yuebao,old_user,recharge', DB::table('recharge_channel_group')->where('id', 1)->value('activity_type'));
  674. }
  675. private function saveGroup(array $params): array
  676. {
  677. $this->app->instance('request', Request::create('/admin/rechargeChannel/updateGroup', 'POST', $params));
  678. return (new RechargeChannel())->updateGroup()->getData(true);
  679. }
  680. private function groupPayload(int $id = 1): array
  681. {
  682. $group = DB::table('recharge_channel_group')->where('id', $id)->first();
  683. return ['id' => $id, 'name' => $group->name, 'recharge_type' => explode(',', $group->recharge_type),
  684. 'withdraw_type' => explode(',', $group->withdraw_type)];
  685. }
  686. public function test_legacy_group_can_be_saved_and_only_invalid_existing_types_are_cleaned(): void
  687. {
  688. $payload = $this->groupPayload();
  689. $result = $this->saveGroup($payload);
  690. $this->assertSame(0, $result['code']);
  691. $this->assertSame([], $result['data']);
  692. $this->assertStringContainsString('已清理', $result['msg']);
  693. $saved = $this->groupPayload();
  694. $this->assertSame(array_values(array_diff($payload['recharge_type'], ['1', '2', '3'])), $saved['recharge_type']);
  695. $this->assertSame(array_values(array_diff($payload['withdraw_type'], ['ylk', 'zfb', 'szrmb'])), $saved['withdraw_type']);
  696. $this->assertSame('yuebao,old_user,recharge', DB::table('recharge_channel_group')->where('id', 1)->value('activity_type'));
  697. $audit = DB::table('operation_audits')->where('resource_type', 'recharge_channel_group')->first();
  698. $this->assertNotNull($audit);
  699. $this->assertSame(['1', '2', '3'], json_decode($audit->changes, true)['after']['removed_types']['recharge_type']);
  700. }
  701. public function test_new_unknown_type_in_existing_group_is_rejected_atomically(): void
  702. {
  703. $payload = $this->groupPayload();
  704. $payload['recharge_type'][] = 'not_a_type';
  705. $before = DB::table('recharge_channel_group')->where('id', 1)->first();
  706. $result = $this->saveGroup($payload);
  707. $this->assertSame(-3, $result['code']);
  708. $this->assertStringContainsString('not_a_type', $result['msg']);
  709. $this->assertSame($before->recharge_type, DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  710. $this->assertSame(0, DB::table('operation_audits')->count());
  711. }
  712. public function test_new_group_cannot_reuse_invalid_values_from_another_group(): void
  713. {
  714. $payload = $this->groupPayload();
  715. unset($payload['id']);
  716. $result = $this->saveGroup($payload);
  717. $this->assertSame(-3, $result['code']);
  718. $this->assertStringContainsString('1,2,3', $result['msg']);
  719. $this->assertSame(2, DB::table('recharge_channel_group')->count());
  720. }
  721. public function test_group_types_trim_deduplicate_and_keep_disabled_company_membership(): void
  722. {
  723. $this->setCompanyStatus('no', 0);
  724. $result = $this->saveGroup(['id' => 1, 'name' => '组合',
  725. 'recharge_type' => [' JDpay ', 'JDpay', ' NOpay12 '], 'withdraw_type' => [' DF001 ']]);
  726. $this->assertSame(0, $result['code']);
  727. $this->assertSame('JDpay,NOpay12', DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
  728. $this->assertSame('DF001', DB::table('recharge_channel_group')->where('id', 1)->value('withdraw_type'));
  729. }
  730. public function test_nested_option_objects_are_rejected_without_writing(): void
  731. {
  732. $result = $this->saveGroup(['id' => 1, 'name' => '组合',
  733. 'recharge_type' => [['type' => 'JDpay', 'name' => 'JD钱包']], 'withdraw_type' => ['DF001']]);
  734. $this->assertSame(-3, $result['code']);
  735. $this->assertStringContainsString('type', $result['msg']);
  736. $this->assertSame(0, DB::table('operation_audits')->count());
  737. }
  738. public function test_existing_deleted_type_is_removed_and_not_restored(): void
  739. {
  740. DB::table('recharge_channel')->where('id', 24)->update(['deleted_at' => now(), 'status' => 0]);
  741. $this->assertSame(0, $this->saveGroup($this->groupPayload())['code']);
  742. $saved = $this->groupPayload();
  743. $this->assertNotContains('JDpay', $saved['recharge_type']);
  744. $this->assertContains('JDpay', $saved['withdraw_type']);
  745. $this->assertTrue(ChannelModel::withTrashed()->findOrFail(24)->trashed());
  746. }
  747. private function deleteGroup(int $id): array
  748. {
  749. $this->app->instance('request', Request::create('/admin/rechargeChannel/deleteGroup', 'POST', ['id' => $id]));
  750. return (new RechargeChannel())->deleteGroup(new RechargeChannelConfigurationService())->getData(true);
  751. }
  752. public function test_unreferenced_group_can_be_soft_deleted_without_deleting_channels(): void
  753. {
  754. $count = ChannelModel::query()->count();
  755. $this->assertSame(0, $this->deleteGroup(2)['code']);
  756. $this->assertNull(\App\Models\RechargeChannelGroup::query()->find(2));
  757. $this->assertTrue(\App\Models\RechargeChannelGroup::withTrashed()->findOrFail(2)->trashed());
  758. $this->assertSame($count, ChannelModel::query()->count());
  759. $this->assertSame(0, $this->deleteGroup(2)['code']);
  760. $this->assertSame(1, DB::table('operation_audits')->where('action', 'delete')->count());
  761. $this->request('/admin/rechargeChannel/groupList', []);
  762. $this->assertSame(1, (new RechargeChannel())->groupList()->getData(true)['data']['total']);
  763. $this->assertSame(-3, $this->saveGroup(['id' => 2, 'name' => '已删除', 'recharge_type' => [], 'withdraw_type' => []])['code']);
  764. }
  765. public function test_default_group_and_member_assigned_group_cannot_be_deleted(): void
  766. {
  767. $this->assertSame(-3, $this->deleteGroup(1)['code']);
  768. $this->assertSame(-3, $this->deleteGroup(999)['code']);
  769. DB::connection()->getSchemaBuilder()->create('users', function (Blueprint $t) {
  770. $t->id(); $t->string('member_id'); $t->unsignedBigInteger('recharge_channel_group_id')->nullable(); $t->timestamps();
  771. });
  772. DB::table('users')->insert(['member_id' => 'test', 'recharge_channel_group_id' => 2]);
  773. $result = $this->deleteGroup(2);
  774. $this->assertSame(-3, $result['code']);
  775. $this->assertStringContainsString('会员', $result['msg']);
  776. $this->assertNotNull(\App\Models\RechargeChannelGroup::query()->find(2));
  777. $this->assertSame(0, DB::table('operation_audits')->count());
  778. }
  779. public function test_numeric_and_string_payment_config_group_references_block_delete(): void
  780. {
  781. DB::connection()->getSchemaBuilder()->create('payment_collection_channels', function (Blueprint $t) {
  782. $t->id(); $t->json('recharge_channel_group_ids'); $t->softDeletes();
  783. });
  784. foreach (['[2]', '["2"]', '["02"]'] as $json) {
  785. DB::table('payment_collection_channels')->delete();
  786. DB::table('payment_collection_channels')->insert(['recharge_channel_group_ids' => $json]);
  787. $result = $this->deleteGroup(2);
  788. $this->assertSame(-3, $result['code']);
  789. $this->assertStringContainsString('被支付配置引用', $result['msg']);
  790. $this->assertNotNull(\App\Models\RechargeChannelGroup::query()->find(2));
  791. }
  792. DB::table('payment_collection_channels')->update(['deleted_at' => now()]);
  793. $this->assertSame(0, $this->deleteGroup(2)['code']);
  794. }
  795. public function test_deleted_group_cannot_be_assigned_to_members_or_new_payment_config(): void
  796. {
  797. DB::connection()->getSchemaBuilder()->create('users', function (Blueprint $t) {
  798. $t->id(); $t->string('member_id'); $t->unsignedBigInteger('recharge_channel_group_id')->nullable(); $t->timestamps();
  799. });
  800. DB::table('users')->insert(['member_id' => 'test', 'recharge_channel_group_id' => 1]);
  801. $this->assertSame(0, $this->deleteGroup(2)['code']);
  802. $this->app->instance('request', Request::create('/admin/user/setRechargeChannelGroup', 'POST',
  803. ['member_id' => ['test'], 'recharge_channel_group_id' => 2]));
  804. $this->assertSame(-3, (new \App\Http\Controllers\admin\User())->setRechargeChannelGroup()->getData(true)['code']);
  805. $this->assertSame(1, DB::table('users')->where('member_id', 'test')->value('recharge_channel_group_id'));
  806. $this->expectException(\RuntimeException::class);
  807. $this->expectExceptionMessage('部分通道组合不存在');
  808. (new PaymentChannelLinkService())->validate(24, [2], 1, true);
  809. }
  810. }