-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |