diff --git a/src/Traits/PayPalAPI.php b/src/Traits/PayPalAPI.php index baa6bd88..5c2ee8cc 100644 --- a/src/Traits/PayPalAPI.php +++ b/src/Traits/PayPalAPI.php @@ -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; diff --git a/src/Traits/PayPalAPI/PaymentMethodsTokens.php b/src/Traits/PayPalAPI/PaymentMethodsTokens.php new file mode 100644 index 00000000..4d46f13a --- /dev/null +++ b/src/Traits/PayPalAPI/PaymentMethodsTokens.php @@ -0,0 +1,126 @@ +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(); + } +} diff --git a/src/Traits/PayPalAPI/PaymentMethodsTokens/Helpers.php b/src/Traits/PayPalAPI/PaymentMethodsTokens/Helpers.php new file mode 100644 index 00000000..1bc3922c --- /dev/null +++ b/src/Traits/PayPalAPI/PaymentMethodsTokens/Helpers.php @@ -0,0 +1,76 @@ +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)); + } +} diff --git a/tests/Feature/AdapterPaymentMethodTokensHelpersTest.php b/tests/Feature/AdapterPaymentMethodTokensHelpersTest.php new file mode 100644 index 00000000..15740dd0 --- /dev/null +++ b/tests/Feature/AdapterPaymentMethodTokensHelpersTest.php @@ -0,0 +1,59 @@ +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); + } +} diff --git a/tests/MockResponsePayloads.php b/tests/MockResponsePayloads.php index 6ca7ff71..514de20d 100644 --- a/tests/MockResponsePayloads.php +++ b/tests/MockResponsePayloads.php @@ -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; diff --git a/tests/Mocks/Responses/PaymentMethodsTokens.php b/tests/Mocks/Responses/PaymentMethodsTokens.php new file mode 100644 index 00000000..baf5b04f --- /dev/null +++ b/tests/Mocks/Responses/PaymentMethodsTokens.php @@ -0,0 +1,51 @@ +