Add guest customer id (WIP)

This commit is contained in:
dinamiko 2021-12-24 12:37:06 +01:00
parent ece3ef95d3
commit d69f1b5a9c
5 changed files with 18 additions and 96 deletions

View file

@ -95,10 +95,14 @@ class IdentityToken {
),
);
if (
$customer_id
&& ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) )
( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) )
&& defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION
) {
if($customer_id === 0) {
$customer_id = uniqid();
WC()->session->set('ppcp_guest_customer_id', $customer_id);
}
$args['body'] = wp_json_encode( array( 'customer_id' => $this->prefix . $customer_id) );
}

View file

@ -93,6 +93,11 @@ class PaymentTokenEndpoint {
$bearer = $this->bearer->bearer();
$customer_id = $this->prefix . $id;
$guest_customer_id_meta = get_user_meta( $id, 'ppcp_guest_customer_id', true );
if($guest_customer_id_meta) {
$customer_id = $this->prefix . $guest_customer_id_meta;
}
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id;
$args = array(
'method' => 'GET',

View file

@ -590,7 +590,7 @@ class SmartButton implements SmartButtonInterface {
return false;
}
return is_user_logged_in();
return true;
}
/**
@ -637,7 +637,7 @@ class SmartButton implements SmartButtonInterface {
$localize = array(
'script_attributes' => $this->attributes(),
'data_client_id' => array(
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token(),
'set_attribute' => $this->can_save_vault_token(),
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
'user' => get_current_user_id(),

View file

@ -98,6 +98,10 @@ class VaultingModule implements ModuleInterface {
}
);
add_action('woocommerce_created_customer', function($customer_id) {
update_user_meta($customer_id, 'ppcp_guest_customer_id', WC()->session->get('ppcp_guest_customer_id'));
});
$asset_loader = $container->get( 'vaulting.assets.myaccount-payments' );
add_action(
'wp_enqueue_scripts',

View file

@ -174,97 +174,6 @@ trait ProcessPaymentTrait {
try {
if ( $this->order_processor->process( $wc_order ) ) {
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
$this->logger->info( "Trying to save payment for subscription parent order #{$order_id}." );
$tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() );
if ( $tokens ) {
$this->logger->info( "Payment for subscription parent order #{$order_id} was saved correctly." );
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
}
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved." );
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
$order = $this->order_endpoint->order( $paypal_order_id );
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$this->logger->debug(
sprintf(
'Trying to void order %1$s, payments: %2$s.',
$order->id(),
wp_json_encode( $payments->to_array() )
)
);
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$this->payments_endpoint->void( $authorization );
}
$this->logger->debug(
sprintf(
'Order %1$s voided successfully.',
$order->id()
)
);
$error_message = __( 'Could not process order because it was not possible to save the payment.', 'woocommerce-paypal-payments' );
$wc_order->update_status( 'failed', $error_message );
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach ( $subscriptions as $key => $subscription ) {
if ( $subscription->get_parent_id() === $order_id ) {
try {
$subscription->update_status( 'cancelled' );
break;
} catch ( Exception $exception ) {
$this->logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
// Adds retry counter meta to avoid duplicate invoice id error on consequent tries.
$wc_order->update_meta_data( 'ppcp-retry', (int) $wc_order->get_meta( 'ppcp-retry' ) + 1 );
$wc_order->save_meta_data();
$this->session_handler->destroy_session_data();
wc_add_notice( $error_message, 'error' );
return $failure_data;
}
WC()->cart->empty_cart();
$this->session_handler->destroy_session_data();