PaymentChannelOptionsTest.php 61 KB

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