From d33ca00ac7cd7e0bd6cd12723c372956eb6a237f Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 3 Jan 2022 15:15:33 +0100 Subject: [PATCH] Fix CI errors --- .../ppcp-api-client/src/Endpoint/IdentityToken.php | 6 +++--- .../src/Endpoint/PaymentTokenEndpoint.php | 12 ++++++------ modules/ppcp-vaulting/src/VaultingModule.php | 13 ++++++++++--- modules/ppcp-wc-gateway/services.php | 2 +- .../src/Checkout/DisableGateways.php | 4 ++-- .../ApiClient/Endpoint/PaymentTokenEndpointTest.php | 11 +++++++++++ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/modules/ppcp-api-client/src/Endpoint/IdentityToken.php b/modules/ppcp-api-client/src/Endpoint/IdentityToken.php index 740219a3c..ae12fdfca 100644 --- a/modules/ppcp-api-client/src/Endpoint/IdentityToken.php +++ b/modules/ppcp-api-client/src/Endpoint/IdentityToken.php @@ -98,12 +98,12 @@ class IdentityToken { ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) ) && defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION ) { - if($customer_id === 0) { + if ( 0 === $customer_id ) { $customer_id = uniqid(); - WC()->session->set('ppcp_guest_customer_id', $customer_id); + WC()->session->set( 'ppcp_guest_customer_id', $customer_id ); } - $args['body'] = wp_json_encode( array( 'customer_id' => $this->prefix . $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 60750fa44..8b27ec969 100644 --- a/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php @@ -92,14 +92,14 @@ class PaymentTokenEndpoint { public function for_user( int $id ): array { $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; + $customer_id = $this->prefix . $id; + $guest_customer_id = get_user_meta( $id, 'ppcp_guest_customer_id', true ); + if ( $guest_customer_id ) { + $customer_id = $this->prefix . $guest_customer_id; } - $url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id; - $args = array( + $url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id; + $args = array( 'method' => 'GET', 'headers' => array( 'Authorization' => 'Bearer ' . $bearer->token(), diff --git a/modules/ppcp-vaulting/src/VaultingModule.php b/modules/ppcp-vaulting/src/VaultingModule.php index 0b2e039cb..bedaec1bc 100644 --- a/modules/ppcp-vaulting/src/VaultingModule.php +++ b/modules/ppcp-vaulting/src/VaultingModule.php @@ -98,9 +98,16 @@ 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')); - }); + $subscription_helper = $container->get( 'subscription.helper' ); + add_action( + 'woocommerce_created_customer', + function( $customer_id ) use ( $subscription_helper ) { + $guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' ); + if ( $guest_customer_id && $subscription_helper->cart_contains_subscription() ) { + 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( diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 1a34c36ef..5de2ff87e 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -120,7 +120,7 @@ return array( 'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways { $session_handler = $container->get( 'session.handler' ); $settings = $container->get( 'wcgateway.settings' ); - return new DisableGateways( $session_handler, $settings); + return new DisableGateways( $session_handler, $settings ); }, 'wcgateway.is-wc-payments-page' => static function ( ContainerInterface $container ): bool { $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; diff --git a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php index 8d3123865..64520b38c 100644 --- a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php +++ b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php @@ -44,8 +44,8 @@ class DisableGateways { ContainerInterface $settings ) { - $this->session_handler = $session_handler; - $this->settings = $settings; + $this->session_handler = $session_handler; + $this->settings = $settings; } /** diff --git a/tests/PHPUnit/ApiClient/Endpoint/PaymentTokenEndpointTest.php b/tests/PHPUnit/ApiClient/Endpoint/PaymentTokenEndpointTest.php index 7f1b8fd7c..032da9793 100644 --- a/tests/PHPUnit/ApiClient/Endpoint/PaymentTokenEndpointTest.php +++ b/tests/PHPUnit/ApiClient/Endpoint/PaymentTokenEndpointTest.php @@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\TestCase; use Mockery; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use function Brain\Monkey\Functions\expect; +use function Brain\Monkey\Functions\when; class PaymentTokenEndpointTest extends TestCase { @@ -73,6 +74,8 @@ class PaymentTokenEndpointTest extends TestCase $this->logger->shouldReceive('debug'); + when('get_user_meta')->justReturn(''); + $result = $this->sut->for_user($id); $this->assertInstanceOf(PaymentToken::class, $result[0]); @@ -96,6 +99,8 @@ class PaymentTokenEndpointTest extends TestCase $this->logger->shouldReceive('log'); $this->logger->shouldReceive('debug'); + when('get_user_meta')->justReturn(''); + $this->expectException(RuntimeException::class); $this->sut->for_user($id); } @@ -123,6 +128,8 @@ class PaymentTokenEndpointTest extends TestCase $this->logger->shouldReceive('log'); $this->logger->shouldReceive('debug'); + when('get_user_meta')->justReturn(''); + $this->expectException(PayPalApiException::class); $this->sut->for_user($id); } @@ -185,6 +192,10 @@ class PaymentTokenEndpointTest extends TestCase $prefix = $this->prefix; expect('wp_remote_get')->andReturnUsing( function ($url, $args) use ($rawResponse, $host, $prefix, $id) { + // echo $url; // https://example.com/v2/vault/payment-tokens/?customer_id=prefixabc123 + // https://example.com/v2/vault/payment-tokens/?customer_id=prefix1 + // https://example.com/v2/vault/payment-tokens/?customer_id=prefixabc123 + echo $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id; if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id) { return false; }