-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1779 from woocommerce/PCP-1393-update-to-vault-v-3
Save payment methods (Vault v3) integration (1393)
- Loading branch information
Showing
100 changed files
with
5,282 additions
and
1,259 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
Large diffs are not rendered by default.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
modules/ppcp-api-client/src/Authentication/UserIdToken.php
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,105 @@ | ||
<?php | ||
/** | ||
* Generates user ID token for payer. | ||
* | ||
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication | ||
*/ | ||
|
||
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; | ||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; | ||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; | ||
use WP_Error; | ||
|
||
/** | ||
* Class UserIdToken | ||
*/ | ||
class UserIdToken { | ||
|
||
use RequestTrait; | ||
|
||
/** | ||
* The host. | ||
* | ||
* @var string | ||
*/ | ||
private $host; | ||
|
||
/** | ||
* The bearer. | ||
* | ||
* @var Bearer | ||
*/ | ||
private $bearer; | ||
|
||
/** | ||
* The logger. | ||
* | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* UserIdToken constructor. | ||
* | ||
* @param string $host The host. | ||
* @param Bearer $bearer The bearer. | ||
* @param LoggerInterface $logger The logger. | ||
*/ | ||
public function __construct( | ||
string $host, | ||
Bearer $bearer, | ||
LoggerInterface $logger | ||
) { | ||
$this->host = $host; | ||
$this->bearer = $bearer; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Returns `id_token` which uniquely identifies the payer. | ||
* | ||
* @param string $target_customer_id Vaulted customer id. | ||
* | ||
* @return string | ||
* | ||
* @throws PayPalApiException If the request fails. | ||
* @throws RuntimeException If something unexpected happens. | ||
*/ | ||
public function id_token( string $target_customer_id = '' ): string { | ||
$bearer = $this->bearer->bearer(); | ||
|
||
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; | ||
if ( $target_customer_id ) { | ||
$url = add_query_arg( | ||
array( | ||
'target_customer_id' => $target_customer_id, | ||
), | ||
$url | ||
); | ||
} | ||
|
||
$args = array( | ||
'method' => 'POST', | ||
'headers' => array( | ||
'Authorization' => 'Bearer ' . $bearer->token(), | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
), | ||
); | ||
|
||
$response = $this->request( $url, $args ); | ||
if ( $response instanceof WP_Error ) { | ||
throw new RuntimeException( $response->get_error_message() ); | ||
} | ||
|
||
$json = json_decode( $response['body'] ); | ||
$status_code = (int) wp_remote_retrieve_response_code( $response ); | ||
if ( 200 !== $status_code ) { | ||
throw new PayPalApiException( $json, $status_code ); | ||
} | ||
|
||
return $json->id_token; | ||
} | ||
} |
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
155 changes: 155 additions & 0 deletions
155
modules/ppcp-api-client/src/Endpoint/PaymentMethodTokensEndpoint.php
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,155 @@ | ||
<?php | ||
/** | ||
* The Payment Method Tokens endpoint. | ||
* | ||
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use stdClass; | ||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; | ||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; | ||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; | ||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; | ||
|
||
/** | ||
* Class PaymentMethodTokensEndpoint | ||
*/ | ||
class PaymentMethodTokensEndpoint { | ||
|
||
use RequestTrait; | ||
|
||
/** | ||
* The host. | ||
* | ||
* @var string | ||
*/ | ||
private $host; | ||
|
||
/** | ||
* The bearer. | ||
* | ||
* @var Bearer | ||
*/ | ||
private $bearer; | ||
|
||
/** | ||
* The logger. | ||
* | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* PaymentMethodTokensEndpoint constructor. | ||
* | ||
* @param string $host The host. | ||
* @param Bearer $bearer The bearer. | ||
* @param LoggerInterface $logger The logger. | ||
*/ | ||
public function __construct( string $host, Bearer $bearer, LoggerInterface $logger ) { | ||
$this->host = $host; | ||
$this->bearer = $bearer; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Creates a setup token. | ||
* | ||
* @param PaymentSource $payment_source The payment source. | ||
* | ||
* @return stdClass | ||
* | ||
* @throws RuntimeException When something when wrong with the request. | ||
* @throws PayPalApiException When something when wrong setting up the token. | ||
*/ | ||
public function setup_tokens( PaymentSource $payment_source ): stdClass { | ||
$data = array( | ||
'payment_source' => array( | ||
$payment_source->name() => $payment_source->properties(), | ||
), | ||
); | ||
|
||
$bearer = $this->bearer->bearer(); | ||
$url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens'; | ||
|
||
$args = array( | ||
'method' => 'POST', | ||
'headers' => array( | ||
'Authorization' => 'Bearer ' . $bearer->token(), | ||
'Content-Type' => 'application/json', | ||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ), | ||
), | ||
'body' => wp_json_encode( $data ), | ||
); | ||
|
||
$response = $this->request( $url, $args ); | ||
|
||
if ( is_wp_error( $response ) || ! is_array( $response ) ) { | ||
throw new RuntimeException( 'Not able to create setup token.' ); | ||
} | ||
|
||
$json = json_decode( $response['body'] ); | ||
$status_code = (int) wp_remote_retrieve_response_code( $response ); | ||
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) { | ||
throw new PayPalApiException( | ||
$json, | ||
$status_code | ||
); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
/** | ||
* Creates a payment token for the given payment source. | ||
* | ||
* @param PaymentSource $payment_source The payment source. | ||
* | ||
* @return stdClass | ||
* | ||
* @throws RuntimeException When something when wrong with the request. | ||
* @throws PayPalApiException When something when wrong setting up the token. | ||
*/ | ||
public function payment_tokens( PaymentSource $payment_source ): stdClass { | ||
$data = array( | ||
'payment_source' => array( | ||
$payment_source->name() => $payment_source->properties(), | ||
), | ||
); | ||
|
||
$bearer = $this->bearer->bearer(); | ||
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens'; | ||
|
||
$args = array( | ||
'method' => 'POST', | ||
'headers' => array( | ||
'Authorization' => 'Bearer ' . $bearer->token(), | ||
'Content-Type' => 'application/json', | ||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ), | ||
), | ||
'body' => wp_json_encode( $data ), | ||
); | ||
|
||
$response = $this->request( $url, $args ); | ||
|
||
if ( is_wp_error( $response ) || ! is_array( $response ) ) { | ||
throw new RuntimeException( 'Not able to create setup token.' ); | ||
} | ||
|
||
$json = json_decode( $response['body'] ); | ||
$status_code = (int) wp_remote_retrieve_response_code( $response ); | ||
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) { | ||
throw new PayPalApiException( | ||
$json, | ||
$status_code | ||
); | ||
} | ||
|
||
return $json; | ||
} | ||
} |
Oops, something went wrong.