PayPlusServiceTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\PayPlus;
  4. use Tests\TestCase;
  5. class PayPlusServiceTest extends TestCase
  6. {
  7. /** @test */
  8. public function it_builds_payout_sign_with_flattened_sorted_payload()
  9. {
  10. $service = new PayPlus([
  11. 'appKey' => '123456',
  12. ]);
  13. $payload = [
  14. 'k2' => [1, 2],
  15. 'empty' => '',
  16. 'k3' => [
  17. 'b1' => 'test1',
  18. 'a2' => 'test2',
  19. ],
  20. 'k1' => 1,
  21. 'null_value' => null,
  22. ];
  23. $sign = $service->signPayoutPayload($payload);
  24. $this->assertSame(
  25. hash('sha256', 'k1=1&k2=[1,2]&k3_a2=test2&k3_b1=test1' . '123456'),
  26. $sign
  27. );
  28. }
  29. /** @test */
  30. public function it_formats_payout_notify_amounts_to_four_decimals_when_verifying_signature()
  31. {
  32. $service = new PayPlus([
  33. 'appKey' => '123456',
  34. ]);
  35. $payload = [
  36. 'transaction_id' => 'T100',
  37. 'reference_id' => 'TX100',
  38. 'status' => 'PAID',
  39. 'timestamp' => 162856465000,
  40. 'payout_amount' => 10,
  41. 'source_amount' => '11',
  42. 'payout_fee' => 1,
  43. ];
  44. $signString = 'payout_amount=10.0000&payout_fee=1.0000&reference_id=TX100'
  45. . '&source_amount=11.0000&status=PAID&timestamp=162856465000&transaction_id=T100';
  46. $this->assertTrue($service->verifyPayoutSignature(
  47. $payload,
  48. hash('sha256', $signString . '123456')
  49. ));
  50. }
  51. /** @test */
  52. public function it_encrypts_and_decrypts_component_delta_with_aes_gcm()
  53. {
  54. $service = new PayPlus([]);
  55. $aesKey = str_repeat('01', 32);
  56. $iv = str_repeat('02', 12);
  57. $payload = '{"hello":"payplus"}';
  58. $encrypted = $service->encryptComponentDelta($payload, $aesKey, $iv);
  59. $decrypted = $service->decryptComponentDelta($encrypted, $aesKey, $iv);
  60. $this->assertSame($payload, $decrypted);
  61. }
  62. /** @test */
  63. public function it_builds_beneficiary_payload_with_safe_defaults()
  64. {
  65. $service = new PayPlus([]);
  66. $payload = $service->buildBeneficiaryPayload([
  67. 'user_id' => 10001,
  68. 'email' => '',
  69. 'phone' => '',
  70. 'first_name' => '',
  71. 'last_name' => '',
  72. ], 1234567890);
  73. $this->assertSame('10001', $payload['payee_id']);
  74. $this->assertSame('en', $payload['language']);
  75. $this->assertSame('US', $payload['country']);
  76. $this->assertSame('+1-0000000000', $payload['phone']);
  77. $this->assertSame('unknown10001@example.com', $payload['email']);
  78. $this->assertSame('unknown', $payload['first_name']);
  79. $this->assertSame('user', $payload['last_name']);
  80. $this->assertSame(1234567890, $payload['timestamp']);
  81. }
  82. /** @test */
  83. public function it_keeps_only_english_letters_for_beneficiary_names()
  84. {
  85. $service = new PayPlus([]);
  86. $payload = $service->buildBeneficiaryPayload([
  87. 'user_id' => 10002,
  88. 'first_name' => '张San-123',
  89. 'last_name' => '李User_456',
  90. ], 1234567890);
  91. $this->assertSame('San', $payload['first_name']);
  92. $this->assertSame('User', $payload['last_name']);
  93. $payload = $service->buildBeneficiaryPayload([
  94. 'user_id' => 10003,
  95. 'first_name' => '张三123',
  96. 'last_name' => '456-',
  97. ], 1234567890);
  98. $this->assertSame('unknown', $payload['first_name']);
  99. $this->assertSame('user', $payload['last_name']);
  100. }
  101. }