Skip to content

Commit

Permalink
Add tests for PaymentMethodsTokens API
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 6, 2023
1 parent 4426a8e commit 40f1c8d
Show file tree
Hide file tree
Showing 7 changed files with 643 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Traits/PayPalAPI/PaymentMethodsTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait PaymentMethodsTokens
*/
public function createPaymentSourceToken(array $data)
{
$this->apiEndPoint = "v3/vault/payment-tokens";
$this->apiEndPoint = 'v3/vault/payment-tokens';

$this->options['json'] = $data;

Expand Down Expand Up @@ -81,7 +81,7 @@ public function deletePaymentSourceToken(string $token)

$this->verb = 'delete';

return $this->doPayPalRequest();
return $this->doPayPalRequest(false);
}

/**
Expand All @@ -97,7 +97,7 @@ public function deletePaymentSourceToken(string $token)
*/
public function createPaymentSetupToken(array $data)
{
$this->apiEndPoint = "v3/vault/setup-tokens";
$this->apiEndPoint = 'v3/vault/setup-tokens';

$this->options['json'] = $data;

Expand All @@ -122,5 +122,5 @@ public function showPaymentSetupTokenDetails(string $token)
$this->verb = 'get';

return $this->doPayPalRequest();
}
}
}
85 changes: 65 additions & 20 deletions src/Traits/PayPalAPI/PaymentMethodsTokens/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace Srmklive\PayPal\Traits\PayPalAPI\PaymentMethodsTokens;

use Carbon\Carbon;
use Illuminate\Support\Str;
use Throwable;

trait Helpers
{
/**
* @var array
*/
protected $token_source = [];
protected $payment_source = [];

/**
* @var array
Expand All @@ -24,53 +20,102 @@ trait Helpers
* @param string $id
* @param string $type
*
* @return \Srmklive\PayPal\Services\PayPal
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setTokenSource(string $id, string $type)
public function setTokenSource(string $id, string $type): \Srmklive\PayPal\Services\PayPal
{
$this->token_source = [
$token_source = [
'id' => $id,
'type' => $type,
];

return $this;
return $this->setPaymentSourceDetails('token', $token_source);
}

/**
* Set payment method token customer id.
*
* @param string $id
*
* @return \Srmklive\PayPal\Services\PayPal
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setCustomerSource(string $id)
public function setCustomerSource(string $id): \Srmklive\PayPal\Services\PayPal
{
$this->customer_source = [
'id' => $id,
];

return $this;
}
}

/**
* Set payment source data for credit card.
*
* @param array $data
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setPaymentSourceCard(array $data): \Srmklive\PayPal\Services\PayPal
{
return $this->setPaymentSourceDetails('card', $data);
}

/**
* Send request for creating payment method token.
* Set payment source data for PayPal.
*
* @param array $data
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setPaymentSourcePayPal(array $data): \Srmklive\PayPal\Services\PayPal
{
return $this->setPaymentSourceDetails('paypal', $data);
}

/**
* Set payment source data for Venmo.
*
* @param array $data
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setPaymentSourceVenmo(array $data): \Srmklive\PayPal\Services\PayPal
{
return $this->setPaymentSourceDetails('venmo', $data);
}

/**
* Set payment source details.
*
* @param string $source
* @param array $data
*
* @return \Srmklive\PayPal\Services\PayPal
*/
protected function setPaymentSourceDetails(string $source, array $data): \Srmklive\PayPal\Services\PayPal
{
$this->payment_source[$source] = $data;

return $this;
}

/**
* Send request for creating payment method token/source.
*
* @param bool $create_source
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*/
public function sendTokenRequest()
public function sendPaymentMethodRequest(bool $create_source = false)
{
$token_payload = ['payment_source' => null];

if (!empty($this->token_source)) {
$token_payload['payment_source']['token'] = $this->token_source;
}
$token_payload = ['payment_source' => $this->payment_source];

if (!empty($this->customer_source)) {
$token_payload['customer'] = $this->customer_source;
}

return $this->createPaymentSourceToken(array_filter($token_payload));
return ($create_source === true) ? $this->createPaymentSetupToken(array_filter($token_payload)) : $this->createPaymentSourceToken(array_filter($token_payload));
}
}
82 changes: 82 additions & 0 deletions tests/Feature/AdapterFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2195,4 +2195,86 @@ public function it_can_verify_web_hook_signature()
$this->assertNotEmpty($response);
$this->assertArrayHasKey('verification_status', $response);
}

/** @test */
public function it_can_list_payment_methods_source_tokens()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockListPaymentMethodsTokensResponse()
)
);

