ApplePay Vaulting Integration

This commit is contained in:
Pedro Silva 2024-01-24 08:47:48 +00:00
parent 827bd2568d
commit 96b83c9d0d
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
7 changed files with 86 additions and 12 deletions

View file

@ -209,11 +209,15 @@ class ApplepayButton {
/**
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
*/
async onButtonClick() {
async onButtonClick(data, actions) {
console.log('data, actions', data, actions);
this.log('onButtonClick', this.context);
const paymentRequest = this.paymentRequest();
window.ppcpFundingSource = 'apple_pay'; // Do this on another place like on create order endpoint handler.
// Trigger woocommerce validation if we are in the checkout page.
if (this.context === 'checkout') {
const checkoutFormSelector = 'form.woocommerce-checkout';

View file

@ -87,7 +87,7 @@ const PayPalComponent = ({
bn_code: '',
context: config.scriptData.context,
payment_method: 'ppcp-gateway',
funding_source: data.paymentSource,
funding_source: window.ppcpFundingSource ?? 'paypal',
createaccount: false
}),
});

View file

@ -7,7 +7,6 @@ class ButtonModuleWatcher {
}
watchContextBootstrap(callable) {
console.log('ButtonModuleWatcher.js: watchContextBootstrap', this.contextBootstrapRegistry)
this.contextBootstrapWatchers.push(callable);
Object.values(this.contextBootstrapRegistry).forEach(callable);
}

View file

@ -119,7 +119,7 @@ class SavePaymentMethodsModule implements ModuleInterface {
),
),
);
} else if ( $funding_source === 'apple_pay' ) {
} elseif ( $funding_source && $funding_source === 'apple_pay' ) {
$data['payment_source'] = array(
'apple_pay' => array(
'stored_credential' => array(
@ -191,7 +191,8 @@ class SavePaymentMethodsModule implements ModuleInterface {
$wc_payment_tokens->create_payment_token_paypal(
$wc_order->get_customer_id(),
$token_id,
$payment_source->properties()->email_address ?? ''
$payment_source->properties()->email_address ?? '',
$payment_source->name()
);
}
}

View file

@ -69,13 +69,15 @@ class WooCommercePaymentTokens {
* @param int $customer_id The WC customer ID.
* @param string $token The PayPal payment token.
* @param string $email The PayPal customer email.
* @param string $payment_source The funding source.
*
* @return int
*/
public function create_payment_token_paypal(
int $customer_id,
string $token,
string $email
string $email,
string $payment_source = ''
): int {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
@ -94,6 +96,10 @@ class WooCommercePaymentTokens {
$payment_token_paypal->set_email( $email );
}
if ( $payment_source ) {
$payment_token_paypal->set_payment_source( $payment_source );
}
try {
$payment_token_paypal->save();
} catch ( Exception $exception ) {

View file

@ -29,6 +29,7 @@ class PaymentTokenPayPal extends WC_Payment_Token {
*/
protected $extra_data = array(
'email' => '',
'payment_source' => '',
);
/**
@ -48,4 +49,22 @@ class PaymentTokenPayPal extends WC_Payment_Token {
public function set_email( $email ) {
$this->add_meta_data( 'email', $email, true );
}
/**
* Get the payment source.
*
* @return string The payment source.
*/
public function get_payment_source() {
return $this->get_meta( 'payment_source' );
}
/**
* Set the payment source.
*
* @param string $payment_source The payment source.
*/
public function set_payment_source( string $payment_source ) {
$this->add_meta_data( 'payment_source', $payment_source, true );
}
}

View file

@ -86,6 +86,37 @@ class VaultingModule implements ModuleInterface {
}
);
add_filter(
'woocommerce_get_customer_payment_tokens',
/**
* Filter available payment tokens depending on context.
*
* @psalm-suppress MissingClosureParamType
* @psalm-suppress MissingClosureReturnType
*/
function( $tokens, $customer_id, $gateway_id ) {
if ( ! is_array( $tokens ) ) {
return $tokens;
}
// Exclude ApplePay tokens from payment pages.
if ( is_checkout() || is_cart() || is_product() ) {
foreach ( $tokens as $index => $token ) {
if (
$token instanceof PaymentTokenPayPal
&& $token->get_payment_source() === 'apple_pay'
) {
unset( $tokens[ $index ] );
}
}
}
return $tokens;
},
10,
3
);
add_filter(
'woocommerce_payment_methods_list_item',
/**
@ -100,8 +131,22 @@ class VaultingModule implements ModuleInterface {
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
assert( $payment_token instanceof PaymentTokenPayPal );
$item['method']['brand'] = $payment_token->get_email();
$email = $payment_token->get_email();
$payment_source = $payment_token->get_payment_source();
$brand_parts = array();
if ( $payment_source !== 'paypal' ) {
$brand_parts[] = ucwords( $payment_source );
}
if ( $email ) {
$brand_parts[] = $email;
} else {
$brand_parts[] = '#' . ( (string) $payment_token->get_id() );
}
$item['method']['brand'] = implode( ' / ', array_filter( $brand_parts ) );
return $item;
}