All files / src/app/core/utils/paypal/adapters/paypal-google-pay paypal-google-pay.adapter.ts

90.9% Statements 90/99
80% Branches 28/35
81.81% Functions 18/22
91.75% Lines 89/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 31213x 13x 13x   13x 13x   13x 13x 13x                       13x                       13x 13x 13x 13x                         30x     30x 30x 30x 30x 30x 30x 30x   30x   30x       30x                     9x 9x   9x 1x     8x   8x 2x     6x 6x 6x   5x 1x     4x 4x   1x           6x 6x       6x     6x 6x   1x     5x   5x           5x             6x     6x                   5x       5x   5x 5x                           4x 4x                 4x 4x                   2x   2x     2x     2x     1x           1x                 10x   10x         10x                                   5x 5x         4x 2x 2x   1x   1x     1x   1x     1x                 5x   5x     5x   4x       4x             8x         8x   8x         8x   8x 8x   8x          
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, NgZone } from '@angular/core';
import { filter, firstValueFrom, take } from 'rxjs';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { PaypalComponentsConfig } from 'ish-core/utils/paypal/adapters/paypal-adapters.builder';
import { PAYPAL_GOOGLE_PAY_BUTTON_STYLING } from 'ish-core/utils/paypal/adapters/paypal-adapters.styling';
import { PaypalConfigService } from 'ish-core/utils/paypal/paypal-config/paypal-config.service';
import {
  PaypalDataTransferService,
  PaypalOrderData,
} from 'ish-core/utils/paypal/paypal-data-transfer/paypal-data-transfer.service';
import {
  GooglePayButton,
  GooglePayConfig,
  GooglePayPaymentAuthorizationResult,
  GooglePayPaymentClient,
  PaypalGooglePayComponent,
} from 'ish-core/utils/paypal/paypal-model/paypal-google-pay.model';
import { PaypalComponent } from 'ish-core/utils/paypal/paypal-model/paypal.model';
import { ScriptLoaderService } from 'ish-core/utils/script-loader/script-loader.service';
 
/**
 * Representation of the PayPal SDK Google Pay object, responsible for rendering the Google Pay button
 * and handling the associated callbacks for order creation, approval, and error handling.
 *
 * The Google Pay integration requires both the PayPal JavaScript SDK (with googlepay component)
 * and the Google Pay JavaScript SDK to work together.
 *
 * Life cycle of this class ends with destroying of parent component PaymentPaypalComponent.
 */
@Injectable()
export class PaypalGooglePayAdapter {
  static readonly GOOGLE_PAY_SDK_URL = 'https://pay.google.com/gp/p/js/pay.js';
  static readonly GOOGLE_PAY_API_VERSION_MAJOR = 2;
  static readonly GOOGLE_PAY_API_VERSION_MINOR = 0;
 
  private googlePaymentClient: GooglePayPaymentClient;
  private googlePayConfig: GooglePayConfig;
  private paypalGooglePay: PaypalGooglePayComponent;
 
  /**
   * Holds order context during the payment flow.
   * Set in startOrderCreation(), used in callbacks, cleared after flow completes.
   * Required because Google Pay's onPaymentAuthorized callback doesn't support custom parameters.
   */
  private orderContext: PaypalOrderData | undefined;
  // default locale for Google Pay button, will be updated to current locale on initialization
  private buttonLocale = 'en';
 
  constructor(
    private ngZone: NgZone,
    private appFacade: AppFacade,
    private checkoutFacade: CheckoutFacade,
    private paypalConfigService: PaypalConfigService,
    private paypalDataTransferService: PaypalDataTransferService,
    private scriptLoaderService: ScriptLoaderService,
    @Inject(DOCUMENT) private document: Document
  ) {
    this.appFacade.currentLocale$
      .pipe(
        filter(locale => !!locale),
        take(1)
      )
      .subscribe(locale => {
        this.buttonLocale = locale.trim().split('_')[0];
      });
  }
 
