| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace Tests\Unit;
- use App\Services\PayPlus;
- use Tests\TestCase;
- class PayPlusServiceTest extends TestCase
- {
- /** @test */
- public function it_builds_payout_sign_with_flattened_sorted_payload()
- {
- $service = new PayPlus([
- 'appKey' => '123456',
- ]);
- $payload = [
- 'k2' => [1, 2],
- 'empty' => '',
- 'k3' => [
- 'b1' => 'test1',
- 'a2' => 'test2',
- ],
- 'k1' => 1,
- 'null_value' => null,
- ];
- $sign = $service->signPayoutPayload($payload);
- $this->assertSame(
- hash('sha256', 'k1=1&k2=[1,2]&k3_a2=test2&k3_b1=test1' . '123456'),
- $sign
- );
- }
- /** @test */
- public function it_formats_payout_notify_amounts_to_four_decimals_when_verifying_signature()
- {
- $service = new PayPlus([
- 'appKey' => '123456',
- ]);
- $payload = [
- 'transaction_id' => 'T100',
- 'reference_id' => 'TX100',
- 'status' => 'PAID',
- 'timestamp' => 162856465000,
- 'payout_amount' => 10,
- 'source_amount' => '11',
- 'payout_fee' => 1,
- ];
- $signString = 'payout_amount=10.0000&payout_fee=1.0000&reference_id=TX100'
- . '&source_amount=11.0000&status=PAID×tamp=162856465000&transaction_id=T100';
- $this->assertTrue($service->verifyPayoutSignature(
- $payload,
- hash('sha256', $signString . '123456')
- ));
- }
- /** @test */
- public function it_encrypts_and_decrypts_component_delta_with_aes_gcm()
- {
- $service = new PayPlus([]);
- $aesKey = str_repeat('01', 32);
- $iv = str_repeat('02', 12);
- $payload = '{"hello":"payplus"}';
- $encrypted = $service->encryptComponentDelta($payload, $aesKey, $iv);
- $decrypted = $service->decryptComponentDelta($encrypted, $aesKey, $iv);
- $this->assertSame($payload, $decrypted);
- }
- /** @test */
- public function it_builds_beneficiary_payload_with_safe_defaults()
- {
- $service = new PayPlus([]);
- $payload = $service->buildBeneficiaryPayload([
- 'user_id' => 10001,
- 'email' => '',
- 'phone' => '',
- 'first_name' => '',
- 'last_name' => '',
- ], 1234567890);
- $this->assertSame('10001', $payload['payee_id']);
- $this->assertSame('en', $payload['language']);
- $this->assertSame('US', $payload['country']);
- $this->assertSame('+1-0000000000', $payload['phone']);
- $this->assertSame('unknown10001@example.com', $payload['email']);
- $this->assertSame('unknown', $payload['first_name']);
- $this->assertSame('user', $payload['last_name']);
- $this->assertSame(1234567890, $payload['timestamp']);
- }
- /** @test */
- public function it_keeps_only_english_letters_for_beneficiary_names()
- {
- $service = new PayPlus([]);
- $payload = $service->buildBeneficiaryPayload([
- 'user_id' => 10002,
- 'first_name' => '张San-123',
- 'last_name' => '李User_456',
- ], 1234567890);
- $this->assertSame('San', $payload['first_name']);
- $this->assertSame('User', $payload['last_name']);
- $payload = $service->buildBeneficiaryPayload([
- 'user_id' => 10003,
- 'first_name' => '张三123',
- 'last_name' => '456-',
- ], 1234567890);
- $this->assertSame('unknown', $payload['first_name']);
- $this->assertSame('user', $payload['last_name']);
- }
- }
|