PaymentChannelOptionsTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\PaymentConfiguration;
  4. use App\Http\Controllers\admin\RechargeChannel;
  5. use App\Services\PaymentChannelLinkService;
  6. use App\Services\Payment\RechargeChannelConfigurationService;
  7. use App\Models\PaymentGateway;
  8. use Illuminate\Config\Repository;
  9. use Illuminate\Database\Capsule\Manager;
  10. use Illuminate\Database\Schema\Blueprint;
  11. use Illuminate\Foundation\Application;
  12. use Illuminate\Http\JsonResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Facade;
  16. use Illuminate\Translation\ArrayLoader;
  17. use Illuminate\Translation\Translator;
  18. use Illuminate\Validation\Factory;
  19. use PHPUnit\Framework\TestCase;
  20. class PaymentChannelOptionsTest extends TestCase
  21. {
  22. private Application $app;
  23. protected function setUp(): void
  24. {
  25. Facade::clearResolvedInstances();
  26. $this->app = new Application(dirname(__DIR__, 2));
  27. $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
  28. Facade::setFacadeApplication($this->app);
  29. $db = new Manager($this->app);
  30. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  31. $db->setAsGlobal();
  32. $db->bootEloquent();
  33. $this->app->instance('db', $db->getDatabaseManager());
  34. $this->app->bind('db.schema', fn () => $db->schema());
  35. $translator = new Translator(new ArrayLoader(), 'zh');
  36. $this->app->instance('translator', $translator);
  37. $this->app->instance('validator', new Factory($translator, $this->app));
  38. $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
  39. public function json($data, $status = 200, $headers = [], $options = 0) {
  40. return new JsonResponse($data, $status, $headers, $options);
  41. }
  42. });
  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. }
  60. protected function tearDown(): void
  61. {
  62. DB::disconnect();
  63. Facade::clearResolvedInstances();
  64. parent::tearDown();
  65. }
  66. private function request(string $path, array $params): void
  67. {
  68. $this->app->instance('request', Request::create($path, 'GET', $params));
  69. }
  70. private function channelList(array $params): array
  71. {
  72. $this->request('/admin/rechargeChannel/list', $params);
  73. $response = (new RechargeChannel())->list()->getData(true);
  74. $this->assertSame(0, $response['code']);
  75. return $response['data'];
  76. }
  77. public function test_company_options_use_all_rows_not_first_page(): void
  78. {
  79. $page1 = $this->channelList(['data_type' => '1']);
  80. $this->assertSame(19, $page1['total']);
  81. $this->assertCount(15, $page1['data']);
  82. $this->assertNotContains('jd', array_column($page1['data'], 'payment_company'));
  83. $page2 = $this->channelList(['data_type' => '1', 'page' => 2]);
  84. $this->assertSame(['zimu', 'no', 'no', 'jd'], array_column($page2['data'], 'payment_company'));
  85. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  86. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true);
  87. $this->assertSame(0, $data['code']);
  88. $this->assertSame([false, false, false, false], array_column($data['data']['payment_companies'], 'disabled'));
  89. }
  90. public function test_channel_list_displays_provider_separately_from_channel_name(): void
  91. {
  92. $data = $this->channelList(['data_type' => '1', 'payment_company' => 'jd']);
  93. $this->assertSame(1, $data['total']);
  94. $this->assertSame('JD钱包', $data['data'][0]['name']);
  95. $this->assertSame('JD支付', $data['data'][0]['payment_company_label']);
  96. $this->assertSame(1, $data['data'][0]['from']);
  97. $this->assertSame(24, $data['data'][0]['id']);
  98. $withdraw = $this->channelList(['data_type' => '2', 'payment_company' => 'jd']);
  99. $this->assertSame(25, $withdraw['data'][0]['id']);
  100. $this->assertSame(0, $this->channelList(['data_type' => '2', 'payment_company' => 'sanjin'])['total']);
  101. }
  102. public function test_missing_group_returns_reason_without_enabling_or_rewriting_config(): void
  103. {
  104. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm,zfbsm']);
  105. $this->request('/admin/paymentConfig/options', ['kind' => 'deposit']);
  106. $data = (new PaymentConfiguration(new PaymentChannelLinkService()))->options()->getData(true)['data'];
  107. $this->assertSame([false, true, true, true], array_column($data['payment_companies'], 'disabled'));
  108. $this->assertSame(['', 'group_missing', 'group_missing', 'group_missing'], array_column($data['payment_companies'], 'disabled_reason_code'));
  109. $this->assertSame('wxsm,zfbsm', DB::table('recharge_channel_group')->value('recharge_type'));
  110. $this->assertSame(0, DB::table('recharge_channel')->where('id', 31)->value('status'));
  111. }
  112. public function test_group_editor_options_include_new_types_even_before_any_group_uses_them(): void
  113. {
  114. DB::table('recharge_channel_group')->update(['recharge_type' => 'wxsm', 'withdraw_type' => 'DF001']);
  115. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  116. $response = (new RechargeChannel())->getChannel()->getData(true);
  117. $this->assertSame(0, $response['code']);
  118. $names = array_column($response['data']['data'], 'name', 'type');
  119. $this->assertSame('JD钱包', $names['JDpay']);
  120. $this->assertSame('NO快捷充值-扫码支付', $names['NOpay12']);
  121. $this->assertSame('NO快捷充值-余额支付', $names['NOpay13']);
  122. $this->assertSame('808充值', $names['ZIMUpay']);
  123. $this->assertSame(13, $response['data']['total']);
  124. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 2]);
  125. $names = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], 'name', 'type');
  126. $this->assertSame('JD钱包', $names['JDpay']);
  127. $this->assertSame('NO快捷提现', $names['NOwithdraw']);
  128. $this->assertSame('808账户提现', $names['ZIMUwithdraw']);
  129. $this->assertArrayNotHasKey('ZIMUcash', $names);
  130. $this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
  131. }
  132. private function formOptions(int $dataType = 1, ?int $id = null): array
  133. {
  134. $this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
  135. $response = (new RechargeChannel())->options(new RechargeChannelConfigurationService())->getData(true);
  136. $this->assertSame(0, $response['code']);
  137. return $response['data'];
  138. }
  139. private function saveChannel(array $params): array
  140. {
  141. $this->app->instance('request', Request::create('/admin/rechargeChannel/update', 'POST', $params));
  142. return (new RechargeChannel())->update(new RechargeChannelConfigurationService())->getData(true);
  143. }
  144. private function newChannel(array $overrides = []): array
  145. {
  146. return $overrides + ['data_type' => 1, 'payment_company' => 'no', 'type' => 'NOpay12',
  147. 'rate' => '0.0300', 'name' => '测试通道', 'key' => 'test-key', 'min' => 1, 'max' => 100];
  148. }
  149. public function test_creation_catalog_works_without_any_database_channels_or_groups(): void
  150. {
  151. DB::table('recharge_channel')->delete();
  152. DB::table('recharge_channel_group')->delete();
  153. $data = $this->formOptions();
  154. $this->assertSame(['sanjin', 'jd', 'no', 'zimu'], array_column($data['payment_companies'], 'value'));
  155. $this->assertTrue($data['identity_editable']);
  156. $this->assertSame('', $data['identity_edit_reason']);
  157. $this->assertContains('NOpay12', array_column($data['types'], 'value'));
  158. $this->assertSame(['qianbao', 'jd', 'no', 'zimu'], array_column($this->formOptions(2)['payment_companies'], 'value'));
  159. $this->assertSame([], $this->formOptions(3)['payment_companies']);
  160. }
  161. public function test_create_selected_company_then_switch_company_and_type_on_same_record(): void
  162. {
  163. $result = $this->saveChannel($this->newChannel());
  164. $this->assertSame(0, $result['code']);
  165. $this->assertSame([], $result['data']);
  166. $row = DB::table('recharge_channel')->where('key', 'test-key')->first();
  167. $this->assertSame(1, $row->from);
  168. $this->assertSame(1, $row->status);
  169. $this->assertSame(0, $row->sort);
  170. $result = $this->saveChannel(['id' => $row->id, 'data_type' => 1,
  171. 'payment_company' => 'jd', 'type' => 'JDpay', 'rate' => '0.02']);
  172. $this->assertSame(0, $result['code']);
  173. $data = $this->channelList(['data_type' => '1', 'key' => 'test-key'])['data'][0];
  174. $this->assertSame($row->id, $data['id']);
  175. $this->assertSame('jd', $data['payment_company']);
  176. $this->assertSame('JD支付 / 测试通道', $data['display_name']);
  177. }
  178. public function test_invalid_company_type_or_source_is_rejected_without_writing(): void
  179. {
  180. $before = DB::table('recharge_channel')->count();
  181. foreach ([
  182. ['payment_company' => 'jd', 'type' => 'NOpay12'],
  183. ['payment_company' => 'no', 'type' => '12'],
  184. ['payment_company' => 'no', 'type' => 'usdt'],
  185. ['payment_company' => 'no', 'from' => 2],
  186. ['payment_company' => 'sanjin', 'type' => 'DF001', 'data_type' => 2],
  187. ] as $override) {
  188. $this->assertSame(-3, $this->saveChannel($this->newChannel($override))['code']);
  189. }
  190. $this->assertSame($before, DB::table('recharge_channel')->count());
  191. }
  192. public function test_native_and_activity_channels_do_not_require_or_fabricate_company(): void
  193. {
  194. foreach ([[1, 'usdt', 2], [1, 'rgcz', 3], [2, 'rgtx', 3], [3, 'recharge', 1]] as [$direction, $type, $from]) {
  195. $result = $this->saveChannel($this->newChannel(['payment_company' => null, 'data_type' => $direction,
  196. 'type' => $type, 'key' => 'local-' . $type]));
  197. $this->assertSame(0, $result['code']);
  198. $row = $this->channelList(['key' => 'local-' . $type])['data'][0];
  199. $this->assertSame($from, $row['from']);
  200. $this->assertNull($row['payment_company']);
  201. $this->assertSame('测试通道', $row['display_name']);
  202. }
  203. }
  204. public function test_existing_valid_clients_can_omit_derived_company_and_from(): void
  205. {
  206. $params = $this->newChannel();
  207. unset($params['payment_company']);
  208. $this->assertSame(0, $this->saveChannel($params)['code']);
  209. $row = $this->channelList(['key' => 'test-key'])['data'][0];
  210. $this->assertSame('no', $row['payment_company']);
  211. $this->assertSame(1, $row['from']);
  212. }
  213. public function test_linked_channel_locks_identity_but_keeps_regular_edit_available(): void
  214. {
  215. DB::connection()->getSchemaBuilder()->create('payment_gateways', function (Blueprint $t) {
  216. $t->id(); $t->unsignedBigInteger('recharge_channel_id'); $t->softDeletes();
  217. });
  218. DB::table('payment_gateways')->insert(['recharge_channel_id' => 24]);
  219. $options = $this->formOptions(1, 24);
  220. $this->assertFalse($options['identity_editable']);
  221. $this->assertNotSame('', $options['identity_edit_reason']);
  222. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'no',
  223. 'type' => 'NOpay12', 'rate' => '0.01']);
  224. $this->assertSame(-3, $result['code']);
  225. $this->assertSame('JDpay', DB::table('recharge_channel')->where('id', 24)->value('type'));
  226. $result = $this->saveChannel(['id' => 24, 'data_type' => 1, 'payment_company' => 'jd',
  227. 'type' => 'JDpay', 'rate' => '0.01', 'name' => 'JD新名称']);
  228. $this->assertSame(0, $result['code']);
  229. DB::table('payment_gateways')->update(['deleted_at' => now()]);
  230. $this->assertTrue($this->formOptions(1, 24)['identity_editable']);
  231. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  232. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  233. }
  234. public function test_switching_company_does_not_rewrite_group_permissions(): void
  235. {
  236. $groups = DB::table('recharge_channel_group')->orderBy('id')->get()->toJson();
  237. $this->assertSame(0, $this->saveChannel(['id' => 24, 'data_type' => 1,
  238. 'payment_company' => 'no', 'type' => 'NOpay12', 'rate' => '0.01'])['code']);
  239. $this->assertSame($groups, DB::table('recharge_channel_group')->orderBy('id')->get()->toJson());
  240. $this->assertSame(-3, $this->saveChannel(['id' => 24, 'data_type' => 2,
  241. 'payment_company' => 'no', 'type' => 'NOwithdraw', 'rate' => '0.01'])['code']);
  242. }
  243. public function test_group_option_names_include_company_without_changing_values(): void
  244. {
  245. $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
  246. $rows = array_column((new RechargeChannel())->getChannel()->getData(true)['data']['data'], null, 'type');
  247. $this->assertSame('JD钱包', $rows['JDpay']['name']);
  248. $this->assertSame('jd', $rows['JDpay']['payment_company']);
  249. $this->assertSame('JD支付 / JD钱包', $rows['JDpay']['display_name']);
  250. $this->assertSame('USDT充值', $rows['usdt']['display_name']);
  251. }
  252. public function test_merchant_save_rechecks_channel_identity_inside_transaction(): void
  253. {
  254. DB::table('recharge_channel')->where('id', 26)->update(['type' => 'JDpay']);
  255. $this->request('/admin/paymentConfig/gateways/save', []);
  256. $controller = new PaymentConfiguration(new PaymentChannelLinkService());
  257. $method = new \ReflectionMethod($controller, 'saveModel');
  258. $method->setAccessible(true);
  259. $this->expectException(\InvalidArgumentException::class);
  260. $this->expectExceptionMessage('支付公司与所选通道不匹配');
  261. $method->invoke($controller, PaymentGateway::class, ['kind' => 'deposit', 'recharge_channel_id' => 26,
  262. 'recharge_channel_group_ids' => [1], 'payment_company' => 'no', 'payment_method' => 'NOpay12'], 'payment_gateway');
  263. }
  264. }