Skip to content

Commit

Permalink
Implement PaymentMethod API.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 6, 2023
1 parent cb53e29 commit 2f234c7
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Traits/PayPalAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait PayPalAPI
use PayPalAPI\Orders;
use PayPalAPI\PartnerReferrals;
use PayPalAPI\PaymentExperienceWebProfiles;
use PayPalAPI\PaymentMethodsTokens;
use PayPalAPI\PaymentAuthorizations;
use PayPalAPI\PaymentCaptures;
use PayPalAPI\PaymentRefunds;
Expand Down
126 changes: 126 additions & 0 deletions src/Traits/PayPalAPI/PaymentMethodsTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Srmklive\PayPal\Traits\PayPalAPI;

trait PaymentMethodsTokens
{
use PaymentMethodsTokens\Helpers;

/**
* Create a payment method token.
*
* @param array $data
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#payment-tokens_create
*/
public function createPaymentSourceToken(array $data)
{
$this->apiEndPoint = "v3/vault/payment-tokens";

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

$this->verb = 'post';

return $this->doPayPalRequest();
}

/**
* List all the payment tokens.
*
* @param int $page
* @param int $page_size
* @param bool $totals
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#customer_payment-tokens_get
*/
public function listPaymentSourceTokens(int $page = 1, int $page_size = 10, bool $totals = true)
{
$this->apiEndPoint = "v3/vault/payment-tokens?customer_id={$this->customer_source['id']}&page={$page}&page_size={$page_size}&total_required={$totals}";

$this->verb = 'get';

return $this->doPayPalRequest();
}

/**
* Show details for a payment method token.
*
* @param string $token
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#payment-tokens_get
*/
public function showPaymentSourceTokenDetails(string $token)
{
$this->apiEndPoint = "v3/vault/payment-tokens/{$token}";

$this->verb = 'get';

return $this->doPayPalRequest();
}

/**
* Show details for a payment token.
*
* @param string $token
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#payment-tokens_delete
*/
public function deletePaymentSourceToken(string $token)
{
$this->apiEndPoint = "v3/vault/payment-tokens/{$token}";

$this->verb = 'delete';

return $this->doPayPalRequest();
}

/**
* Create a payment setup token.
*
* @param array $data
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#setup-tokens_create
*/
public function createPaymentSetupToken(array $data)
{
$this->apiEndPoint = "v3/vault/setup-tokens";

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

$this->verb = 'post';

return $this->doPayPalRequest();
}

/**
* Show details for a payment setup token.
*
* @param string $token
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/payment-tokens/v3/#setup-tokens_get
*/
public function showPaymentSetupTokenDetails(string $token)
{
$this->apiEndPoint = "v3/vault/setup-tokens/{$token}";

$this->verb = 'get';

return $this->doPayPalRequest();
}
}
76 changes: 76 additions & 0 deletions src/Traits/PayPalAPI/PaymentMethodsTokens/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Srmklive\PayPal\Traits\PayPalAPI\PaymentMethodsTokens;

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

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

/**
* @var array
*/
protected $customer_source = [];

/**
* Set payment method token by token id.
*
* @param string $id
* @param string $type
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function setTokenSource(string $id, string $type)
{
$this->token_source = [
'id' => $id,
'type' => $type,
];

return $this;
}

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

return $this;
}

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

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

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

return $this->createPaymentSourceToken(array_filter($token_payload));
}
}
59 changes: 59 additions & 0 deletions tests/Feature/AdapterPaymentMethodTokensHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Srmklive\PayPal\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Srmklive\PayPal\Tests\MockClientClasses;
use Srmklive\PayPal\Tests\MockResponsePayloads;

class AdapterPaymentMethodTokensHelpersTest extends TestCase
{
use MockClientClasses;
use MockResponsePayloads;

/** @var string */
protected static $access_token = '';

/** @var \Srmklive\PayPal\Services\PayPal */
protected $client;

protected function setUp(): void
{
$this->client = new PayPalClient($this->getApiCredentials());

$this->client->setClient(
$this->mock_http_client(
$this->mockAccessTokenResponse()
)
);
$response = $this->client->getAccessToken();

self::$access_token = $response['access_token'];

parent::setUp();
}

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

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

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

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

$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('customer', $response);
}
}
1 change: 1 addition & 0 deletions tests/MockResponsePayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait MockResponsePayloads
use Mocks\Responses\Orders;
use Mocks\Responses\PartnerReferrals;
use Mocks\Responses\PaymentExperienceWebProfiles;
use Mocks\Responses\PaymentMethodsTokens;
use Mocks\Responses\PaymentAuthorizations;
use Mocks\Responses\PaymentCaptures;
use Mocks\Responses\PaymentRefunds;
Expand Down
51 changes: 51 additions & 0 deletions tests/Mocks/Responses/PaymentMethodsTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Srmklive\PayPal\Tests\Mocks\Responses;

use GuzzleHttp\Utils;

trait PaymentMethodsTokens
{
/**
* @return array
*/
private function mockCreatePaymentMethodsTokenResponse(): array
{
return Utils::jsonDecode('{
"id": "8kk8451t",
"customer": {
"id": "customer_4029352050"
},
"payment_source": {
"card": {
"brand": "VISA",
"last_digits": "1111",
"expiry": "2027-02",
"name": "John Doe",
"billing_address": {
"address_line_1": "2211 N First Street",
"address_line_2": "17.3.160",
"admin_area_2": "San Jose",
"admin_area_1": "CA",
"postal_code": "95131",
"country_code": "US"
}
}
},
"links": [
{
"rel": "self",
"href": "https://api-m.paypal.com/v3/vault/payment-tokens/8kk8451t",
"method": "GET",
"encType": "application/json"
},
{
"rel": "delete",
"href": "https://api-m.paypal.com/v3/vault/payment-tokens/8kk8451t",
"method": "DELETE",
"encType": "application/json"
}
]
}', true);
}
}

0 comments on commit 2f234c7

Please sign in to comment.