  /**
   * Renders the Google Pay button in the specified container.
   *
   * @param config - Configuration for the Google Pay component
   * @returns Promise that resolves when the button is rendered
   */
  async renderGooglePayButton(config: PaypalComponentsConfig): Promise<void> {
    const containerId = config.containerId;
    const container = this.document.getElementById(containerId);
 
    if (!container) {
      return Promise.reject(new Error(`Container element '${containerId}' not found in DOM`));
    }
 
    const paypalObject = this.paypalConfigService.getPaypalComponent(config.paypalPaymentMethod);
 
    if (!paypalObject?.Googlepay) {
      return Promise.reject(new Error(`PayPal Googlepay not available on namespace '${config.scriptNamespace}'`));
    }
 
    return this.ngZone.run(async () => {
      try {
        const isGooglePayApplicable = await this.isGooglePayApplicable(paypalObject, true);
 
        if (!isGooglePayApplicable) {
          return Promise.reject('Google Pay is not available for the current user or device');
        }
 
        this.renderButton(container);
        return Promise.resolve();
      } catch (error) {
        return Promise.reject(error);
      }
    });
  }
 
  private async isGooglePayApplicable(paypalObject: PaypalComponent, loadScript?: boolean): Promise<boolean> {
    Eif (loadScript) {
      await this.loadGooglePaySdk();
    }
 
    // Initialize PayPal Google Pay component
    this.paypalGooglePay = paypalObject.Googlepay();
 
    // Get Google Pay configuration from PayPal
    try {
      this.googlePayConfig = await this.paypalGooglePay.config();
    } catch (configError) {
      return Promise.reject(configError);
    }
 
    await this.createGooglePaymentsClient();
    // Check if Google Pay is available
    return await this.googlePaymentClient
      .isReadyToPay({
        apiVersion: PaypalGooglePayAdapter.GOOGLE_PAY_API_VERSION_MAJOR,
        apiVersionMinor: PaypalGooglePayAdapter.GOOGLE_PAY_API_VERSION_MINOR,
        allowedPaymentMethods: this.googlePayConfig.allowedPaymentMethods,
      })
      .then(result => result.result);
  }
 
  /**
   * Loads the Google Pay JavaScript SDK.
   */
  private async loadGooglePaySdk(): Promise<void> {
    const result = await firstValueFrom(
      this.scriptLoaderService.load(PaypalGooglePayAdapter.GOOGLE_PAY_SDK_URL).pipe(take(1))
    );
    Iif (!result.loaded) {
      throw new Error('Failed to load Google Pay SDK');
    }
  }
 
  /**
   * Get a Google Payments Client instance. In case of multiple calls, the same instance will be returned to avoid unnecessary re-initialization.
   * Must be called after googlePayConfig and googlePayEnvironment are loaded.
   */
  private async createGooglePaymentsClient() {
    Iif (this.googlePaymentClient) {
      return;
    }
    // Load Google Pay environment setting
    const paypalSettings = await firstValueFrom(this.appFacade.paypalClientConfig$.pipe(take(1)));
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const googlePayments = (window as any).google.payments.api;
    this.googlePaymentClient = new googlePayments.PaymentsClient({
      environment: paypalSettings.googlePayEnvironment,
      merchantInfo: this.googlePayConfig?.merchantInfo,
      paymentDataCallbacks: {
        onPaymentAuthorized: (paymentData: { paymentMethodData: unknown }) =>
          this.onPaymentAuthorizedCallback(paymentData.paymentMethodData),
      },
    });
  }
 
  /**
   * Renders the Google Pay button in the specified container.
   */
  private renderButton(container: HTMLElement): void {
    container.replaceChildren();
    const buttonConfig: GooglePayButton = {
      onClick: () => {
        this.ngZone.run(() => this.onGooglePayButtonClicked());
      },
      allowedPaymentMethods: this.googlePayConfig.allowedPaymentMethods,
      ...(PAYPAL_GOOGLE_PAY_BUTTON_STYLING as GooglePayButton),
      buttonLocale: this.buttonLocale,
    };
 
    const button = this.googlePaymentClient.createButton(buttonConfig);
    container.appendChild(button);
  }
 
