PayPlusCashierLogicTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Http\logic\api\PayPlusCashierLogic;
  4. use App\Services\PayPlus;
  5. use Tests\TestCase;
  6. class PayPlusCashierLogicTest extends TestCase
  7. {
  8. /** @test */
  9. public function it_queries_existing_beneficiary_when_create_reports_duplicate()
  10. {
  11. $service = new FakePayPlusForCashier([
  12. ['code' => 4002001, 'message' => 'beneficiary exists'],
  13. ['code' => 200, 'data' => ['beneficiary_id' => 5006]],
  14. [
  15. 'code' => 200,
  16. 'data' => [
  17. 'transaction_status' => 'PROCESSING',
  18. 'transaction_id' => 'T100',
  19. ],
  20. ],
  21. ]);
  22. $logic = new PayPlusCashierLogic($service);
  23. $result = $logic->createBeneficiaryAndPayout([
  24. 'user_id' => 10001,
  25. 'amount' => 10,
  26. 'currency' => 'USD',
  27. 'order_id' => 'TX100',
  28. 'pix_type' => 1,
  29. 'cashapp_account' => 'cashuser',
  30. 'email' => 'user@example.com',
  31. ]);
  32. $this->assertSame('PROCESSING', $result['data']['transaction_status']);
  33. $this->assertSame('/rest/v2/beneficiaries/create', $service->calls[0]['path']);
  34. $this->assertSame('/rest/v2/beneficiaries/query', $service->calls[1]['path']);
  35. $this->assertSame('/rest/v2/payouts/cashApp', $service->calls[2]['path']);
  36. $this->assertSame('$cashuser', $service->calls[2]['payload']['cashapp_account']);
  37. }
  38. /** @test */
  39. public function it_does_not_payout_when_beneficiary_id_cannot_be_resolved()
  40. {
  41. $service = new FakePayPlusForCashier([
  42. ['code' => 4002001, 'message' => 'beneficiary exists'],
  43. ['code' => 200],
  44. ]);
  45. $logic = new PayPlusCashierLogic($service);
  46. $result = $logic->createBeneficiaryAndPayout([
  47. 'user_id' => 10001,
  48. 'amount' => 10,
  49. 'currency' => 'USD',
  50. 'order_id' => 'TX100',
  51. 'pix_type' => 2,
  52. 'email' => 'user@example.com',
  53. ]);
  54. $this->assertFalse($result);
  55. $this->assertCount(2, $service->calls);
  56. }
  57. /** @test */
  58. public function it_maps_payplus_payout_statuses()
  59. {
  60. $logic = new PayPlusCashierLogic(new PayPlus([]));
  61. $this->assertSame(1, $logic->resolvePayoutStatus('PAID'));
  62. $this->assertSame(2, $logic->resolvePayoutStatus('REJECTED'));
  63. $this->assertSame(2, $logic->resolvePayoutStatus('REFUNDED'));
  64. $this->assertSame(0, $logic->resolvePayoutStatus('PROCESSING'));
  65. }
  66. }
  67. class FakePayPlusForCashier extends PayPlus
  68. {
  69. public $calls = [];
  70. private $responses;
  71. public function __construct(array $responses)
  72. {
  73. parent::__construct([
  74. 'appId' => 200000,
  75. 'appKey' => 'test-key',
  76. 'currency' => 'USD',
  77. ]);
  78. $this->responses = $responses;
  79. }
  80. public function postPayout(string $path, array $payload)
  81. {
  82. $this->calls[] = [
  83. 'path' => $path,
  84. 'payload' => $payload,
  85. ];
  86. return array_shift($this->responses);
  87. }
  88. }