$response = $this->client->setCustomerSource('customer_4029352050')
->listPaymentSourceTokens();

$this->assertNotEmpty($response);
$this->assertArrayHasKey('payment_tokens', $response);
}

/** @test */
public function it_can_show_details_for_payment_method_source_token()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockCreatePaymentMethodsTokenResponse()
)
);

$response = $this->client->showPaymentSourceTokenDetails('8kk8451t');

$this->assertNotEmpty($response);
$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('customer', $response);
$this->assertArrayHasKey('payment_source', $response);
}

/** @test */
public function it_can_delete_a_payment_method_source_token()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(false)
);

$response = $this->client->deletePaymentSourceToken('8kk8451t');

$this->assertEmpty($response);
}

/** @test */
public function it_can_show_details_for_payment_setup_token()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockListPaymentSetupTokenResponse()
)
);

$response = $this->client->showPaymentSetupTokenDetails('5C991763VB2781612');

$this->assertNotEmpty($response);
$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('customer', $response);
$this->assertArrayHasKey('payment_source', $response);
}
}
96 changes: 95 additions & 1 deletion tests/Feature/AdapterPaymentMethodTokensHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use PHPUnit\Framework\TestCase;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Srmklive\PayPal\Tests\MockClientClasses;
use Srmklive\PayPal\Tests\MockRequestPayloads;
use Srmklive\PayPal\Tests\MockResponsePayloads;

class AdapterPaymentMethodTokensHelpersTest extends TestCase
{
use MockClientClasses;
use MockRequestPayloads;
use MockResponsePayloads;

/** @var string */
Expand Down Expand Up @@ -51,9 +53,101 @@ public function it_can_create_payment_token_from_a_vault_token()
$this->client = $this->client->setTokenSource('5C991763VB2781612', 'SETUP_TOKEN')
->setCustomerSource('customer_4029352050');

$response = $this->client->sendTokenRequest();
$response = $this->client->sendPaymentMethodRequest();

$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('customer', $response);
}

/** @test */
public function it_can_create_payment_source_from_a_vault_token()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockCreatePaymentSetupTokenResponse()
)
);

$this->client = $this->client->setTokenSource('5C991763VB2781612', 'SETUP_TOKEN')
->setCustomerSource('customer_4029352050');

$response = $this->client->sendPaymentMethodRequest(true);

$this->assertArrayHasKey('payment_source', $response);
}

/** @test */
public function it_can_create_payment_source_from_a_credit_card()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockCreatePaymentSetupTokenResponse()
)
);

$this->client = $this->client->setPaymentSourceCard($this->mockCreatePaymentSetupTokensParams()['payment_source']['card'])
->setCustomerSource('customer_4029352050');

$response = $this->client->sendPaymentMethodRequest(true);

$this->assertArrayHasKey('payment_source', $response);
}

/** @test */
public function it_can_create_payment_source_from_a_paypal_account()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$response_data = $this->mockCreatePaymentSetupTokenResponse();
$response_data['payment_source']['paypal'] = $this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'];
unset($response_data['payment_source']['card']);

$this->client->setClient(
$this->mock_http_client($response_data)
);

$this->client = $this->client->setPaymentSourcePayPal($this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'])
->setCustomerSource('customer_4029352050');

$response = $this->client->sendPaymentMethodRequest(true);

$this->assertArrayHasKey('payment_source', $response);
}

/** @test */
public function it_can_create_payment_source_from_a_venmo_account()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$response_data = $this->mockCreatePaymentSetupTokenResponse();
$response_data['payment_source']['venmo'] = $this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'];
unset($response_data['payment_source']['card']);

$this->client->setClient(
$this->mock_http_client($response_data)
);

$this->client = $this->client->setPaymentSourceVenmo($this->mockCreatePaymentSetupPayPalParams()['payment_source']['paypal'])
->setCustomerSource('customer_4029352050');

$response = $this->client->sendPaymentMethodRequest(true);

$this->assertArrayHasKey('payment_source', $response);
}
}
1 change: 1 addition & 0 deletions tests/MockRequestPayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait MockRequestPayloads
use Mocks\Requests\Orders;
use Mocks\Requests\PartnerReferrals;
use Mocks\Requests\PaymentExperienceWebProfiles;
use Mocks\Requests\PaymentMethodsTokens;
use Mocks\Requests\PaymentAuthorizations;
use Mocks\Requests\PaymentCaptures;
use Mocks\Requests\Payouts;
Expand Down
Loading

0 comments on commit 40f1c8d

Please sign in to comment.