  /**
   * Handles the Google Pay button click event.
   * Creates the payment data request and opens the Google Pay payment sheet.
   * Order creation starts immediately when button is clicked to have PayPal order ID ready
   * when user completes Google Pay authentication.
   */
  private async onGooglePayButtonClicked(): Promise<void> {
    try {
      // Step 1: Start order creation and wait for orderId to be available
      await this.startOrderCreation();
 
      // Step 2: Create payment data request with basket information
      const paymentDataRequest = await this.getPaymentDataRequest();
 
      // Step 3: Load payment data - this triggers onPaymentAuthorized callback
      await this.googlePaymentClient.loadPaymentData(paymentDataRequest);
    } catch (error) {
      // User closed the Google Pay popup without completing payment
      Eif (
        error &&
        typeof error === 'object' &&
        (error as { statusCode?: string }).statusCode === 'CANCELED' &&
        this.orderContext?.orderId
      ) {
        this.continueICMOrderCreation();
      }
    }
  }
 
  /**
   * Creates the payment data request for Google Pay.
   */
  private async getPaymentDataRequest() {
    const basket = await firstValueFrom(
      this.checkoutFacade.basket$.pipe(
        filter(b => !!b),
        take(1)
      )
    );
 
    return {
      apiVersion: PaypalGooglePayAdapter.GOOGLE_PAY_API_VERSION_MAJOR,
      apiVersionMinor: PaypalGooglePayAdapter.GOOGLE_PAY_API_VERSION_MINOR,
      allowedPaymentMethods: this.googlePayConfig.allowedPaymentMethods,
      merchantInfo: this.googlePayConfig.merchantInfo,
      transactionInfo: {
        currencyCode: basket.totals?.total?.currency || 'USD',
        totalPriceStatus: 'FINAL',
        totalPrice: basket.totals?.total?.gross?.toString() || '0',
      },
      callbackIntents: ['PAYMENT_AUTHORIZATION'],
    };
  }
 
  /**
   * Handles the payment authorization callback from Google Pay.
   */
  private async onPaymentAuthorizedCallback(paymentMethodData: unknown): Promise<GooglePayPaymentAuthorizationResult> {
    try {
      const confirmOrderResponse = await this.paypalGooglePay.confirmOrder({
        orderId: this.orderContext?.paypalOrderId,
        paymentMethodData,
      });
 
      if (confirmOrderResponse.status === 'APPROVED') {
        return await this.continueICMOrderCreation();
      } else if (confirmOrderResponse.status === 'PAYER_ACTION_REQUIRED') {
        // Handle 3D Secure authentication using .then() pattern as recommended
        this.paypalGooglePay
          .initiatePayerAction({ orderId: this.orderContext?.paypalOrderId })
          .then(() => this.continueICMOrderCreation())
          .catch(() => this.continueICMOrderCreation());
        // Return immediately so Google Pay sheet closes
        return { transactionState: 'SUCCESS' };
      } else {
        return await this.continueICMOrderCreation();
      }
    } catch {
      return await this.continueICMOrderCreation();
    }
  }
 
  /**
   * Start ICM order creation
   */
  private async startOrderCreation(): Promise<void> {
    // Subscribe first, then dispatch action to avoid race condition with Subject
    const orderContentPromise = firstValueFrom(this.paypalDataTransferService.paypalOrder$.pipe(take(1)));
 
    this.checkoutFacade.processPaypalOrderCreation();
 
    // Wait for order content to be available before opening Google Pay sheet
    const orderContent = await orderContentPromise;
 
    Iif (orderContent.orderStatus === 'ERROR' || !orderContent.orderId || !orderContent.paypalOrderId) {
      throw new Error('Error during order creation');
    }
 
    this.orderContext = orderContent;
  }
 
  /**
   * ICM order creation needs to be continued after Google Pay authorization, regardless of the result of the PayPal order confirmation.
   */
  private async continueICMOrderCreation(): Promise<GooglePayPaymentAuthorizationResult> {
    Iif (!this.orderContext?.orderId) {
      this.orderContext = undefined;
      return { transactionState: 'ERROR' };
    }
 
    const orderContextPromise = firstValueFrom(
      this.paypalDataTransferService.paypalOrder$.pipe(
        filter(order => !!order?.orderStatus),
        take(1)
      )
    );
 
    this.checkoutFacade.processPaypalOrderCreation(this.orderContext.orderId);
 
    const orderContext = await orderContextPromise;
    this.orderContext = undefined;
 
    return {
      transactionState: orderContext.orderStatus === 'SUCCESS' ? 'SUCCESS' : 'ERROR',
    };
  }
}