PayPlusLogicTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Http\logic\api\PayPlusLogic;
  4. use App\Services\PayPlus;
  5. use Tests\TestCase;
  6. class PayPlusLogicTest extends TestCase
  7. {
  8. /** @test */
  9. public function it_builds_payment_payload_with_configured_method_mapping()
  10. {
  11. $service = new PayPlus([
  12. 'currency' => 'USD',
  13. 'return' => 'https://example.com/return',
  14. 'payment_methods' => [
  15. 1 => 8,
  16. 2 => 2,
  17. ],
  18. ]);
  19. $logic = new PayPlusLogic($service);
  20. $payload = $logic->buildPaymentPayload([
  21. 'order_sn' => 'P100',
  22. 'amount' => 12.34,
  23. 'user_id' => 10001,
  24. 'user_email' => 'user@example.com',
  25. 'user_phone' => '1234567890',
  26. 'user_name' => 'Nico Smith',
  27. 'buy_ip' => '8.8.8.8',
  28. 'pay_method' => 2,
  29. ]);
  30. $this->assertSame('P100', $payload['platform_order_id']);
  31. $this->assertSame('RECHARGE', $payload['order_type']);
  32. $this->assertSame('12.34', $payload['amount']);
  33. $this->assertSame(2, $payload['payment_method']);
  34. $this->assertSame('10001', $payload['account_info']['merchant_user_id']);
  35. $this->assertRegExp('/^[A-Z]user$/', $payload['account_info']['first_name']);
  36. $this->assertRegExp('/^[A-Z]user$/', $payload['account_info']['last_name']);
  37. }
  38. /** @test */
  39. public function it_maps_successful_capture_only_as_paid()
  40. {
  41. $logic = new PayPlusLogic(new PayPlus([]));
  42. $this->assertTrue($logic->isSuccessfulPayment([
  43. 'event' => 'PAYMENT.CAPTURE.COMPLETED',
  44. 'event_detail_name' => 'payment_captured',
  45. 'data' => ['order_status' => 3],
  46. ]));
  47. $this->assertFalse($logic->isSuccessfulPayment([
  48. 'event' => 'PAYMENT.CAPTURE.COMPLETED',
  49. 'event_detail_name' => 'payment_declined',
  50. 'data' => ['order_status' => 4],
  51. ]));
  52. }
  53. /** @test */
  54. public function it_confirms_successful_notify_with_order_query()
  55. {
  56. $service = new FakePayPlusForPayinQuery([
  57. 'decryptedComponentDelta' => [
  58. 'order_status' => 3,
  59. 'event_detail_name' => 'payment_captured',
  60. ],
  61. ]);
  62. $logic = new PayPlusLogic($service);
  63. $this->assertTrue($logic->confirmSuccessfulPayment([
  64. 'event' => 'PAYMENT.CAPTURE.COMPLETED',
  65. 'event_detail_name' => 'payment_captured',
  66. 'data' => [
  67. 'platform_order_id' => 'P100',
  68. 'order_id' => 'O100',
  69. 'order_status' => 3,
  70. ],
  71. ]));
  72. $this->assertSame('P100', $service->queriedPlatformOrderId);
  73. $this->assertSame('O100', $service->queriedOrderId);
  74. }
  75. /** @test */
  76. public function it_rejects_successful_notify_when_order_query_is_not_successful()
  77. {
  78. $service = new FakePayPlusForPayinQuery([
  79. 'decryptedComponentDelta' => [
  80. 'order_status' => 4,
  81. 'event_detail_name' => 'payment_declined',
  82. ],
  83. ]);
  84. $logic = new PayPlusLogic($service);
  85. $this->assertFalse($logic->confirmSuccessfulPayment([
  86. 'event' => 'PAYMENT.CAPTURE.COMPLETED',
  87. 'event_detail_name' => 'payment_captured',
  88. 'data' => [
  89. 'platform_order_id' => 'P100',
  90. 'order_id' => 'O100',
  91. 'order_status' => 3,
  92. ],
  93. ]));
  94. }
  95. }
  96. class FakePayPlusForPayinQuery extends PayPlus
  97. {
  98. public $queriedPlatformOrderId;
  99. public $queriedOrderId;
  100. private $queryResult;
  101. public function __construct(array $queryResult)
  102. {
  103. parent::__construct([]);
  104. $this->queryResult = $queryResult;
  105. }
  106. public function queryPayinOrder($platformOrderId, $orderId = '')
  107. {
  108. $this->queriedPlatformOrderId = $platformOrderId;
  109. $this->queriedOrderId = $orderId;
  110. return $this->queryResult;
  111. }
  112. }