From d69f1b5a9c02942b38d3e42326d6e5541d1c732a Mon Sep 17 00:00:00 2001 From: dinamiko Date: Fri, 24 Dec 2021 12:37:06 +0100 Subject: [PATCH] Add guest customer id (WIP) --- .../src/Endpoint/IdentityToken.php | 10 +- .../src/Endpoint/PaymentTokenEndpoint.php | 5 + .../ppcp-button/src/Assets/SmartButton.php | 4 +- modules/ppcp-vaulting/src/VaultingModule.php | 4 + .../src/Gateway/ProcessPaymentTrait.php | 91 ------------------- 5 files changed, 18 insertions(+), 96 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/IdentityToken.php b/modules/ppcp-api-client/src/Endpoint/IdentityToken.php index 2e73b1af9..740219a3c 100644 --- a/modules/ppcp-api-client/src/Endpoint/IdentityToken.php +++ b/modules/ppcp-api-client/src/Endpoint/IdentityToken.php @@ -95,11 +95,15 @@ 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 ) { - $args['body'] = wp_json_encode( array( 'customer_id' => $this->prefix . $customer_id ) ); + 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) ); } $response = $this->request( $url, $args ); diff --git a/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php b/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php index e3c8ed903..60750fa44 100644 --- a/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php @@ -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', diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index aaa8cc809..e7b9bd348 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -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(), diff --git a/modules/ppcp-vaulting/src/VaultingModule.php b/modules/ppcp-vaulting/src/VaultingModule.php index a2090a2c1..0b2e039cb 100644 --- a/modules/ppcp-vaulting/src/VaultingModule.php +++ b/modules/ppcp-vaulting/src/VaultingModule.php @@ -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', diff --git a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php index 7e974664c..944594f36 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php @@ -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();