mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 06:52:50 +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
|
||||
*/
|
||||
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';
|
||||
|
|
|
@ -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
|
||||
}),
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -119,14 +119,14 @@ 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(
|
||||
'payment_initiator' => 'CUSTOMER',
|
||||
'payment_type' => 'RECURRING',
|
||||
),
|
||||
'attributes' => array(
|
||||
'attributes' => array(
|
||||
'vault' => array(
|
||||
'store_in_vault' => 'ON_SUCCESS',
|
||||
),
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,16 +66,18 @@ class WooCommercePaymentTokens {
|
|||
/**
|
||||
* Creates a WC Payment Token for PayPal payment.
|
||||
*
|
||||
* @param int $customer_id The WC customer ID.
|
||||
* @param string $token The PayPal payment token.
|
||||
* @param string $email The PayPal customer email.
|
||||
* @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 ) {
|
||||
|
|
|
@ -28,7 +28,8 @@ class PaymentTokenPayPal extends WC_Payment_Token {
|
|||
* @var string[]
|
||||
*/
|
||||
protected $extra_data = array(
|
||||
'email' => '',
|
||||
'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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue