Merge pull request #581 from woocommerce/pcp-475-free-trial

Free trial
This commit is contained in:
Emili Castells 2022-04-13 10:28:26 +02:00 committed by GitHub
commit 97f7f644bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 1360 additions and 138 deletions

View file

@ -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;
@ -48,6 +49,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
@ -112,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 {
@ -228,12 +232,20 @@ return array(
$prefix = $container->get( 'api.prefix' );
return new CustomerRepository( $prefix );
},
'api.repository.order' => static function( ContainerInterface $container ): OrderRepository {
return new OrderRepository(
$container->get( 'api.endpoint.order' )
);
},
'api.factory.application-context' => static function ( ContainerInterface $container ) : ApplicationContextFactory {
return new ApplicationContextFactory();
},
'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();
},

View file

@ -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;
}
}

View file

@ -0,0 +1,76 @@
<?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;
/**
* 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;
}
}

View file

@ -157,6 +157,15 @@ class PurchaseUnit {
return $this->amount;
}
/**
* Sets the amount.
*
* @param Amount $amount The value to set.
*/
public function set_amount( Amount $amount ): void {
$this->amount = $amount;
}
/**
* Returns the shipping.
*

View file

@ -14,12 +14,15 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AmountBreakdown;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
/**
* Class AmountFactory
*/
class AmountFactory {
use FreeTrialHandlerTrait;
/**
* The item factory.
@ -117,9 +120,15 @@ class AmountFactory {
* @return Amount
*/
public function from_wc_order( \WC_Order $order ): Amount {
$currency = $order->get_currency();
$items = $this->item_factory->from_wc_order( $order );
$total = new Money( (float) $order->get_total(), $currency );
$currency = $order->get_currency();
$items = $this->item_factory->from_wc_order( $order );
$total_value = (float) $order->get_total();
if ( CreditCardGateway::ID === $order->get_payment_method() && $this->is_free_trial_order( $order ) ) {
$total_value = 1.0;
}
$total = new Money( $total_value, $currency );
$item_total = new Money(
(float) array_reduce(
$items,

View file

@ -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'] ?? ''
);
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* PayPal order repository.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Repository
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class OrderRepository
*/
class OrderRepository {
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* OrderRepository constructor.
*
* @param OrderEndpoint $order_endpoint The order endpoint.
*/
public function __construct( OrderEndpoint $order_endpoint ) {
$this->order_endpoint = $order_endpoint;
}
/**
* Gets a PayPal order for the given WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @return Order The PayPal order.
* @throws RuntimeException When there is a problem getting the PayPal order.
*/
public function for_wc_order( WC_Order $wc_order ): Order {
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
return $this->order_endpoint->order( $paypal_order_id );
}
}

View file

@ -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,16 +49,39 @@ class PayPalRequestIdRepository {
* @return bool
*/
public function set_for_order( Order $order, string $request_id ): bool {
$all = $this->all();
$all[ $order->id() ] = array(
'id' => $request_id,
'expiration' => time() + 10 * DAY_IN_SECONDS,
);
$all = $this->cleanup( $all );
update_option( self::KEY, $all );
$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();
$day_in_seconds = 86400;
$all[ $key ] = array(
'id' => $request_id,
'expiration' => time() + 10 * $day_in_seconds,
);
$all = $this->cleanup( $all );
update_option( self::KEY, $all );
}
/**
* 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'] : '';
}
/**
* Return all IDs.
*

View file

@ -16,6 +16,7 @@ import {
} from "./modules/Helper/CheckoutMethodState";
import {hide, setVisible} from "./modules/Helper/Hiding";
import {isChangePaymentPage} from "./modules/Helper/Subscriptions";
import FreeTrialHandler from "./modules/ActionHandler/FreeTrialHandler";
const buttonsSpinner = new Spinner('.ppc-button-wrapper');
@ -23,8 +24,17 @@ const bootstrap = () => {
const errorHandler = new ErrorHandler(PayPalCommerceGateway.labels.error.generic);
const spinner = new Spinner();
const creditCardRenderer = new CreditCardRenderer(PayPalCommerceGateway, errorHandler, spinner);
const onSmartButtonClick = data => {
const freeTrialHandler = new FreeTrialHandler(PayPalCommerceGateway, spinner, errorHandler);
const onSmartButtonClick = (data, actions) => {
window.ppcpFundingSource = data.fundingSource;
const isFreeTrial = PayPalCommerceGateway.is_free_trial_cart;
if (isFreeTrial) {
freeTrialHandler.handle();
return actions.reject();
}
};
const onSmartButtonsInit = () => {
buttonsSpinner.unblock();
@ -112,6 +122,7 @@ document.addEventListener(
if (
!['checkout', 'pay-now'].includes(PayPalCommerceGateway.context)
|| isChangePaymentPage()
|| (PayPalCommerceGateway.is_free_trial_cart && PayPalCommerceGateway.vaulted_paypal_email !== '')
) {
return;
}

View file

@ -1,5 +1,6 @@
import onApprove from '../OnApproveHandler/onApproveForContinue.js';
import {payerData} from "../Helper/PayerData";
import {PaymentMethods} from "../Helper/CheckoutMethodState";
class CartActionHandler {
@ -18,6 +19,7 @@ class CartActionHandler {
body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce,
purchase_units: [],
payment_method: PaymentMethods.PAYPAL,
bn_code:bnCode,
payer,
context:this.config.context

View file

@ -1,5 +1,6 @@
import onApprove from '../OnApproveHandler/onApproveForPayNow.js';
import {payerData} from "../Helper/PayerData";
import {getCurrentPaymentMethod} from "../Helper/CheckoutMethodState";
class CheckoutActionHandler {
@ -31,6 +32,7 @@ class CheckoutActionHandler {
bn_code:bnCode,
context:this.config.context,
order_id:this.config.order_id,
payment_method: getCurrentPaymentMethod(),
form:formValues,
createaccount: createaccount
})

View file

@ -0,0 +1,43 @@
import {PaymentMethods} from "../Helper/CheckoutMethodState";
import errorHandler from "../ErrorHandler";
class FreeTrialHandler {
constructor(
config,
spinner,
errorHandler
) {
this.config = config;
this.spinner = spinner;
this.errorHandler = errorHandler;
}
handle()
{
this.spinner.block();
fetch(this.config.ajax.vault_paypal.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: this.config.ajax.vault_paypal.nonce,
return_url: location.href
}),
}).then(res => {
return res.json();
}).then(data => {
if (!data.success) {
this.spinner.unblock();
console.error(data);
this.errorHandler.message(data.data.message);
throw Error(data.data.message);
}
location.href = data.data.approve_link;
}).catch(error => {
this.spinner.unblock();
console.error(error);
this.errorHandler.genericError();
});
}
}
export default FreeTrialHandler;

View file

@ -2,6 +2,7 @@ import ButtonsToggleListener from '../Helper/ButtonsToggleListener';
import Product from '../Entity/Product';
import onApprove from '../OnApproveHandler/onApproveForContinue';
import {payerData} from "../Helper/PayerData";
import {PaymentMethods} from "../Helper/CheckoutMethodState";
class SingleProductActionHandler {
@ -84,6 +85,7 @@ class SingleProductActionHandler {
purchase_units,
payer,
bn_code:bnCode,
payment_method: PaymentMethods.PAYPAL,
context:this.config.context
})
}).then(function (res) {

View file

@ -86,13 +86,16 @@ class CheckoutBootstap {
const isCard = currentPaymentMethod === PaymentMethods.CARDS;
const isSavedCard = isCard && isSavedCardSelected();
const isNotOurGateway = !isPaypal && !isCard;
const isFreeTrial = PayPalCommerceGateway.is_free_trial_cart;
const hasVaultedPaypal = PayPalCommerceGateway.vaulted_paypal_email !== '';
setVisible(this.standardOrderButtonSelector, isNotOurGateway || isSavedCard, true);
setVisible(this.gateway.button.wrapper, isPaypal);
setVisible(this.gateway.messages.wrapper, isPaypal);
setVisible(this.standardOrderButtonSelector, (isPaypal && isFreeTrial && hasVaultedPaypal) || isNotOurGateway || isSavedCard, true);
setVisible('.ppcp-vaulted-paypal-details', isPaypal);
setVisible(this.gateway.button.wrapper, isPaypal && !(isFreeTrial && hasVaultedPaypal));
setVisible(this.gateway.messages.wrapper, isPaypal && !isFreeTrial);
setVisible(this.gateway.hosted_fields.wrapper, isCard && !isSavedCard);
if (isPaypal) {
if (isPaypal && !isFreeTrial) {
this.messages.render();
}

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
@ -85,7 +86,9 @@ return array(
$environment,
$payment_token_repository,
$settings_status,
$currency
$currency,
$container->get( 'wcgateway.all-funding-sources' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'button.url' => static function ( ContainerInterface $container ): string {
@ -169,6 +172,13 @@ return array(
$logger
);
},
'button.endpoint.vault-paypal' => static function( ContainerInterface $container ) : StartPayPalVaultingEndpoint {
return new StartPayPalVaultingEndpoint(
$container->get( 'button.request-data' ),
$container->get( 'api.endpoint.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'button.helper.three-d-secure' => static function ( ContainerInterface $container ): ThreeDSecure {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new ThreeDSecure( $logger );

View file

@ -9,6 +9,9 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Assets;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
@ -16,9 +19,11 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
@ -30,6 +35,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/
class SmartButton implements SmartButtonInterface {
use FreeTrialHandlerTrait;
/**
* The Settings status helper.
*
@ -128,6 +135,27 @@ class SmartButton implements SmartButtonInterface {
*/
private $currency;
/**
* All existing funding sources.
*
* @var array
*/
private $all_funding_sources;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* Cached payment tokens.
*
* @var PaymentToken[]|null
*/
private $payment_tokens = null;
/**
* SmartButton constructor.
*
@ -145,6 +173,8 @@ class SmartButton implements SmartButtonInterface {
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param SettingsStatus $settings_status The Settings status helper.
* @param string $currency 3-letter currency code of the shop.
* @param array $all_funding_sources All existing funding sources.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
string $module_url,
@ -160,7 +190,9 @@ class SmartButton implements SmartButtonInterface {
Environment $environment,
PaymentTokenRepository $payment_token_repository,
SettingsStatus $settings_status,
string $currency
string $currency,
array $all_funding_sources,
LoggerInterface $logger
) {
$this->module_url = $module_url;
@ -177,6 +209,8 @@ class SmartButton implements SmartButtonInterface {
$this->payment_token_repository = $payment_token_repository;
$this->settings_status = $settings_status;
$this->currency = $currency;
$this->all_funding_sources = $all_funding_sources;
$this->logger = $logger;
}
/**
@ -262,6 +296,38 @@ class SmartButton implements SmartButtonInterface {
2
);
}
if ( $this->is_free_trial_cart() ) {
add_action(
'woocommerce_review_order_after_submit',
function () {
$vaulted_email = $this->get_vaulted_paypal_email();
if ( ! $vaulted_email ) {
return;
}
?>
<div class="ppcp-vaulted-paypal-details">
<?php
echo wp_kses_post(
sprintf(
// translators: %1$s - email, %2$s, %3$s - HTML tags for a link.
esc_html__(
'Using %2$s%1$s%3$s PayPal.',
'woocommerce-paypal-payments'
),
$vaulted_email,
'<b>',
'</b>'
)
);
?>
</div>
<?php
}
);
}
return true;
}
@ -341,6 +407,9 @@ class SmartButton implements SmartButtonInterface {
if (
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
&& ! $not_enabled_on_product_page
// TODO: it seems like there is no easy way to properly handle vaulted PayPal free trial,
// so disable the buttons for now everywhere except checkout for free trial.
&& ! $this->is_free_trial_product()
) {
add_action(
$this->single_product_renderer_hook(),
@ -358,11 +427,12 @@ class SmartButton implements SmartButtonInterface {
! $this->settings->get( 'button_mini_cart_enabled' );
if (
! $not_enabled_on_minicart
&& ! $this->is_free_trial_cart()
) {
add_action(
$this->mini_cart_button_renderer_hook(),
function () {
if ( $this->is_cart_price_total_zero() ) {
if ( $this->is_cart_price_total_zero() || $this->is_free_trial_cart() ) {
return;
}
@ -375,7 +445,7 @@ class SmartButton implements SmartButtonInterface {
);
}
if ( $this->is_cart_price_total_zero() ) {
if ( $this->is_cart_price_total_zero() && ! $this->is_free_trial_cart() ) {
return false;
}
@ -384,6 +454,7 @@ class SmartButton implements SmartButtonInterface {
if (
is_cart()
&& ! $not_enabled_on_cart
&& ! $this->is_free_trial_cart()
) {
add_action(
$this->proceed_to_checkout_button_renderer_hook(),
@ -671,6 +742,8 @@ class SmartButton implements SmartButtonInterface {
private function localize_script(): array {
global $wp;
$is_free_trial_cart = $this->is_free_trial_cart();
$this->request_data->enqueue_nonce_fix();
$localize = array(
'script_attributes' => $this->attributes(),
@ -696,9 +769,15 @@ class SmartButton implements SmartButtonInterface {
'endpoint' => \WC_AJAX::get_endpoint( ApproveOrderEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( ApproveOrderEndpoint::nonce() ),
),
'vault_paypal' => array(
'endpoint' => \WC_AJAX::get_endpoint( StartPayPalVaultingEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( StartPayPalVaultingEndpoint::nonce() ),
),
),
'enforce_vault' => $this->has_subscriptions(),
'can_save_vault_token' => $this->can_save_vault_token(),
'is_free_trial_cart' => $is_free_trial_cart,
'vaulted_paypal_email' => ( is_checkout() && $is_free_trial_cart ) ? $this->get_vaulted_paypal_email() : '',
'bn_codes' => $this->bn_codes(),
'payer' => $this->payerData(),
'button' => array(
@ -824,6 +903,10 @@ class SmartButton implements SmartButtonInterface {
}
}
if ( $this->is_free_trial_cart() ) {
$disable_funding = array_keys( $this->all_funding_sources );
}
if ( count( $disable_funding ) > 0 ) {
$params['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
}
@ -832,6 +915,11 @@ class SmartButton implements SmartButtonInterface {
if ( $this->settings_status->pay_later_messaging_is_enabled() || ! in_array( 'credit', $disable_funding, true ) ) {
$enable_funding[] = 'paylater';
}
if ( $this->is_free_trial_cart() ) {
$enable_funding = array();
}
if ( count( $enable_funding ) > 0 ) {
$params['enable-funding'] = implode( ',', array_unique( $enable_funding ) );
}
@ -890,7 +978,10 @@ class SmartButton implements SmartButtonInterface {
if ( $this->load_button_component() ) {
$components[] = 'buttons';
}
if ( $this->messages_apply->for_country() ) {
if (
$this->messages_apply->for_country()
&& ! $this->is_free_trial_cart()
) {
$components[] = 'messages';
}
if ( $this->dcc_is_enabled() ) {
@ -1126,4 +1217,37 @@ class SmartButton implements SmartButtonInterface {
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
return WC()->cart->get_cart_contents_total() == 0;
}
/**
* Retrieves all payment tokens for the user, via API or cached if already queried.
*
* @return PaymentToken[]
*/
private function get_payment_tokens(): array {
if ( null === $this->payment_tokens ) {
$this->payment_tokens = $this->payment_token_repository->all_for_user_id( get_current_user_id() );
}
return $this->payment_tokens;
}
/**
* Returns the vaulted PayPal email or empty string.
*
* @return string
*/
private function get_vaulted_paypal_email(): string {
try {
$tokens = $this->get_payment_tokens();
foreach ( $tokens as $token ) {
if ( isset( $token->source()->paypal ) ) {
return $token->source()->paypal->payer->email_address;
}
}
} catch ( Exception $exception ) {
$this->logger->error( 'Failed to get PayPal vaulted email. ' . $exception->getMessage() );
}
return '';
}
}

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
@ -107,6 +108,15 @@ class ButtonModule implements ModuleInterface {
$endpoint->handle_request();
}
);
add_action(
'wc_ajax_' . StartPayPalVaultingEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'button.endpoint.vault-paypal' );
assert( $endpoint instanceof StartPayPalVaultingEndpoint );
$endpoint->handle_request();
}
);
add_action(
'wc_ajax_' . ChangeCartEndpoint::ENDPOINT,

View file

@ -12,10 +12,10 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Address;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerName;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -25,7 +25,9 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
@ -33,6 +35,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/
class CreateOrderEndpoint implements EndpointInterface {
use FreeTrialHandlerTrait;
const ENDPOINT = 'ppc-create-order';
/**
@ -177,6 +181,7 @@ class CreateOrderEndpoint implements EndpointInterface {
try {
$data = $this->request_data->read_request( $this->nonce() );
$this->parsed_request_data = $data;
$payment_method = $data['payment_method'] ?? '';
$wc_order = null;
if ( 'pay-now' === $data['context'] ) {
$wc_order = wc_get_order( (int) $data['order_id'] );
@ -193,6 +198,16 @@ class CreateOrderEndpoint implements EndpointInterface {
$this->purchase_units = array( $this->purchase_unit_factory->from_wc_order( $wc_order ) );
} else {
$this->purchase_units = $this->cart_repository->all();
// The cart does not have any info about payment method, so we must handle free trial here.
if ( CreditCardGateway::ID === $payment_method && $this->is_free_trial_cart() ) {
$this->purchase_units[0]->set_amount(
new Amount(
new Money( 1.0, $this->purchase_units[0]->amount()->currency_code() ),
$this->purchase_units[0]->amount()->breakdown()
)
);
}
}
$this->set_bn_code( $data );

View file

@ -0,0 +1,111 @@
<?php
/**
* The endpoint for starting vaulting of PayPal account (for free trial).
*
* @package WooCommerce\PayPalCommerce\Button\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
/**
* Class StartPayPalVaultingEndpoint.
*/
class StartPayPalVaultingEndpoint implements EndpointInterface {
const ENDPOINT = 'ppc-vault-paypal';
/**
* The Request Data Helper.
*
* @var RequestData
*/
private $request_data;
/**
* The PaymentTokenEndpoint.
*
* @var PaymentTokenEndpoint
*/
private $payment_token_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* StartPayPalVaultingEndpoint constructor.
*
* @param RequestData $request_data The Request Data Helper.
* @param PaymentTokenEndpoint $payment_token_endpoint The PaymentTokenEndpoint.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
RequestData $request_data,
PaymentTokenEndpoint $payment_token_endpoint,
LoggerInterface $logger
) {
$this->request_data = $request_data;
$this->payment_token_endpoint = $payment_token_endpoint;
$this->logger = $logger;
}
/**
* Returns the nonce.
*
* @return string
*/
public static function nonce(): string {
return self::ENDPOINT;
}
/**
* Handles the request.
*
* @return bool
*/
public function handle_request(): bool {
try {
$data = $this->request_data->read_request( $this->nonce() );
$user_id = get_current_user_id();
$return_url = $data['return_url'];
$cancel_url = add_query_arg( array( 'ppcp_vault' => 'cancel' ), $return_url );
$links = $this->payment_token_endpoint->start_paypal_token_creation(
$user_id,
$return_url,
$cancel_url
);
wp_send_json_success(
array(
'approve_link' => $links->approve_link(),
)
);
return true;
} catch ( Exception $error ) {
$this->logger->error( 'Failed to start PayPal vaulting: ' . $error->getMessage() );
wp_send_json_error(
array(
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
'message' => $error->getMessage(),
)
);
return false;
}
}
}

View file

@ -0,0 +1,94 @@
<?php
/**
* Helper trait for the subscriptions handling.
*
* @package WooCommerce\PayPalCommerce\Subscription
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Subscription;
use WC_Order;
use WC_Product;
use WC_Subscription;
use WC_Subscriptions_Product;
/**
* Class FreeTrialHandlerTrait
*/
trait FreeTrialHandlerTrait {
use SubscriptionsHandlerTrait;
/**
* Checks if the cart contains only free trial.
*
* @return bool
*/
protected function is_free_trial_cart(): bool {
if ( ! $this->is_wcs_plugin_active() ) {
return false;
}
$cart = WC()->cart;
if ( ! $cart || $cart->is_empty() || (float) $cart->get_total( 'numeric' ) > 0 ) {
return false;
}
foreach ( $cart->get_cart() as $item ) {
$product = $item['data'] ?? null;
if ( ! $product instanceof WC_Product ) {
continue;
}
if ( WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) {
return true;
}
}
return false;
}
/**
* Checks if the current product contains free trial.
*
* @return bool
*/
protected function is_free_trial_product(): bool {
if ( ! $this->is_wcs_plugin_active() ) {
return false;
}
$product = wc_get_product();
return $product
&& WC_Subscriptions_Product::is_subscription( $product )
&& WC_Subscriptions_Product::get_trial_length( $product ) > 0;
}
/**
* Checks if the given order contains only free trial.
*
* @param WC_Order $wc_order The WooCommerce order.
* @return bool
*/
protected function is_free_trial_order( WC_Order $wc_order ): bool {
if ( ! $this->is_wcs_plugin_active() ) {
return false;
}
if ( (float) $wc_order->get_total( 'numeric' ) > 0 ) {
return false;
}
$subs = wcs_get_subscriptions_for_order( $wc_order );
return ! empty(
array_filter(
$subs,
function ( WC_Subscription $sub ): bool {
return (float) $sub->get_total_initial_payment() <= 0;
}
)
);
}
}

View file

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Subscription\Helper;
use WC_Product;
use WC_Subscriptions_Product;
/**
@ -46,7 +47,7 @@ class SubscriptionHelper {
}
foreach ( $cart->get_cart() as $item ) {
if ( ! isset( $item['data'] ) || ! is_a( $item['data'], \WC_Product::class ) ) {
if ( ! isset( $item['data'] ) || ! is_a( $item['data'], WC_Product::class ) ) {
continue;
}
if ( $item['data']->is_type( 'subscription' ) || $item['data']->is_type( 'subscription_variation' ) ) {

View file

@ -0,0 +1,26 @@
<?php
/**
* Helper trait for the free trial subscriptions handling.
*
* @package WooCommerce\PayPalCommerce\Subscription
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Subscription;
use WC_Subscriptions;
/**
* Class SubscriptionsHandlerTrait
*/
trait SubscriptionsHandlerTrait {
/**
* Whether the subscription plugin is active or not.
*
* @return bool
*/
protected function is_wcs_plugin_active(): bool {
return class_exists( WC_Subscriptions::class );
}
}

View file

@ -14,41 +14,47 @@ use WooCommerce\PayPalCommerce\Vaulting\Assets\MyAccountPaymentsAssets;
use WooCommerce\PayPalCommerce\Vaulting\Endpoint\DeletePaymentTokenEndpoint;
return array(
'vaulting.module-url' => static function ( ContainerInterface $container ): string {
'vaulting.module-url' => static function ( ContainerInterface $container ): string {
return plugins_url(
'/modules/ppcp-vaulting/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'vaulting.assets.myaccount-payments' => function( ContainerInterface $container ) : MyAccountPaymentsAssets {
'vaulting.assets.myaccount-payments' => function( ContainerInterface $container ) : MyAccountPaymentsAssets {
return new MyAccountPaymentsAssets(
$container->get( 'vaulting.module-url' ),
$container->get( 'ppcp.asset-version' )
);
},
'vaulting.payment-tokens-renderer' => static function (): PaymentTokensRenderer {
'vaulting.payment-tokens-renderer' => static function (): PaymentTokensRenderer {
return new PaymentTokensRenderer();
},
'vaulting.repository.payment-token' => static function ( ContainerInterface $container ): PaymentTokenRepository {
'vaulting.repository.payment-token' => static function ( ContainerInterface $container ): PaymentTokenRepository {
$factory = $container->get( 'api.factory.payment-token' );
$endpoint = $container->get( 'api.endpoint.payment-token' );
return new PaymentTokenRepository( $factory, $endpoint );
},
'vaulting.endpoint.delete' => function( ContainerInterface $container ) : DeletePaymentTokenEndpoint {
'vaulting.endpoint.delete' => function( ContainerInterface $container ) : DeletePaymentTokenEndpoint {
return new DeletePaymentTokenEndpoint(
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'button.request-data' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'vaulting.payment-token-checker' => function( ContainerInterface $container ) : PaymentTokenChecker {
'vaulting.payment-token-checker' => function( ContainerInterface $container ) : PaymentTokenChecker {
return new PaymentTokenChecker(
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'api.repository.order' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.processor.authorized-payments' ),
$container->get( 'api.endpoint.order' ),
$container->get( 'api.endpoint.payments' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'vaulting.customer-approval-listener' => function( ContainerInterface $container ) : CustomerApprovalListener {
return new CustomerApprovalListener(
$container->get( 'api.endpoint.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
);

View file

@ -0,0 +1,78 @@
<?php
/**
* Confirm approval token after the PayPal vaulting approval by customer (v2/vault/payment-tokens with CUSTOMER_ACTION_REQUIRED response).
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
/**
* Class CustomerApprovalListener
*/
class CustomerApprovalListener {
use FreeTrialHandlerTrait;
/**
* The PaymentTokenEndpoint.
*
* @var PaymentTokenEndpoint
*/
private $payment_token_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* CustomerApprovalListener constructor.
*
* @param PaymentTokenEndpoint $payment_token_endpoint The PaymentTokenEndpoint.
* @param LoggerInterface $logger The logger.
*/
public function __construct( PaymentTokenEndpoint $payment_token_endpoint, LoggerInterface $logger ) {
$this->payment_token_endpoint = $payment_token_endpoint;
$this->logger = $logger;
}
/**
* Listens for redirects after the PayPal vaulting approval by customer.
*
* @return void
*/
public function listen(): void {
$token = filter_input( INPUT_GET, 'approval_token_id', FILTER_SANITIZE_STRING );
if ( ! is_string( $token ) ) {
return;
}
$url = (string) filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
try {
$query = wp_parse_url( $url, PHP_URL_QUERY );
if ( $query && str_contains( $query, 'ppcp_vault=cancel' ) ) {
return;
}
try {
$this->payment_token_endpoint->create_from_approval_token( $token, get_current_user_id() );
} catch ( Exception $exception ) {
$this->logger->error( 'Failed to create payment token. ' . $exception->getMessage() );
}
} finally {
wp_safe_redirect( remove_query_arg( array( 'ppcp_vault', 'approval_token_id', 'approval_session_id' ), $url ) );
exit();
}
}
}

View file

@ -13,11 +13,10 @@ use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -26,6 +25,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/
class PaymentTokenChecker {
use FreeTrialHandlerTrait;
/**
* The payment token repository.
*
@ -33,6 +34,13 @@ class PaymentTokenChecker {
*/
protected $payment_token_repository;
/**
* The order repository.
*
* @var OrderRepository
*/
protected $order_repository;
/**
* The settings.
*
@ -47,13 +55,6 @@ class PaymentTokenChecker {
*/
protected $authorized_payments_processor;
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* The payments endpoint.
*
@ -72,24 +73,24 @@ class PaymentTokenChecker {
* PaymentTokenChecker constructor.
*
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param OrderRepository $order_repository The order repository.
* @param Settings $settings The settings.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payments processor.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
PaymentTokenRepository $payment_token_repository,
OrderRepository $order_repository,
Settings $settings,
AuthorizedPaymentsProcessor $authorized_payments_processor,
OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_endpoint,
LoggerInterface $logger
) {
$this->payment_token_repository = $payment_token_repository;
$this->order_repository = $order_repository;
$this->settings = $settings;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
}
@ -115,6 +116,16 @@ class PaymentTokenChecker {
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
if ( $tokens ) {
try {
if ( $this->is_free_trial_order( $wc_order ) ) {
if ( CreditCardGateway::ID === $wc_order->get_payment_method() ) {
$order = $this->order_repository->for_wc_order( $wc_order );
$this->authorized_payments_processor->void_authorizations( $order );
$wc_order->payment_complete();
}
return;
}
$this->capture_authorized_payment( $wc_order );
} catch ( Exception $exception ) {
$this->logger->error( $exception->getMessage() );
@ -126,8 +137,8 @@ class PaymentTokenChecker {
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved on PayPal." );
try {
$order = $this->get_order( $wc_order );
$this->void_authorizations( $order );
$order = $this->order_repository->for_wc_order( $wc_order );
$this->authorized_payments_processor->void_authorizations( $order );
} catch ( RuntimeException $exception ) {
$this->logger->warning( $exception->getMessage() );
}
@ -149,55 +160,6 @@ class PaymentTokenChecker {
}
}
/**
* Voids authorizations for the given PayPal order.
*
* @param Order $order The PayPal order.
* @return void
* @throws RuntimeException When there is a problem voiding authorizations.
*/
private function void_authorizations( Order $order ): void {
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$this->payments_endpoint->void( $authorization );
}
}
/**
* Gets a PayPal order from the given WooCommerce order.
*
* @param WC_Order $wc_order The WooCommerce order.
* @return Order The PayPal order.
* @throws RuntimeException When there is a problem getting the PayPal order.
*/
private function get_order( WC_Order $wc_order ): Order {
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
return $this->order_endpoint->order( $paypal_order_id );
}
/**
* Updates WC order and subscription status to failed and canceled respectively.
*

View file

@ -43,6 +43,11 @@ class VaultingModule implements ModuleInterface {
return;
}
$listener = $container->get( 'vaulting.customer-approval-listener' );
assert( $listener instanceof CustomerApprovalListener );
$listener->listen();
add_filter(
'woocommerce_account_menu_items',
function( $menu_links ) {

View file

@ -771,21 +771,7 @@ return array(
>',
'</a>'
),
'options' => array(
'card' => _x( 'Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-payments' ),
'credit' => _x( 'Pay Later', 'Name of payment method', 'woocommerce-paypal-payments' ),
'sepa' => _x( 'SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-payments' ),
'bancontact' => _x( 'Bancontact', 'Name of payment method', 'woocommerce-paypal-payments' ),
'blik' => _x( 'BLIK', 'Name of payment method', 'woocommerce-paypal-payments' ),
'eps' => _x( 'eps', 'Name of payment method', 'woocommerce-paypal-payments' ),
'giropay' => _x( 'giropay', 'Name of payment method', 'woocommerce-paypal-payments' ),
'ideal' => _x( 'iDEAL', 'Name of payment method', 'woocommerce-paypal-payments' ),
'mercadopago' => _x( 'Mercado Pago', 'Name of payment method', 'woocommerce-paypal-payments' ),
'mybank' => _x( 'MyBank', 'Name of payment method', 'woocommerce-paypal-payments' ),
'p24' => _x( 'Przelewy24', 'Name of payment method', 'woocommerce-paypal-payments' ),
'sofort' => _x( 'Sofort', 'Name of payment method', 'woocommerce-paypal-payments' ),
'venmo' => _x( 'Venmo', 'Name of payment method', 'woocommerce-paypal-payments' ),
),
'options' => $container->get( 'wcgateway.all-funding-sources' ),
'screens' => array(
State::STATE_START,
State::STATE_ONBOARDED,
@ -2064,6 +2050,24 @@ return array(
return $fields;
},
'wcgateway.all-funding-sources' => static function( ContainerInterface $container ): array {
return array(
'card' => _x( 'Credit or debit cards', 'Name of payment method', 'woocommerce-paypal-payments' ),
'credit' => _x( 'Pay Later', 'Name of payment method', 'woocommerce-paypal-payments' ),
'sepa' => _x( 'SEPA-Lastschrift', 'Name of payment method', 'woocommerce-paypal-payments' ),
'bancontact' => _x( 'Bancontact', 'Name of payment method', 'woocommerce-paypal-payments' ),
'blik' => _x( 'BLIK', 'Name of payment method', 'woocommerce-paypal-payments' ),
'eps' => _x( 'eps', 'Name of payment method', 'woocommerce-paypal-payments' ),
'giropay' => _x( 'giropay', 'Name of payment method', 'woocommerce-paypal-payments' ),
'ideal' => _x( 'iDEAL', 'Name of payment method', 'woocommerce-paypal-payments' ),
'mercadopago' => _x( 'Mercado Pago', 'Name of payment method', 'woocommerce-paypal-payments' ),
'mybank' => _x( 'MyBank', 'Name of payment method', 'woocommerce-paypal-payments' ),
'p24' => _x( 'Przelewy24', 'Name of payment method', 'woocommerce-paypal-payments' ),
'sofort' => _x( 'Sofort', 'Name of payment method', 'woocommerce-paypal-payments' ),
'venmo' => _x( 'Venmo', 'Name of payment method', 'woocommerce-paypal-payments' ),
);
},
'wcgateway.checkout.address-preset' => static function( ContainerInterface $container ): CheckoutPayPalAddressPreset {
return new CheckoutPayPalAddressPreset(

View file

@ -11,9 +11,11 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Exception;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
@ -24,7 +26,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
*/
trait ProcessPaymentTrait {
use OrderMetaTrait, PaymentsStatusHandlingTrait, TransactionIdHandlingTrait;
use OrderMetaTrait, PaymentsStatusHandlingTrait, TransactionIdHandlingTrait, FreeTrialHandlerTrait;
/**
* Process a payment for an WooCommerce order.
@ -115,7 +117,10 @@ trait ProcessPaymentTrait {
$this->handle_new_order_status( $order, $wc_order );
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
if ( $this->is_free_trial_order( $wc_order ) ) {
$this->authorized_payments_processor->void_authorizations( $order );
$wc_order->payment_complete();
} elseif ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
}
@ -130,6 +135,28 @@ trait ProcessPaymentTrait {
}
}
if ( PayPalGateway::ID === $payment_method && $this->is_free_trial_order( $wc_order ) ) {
$user_id = (int) $wc_order->get_customer_id();
$tokens = $this->payment_token_repository->all_for_user_id( $user_id );
if ( ! array_filter(
$tokens,
function ( PaymentToken $token ): bool {
return isset( $token->source()->paypal );
}
) ) {
$this->handle_failure( $wc_order, new Exception( 'No saved PayPal account.' ) );
return null;
}
$wc_order->payment_complete();
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
/**
* If customer has chosen change Subscription payment.
*/

View file

@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
@ -244,6 +245,39 @@ class AuthorizedPaymentsProcessor {
}
}
/**
* Voids authorizations for the given PayPal order.
*
* @param Order $order The PayPal order.
* @return void
* @throws RuntimeException When there is a problem voiding authorizations.
*/
public function void_authorizations( Order $order ): void {
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$this->payments_endpoint->void( $authorization );
}
}
/**
* Displays the notice for a status.
*

View file

@ -10,10 +10,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
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 WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
@ -24,8 +26,9 @@ class PaymentTokenEndpointTest extends TestCase
private $host;
private $bearer;
private $factory;
private $logger;
private $payment_token_action_links_factory;
private $customer_repository;
private $request_id_repository;
private $sut;
public function setUp(): void
@ -35,14 +38,18 @@ class PaymentTokenEndpointTest extends TestCase
$this->host = 'https://example.com/';
$this->bearer = Mockery::mock(Bearer::class);
$this->factory = Mockery::mock(PaymentTokenFactory::class);
$this->payment_token_action_links_factory = Mockery::mock(PaymentTokenActionLinksFactory::class);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->customer_repository = Mockery::mock(CustomerRepository::class);
$this->request_id_repository = Mockery::mock(PayPalRequestIdRepository::class);
$this->sut = new PaymentTokenEndpoint(
$this->host,
$this->bearer,
$this->factory,
$this->payment_token_action_links_factory,
$this->logger,
$this->customer_repository
$this->customer_repository,
$this->request_id_repository
);
}

View file

@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\TestCase;
use Mockery;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
@ -141,6 +142,10 @@ class AmountFactoryTest extends TestCase
->with($order)
->andReturn([$item]);
$order
->shouldReceive('get_payment_method')
->andReturn(PayPalGateway::ID);
$order
->shouldReceive('get_total')
->andReturn(100);
@ -197,6 +202,10 @@ class AmountFactoryTest extends TestCase
->with($order)
->andReturn([$item]);
$order
->shouldReceive('get_payment_method')
->andReturn(PayPalGateway::ID);
$order
->shouldReceive('get_total')
->andReturn(100);

View file

@ -0,0 +1,158 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\TestCase;
class PaymentTokenActionLinksFactoryTest extends TestCase
{
private $testee;
public function setUp(): void
{
parent::setUp();
$this->testee = new PaymentTokenActionLinksFactory();
}
/**
* @dataProvider validData
*/
public function testSuccess(string $json, string $approve_link, string $confirm_link, string $status_link)
{
$obj = json_decode($json);
$result = $this->testee->from_paypal_response($obj);
self::assertEquals($approve_link, $result->approve_link());
self::assertEquals($confirm_link, $result->confirm_link());
self::assertEquals($status_link, $result->status_link());
}
/**
* @dataProvider invalidData
*/
public function testFailure(string $json)
{
$obj = json_decode($json);
$this->expectException(RuntimeException::class);
$this->testee->from_paypal_response($obj);
}
public function validData() : array
{
return [
[
'
{
"links": [
{
"href": "https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123",
"rel": "approve",
"method": "POST"
},
{
"href": "https://api-m.sandbox.paypal.com/v2/vault/approval-tokens/asd123/confirm-payment-token",
"rel": "confirm",
"method": "POST"
},
{
"href": "https://api-m.sandbox.paypal.com/v2/vault/approval-tokens/asd123",
"rel": "status",
"method": "GET"
}
]
}
',
'https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123',
'https://api-m.sandbox.paypal.com/v2/vault/approval-tokens/asd123/confirm-payment-token',
'https://api-m.sandbox.paypal.com/v2/vault/approval-tokens/asd123',
],
[
'
{
"links": [
{
"href": "https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123",
"rel": "approve",
"method": "POST"
}
]
}
',
'https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123',
'',
'',
],
[
'
{
"links": [
{
"href": "https://example.com",
"rel": "new",
"method": "POST"
},
{
"href": "https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123",
"rel": "approve",
"method": "POST"
}
]
}
',
'https://www.sandbox.paypal.com/webapps/agreements/approve?approval_session_id=qwe123',
'',
'',
],
];
}
public function invalidData() : array
{
return [
[
'
{
"links": [
{}
]
}
',
'
{
"links": []
}
',
'{}',
'
{
"links": [
{},
{
"href": "https://example.com",
"rel": "new",
"method": "POST"
}
]
}
',
'no approve link' => '
{
"links": [
{
"href": "https://api-m.sandbox.paypal.com/v2/vault/approval-tokens/asd123/confirm-payment-token",
"rel": "confirm",
"method": "POST"
}
]
}
',
],
];
}
}

View file

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
use Mockery;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\when;
class PayPalRequestIdRepositoryTest extends TestCase
{
private $testee;
private $data = [];
public function setUp(): void
{
parent::setUp();
$this->testee = new PayPalRequestIdRepository();
when('get_option')->alias(function () {
return $this->data;
});
when('update_option')->alias(function (string $key, array $data) {
$this->data = $data;
});
}
public function testForOrder()
{
$this->testee->set_for_order($this->createPaypalOrder('42'), 'request1');
$this->testee->set_for_order($this->createPaypalOrder('43'), 'request2');
self::assertEquals('request1', $this->testee->get_for_order($this->createPaypalOrder('42')));
self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43')));
self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('41')));
}
public function testExpiration()
{
$this->testee->set_for_order($this->createPaypalOrder('42'), 'request1');
$this->data['42']['expiration'] = time() - 1;
$this->testee->set_for_order($this->createPaypalOrder('43'), 'request2');
self::assertEquals('', $this->testee->get_for_order($this->createPaypalOrder('42')));
self::assertEquals('request2', $this->testee->get_for_order($this->createPaypalOrder('43')));
}
private function createPaypalOrder(string $id): Order {
$order = Mockery::mock(Order::class);
$order
->shouldReceive('id')
->andReturn($id);
return $order;
}
}

View file

@ -32,11 +32,12 @@ class AuthorizedPaymentsProcessorTest extends TestCase
private $amount = 42.0;
private $currency = 'EUR';
private $paypalOrder;
private $authorization;
private $orderEndpoint;
private $paymentsEndpoint;
private $notice;
private $config;
private $subscription_helper;
private $subscription_helperauthorization;
private $testee;
public function setUp(): void {
@ -44,7 +45,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase
$this->wcOrder = $this->createWcOrder($this->paypalOrderId);
$this->paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED)]);
$this->authorization = $this->createAuthorization($this->authorizationId, AuthorizationStatus::CREATED);
$this->paypalOrder = $this->createPaypalOrder([$this->authorization]);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->orderEndpoint
@ -176,6 +178,57 @@ class AuthorizedPaymentsProcessorTest extends TestCase
);
}
public function testVoid()
{
$authorizations = [
$this->createAuthorization('id1', AuthorizationStatus::CREATED),
$this->createAuthorization('id2', AuthorizationStatus::VOIDED),
$this->createAuthorization('id3', AuthorizationStatus::PENDING),
$this->createAuthorization('id4', AuthorizationStatus::CAPTURED),
$this->createAuthorization('id5', AuthorizationStatus::DENIED),
$this->createAuthorization('id6', AuthorizationStatus::EXPIRED),
$this->createAuthorization('id7', AuthorizationStatus::COMPLETED),
];
$this->paypalOrder = $this->createPaypalOrder($authorizations);
$this->paymentsEndpoint
->expects('void')
->with($authorizations[0]);
$this->paymentsEndpoint
->expects('void')
->with($authorizations[2]);
$this->testee->void_authorizations($this->paypalOrder);
self::assertTrue(true); // fix no assertions warning
}
public function testVoidWhenNoVoidable()
{
$exception = new RuntimeException('void error');
$this->paymentsEndpoint
->expects('void')
->with($this->authorization)
->andThrow($exception);
$this->expectExceptionObject($exception);
$this->testee->void_authorizations($this->paypalOrder);
}
public function testVoidWhenNoError()
{
$authorizations = [
$this->createAuthorization('id1', AuthorizationStatus::VOIDED),
$this->createAuthorization('id2', AuthorizationStatus::EXPIRED),
];
$this->paypalOrder = $this->createPaypalOrder($authorizations);
$this->expectException(RuntimeException::class);
$this->testee->void_authorizations($this->paypalOrder);
}
private function createWcOrder(string $paypalOrderId): WC_Order {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder
@ -192,14 +245,7 @@ class AuthorizedPaymentsProcessorTest extends TestCase
}
private function createAuthorization(string $id, string $status): Authorization {
$authorization = Mockery::mock(Authorization::class);
$authorization
->shouldReceive('id')
->andReturn($id);
$authorization
->shouldReceive('status')
->andReturn(new AuthorizationStatus($status));
return $authorization;
return new Authorization($id, new AuthorizationStatus($status));
}
private function createCapture(string $status): Capture {