mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Handle free trial for paypal
Vault account without payment
This commit is contained in:
parent
cdda2963c8
commit
1c0df35f53
18 changed files with 733 additions and 33 deletions
|
@ -35,6 +35,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PayeeFactory;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentsFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentSourceFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenActionLinksFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PlatformFeeFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
|
@ -113,8 +114,10 @@ return array(
|
|||
$container->get( 'api.host' ),
|
||||
$container->get( 'api.bearer' ),
|
||||
$container->get( 'api.factory.payment-token' ),
|
||||
$container->get( 'api.factory.payment-token-action-links' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'api.repository.customer' )
|
||||
$container->get( 'api.repository.customer' ),
|
||||
$container->get( 'api.repository.paypal-request-id' )
|
||||
);
|
||||
},
|
||||
'api.endpoint.webhook' => static function ( ContainerInterface $container ) : WebhookEndpoint {
|
||||
|
@ -240,6 +243,9 @@ return array(
|
|||
'api.factory.payment-token' => static function ( ContainerInterface $container ) : PaymentTokenFactory {
|
||||
return new PaymentTokenFactory();
|
||||
},
|
||||
'api.factory.payment-token-action-links' => static function ( ContainerInterface $container ) : PaymentTokenActionLinksFactory {
|
||||
return new PaymentTokenActionLinksFactory();
|
||||
},
|
||||
'api.factory.webhook' => static function ( ContainerInterface $container ): WebhookFactory {
|
||||
return new WebhookFactory();
|
||||
},
|
||||
|
|
|
@ -11,11 +11,14 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
|||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentTokenActionLinks;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenActionLinksFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
||||
|
||||
/**
|
||||
* Class PaymentTokenEndpoint
|
||||
|
@ -45,6 +48,13 @@ class PaymentTokenEndpoint {
|
|||
*/
|
||||
private $factory;
|
||||
|
||||
/**
|
||||
* The PaymentTokenActionLinks factory.
|
||||
*
|
||||
* @var PaymentTokenActionLinksFactory
|
||||
*/
|
||||
private $payment_token_action_links_factory;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
|
@ -59,28 +69,41 @@ class PaymentTokenEndpoint {
|
|||
*/
|
||||
protected $customer_repository;
|
||||
|
||||
/**
|
||||
* The request id repository.
|
||||
*
|
||||
* @var PayPalRequestIdRepository
|
||||
*/
|
||||
private $request_id_repository;
|
||||
|
||||
/**
|
||||
* PaymentTokenEndpoint constructor.
|
||||
*
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param PaymentTokenFactory $factory The payment token factory.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param CustomerRepository $customer_repository The customer repository.
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param PaymentTokenFactory $factory The payment token factory.
|
||||
* @param PaymentTokenActionLinksFactory $payment_token_action_links_factory The PaymentTokenActionLinks factory.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param CustomerRepository $customer_repository The customer repository.
|
||||
* @param PayPalRequestIdRepository $request_id_repository The request id repository.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
PaymentTokenFactory $factory,
|
||||
PaymentTokenActionLinksFactory $payment_token_action_links_factory,
|
||||
LoggerInterface $logger,
|
||||
CustomerRepository $customer_repository
|
||||
CustomerRepository $customer_repository,
|
||||
PayPalRequestIdRepository $request_id_repository
|
||||
) {
|
||||
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->factory = $factory;
|
||||
$this->logger = $logger;
|
||||
$this->customer_repository = $customer_repository;
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->factory = $factory;
|
||||
$this->payment_token_action_links_factory = $payment_token_action_links_factory;
|
||||
$this->logger = $logger;
|
||||
$this->customer_repository = $customer_repository;
|
||||
$this->request_id_repository = $request_id_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -183,4 +206,120 @@ class PaymentTokenEndpoint {
|
|||
|
||||
return wp_remote_retrieve_response_code( $response ) === 204;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the process of PayPal account vaulting (without payment), returns the links for further actions.
|
||||
*
|
||||
* @param int $user_id The WP user id.
|
||||
* @param string $return_url The URL to which the customer is redirected after finishing the approval.
|
||||
* @param string $cancel_url The URL to which the customer is redirected if cancelled the operation.
|
||||
*
|
||||
* @return PaymentTokenActionLinks
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function start_paypal_token_creation(
|
||||
int $user_id,
|
||||
string $return_url,
|
||||
string $cancel_url
|
||||
): PaymentTokenActionLinks {
|
||||
$bearer = $this->bearer->bearer();
|
||||
|
||||
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens';
|
||||
|
||||
$customer_id = $this->customer_repository->customer_id_for_user( ( $user_id ) );
|
||||
$data = array(
|
||||
'customer_id' => $customer_id,
|
||||
'source' => array(
|
||||
'paypal' => array(
|
||||
'usage_type' => 'MERCHANT',
|
||||
),
|
||||
),
|
||||
'application_context' => array(
|
||||
'return_url' => $return_url,
|
||||
'cancel_url' => $cancel_url,
|
||||
// TODO: can use vault_on_approval to avoid /confirm-payment-token, but currently it's not working.
|
||||
),
|
||||
);
|
||||
|
||||
$request_id = uniqid( 'ppcp-vault', true );
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Request-Id' => $request_id,
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Failed to create payment token.' );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 200 !== $status_code ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
|
||||
$status = $json->status;
|
||||
if ( 'CUSTOMER_ACTION_REQUIRED' !== $status ) {
|
||||
throw new RuntimeException( 'Unexpected payment token creation status. ' . $status );
|
||||
}
|
||||
|
||||
$links = $this->payment_token_action_links_factory->from_paypal_response( $json );
|
||||
|
||||
$this->request_id_repository->set( "ppcp-vault-{$user_id}", $request_id );
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes the process of PayPal account vaulting.
|
||||
*
|
||||
* @param string $approval_token The id of the approval token approved by the customer.
|
||||
* @param int $user_id The WP user id.
|
||||
*
|
||||
* @return string
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function create_from_approval_token( string $approval_token, int $user_id ): string {
|
||||
$bearer = $this->bearer->bearer();
|
||||
|
||||
$url = trailingslashit( $this->host ) . 'v2/vault/approval-tokens/' . $approval_token . '/confirm-payment-token';
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Request-Id' => $this->request_id_repository->get( "ppcp-vault-{$user_id}" ),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Failed to create payment token from approval 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->id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
/**
|
||||
* The links from CUSTOMER_ACTION_REQUIRED v2/vault/payment-tokens response.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class PaymentTokenActionLinks
|
||||
*/
|
||||
class PaymentTokenActionLinks {
|
||||
/**
|
||||
* The URL for customer PayPal hosted contingency flow.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $approve_link;
|
||||
|
||||
/**
|
||||
* The URL for a POST request to save an approved approval token and vault the underlying instrument.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $confirm_link;
|
||||
|
||||
/**
|
||||
* The URL for a GET request to get the state of the approval token.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $status_link;
|
||||
|
||||
/**
|
||||
* PaymentTokenActionLinks constructor.
|
||||
*
|
||||
* @param string $approve_link The URL for customer PayPal hosted contingency flow.
|
||||
* @param string $confirm_link The URL for a POST request to save an approved approval token and vault the underlying instrument.
|
||||
* @param string $status_link The URL for a GET request to get the state of the approval token.
|
||||
*/
|
||||
public function __construct( string $approve_link, string $confirm_link, string $status_link ) {
|
||||
$this->approve_link = $approve_link;
|
||||
$this->confirm_link = $confirm_link;
|
||||
$this->status_link = $status_link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for customer PayPal hosted contingency flow.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function approve_link(): string {
|
||||
return $this->approve_link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for a POST request to save an approved approval token and vault the underlying instrument.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function confirm_link(): string {
|
||||
return $this->confirm_link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL for a GET request to get the state of the approval token.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function status_link(): string {
|
||||
return $this->status_link;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* The factory for links from CUSTOMER_ACTION_REQUIRED v2/vault/payment-tokens response.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentTokenActionLinks;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class PaymentTokenActionLinksFactory
|
||||
*/
|
||||
class PaymentTokenActionLinksFactory {
|
||||
|
||||
/**
|
||||
* Returns a PaymentTokenActionLinks object based off a PayPal response.
|
||||
*
|
||||
* @param stdClass $data The JSON object.
|
||||
*
|
||||
* @return PaymentTokenActionLinks
|
||||
* @throws RuntimeException When JSON object is malformed.
|
||||
*/
|
||||
public function from_paypal_response( stdClass $data ): PaymentTokenActionLinks {
|
||||
if ( ! isset( $data->links ) ) {
|
||||
throw new RuntimeException( 'Links not found.' );
|
||||
}
|
||||
|
||||
$links_map = array();
|
||||
foreach ( $data->links as $link ) {
|
||||
if ( ! isset( $link->rel ) || ! isset( $link->href ) ) {
|
||||
throw new RuntimeException( 'Invalid link data.' );
|
||||
}
|
||||
|
||||
$links_map[ $link->rel ] = $link->href;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'approve', $links_map ) ) {
|
||||
throw new RuntimeException( 'Payment token approve link not found.' );
|
||||
}
|
||||
|
||||
return new PaymentTokenActionLinks(
|
||||
$links_map['approve'],
|
||||
$links_map['confirm'] ?? '',
|
||||
$links_map['status'] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
|
@ -26,8 +26,7 @@ class PayPalRequestIdRepository {
|
|||
* @return string
|
||||
*/
|
||||
public function get_for_order_id( string $order_id ): string {
|
||||
$all = $this->all();
|
||||
return isset( $all[ $order_id ] ) ? (string) $all[ $order_id ]['id'] : '';
|
||||
return $this->get( $order_id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,14 +49,36 @@ class PayPalRequestIdRepository {
|
|||
* @return bool
|
||||
*/
|
||||
public function set_for_order( Order $order, string $request_id ): bool {
|
||||
$all = $this->all();
|
||||
$all[ $order->id() ] = array(
|
||||
$this->set( $order->id(), $request_id );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a request ID for the given key.
|
||||
*
|
||||
* @param string $key The key in the request ID storage.
|
||||
* @param string $request_id The ID.
|
||||
*/
|
||||
public function set( string $key, string $request_id ): void {
|
||||
$all = $this->all();
|
||||
$all[ $key ] = array(
|
||||
'id' => $request_id,
|
||||
'expiration' => time() + 10 * DAY_IN_SECONDS,
|
||||
);
|
||||
$all = $this->cleanup( $all );
|
||||
$all = $this->cleanup( $all );
|
||||
update_option( self::KEY, $all );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a request ID.
|
||||
*
|
||||
* @param string $key The key in the request ID storage.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get( string $key ): string {
|
||||
$all = $this->all();
|
||||
return isset( $all[ $key ] ) ? (string) $all[ $key ]['id'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue