2023-10-17 15:11:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The save payment methods module.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\PayPalCommerce\Applepay
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WooCommerce\PayPalCommerce\SavePaymentMethods;
|
|
|
|
|
2023-10-19 16:15:20 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2023-10-20 12:54:00 +02:00
|
|
|
use WC_Order;
|
2023-10-19 16:15:20 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
|
2023-10-20 12:54:00 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
2023-10-27 15:28:01 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
2023-10-19 16:15:20 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
2023-10-26 16:36:38 +02:00
|
|
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
|
|
|
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
|
2023-10-20 16:26:19 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenFactory;
|
|
|
|
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenHelper;
|
2023-10-17 15:11:48 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
2023-10-20 16:26:19 +02:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
2023-10-17 15:11:48 +02:00
|
|
|
|
2023-10-18 17:03:15 +02:00
|
|
|
/**
|
|
|
|
* Class SavePaymentMethodsModule
|
|
|
|
*/
|
2023-10-17 15:11:48 +02:00
|
|
|
class SavePaymentMethodsModule implements ModuleInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setup(): ServiceProviderInterface {
|
|
|
|
return new ServiceProvider(
|
|
|
|
require __DIR__ . '/../services.php',
|
|
|
|
require __DIR__ . '/../extensions.php'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-18 17:03:15 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2023-10-19 11:18:01 +02:00
|
|
|
public function run( ContainerInterface $c ): void {
|
|
|
|
if ( ! $c->get( 'save-payment-methods.eligible' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-19 16:15:20 +02:00
|
|
|
|
|
|
|
// Adds `id_token` to localized script data.
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_paypal_payments_localized_script_data',
|
|
|
|
function( array $localized_script_data ) use ( $c ) {
|
|
|
|
$api = $c->get( 'api.user-id-token' );
|
|
|
|
assert( $api instanceof UserIdToken );
|
|
|
|
|
|
|
|
try {
|
2023-10-20 16:46:02 +02:00
|
|
|
$target_customer_id = '';
|
2023-10-26 16:36:38 +02:00
|
|
|
if ( is_user_logged_in() ) {
|
2023-10-20 16:46:02 +02:00
|
|
|
$target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
|
|
|
|
}
|
|
|
|
|
|
|
|
$id_token = $api->id_token( $target_customer_id );
|
2023-10-19 16:15:20 +02:00
|
|
|
$localized_script_data['save_payment_methods'] = array(
|
|
|
|
'id_token' => $id_token,
|
|
|
|
);
|
|
|
|
|
|
|
|
$localized_script_data['data_client_id']['set_attribute'] = false;
|
|
|
|
|
|
|
|
} catch ( RuntimeException $exception ) {
|
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
assert( $logger instanceof LoggerInterface );
|
|
|
|
|
|
|
|
$error = $exception->getMessage();
|
|
|
|
if ( is_a( $exception, PayPalApiException::class ) ) {
|
|
|
|
$error = $exception->get_details( $error );
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger->error( $error );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $localized_script_data;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Adds attributes needed to save payment method.
|
|
|
|
add_filter(
|
|
|
|
'ppcp_create_order_request_body_data',
|
2023-10-27 15:28:01 +02:00
|
|
|
function( array $data ): array {
|
2023-10-19 16:15:20 +02:00
|
|
|
$data['payment_source'] = array(
|
|
|
|
'paypal' => array(
|
|
|
|
'attributes' => array(
|
|
|
|
'vault' => array(
|
|
|
|
'store_in_vault' => 'ON_SUCCESS',
|
|
|
|
'usage_type' => 'MERCHANT',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
);
|
2023-10-20 12:54:00 +02:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_paypal_payments_after_order_processor',
|
2023-10-20 16:26:19 +02:00
|
|
|
function( WC_Order $wc_order, Order $order ) use ( $c ) {
|
2023-10-27 15:28:01 +02:00
|
|
|
$payment_source = $order->payment_source();
|
|
|
|
assert( $payment_source instanceof PaymentSource );
|
|
|
|
|
|
|
|
$payment_vault_attributes = $payment_source->properties()->attributes->vault ?? null;
|
2023-10-20 16:26:19 +02:00
|
|
|
if ( $payment_vault_attributes ) {
|
|
|
|
update_user_meta( $wc_order->get_customer_id(), '_ppcp_target_customer_id', $payment_vault_attributes->customer->id );
|
|
|
|
|
|
|
|
$payment_token_helper = $c->get( 'vaulting.payment-token-helper' );
|
|
|
|
assert( $payment_token_helper instanceof PaymentTokenHelper );
|
|
|
|
|
|
|
|
$payment_token_factory = $c->get( 'vaulting.payment-token-factory' );
|
|
|
|
assert( $payment_token_factory instanceof PaymentTokenFactory );
|
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
assert( $logger instanceof LoggerInterface );
|
2023-10-20 16:26:19 +02:00
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$wc_payment_tokens = $c->get( 'save-payment-methods.wc-payment-tokens' );
|
|
|
|
assert( $wc_payment_tokens instanceof WooCommercePaymentTokens );
|
2023-10-20 16:26:19 +02:00
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$wc_payment_tokens->create_payment_token_paypal(
|
|
|
|
$wc_order->get_customer_id(),
|
|
|
|
$payment_vault_attributes->id,
|
2023-10-27 15:28:01 +02:00
|
|
|
$payment_source->properties()->email_address ?? ''
|
2023-10-27 12:25:42 +02:00
|
|
|
);
|
2023-10-20 16:26:19 +02:00
|
|
|
}
|
2023-10-20 12:54:00 +02:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2023-10-23 16:50:43 +02:00
|
|
|
|
|
|
|
add_filter( 'woocommerce_paypal_payments_disable_add_payment_method', '__return_false' );
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'wp_enqueue_scripts',
|
|
|
|
function() use ( $c ) {
|
|
|
|
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$module_url = $c->get( 'save-payment-methods.module.url' );
|
|
|
|
wp_enqueue_script(
|
|
|
|
'ppcp-add-payment-method',
|
|
|
|
untrailingslashit( $module_url ) . '/assets/js/add-payment-method.js',
|
|
|
|
array( 'jquery' ),
|
|
|
|
$c->get( 'ppcp.asset-version' ),
|
|
|
|
true
|
|
|
|
);
|
2023-10-26 16:36:38 +02:00
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$api = $c->get( 'api.user-id-token' );
|
|
|
|
assert( $api instanceof UserIdToken );
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$target_customer_id = '';
|
2023-10-27 12:25:42 +02:00
|
|
|
if ( is_user_logged_in() ) {
|
|
|
|
$target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
|
2023-10-26 16:36:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$id_token = $api->id_token( $target_customer_id );
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
wp_localize_script(
|
|
|
|
'ppcp-add-payment-method',
|
|
|
|
'ppcp_add_payment_method',
|
|
|
|
array(
|
2023-10-27 12:25:42 +02:00
|
|
|
'client_id' => $c->get( 'button.client_id' ),
|
2023-10-26 16:36:38 +02:00
|
|
|
'merchant_id' => $c->get( 'api.merchant_id' ),
|
2023-10-27 12:25:42 +02:00
|
|
|
'id_token' => $id_token,
|
|
|
|
'ajax' => array(
|
|
|
|
'create_setup_token' => array(
|
2023-10-26 16:36:38 +02:00
|
|
|
'endpoint' => \WC_AJAX::get_endpoint( CreateSetupToken::ENDPOINT ),
|
|
|
|
'nonce' => wp_create_nonce( CreateSetupToken::nonce() ),
|
|
|
|
),
|
2023-10-27 12:25:42 +02:00
|
|
|
'create_payment_token' => array(
|
2023-10-26 16:36:38 +02:00
|
|
|
'endpoint' => \WC_AJAX::get_endpoint( CreatePaymentToken::ENDPOINT ),
|
|
|
|
'nonce' => wp_create_nonce( CreatePaymentToken::nonce() ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2023-10-27 12:25:42 +02:00
|
|
|
} catch ( RuntimeException $exception ) {
|
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
assert( $logger instanceof LoggerInterface );
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
$error = $exception->getMessage();
|
2023-10-27 12:25:42 +02:00
|
|
|
if ( is_a( $exception, PayPalApiException::class ) ) {
|
|
|
|
$error = $exception->get_details( $error );
|
2023-10-26 16:36:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
$logger->error( $error );
|
2023-10-26 16:36:38 +02:00
|
|
|
}
|
2023-10-23 16:50:43 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_add_payment_method_form_bottom',
|
|
|
|
function () {
|
|
|
|
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-27 12:25:42 +02:00
|
|
|
echo '<div id="ppc-button-' . esc_attr( PayPalGateway::ID ) . '-save-payment-method"></div>';
|
2023-10-23 16:50:43 +02:00
|
|
|
}
|
|
|
|
);
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'wc_ajax_' . CreateSetupToken::ENDPOINT,
|
|
|
|
static function () use ( $c ) {
|
|
|
|
$endpoint = $c->get( 'save-payment-methods.endpoint.create-setup-token' );
|
2023-10-27 12:25:42 +02:00
|
|
|
assert( $endpoint instanceof CreateSetupToken );
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
$endpoint->handle_request();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'wc_ajax_' . CreatePaymentToken::ENDPOINT,
|
|
|
|
static function () use ( $c ) {
|
|
|
|
$endpoint = $c->get( 'save-payment-methods.endpoint.create-payment-token' );
|
2023-10-27 12:25:42 +02:00
|
|
|
assert( $endpoint instanceof CreatePaymentToken );
|
2023-10-26 16:36:38 +02:00
|
|
|
|
|
|
|
$endpoint->handle_request();
|
|
|
|
}
|
|
|
|
);
|
2023-10-19 11:18:01 +02:00
|
|
|
}
|
2023-10-17 15:11:48 +02:00
|
|
|
}
|