PayPlusServiceTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_encrypts_and_decrypts_component_delta_with_aes_gcm()
  31. {
  32. $service = new PayPlus([]);
  33. $aesKey = str_repeat('01', 32);
  34. $iv = str_repeat('02', 12);
  35. $payload = '{"hello":"payplus"}';
  36. $encrypted = $service->encryptComponentDelta($payload, $aesKey, $iv);
  37. $decrypted = $service->decryptComponentDelta($encrypted, $aesKey, $iv);
  38. $this->assertSame($payload, $decrypted);
  39. }
  40. /** @test */
  41. public function it_builds_beneficiary_payload_with_safe_defaults()
  42. {
  43. $service = new PayPlus([]);
  44. $payload = $service->buildBeneficiaryPayload([
  45. 'user_id' => 10001,
  46. 'email' => '',
  47. 'phone' => '',
  48. 'first_name' => '',
  49. 'last_name' => '',
  50. ], 1234567890);
  51. $this->assertSame('10001', $payload['payee_id']);
  52. $this->assertSame('en', $payload['language']);
  53. $this->assertSame('US', $payload['country']);
  54. $this->assertSame('+1-0000000000', $payload['phone']);
  55. $this->assertSame('unknown10001@example.com', $payload['email']);
  56. $this->assertSame('unknown', $payload['first_name']);
  57. $this->assertSame('user', $payload['last_name']);
  58. $this->assertSame(1234567890, $payload['timestamp']);
  59. }
  60. /** @test */
  61. public function it_keeps_only_english_letters_for_beneficiary_names()
  62. {
  63. $service = new PayPlus([]);
  64. $payload = $service->buildBeneficiaryPayload([
  65. 'user_id' => 10002,
  66. 'first_name' => '张San-123',
  67. 'last_name' => '李User_456',
  68. ], 1234567890);
  69. $this->assertSame('San', $payload['first_name']);
  70. $this->assertSame('User', $payload['last_name']);
  71. $payload = $service->buildBeneficiaryPayload([
  72. 'user_id' => 10003,
  73. 'first_name' => '张三123',
  74. 'last_name' => '456-',
  75. ], 1234567890);
  76. $this->assertSame('unknown', $payload['first_name']);
  77. $this->assertSame('user', $payload['last_name']);
  78. }
  79. }