|
@@ -22,7 +22,11 @@ use Illuminate\Support\Facades\Facade;
|
|
|
use Illuminate\Translation\ArrayLoader;
|
|
use Illuminate\Translation\ArrayLoader;
|
|
|
use Illuminate\Translation\Translator;
|
|
use Illuminate\Translation\Translator;
|
|
|
use Illuminate\Validation\Factory;
|
|
use Illuminate\Validation\Factory;
|
|
|
|
|
+use PHPUnit\Framework\Attributes\PreserveGlobalState;
|
|
|
|
|
+use PHPUnit\Framework\Attributes\RunInSeparateProcess;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
+use Psr\Log\NullLogger;
|
|
|
|
|
+use Tests\Support\RecordingProviderClient;
|
|
|
|
|
|
|
|
class PaymentChannelOptionsTest extends TestCase
|
|
class PaymentChannelOptionsTest extends TestCase
|
|
|
{
|
|
{
|
|
@@ -136,21 +140,21 @@ class PaymentChannelOptionsTest extends TestCase
|
|
|
DB::table('recharge_channel')->where('data_type', 1)->update(['status' => 0]);
|
|
DB::table('recharge_channel')->where('data_type', 1)->update(['status' => 0]);
|
|
|
DB::table('payment_providers')->update(['status' => 0]);
|
|
DB::table('payment_providers')->update(['status' => 0]);
|
|
|
$options = $service->options();
|
|
$options = $service->options();
|
|
|
- $this->assertSame(13, $options['total']);
|
|
|
|
|
|
|
+ $this->assertSame(17, $options['total']);
|
|
|
$rows = array_column($options['data'], null, 'channel_type');
|
|
$rows = array_column($options['data'], null, 'channel_type');
|
|
|
- foreach (['wxsm','ylsm','szrmb','zfbsm','zfbge','zfbzk','sdjt','JDpay','NOpay12','NOpay13','ZIMUpay','usdt','rgcz'] as $type) {
|
|
|
|
|
|
|
+ foreach (['wxsm','ylsm','szrmb','zfbsm','zfbge','zfbzk','qq','xeh5','zfbqy','zfbh5','sdjt','JDpay','NOpay12','NOpay13','ZIMUpay','usdt','rgcz'] as $type) {
|
|
|
$this->assertArrayHasKey($type, $rows);
|
|
$this->assertArrayHasKey($type, $rows);
|
|
|
}
|
|
}
|
|
|
$this->assertSame('三斤支付 / 微信扫码', $rows['wxsm']['channel_name']);
|
|
$this->assertSame('三斤支付 / 微信扫码', $rows['wxsm']['channel_name']);
|
|
|
DB::table('recharge_channel')->delete(); DB::table('recharge_channel_group')->delete();
|
|
DB::table('recharge_channel')->delete(); DB::table('recharge_channel_group')->delete();
|
|
|
- $this->assertSame(13, $service->options()['total']);
|
|
|
|
|
|
|
+ $this->assertSame(17, $service->options()['total']);
|
|
|
$result = $service->save(['channel_type' => 'wxsm', 'logo' => 'https://cdn.example.test/wx.png']);
|
|
$result = $service->save(['channel_type' => 'wxsm', 'logo' => 'https://cdn.example.test/wx.png']);
|
|
|
$this->assertSame('https://cdn.example.test/wx.png', $result['logo']);
|
|
$this->assertSame('https://cdn.example.test/wx.png', $result['logo']);
|
|
|
$this->assertSame(0, DB::table('recharge_channel')->count());
|
|
$this->assertSame(0, DB::table('recharge_channel')->count());
|
|
|
$this->assertSame('https://cdn.example.test/wx.png', array_column($service->options()['data'], null, 'channel_type')['wxsm']['logo']);
|
|
$this->assertSame('https://cdn.example.test/wx.png', array_column($service->options()['data'], null, 'channel_type')['wxsm']['logo']);
|
|
|
$this->assertSame('', $service->save(['channel_type' => 'wxsm', 'logo' => null])['logo']);
|
|
$this->assertSame('', $service->save(['channel_type' => 'wxsm', 'logo' => null])['logo']);
|
|
|
$this->request('/admin/rechargeChannel/logoOptions', []);
|
|
$this->request('/admin/rechargeChannel/logoOptions', []);
|
|
|
- $this->assertSame(13, (new RechargeChannel())->logoOptions($service)->getData(true)['data']['total']);
|
|
|
|
|
|
|
+ $this->assertSame(17, (new RechargeChannel())->logoOptions($service)->getData(true)['data']['total']);
|
|
|
$router = new \Illuminate\Routing\Router(new \Illuminate\Events\Dispatcher($this->app), $this->app);
|
|
$router = new \Illuminate\Routing\Router(new \Illuminate\Events\Dispatcher($this->app), $this->app);
|
|
|
$this->app->instance('router', $router);
|
|
$this->app->instance('router', $router);
|
|
|
$router->group(['prefix' => 'admin'], function () { require dirname(__DIR__, 2) . '/routes/admin.php'; });
|
|
$router->group(['prefix' => 'admin'], function () { require dirname(__DIR__, 2) . '/routes/admin.php'; });
|
|
@@ -284,6 +288,118 @@ class PaymentChannelOptionsTest extends TestCase
|
|
|
$this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
|
|
$this->assertSame('wxsm', DB::table('recharge_channel_group')->value('recharge_type'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[RunInSeparateProcess]
|
|
|
|
|
+ #[PreserveGlobalState(false)]
|
|
|
|
|
+ public function test_new_sanjin_channels_use_their_own_limits_and_product_codes(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->app->instance('log', new NullLogger());
|
|
|
|
|
+ $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_14_120000_add_sanjin_recharge_channels.php';
|
|
|
|
|
+ $migration->up();
|
|
|
|
|
+ $migration->up();
|
|
|
|
|
+ $expected = [
|
|
|
|
|
+ 'qq' => ['QQ', 10, 1000, '0.0100'],
|
|
|
|
|
+ 'xeh5' => ['XEH5', 100, 199, '0.1500'],
|
|
|
|
|
+ 'zfbqy' => ['ZFBQY', 200, 5000, '0.1300'],
|
|
|
|
|
+ 'zfbh5' => ['ZFBH5', 200, 5000, '0.1250'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ $this->assertSame(4, ChannelModel::whereIn('type', array_keys($expected))->count());
|
|
|
|
|
+ $this->request('/admin/rechargeChannel/getChannel', ['data_type' => 1]);
|
|
|
|
|
+ $groupOptions = (new RechargeChannel())->getChannel()->getData(true)['data']['data'];
|
|
|
|
|
+ $this->assertSame(array_keys($expected), array_values(array_intersect(
|
|
|
|
|
+ array_keys($expected), array_column($groupOptions, 'type')
|
|
|
|
|
+ )));
|
|
|
|
|
+ (require dirname(__DIR__, 2) . '/database/migrations/2026_11_01_120000_add_config_to_payment_providers.php')->up();
|
|
|
|
|
+ config(['app.tree_pay_mch_id' => 'test-sanjin', 'app.tree_pay_key' => 'test-secret', 'app.url' => 'https://example.test']);
|
|
|
|
|
+ $this->assertFalse(class_exists(\GuzzleHttp\Client::class, false));
|
|
|
|
|
+ class_alias(RecordingProviderClient::class, \GuzzleHttp\Client::class);
|
|
|
|
|
+ RecordingProviderClient::$responseBody = '{"code":88,"message":"mock gateway rejected"}';
|
|
|
|
|
+ $channels = array_column(ChannelModel::getFormatChannel(1, 1), null, 'value');
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($expected as $type => [$key, $min, $max, $rate]) {
|
|
|
|
|
+ $channel = ChannelModel::checkRechargeChannel($type, 1);
|
|
|
|
|
+ $this->assertSame($key, $channel->key);
|
|
|
|
|
+ $this->assertSame([$min, $max], [(int) $channel->min, (int) $channel->max]);
|
|
|
|
|
+ $this->assertSame($rate, bcadd((string) $channel->rate, '0', 4));
|
|
|
|
|
+ $this->assertArrayHasKey($type, $channels);
|
|
|
|
|
+ $this->assertFalse(ChannelModel::checkRechargeChannel($type, 2));
|
|
|
|
|
+
|
|
|
|
|
+ $rejected = PaymentOrderService::createPayOrder(\App\Models\PaymentOrder::class, 'member', $max + 1, $type, 1, $channel);
|
|
|
|
|
+ $this->assertSame(20001, $rejected['code']);
|
|
|
|
|
+ $this->assertSame([], RecordingProviderClient::$requests);
|
|
|
|
|
+ $fractionalCent = PaymentOrderService::createPayOrder(\App\Models\PaymentOrder::class, 'member', $min . '.001', $type, 1, $channel);
|
|
|
|
|
+ $this->assertSame(20001, $fractionalCent['code']);
|
|
|
|
|
+ $this->assertSame([], RecordingProviderClient::$requests);
|
|
|
|
|
+
|
|
|
|
|
+ // The transport captures the request and rejects it before QR generation or order creation.
|
|
|
|
|
+ $result = PaymentOrderService::createPayOrder(\App\Models\PaymentOrder::class, 'member', $min + 0.20, $type, 1, $channel);
|
|
|
|
|
+ $this->assertSame(20002, $result['code']);
|
|
|
|
|
+ $request = array_pop(RecordingProviderClient::$requests);
|
|
|
|
|
+ $this->assertSame('api/v1/pay/unifiedOrder', $request['url']);
|
|
|
|
|
+ $this->assertSame($key, $request['options']['json']['productId']);
|
|
|
|
|
+ $this->assertSame($min * 100 + 20, $request['options']['json']['amount']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $channel = ChannelModel::findOrFail(6);
|
|
|
|
|
+ $model = \App\Models\PaymentOrder::class;
|
|
|
|
|
+ $result = PaymentOrderService::createPayOrder($model, 'member', 100.01 + 0.20, 'zfbsm', 1, $channel, channelId: 6);
|
|
|
|
|
+ $this->assertSame(20002, $result['code']);
|
|
|
|
|
+ $request = array_pop(RecordingProviderClient::$requests);
|
|
|
|
|
+ $this->assertSame(10021, $request['options']['json']['amount']);
|
|
|
|
|
+ foreach ([100.211, '100.21000000000001'] as $amount) {
|
|
|
|
|
+ $result = PaymentOrderService::createPayOrder($model, 'member', $amount, 'zfbsm', 1, $channel, channelId: 6);
|
|
|
|
|
+ $this->assertSame(20001, $result['code']);
|
|
|
|
|
+ $this->assertSame([], RecordingProviderClient::$requests);
|
|
|
|
|
+ }
|
|
|
|
|
+ $channel = ChannelModel::checkRechargeChannel('xeh5', 1);
|
|
|
|
|
+ $result = PaymentOrderService::createPayOrder($model, 'member', 199.00000000000003, 'xeh5', 1, $channel);
|
|
|
|
|
+ $this->assertSame(20002, $result['code']);
|
|
|
|
|
+ $request = array_pop(RecordingProviderClient::$requests);
|
|
|
|
|
+ $this->assertSame(19900, $request['options']['json']['amount']);
|
|
|
|
|
+
|
|
|
|
|
+ DB::table('recharge_channel')->where('key', 'QQ')->update(['status' => 0, 'min' => 20]);
|
|
|
|
|
+ $migration->up();
|
|
|
|
|
+ $this->assertSame(20, (int) DB::table('recharge_channel')->where('key', 'QQ')->value('min'));
|
|
|
|
|
+ $this->assertFalse(ChannelModel::checkRechargeChannel('qq', 1));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function test_sanjin_channel_migration_preserves_matching_live_channel_after_deleted_record(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $channels = DB::table('recharge_channel');
|
|
|
|
|
+ $row = ['data_type' => 1, 'from' => 1, 'key' => 'QQ', 'type' => 'qq', 'name' => 'QQ transfer',
|
|
|
|
|
+ 'rate' => '0.02', 'min' => 30, 'max' => 1000, 'status' => 1, 'sort' => 46];
|
|
|
|
|
+ $deleted = $channels->insertGetId($row + ['deleted_at' => now()]);
|
|
|
|
|
+ $live = $channels->insertGetId($row);
|
|
|
|
|
+ $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_14_120000_add_sanjin_recharge_channels.php';
|
|
|
|
|
+
|
|
|
|
|
+ $migration->up();
|
|
|
|
|
+
|
|
|
|
|
+ $channel = ChannelModel::checkRechargeChannel('qq', 1);
|
|
|
|
|
+ $this->assertSame($live, $channel->id);
|
|
|
|
|
+ $this->assertSame(30, (int) $channel->min);
|
|
|
|
|
+ $this->assertNotNull(ChannelModel::withTrashed()->findOrFail($deleted)->deleted_at);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function test_sanjin_channel_migration_rolls_back_when_a_key_conflicts(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $channels = DB::table('recharge_channel');
|
|
|
|
|
+ $conflict = $channels->insertGetId(['data_type' => 1, 'from' => 1, 'key' => 'XEH5', 'type' => 'zfbsm',
|
|
|
|
|
+ 'name' => 'Existing Alipay channel', 'rate' => '0.10', 'min' => 100, 'max' => 199, 'status' => 1, 'sort' => 46]);
|
|
|
|
|
+ $groupTypes = DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type');
|
|
|
|
|
+ $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_11_14_120000_add_sanjin_recharge_channels.php';
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $migration->up();
|
|
|
|
|
+ $this->fail('Expected migration to report the occupied XEH5 Key');
|
|
|
|
|
+ } catch (\RuntimeException $exception) {
|
|
|
|
|
+ $this->assertStringContainsString('Key XEH5', $exception->getMessage());
|
|
|
|
|
+ $this->assertStringContainsString("通道 ID {$conflict}", $exception->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertFalse($channels->where('key', 'QQ')->exists());
|
|
|
|
|
+ $this->assertSame('zfbsm', ChannelModel::findOrFail($conflict)->type);
|
|
|
|
|
+ $this->assertSame($groupTypes, DB::table('recharge_channel_group')->where('id', 1)->value('recharge_type'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function formOptions(int $dataType = 1, ?int $id = null): array
|
|
private function formOptions(int $dataType = 1, ?int $id = null): array
|
|
|
{
|
|
{
|
|
|
$this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
|
|
$this->request('/admin/rechargeChannel/options', ['data_type' => $dataType] + ($id ? ['id' => $id] : []));
|