Introduce payment tokens migration logic (WIP)

This commit is contained in:
emilicastells 2022-12-05 17:20:11 +01:00
parent 47cb0a2317
commit 8c4c89da00
No known key found for this signature in database
GPG key ID: 1520C07081754570
6 changed files with 76 additions and 101 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

File diff suppressed because one or more lines are too long

View file

@ -20,27 +20,11 @@ return array(
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'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 {
return new PaymentTokensRenderer();
},
'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 {
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 {
return new PaymentTokenChecker(
$container->get( 'vaulting.repository.payment-token' ),
@ -72,5 +56,10 @@ return array(
},
'vaulting.payment-token-paypal' => function( ContainerInterface $container ): PaymentTokenPayPal {
return new PaymentTokenPayPal();
}
},
'vaulting.payment-tokens-migration' => function( ContainerInterface $container ): PaymentTokensMigration {
return new PaymentTokensMigration(
$container->get( 'vaulting.payment-token-paypal' )
);
},
);

View file

@ -0,0 +1,64 @@
<?php
/**
* The payment tokens migration handler.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WC_Payment_Token_CC;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
/**
* Class PaymentTokensMigration
*/
class PaymentTokensMigration {
/**
* WC Payment token PayPal.
*
* @var PaymentTokenPayPal
*/
private $payment_token_paypal;
/**
* PaymentTokensMigration constructor.
*
* @param PaymentTokenPayPal $payment_token_paypal WC Payment token PayPal.
*/
public function __construct( PaymentTokenPayPal $payment_token_paypal ) {
$this->payment_token_paypal = $payment_token_paypal;
}
/**
* Migrates user existing vaulted tokens into WC payment tokens API.
*
* @param int $id WooCommerce customer id.
*/
public function migrate_payment_tokens_for_user( int $id ) {
$tokens = (array) get_user_meta( $id, PaymentTokenRepository::USER_META, true );
foreach ( $tokens as $token ) {
if ( isset( $token->source->card ) ) {
$payment_token = new WC_Payment_Token_CC();
$payment_token->set_token( $token->id() );
$payment_token->set_user_id( $id );
$payment_token->set_gateway_id( CreditCardGateway::ID );
$payment_token->set_last4( $token->source()->card->last_digits );
$token->set_card_type( $token->source()->card->brand );
$token->save();
} elseif ( $token->source->card ) {
$this->payment_token_paypal->set_token( $token->id() );
$this->payment_token_paypal->set_user_id( $id );
$this->payment_token_paypal->set_gateway_id( PayPalGateway::ID );
$this->payment_token_paypal->save();
}
}
}
}

View file

@ -1,83 +0,0 @@
<?php
/**
* The payment tokens renderer.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
/**
* Class PaymentTokensRendered
*/
class PaymentTokensRenderer {
/**
* Render payment tokens.
*
* @param PaymentToken[] $tokens The tokens.
* @return false|string
*/
public function render( array $tokens ) {
ob_start();
?>
<table class="shop_table shop_table_responsive">
<thead>
<tr>
<th><?php echo esc_html__( 'Payment sources', 'woocommerce-paypal-payments' ); ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $tokens as $token ) {
$source = $token->source() ?? null;
if ( $source && isset( $source->card ) ) {
?>
<tr>
<td><?php echo esc_attr( $source->card->brand ) . ' ...' . esc_attr( $source->card->last_digits ); ?></td>
<td>
<a class="ppcp-delete-payment-button" id="<?php echo esc_attr( $token->id() ); ?>" href=""><?php echo esc_html__( 'Delete', 'woocommerce-paypal-payments' ); ?></a>
</td>
</tr>
<?php
}
if ( $source && isset( $source->paypal ) ) {
?>
<tr>
<td><?php echo esc_attr( $source->paypal->payer->email_address ); ?></td>
<td>
<a class="ppcp-delete-payment-button" id="<?php echo esc_attr( $token->id() ); ?>" href=""><?php echo esc_html__( 'Delete', 'woocommerce-paypal-payments' ); ?></a>
</td>
</tr>
<?php
}
?>
<?php
}
?>
</tbody>
</table>
<?php
return ob_get_clean();
}
/**
* Render no payments message.
*
* @return false|string
*/
public function render_no_tokens() {
ob_start();
?>
<div class="woocommerce-Message woocommerce-Message--info woocommerce-info">
<?php echo esc_html__( 'No payments available yet.', 'woocommerce-paypal-payments' ); ?>
</div>
<?php
return ob_get_clean();
}
}