mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-03 08:37:53 +08:00
ApplePay Vaulting Integration
This commit is contained in:
parent
827bd2568d
commit
96b83c9d0d
7 changed files with 86 additions and 12 deletions
|
@ -209,11 +209,15 @@ class ApplepayButton {
|
||||||
/**
|
/**
|
||||||
* Show Apple Pay payment sheet when Apple Pay payment button is clicked
|
* 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);
|
this.log('onButtonClick', this.context);
|
||||||
|
|
||||||
const paymentRequest = this.paymentRequest();
|
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.
|
// Trigger woocommerce validation if we are in the checkout page.
|
||||||
if (this.context === 'checkout') {
|
if (this.context === 'checkout') {
|
||||||
const checkoutFormSelector = 'form.woocommerce-checkout';
|
const checkoutFormSelector = 'form.woocommerce-checkout';
|
||||||
|
|
|
@ -87,7 +87,7 @@ const PayPalComponent = ({
|
||||||
bn_code: '',
|
bn_code: '',
|
||||||
context: config.scriptData.context,
|
context: config.scriptData.context,
|
||||||
payment_method: 'ppcp-gateway',
|
payment_method: 'ppcp-gateway',
|
||||||
funding_source: data.paymentSource,
|
funding_source: window.ppcpFundingSource ?? 'paypal',
|
||||||
createaccount: false
|
createaccount: false
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,7 +7,6 @@ class ButtonModuleWatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
watchContextBootstrap(callable) {
|
watchContextBootstrap(callable) {
|
||||||
console.log('ButtonModuleWatcher.js: watchContextBootstrap', this.contextBootstrapRegistry)
|
|
||||||
this.contextBootstrapWatchers.push(callable);
|
this.contextBootstrapWatchers.push(callable);
|
||||||
Object.values(this.contextBootstrapRegistry).forEach(callable);
|
Object.values(this.contextBootstrapRegistry).forEach(callable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
$data['payment_source'] = array(
|
||||||
'apple_pay' => array(
|
'apple_pay' => array(
|
||||||
'stored_credential' => array(
|
'stored_credential' => array(
|
||||||
|
@ -191,7 +191,8 @@ class SavePaymentMethodsModule implements ModuleInterface {
|
||||||
$wc_payment_tokens->create_payment_token_paypal(
|
$wc_payment_tokens->create_payment_token_paypal(
|
||||||
$wc_order->get_customer_id(),
|
$wc_order->get_customer_id(),
|
||||||
$token_id,
|
$token_id,
|
||||||
$payment_source->properties()->email_address ?? ''
|
$payment_source->properties()->email_address ?? '',
|
||||||
|
$payment_source->name()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,13 +69,15 @@ class WooCommercePaymentTokens {
|
||||||
* @param int $customer_id The WC customer ID.
|
* @param int $customer_id The WC customer ID.
|
||||||
* @param string $token The PayPal payment token.
|
* @param string $token The PayPal payment token.
|
||||||
* @param string $email The PayPal customer email.
|
* @param string $email The PayPal customer email.
|
||||||
|
* @param string $payment_source The funding source.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function create_payment_token_paypal(
|
public function create_payment_token_paypal(
|
||||||
int $customer_id,
|
int $customer_id,
|
||||||
string $token,
|
string $token,
|
||||||
string $email
|
string $email,
|
||||||
|
string $payment_source = ''
|
||||||
): int {
|
): int {
|
||||||
|
|
||||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
|
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
|
||||||
|
@ -94,6 +96,10 @@ class WooCommercePaymentTokens {
|
||||||
$payment_token_paypal->set_email( $email );
|
$payment_token_paypal->set_email( $email );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $payment_source ) {
|
||||||
|
$payment_token_paypal->set_payment_source( $payment_source );
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$payment_token_paypal->save();
|
$payment_token_paypal->save();
|
||||||
} catch ( Exception $exception ) {
|
} catch ( Exception $exception ) {
|
||||||
|
|
|
@ -29,6 +29,7 @@ class PaymentTokenPayPal extends WC_Payment_Token {
|
||||||
*/
|
*/
|
||||||
protected $extra_data = array(
|
protected $extra_data = array(
|
||||||
'email' => '',
|
'email' => '',
|
||||||
|
'payment_source' => '',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,4 +49,22 @@ class PaymentTokenPayPal extends WC_Payment_Token {
|
||||||
public function set_email( $email ) {
|
public function set_email( $email ) {
|
||||||
$this->add_meta_data( 'email', $email, true );
|
$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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
add_filter(
|
||||||
'woocommerce_payment_methods_list_item',
|
'woocommerce_payment_methods_list_item',
|
||||||
/**
|
/**
|
||||||
|
@ -100,8 +131,22 @@ class VaultingModule implements ModuleInterface {
|
||||||
|
|
||||||
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
|
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
|
||||||
assert( $payment_token instanceof PaymentTokenPayPal );
|
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;
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue