mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Allow credit card gateway to use saved credit cards
This commit is contained in:
parent
38d1b1ad9d
commit
fd7dfaf13b
10 changed files with 229 additions and 16 deletions
|
@ -15,6 +15,20 @@ class CheckoutBootstap {
|
|||
|
||||
jQuery(document.body).on('updated_checkout', () => {
|
||||
this.render();
|
||||
|
||||
jQuery('#saved-credit-card').on('change', () => {
|
||||
if(jQuery('#saved-credit-card').val() !== '') {
|
||||
this.renderer.hideButtons(this.gateway.button.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.messages.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
||||
jQuery('#place_order').show();
|
||||
} else {
|
||||
jQuery('#place_order').hide();
|
||||
this.renderer.hideButtons(this.gateway.button.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.messages.wrapper);
|
||||
this.renderer.showButtons(this.gateway.hosted_fields.wrapper);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
jQuery(document.body).
|
||||
|
@ -79,4 +93,4 @@ class CheckoutBootstap {
|
|||
}
|
||||
}
|
||||
|
||||
export default CheckoutBootstap;
|
||||
export default CheckoutBootstap;
|
||||
|
|
|
@ -69,6 +69,7 @@ return array(
|
|||
$subscription_helper = $container->get( 'subscription.helper' );
|
||||
$messages_apply = $container->get( 'button.helper.messages-apply' );
|
||||
$environment = $container->get( 'onboarding.environment' );
|
||||
$payment_token_repository = $container->get('subscription.repository.payment-token');
|
||||
return new SmartButton(
|
||||
$container->get( 'button.url' ),
|
||||
$container->get( 'session.handler' ),
|
||||
|
@ -81,7 +82,8 @@ return array(
|
|||
$dcc_applies,
|
||||
$subscription_helper,
|
||||
$messages_apply,
|
||||
$environment
|
||||
$environment,
|
||||
$payment_token_repository
|
||||
);
|
||||
},
|
||||
'button.url' => static function ( $container ): string {
|
||||
|
|
|
@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\Button\Assets;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||
|
@ -22,6 +23,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
|
|||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
|
@ -114,7 +116,12 @@ class SmartButton implements SmartButtonInterface {
|
|||
*/
|
||||
private $environment;
|
||||
|
||||
/**
|
||||
/**
|
||||
* @var PaymentTokenRepository
|
||||
*/
|
||||
private $payment_token_repository;
|
||||
|
||||
/**
|
||||
* SmartButton constructor.
|
||||
*
|
||||
* @param string $module_url The URL to the module.
|
||||
|
@ -142,7 +149,8 @@ class SmartButton implements SmartButtonInterface {
|
|||
DccApplies $dcc_applies,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
MessagesApply $messages_apply,
|
||||
Environment $environment
|
||||
Environment $environment,
|
||||
PaymentTokenRepository $payment_token_repository
|
||||
) {
|
||||
|
||||
$this->module_url = $module_url;
|
||||
|
@ -157,7 +165,8 @@ class SmartButton implements SmartButtonInterface {
|
|||
$this->subscription_helper = $subscription_helper;
|
||||
$this->messages_apply = $messages_apply;
|
||||
$this->environment = $environment;
|
||||
}
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the necessary action hooks to render the HTML depending on the settings.
|
||||
|
@ -199,15 +208,39 @@ class SmartButton implements SmartButtonInterface {
|
|||
11
|
||||
);
|
||||
|
||||
$payment_token_repository = $this->payment_token_repository;
|
||||
add_filter(
|
||||
'woocommerce_credit_card_form_fields',
|
||||
function ( $default_fields, $id ) {
|
||||
function ( $default_fields, $id ) use( $payment_token_repository ) {
|
||||
if ( $this->can_save_credit_card() ) {
|
||||
$default_fields['card-vault'] = sprintf(
|
||||
'<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%1$s</label></p>',
|
||||
'<p class="form-row form-row-wide"><label for="vault"><input class="ppcp-credit-card-vault" type="checkbox" id="ppcp-credit-card-vault" name="vault">%s</label></p>',
|
||||
esc_html__( 'Save your Credit Card', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
|
||||
$tokens = $payment_token_repository->all_for_user_id( 1 );
|
||||
if($tokens && $this->tokens_contains_card($tokens) ) {
|
||||
|
||||
$output = sprintf(
|
||||
'<select id="saved-credit-card" name="saved_credit_card"><option value="">%s</option>',
|
||||
esc_html__( 'Choose a saved payment', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
foreach ($tokens as $token) {
|
||||
if(isset($token->source()->card)) {
|
||||
$output .= sprintf(
|
||||
'<option value="%1$s">%2$s ...%3$s</option>',
|
||||
$token->id(),
|
||||
$token->source()->card->brand,
|
||||
$token->source()->card->last_digits
|
||||
);
|
||||
}
|
||||
}
|
||||
$output .= '</select>';
|
||||
|
||||
$default_fields['saved-credit-card'] = $output;
|
||||
}
|
||||
}
|
||||
|
||||
return $default_fields;
|
||||
},
|
||||
10,
|
||||
|
@ -988,4 +1021,19 @@ class SmartButton implements SmartButtonInterface {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tokens has card source.
|
||||
*
|
||||
* @param PaymentToken[] $tokens
|
||||
* @return bool
|
||||
*/
|
||||
protected function tokens_contains_card($tokens) {
|
||||
foreach ($tokens as $token) {
|
||||
if(isset($token->source()->card)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue