AdapterPaymentMethodTokensHelpersTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Srmklive\PayPal\Tests\Feature;
  3. use PHPUnit\Framework\Attributes\Test;
  4. use PHPUnit\Framework\TestCase;
  5. use Srmklive\PayPal\Services\PayPal as PayPalClient;
  6. use Srmklive\PayPal\Tests\MockClientClasses;
  7. use Srmklive\PayPal\Tests\MockRequestPayloads;
  8. use Srmklive\PayPal\Tests\MockResponsePayloads;
  9. class AdapterPaymentMethodTokensHelpersTest extends TestCase
  10. {
  11. use MockClientClasses;
  12. use MockRequestPayloads;
  13. use MockResponsePayloads;
  14. /** @var string */
  15. protected static string $access_token = '';
  16. /** @var PayPalClient */
  17. protected PayPalClient $client;
  18. protected function setUp(): void
  19. {
  20. $this->client = new PayPalClient($this->getApiCredentials());
  21. $this->client->setClient(
  22. $this->mock_http_client(
  23. $this->mockAccessTokenResponse()
  24. )
  25. );
  26. $response = $this->client->getAccessToken();
  27. self::$access_token = $response['access_token'];
  28. parent::setUp();
  29. }
  30. #[Test]
  31. public function it_can_create_payment_token_from_a_vault_token(): void
  32. {
  33. $this->client->setAccessToken([
  34. 'access_token' => self::$access_token,
  35. 'token_type' => 'Bearer',
  36. ]);
  37. $this->client->setClient(
  38. $this->mock_http_client(
  39. $this->mockCreatePaymentMethodsTokenResponse()
  40. )
  41. );
  42. $this->client = $this->client->setTokenSource('5C991763VB2781612', 'SETUP_TOKEN')
  43. ->setCustomerSource('customer_4029352050');
  44. $response = $this->client->sendPaymentMethodRequest();
  45. $this->assertArrayHasKey('id', $response);
  46. $this->assertArrayHasKey('customer', $response);
  47. }
  48. #[Test]
  49. public function it_can_create_payment_source_from_a_vault_token(): void
  50. {
  51. $this->client->setAccessToken([
  52. 'access_token' => self::$access_token,
  53. 'token_type' => 'Bearer',
  54. ]);
  55. $this->client->setClient(
  56. $this->mock_http_client(
  57. $this->mockCreatePaymentSetupTokenResponse()
  58. )
  59. );
  60. $this->client = $this->client->setTokenSource('5C991763VB2781612', 'SETUP_TOKEN')
  61. ->setCustomerSource('customer_4029352050');
  62. $response = $this->client->sendPaymentMethodRequest(true);
  63. $this->assertArrayHasKey('payment_source', $response);
  64. }
  65. #[Test]
  66. public function it_can_create_payment_source_from_a_credit_card(): void
  67. {
  68. $this->client->setAccessToken([
  69. 'access_token' => self::$access_token,
  70. 'token_type' => 'Bearer',
  71. ]);
  72. $this->client->setClient(
  73. $this->mock_http_client(
  74. $this->mockCreatePaymentSetupTokenResponse()
  75. )
  76. );
  77. $this->client = $this->client->setPaymentSourceCard($this->mockCreatePaymentSetupTokensParams()['payment_source']['card'])
  78. ->setCustomerSource('customer_4029352050');
  79. $response = $this->client->sendPaymentMethodRequest(true);
  80. $this->assertArrayHasKey('payment_source', $response);
  81. }
  82. #[Test]
  83. public function it_can_create_payment_source_from_a_paypal_account(): void
  84. {
  85. $this->client->setAccessToken([
  86. 'access_token' => self::$access_token,
  87. 'token_type' => 'Bearer',
  88. ]);
  89. $response_data = $this->mockCreatePaymentSetupTokenResponse();
  90. $response_data['payment_source']['paypal'] = $this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'];
  91. unset($response_data['payment_source']['card']);
  92. $this->client->setClient(
  93. $this->mock_http_client($response_data)
  94. );
  95. $this->client = $this->client->setPaymentSourcePayPal($this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'])
  96. ->setCustomerSource('customer_4029352050');
  97. $response = $this->client->sendPaymentMethodRequest(true);
  98. $this->assertArrayHasKey('payment_source', $response);
  99. }
  100. #[Test]
  101. public function it_can_create_payment_source_from_a_venmo_account(): void
  102. {
  103. $this->client->setAccessToken([
  104. 'access_token' => self::$access_token,
  105. 'token_type' => 'Bearer',
  106. ]);
  107. $response_data = $this->mockCreatePaymentSetupTokenResponse();
  108. $response_data['payment_source']['venmo'] = $this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'];
  109. unset($response_data['payment_source']['card']);
  110. $this->client->setClient(
  111. $this->mock_http_client($response_data)
  112. );
  113. $this->client = $this->client->setPaymentSourceVenmo($this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'])
  114. ->setCustomerSource('customer_4029352050');
  115. $response = $this->client->sendPaymentMethodRequest(true);
  116. $this->assertArrayHasKey('payment_source', $response);
  117. }
  118. }