Save payment after order processor

This commit is contained in:
Emili Castells Guasch 2023-10-20 16:26:19 +02:00
parent b6734a82da
commit ffb279115c

View file

@ -9,16 +9,22 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavePaymentMethods;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\UserIdToken;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenFactory;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenPayPal;
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;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class SavePaymentMethodsModule
@ -95,8 +101,45 @@ class SavePaymentMethodsModule implements ModuleInterface {
add_action(
'woocommerce_paypal_payments_after_order_processor',
function( WC_Order $wc_order, Order $order ) {
// vault payment here ...
function( WC_Order $wc_order, Order $order ) use ( $c ) {
$payment_vault_attributes = $order->payment_source()->properties()->attributes->vault ?? null;
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 );
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), PayPalGateway::ID );
if ( $payment_token_helper->token_exist( $wc_tokens, $payment_vault_attributes->id ) ) {
return;
}
$payment_token_factory = $c->get( 'vaulting.payment-token-factory' );
assert( $payment_token_factory instanceof PaymentTokenFactory );
$payment_token_paypal = $payment_token_factory->create( 'paypal' );
assert( $payment_token_paypal instanceof PaymentTokenPayPal );
$payment_token_paypal->set_token( $payment_vault_attributes->id );
$payment_token_paypal->set_user_id( $wc_order->get_customer_id() );
$payment_token_paypal->set_gateway_id( PayPalGateway::ID );
$email = $order->payment_source()->properties()->email_address ?? '';
if ( $email && is_email( $email ) ) {
$payment_token_paypal->set_email( $email );
}
try {
$payment_token_paypal->save();
} catch ( Exception $exception ) {
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$logger->error(
"Could not save WC payment token PayPal for order #{$wc_order->get_id()}. " . $exception->getMessage()
);
}
}
},
10,
2