| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace Tests\Integration;
- use App\Http\Controllers\admin\PaymentConfiguration;
- use App\Http\Controllers\admin\RechargeChannel;
- use App\Services\PaymentChannelLinkService;
- use App\Services\Payment\RechargeChannelConfigurationService;
- use App\Models\PaymentGateway;
- use Illuminate\Config\Repository;
- use Illuminate\Database\Capsule\Manager;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Foundation\Application;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Facade;
- use Illuminate\Translation\ArrayLoader;
- use Illuminate\Translation\Translator;
- use Illuminate\Validation\Factory;
- use PHPUnit\Framework\TestCase;
- class PaymentChannelOptionsTest extends TestCase
- {
- private Application $app;
- protected function setUp(): void
- {
- Facade::clearResolvedInstances();
- $this->app = new Application(dirname(__DIR__, 2));
- $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
- Facade::setFacadeApplication($this->app);
- $db = new Manager($this->app);
- $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
- $db->setAsGlobal();
- $db->bootEloquent();
- $this->app->instance('db', $db->getDatabaseManager());
- $this->app->bind('db.schema', fn () => $db->schema());
- $translator = new Translator(new ArrayLoader(), 'zh');
- $this->app->instance('translator', $translator);
- $this->app->instance('validator', new Factory($translator, $this->app));
- $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
- public function json($data, $status = 200, $headers = [], $options = 0) {
- return new JsonResponse($data, $status, $headers, $options);
- }
- });
- Request::macro('validate', function (array $rules) {
- return app('validator')->make($this->all(), $rules)->validate();
- });
- $db->schema()->create('recharge_channel', function (Blueprint $t) {
- $t->id(); $t->integer('from'); $t->integer('data_type'); $t->string('key');
- $t->string('name'); $t->string('type'); $t->decimal('rate', 8, 4);
- $t->decimal('min')->nullable(); $t->decimal('max')->nullable(); $t->string('fixed')->nullable();
- $t->integer('status'); $t->integer('sort'); $t->timestamps();
- });
- $db->schema()->create('recharge_channel_group', function (Blueprint $t) {
- $t->id(); $t->string('name'); $t->text('recharge_type')->nullable();
- $t->text('withdraw_type')->nullable(); $t->text('activity_type')->nullable(); $t->timestamps();
- });
- $fixture = require dirname(__DIR__) . '/fixtures/payment_provider_channels.php';
- DB::table('recharge_channel')->insert($fixture['channels']);
- DB::table('recharge_channel_group')->insert($fixture['groups']);
- }
- protected function tearDown(): void
- {
- DB::disconnect();
- Facade::clearResolvedInstances();
- parent::tearDown();
- }
- private function request(string $path, array $params): void
- {
- $this->app->instance('request', Request::create($path, 'GET', $params));
- }
- private function channelList(array $params): array
- {
- $this->request('/admin/rechargeChannel/list', $params);
- $response = (new RechargeChannel())->list()->getData(true);
- $this->assertSame(0, $response['code']);
- return $response['data'];
- }
- public function test_company_options_use_all_rows_not_first_page(): void
- {
- $page1 = $this->channelList(['data_type' => '1']);
- $this->assertSame(19, $page1['total']);
- $this->assertCount(15, $page1['data']);
- $this->assertNotContains('jd', array_column($page1['data'], 'payment_company'));
- $page2 = $this->channelList(['data_type' => '1', 'page' => 2]);
- $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company'));
- $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
- $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true);
- $this->assertSame(0, $data['code']);
- $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled'));
- }
- public function test_channel_list_displays_provider_separately_from_channel_name(): void
- {
- $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']);
- $this->assertSame(1, $data['total']);
- $this->assertSame('JD钱包', $data['data'][0]['name']);
- $this->assertSame('JD支付', $data['data'][0]['payment_company_label']);
- $this->assertSame(1, $data['data'][0]['from']);
- $this->assertSame(24, $data['data'][0]['id']);
- $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']);
- $this->assertSame(25, $withdraw['data'][0]['id']);
- $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']);
- }
- public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void
- {
- DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']);
- $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
- $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
- $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled'));
- $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code'));
- $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type'));
- $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
- }
- public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void
- {
- DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']);
- $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
- $response = (new RechargeChannel())->getChannel()->getData(true);
- $this->assertSame(0, $response['code']);
- $names = array_column($response['data']['data'], 'name', 'type');
- $this->assertSame('JD钱包', $names['JDpay']);
- $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']);
- $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']);
- $this->assertSame('808充值', $names['ZIMUpay']);
- $this->assertSame(13, $response['data']['total']);
- $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]);
- $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type');
- $this->assertSame('JD钱包', $names['JDpay']);
- $this->assertSame('NO快捷提现', $names['NOwithdraw']);
- $this->assertSame('808账户提现', $names['ZIMUwithdraw']);
- $this->assertArrayNotHasKey('ZIMUcash', $names);
- $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
- }
- private function formOptions(int $dataType = 1, ?int $id = null): array
- {
- $this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
- $response = (new RechargeChannel())->options(new RechargeChannelConfigurationService())->getData(true);
- $this->assertSame(0, $response['code']);
- return $response['data'];
- }
- private function saveChannel(array $params): array
- {
- $this->app->instance('request', Request::create('/admin/rechargeChannel/update', 'POST', $params));
- return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true);
- }
- private function newChannel(array $overrides = []): array
- {
- return $overrides + ['data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12',
- 'rate' => '0.0300', 'name' => '测试通道', 'key' => 'test-key', 'min' => 1, 'max' => 100];
- }
- public function test_creation_catalog_works_without_any_database_channels_or_groups(): void
- {
- DB::table('recharge_channel')->delete();
- DB::table('recharge_channel_group')->delete();
- $data = $this->formOptions();
- $this->assertSame(['sanjin', 'jd', 'no', 'zimu'], array_column($data['payment_companies'], 'value'));
- $this->assertTrue($data['identity_editable']);
- $this->assertSame('', $data['identity_edit_reason']);
- $this->assertContains('NOpay12', array_column($data['types'], 'value'));
- $this->assertSame(['qianbao', 'jd', 'no', 'zimu'], array_column($this->formOptions(2)['payment_companies'], 'value'));
- $this->assertSame([], $this->formOptions(3)['payment_companies']);
- }
- public function test_create_selected_company_then_switch_company_and_type_on_same_record(): void
- {
- $result = $this->saveChannel($this->newChannel());
- $this->assertSame(0, $result['code']);
- $this->assertSame([], $result['data']);
- $row = DB::table('recharge_channel')->where('key', 'test-key')->first();
- $this->assertSame(1, $row->from);
- $this->assertSame(1, $row->status);
- $this->assertSame(0, $row->sort);
- $result = $this->saveChannel(['id' => $row->id, 'data_type' => 1,
- 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']);
- $this->assertSame(0, $result['code']);
- $data = $this->channelList(['data_type' => '1', 'key' => 'test-key'])['data'][0];
- $this->assertSame($row->id, $data['id']);
- $this->assertSame('jd', $data['payment_company']);
- $this->assertSame('JD支付 / 测试通道', $data['display_name']);
- }
- public function test_invalid_company_type_or_source_is_rejected_without_writing(): void
- {
- $before = DB::table('recharge_channel')->count();
- foreach ([
- ['payment_company' => 'jd', 'type' => 'NOpay12'],
- ['payment_company' => 'no', 'type' => '12'],
- ['payment_company' => 'no', 'type' => 'usdt'],
- ['payment_company' => 'no', 'from' => 2],
- ['payment_company' => 'sanjin', 'type' => 'DF001', 'data_type' => 2],
- ] as $override) {
- $this->assertSame(-3, $this->saveChannel($this->newChannel($override))['code']);
- }
- $this->assertSame($before, DB::table('recharge_channel')->count());
- }
- public function test_native_and_activity_channels_do_not_require_or_fabricate_company(): void
- {
- foreach ([[1, 'usdt', 2], [1, 'rgcz', 3], [2, 'rgtx', 3], [3, 'recharge', 1]] as [$direction, $type, $from]) {
- $result = $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => $direction,
- 'type' => $type, 'key' => 'local-' . $type]));
- $this->assertSame(0, $result['code']);
- $row = $this->channelList(['key' => 'local-' . $type])['data'][0];
- $this->assertSame($from, $row['from']);
- $this->assertNull($row['payment_company']);
- $this->assertSame('测试通道', $row['display_name']);
- }
- }
- public function test_existing_valid_clients_can_omit_derived_company_and_from(): void
- {
- $params = $this->newChannel();
- unset($params['payment_company']);
- $this->assertSame(0, $this->saveChannel($params)['code']);
- $row = $this->channelList(['key' => 'test-key'])['data'][0];
- $this->assertSame('no', $row['payment_company']);
- $this->assertSame(1, $row['from']);
- }
- public function test_linked_channel_locks_identity_but_keeps_regular_edit_available(): void
- {
- DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
- $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
- });
- DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
- $options = $this->formOptions(1, 24);
- $this->assertFalse($options['identity_editable']);
- $this->assertNotSame('', $options['identity_edit_reason']);
- $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no',
- 'type' => 'NOpay12', 'rate' => '0.01']);
- $this->assertSame(-3, $result['code']);
- $this->assertSame('JDpay', DB::table('recharge_channel')->where('id', 24)->value('type'));
- $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd',
- 'type' => 'JDpay', 'rate' => '0.01', 'name' => 'JD新名称']);
- $this->assertSame(0, $result['code']);
- DB::table('payment_gateways')->update(['deleted_at' => now()]);
- $this->assertTrue($this->formOptions(1, 24)['identity_editable']);
- $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
- 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
- }
- public function test_switching_company_does_not_rewrite_group_permissions(): void
- {
- $groups = DB::table('recharge_channel_group')->orderBy('id')->get()->toJson();
- $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
- 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
- $this->assertSame($groups, DB::table('recharge_channel_group')->orderBy('id')->get()->toJson());
- $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 2,
- 'payment_company' => 'no', 'type' => 'NOwithdraw', 'rate' => '0.01'])['code']);
- }
- public function test_group_option_names_include_company_without_changing_values(): void
- {
- $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
- $rows = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], null, 'type');
- $this->assertSame('JD钱包', $rows['JDpay']['name']);
- $this->assertSame('jd', $rows['JDpay']['payment_company']);
- $this->assertSame('JD支付 / JD钱包', $rows['JDpay']['display_name']);
- $this->assertSame('USDT充值', $rows['usdt']['display_name']);
- }
- public function test_merchant_save_rechecks_channel_identity_inside_transaction(): void
- {
- DB::table('recharge_channel')->where('id', 26)->update(['type' => 'JDpay']);
- $this->request('/admin/paymentConfig/gateways/save', []);
- $controller = new PaymentConfiguration(new PaymentChannelLinkService());
- $method = new \ReflectionMethod($controller, 'saveModel');
- $method->setAccessible(true);
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('支付公司与所选通道不匹配');
- $method->invoke($controller, PaymentGateway::class, ['kind' => 'deposit', 'recharge_channel_id' => 26,
- 'recharge_channel_group_ids' => [1], 'payment_company' => 'no', 'payment_method' => 'NOpay12'], 'payment_gateway');
- }
- }
|