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

88.02% Statements 125/142
78.04% Branches 32/41
84% Functions 21/25
89.13% Lines 123/138

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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 38013x 13x 13x 13x   13x 13x   13x   13x 13x 13x                       13x                               13x 13x 13x       39x               39x 39x 39x 39x 39x 39x 39x 39x   39x             19x 3x   16x 3x   13x 13x     1x                     9x 9x 9x   9x   9x 1x       8x 1x     7x   7x 2x     5x 5x   5x     5x 5x   1x 1x       4x 1x       3x   3x 3x                       39x 3x   39x 39x 39x             9x   9x                     3x 3x 3x     3x     3x 3x 3x 3x 3x 3x 3x 3x   3x 3x   3x 3x                   5x 1x   4x     4x 4x       4x 4x             4x   1x   1x 1x           4x       4x                       4x   4x 4x 4x 1x 1x 1x                 4x     4x                   12x                                 6x 6x 5x                       1x               3x 3x       2x   1x 1x 1x                     3x   3x             2x 2x 2x         1x 1x   3x               5x   5x         5x   5x   5x          
import { DOCUMENT } from '@angular/common';
import { DestroyRef, Inject, Injectable, NgZone } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { filter, firstValueFrom, take } from 'rxjs';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { BasketView } from 'ish-core/models/basket/basket.model';
import { whenTruthy } from 'ish-core/utils/operators';
import { PaypalComponentsConfig } from 'ish-core/utils/paypal/adapters/paypal-adapters.builder';
import { PAYPAL_APPLE_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 {
  ApplePayConfig,
  ApplePayPaymentAuthorizedEvent,
  ApplePayPaymentRequest,
  ApplePaySessionInstance,
  ApplePaySessionStatic,
  PaypalApplePayComponent,
} from 'ish-core/utils/paypal/paypal-model/paypal-apple-pay.model';
import { ScriptLoaderService } from 'ish-core/utils/script-loader/script-loader.service';
 
// ApplePaySession is a global browser API, declared here for type safety
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const ApplePaySession: ApplePaySessionStatic;
 
/**
 * Representation of the PayPal SDK Apple Pay object, responsible for rendering the Apple Pay button
 * and handling the associated callbacks for order creation, approval, and error handling.
 *
 * The Apple Pay integration requires both the PayPal JavaScript SDK (with applepay component)
 * and the Apple Pay JavaScript SDK to work together.
 *
 * Life cycle of this class ends with destroying of parent component PaymentPaypalComponent.
 */
@Injectable()
export class PaypalApplePayAdapter {
  static readonly APPLE_PAY_SDK_URL = 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js';
  static readonly APPLE_PAY_API_VERSION = 4;
 
  private applePayConfig: ApplePayConfig;
  private paypalApplePay: PaypalApplePayComponent;
  private loading = false;
  private merchantId: string;
  private orderContext: PaypalOrderData;
  private currentBasket: BasketView;
  private applePayButton: HTMLElement | undefined;
  private buttonClickHandler: (() => void) | undefined;
 
  constructor(
    private ngZone: NgZone,
    private appFacade: AppFacade,
    private destroyRef: DestroyRef,
    private checkoutFacade: CheckoutFacade,
    private paypalConfigService: PaypalConfigService,
    private paypalDataTransferService: PaypalDataTransferService,
    private scriptLoaderService: ScriptLoaderService,
    @Inject(DOCUMENT) private document: Document
  ) {
    this.destroyRef.onDestroy(() => this.cleanup());
  }
 
  /**
   * Checks if Apple Pay is available in the current browser.
   */
  static isApplePayAvailable(): boolean {
    if (typeof ApplePaySession === 'undefined') {
      return false;
    }
    if (!ApplePaySession.canMakePayments()) {
      return false;
    }
    try {
      return ApplePaySession.supportsVersion(PaypalApplePayAdapter.APPLE_PAY_API_VERSION);
    } catch {
      // supportsVersion may throw if version is invalid
      return false;
    }
  }
 
  /**
   * Renders the Apple Pay button in the specified container.
   *
   * @param config - Configuration for the Apple Pay component
   * @returns Promise that resolves when the button is rendered
   */
  async renderApplePayButton(config: PaypalComponentsConfig): Promise<void> {
    const containerId = config.containerId;
    const container = this.document.getElementById(containerId);
    this.merchantId = config.merchantId;
 
    await this.loadApplePaySdk();
 
    if (!container) {
      return Promise.reject(new Error(`Container element '${containerId}' not found in DOM`));
    }
 
    // Check if Apple Pay is available in the browser
    if (!PaypalApplePayAdapter.isApplePayAvailable()) {
      return Promise.reject(new Error('Apple Pay is not available in this browser'));
    }
 
    const paypalObject = this.paypalConfigService.getPaypalComponent(config.paypalPaymentMethod);
 
    if (!paypalObject?.Applepay) {
      return Promise.reject(new Error(`PayPal Applepay not available on namespace '${config.scriptNamespace}'`));
    }
 
    return this.ngZone.run(async () => {
      try {
        // Initialize PayPal Apple Pay component
        this.paypalApplePay = paypalObject.Applepay();
 
        // Get Apple Pay configuration from PayPal
        try {
          this.applePayConfig = await this.paypalApplePay.config();
        } catch (configError) {
          console.error('PayPal Apple Pay configuration could not be estimated:', configError);
          return Promise.reject(configError);
        }
 
        // Check if Apple Pay is eligible for this merchant
        if (!this.applePayConfig.isEligible) {
          return Promise.reject(new Error('Apple Pay is not eligible for this merchant'));
        }
 
        // Pre-cache basket data for synchronous access in click handler
        await this.cacheBasketData();
 
        this.renderButton(container);
        return Promise.resolve();
      } catch (error) {
        console.error('Apple Pay initialization failed:', error);
        return Promise.reject(error);
      }
    });
  }
 
  /**
   * Cleanup resources to prevent memory leaks.
   */
  private cleanup(): void {
    if (this.applePayButton && this.buttonClickHandler) {
      this.applePayButton.removeEventListener('click', this.buttonClickHandler);
    }
    this.applePayButton?.remove();
    this.applePayButton = undefined;
    this.buttonClickHandler = undefined;
  }
 
  /**
   * Loads the Apple Pay JavaScript SDK.
   */
  private loadApplePaySdk(): Promise<void> {
    return firstValueFrom(this.scriptLoaderService.load(PaypalApplePayAdapter.APPLE_PAY_SDK_URL).pipe(take(1))).then(
      result => {
        Iif (!result.loaded) {
          throw new Error('Failed to load Apple Pay SDK');
        }
      }
    );
  }
 
  /**
   * Renders the Apple Pay button in the specified container.
   */
  private renderButton(container: HTMLElement): void {
    let locale = 'en';
    this.appFacade.currentLocale$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(currentLocale => {
      locale = currentLocale.split('_')[0];
    });
 
    const button = this.document.createElement('apple-pay-button');
 
    // Apply button styling
    button.setAttribute('id', 'btn-appl');
    button.setAttribute('locale', locale);
    button.setAttribute('buttonstyle', PAYPAL_APPLE_PAY_BUTTON_STYLING.buttonStyle);
    button.setAttribute('type', PAYPAL_APPLE_PAY_BUTTON_STYLING.buttonType);
    button.style.setProperty('--apple-pay-button-width', PAYPAL_APPLE_PAY_BUTTON_STYLING.width);
    button.style.setProperty('--apple-pay-button-height', PAYPAL_APPLE_PAY_BUTTON_STYLING.height);
    button.style.setProperty('--apple-pay-button-border-radius', PAYPAL_APPLE_PAY_BUTTON_STYLING.borderRadius);
    button.style.cursor = 'pointer';
 
    this.buttonClickHandler = () => this.ngZone.run(() => this.onApplePayButtonClicked());
    button.addEventListener('click', this.buttonClickHandler);
 
    this.applePayButton = button;
    container.appendChild(button);
  }
 
  /**
   * Handles the Apple Pay button click event.
   * Creates the payment request and opens the Apple Pay payment sheet.
   * IMPORTANT: ApplePaySession must be created synchronously from user gesture.
   * Order creation is kicked off without awaiting and resolved inside onvalidatemerchant.
   */
  private onApplePayButtonClicked(): void {
    if (this.loading) {
      return;
    }
    this.loading = true;
 
    // Kick off ICM order creation without awaiting – must not block synchronous session creation
    this.checkoutFacade.processPaypalOrderCreation();
    const orderContextPromise = firstValueFrom(this.paypalDataTransferService.paypalOrder$);
 
    // ApplePaySession MUST be created synchronously as part of the user gesture
    let session: ApplePaySessionInstance;
    try {
      session = new ApplePaySession(PaypalApplePayAdapter.APPLE_PAY_API_VERSION, this.getPaymentRequest());
    } catch (error) {
      this.loading = false;
      console.error('Error creating Apple Pay session:', error);
      return;
    }
 
    session.onvalidatemerchant = async (event: { validationURL: string }) => {
      // Ensure order context is resolved before proceeding (already assigned via .then() at session.begin())
      await orderContextPromise;
 
      Eif (this.orderContext.orderStatus === 'ERROR' || !this.orderContext.paypalOrderId) {
        return;
      }
 
      await this.onValidateMerchant(event.validationURL, session);
    };
 
    session.onpaymentauthorized = async (event: ApplePayPaymentAuthorizedEvent) => {
      await this.onPaymentAuthorized(event, session);
    };
 
    session.oncancel = async () => {
      this.loading = false;
      if (!this.orderContext?.orderId) {
        return;
      }
      try {
        await this.continueICMOrderCreation(this.orderContext.orderId);
      } catch (error) {
        console.error('Error during ICM order continuation after Apple Pay cancellation:', error);
      }
    };
 
    session.begin();
 
    orderContextPromise.then(context => {
      this.orderContext = context;
      if (this.orderContext.orderStatus === 'ERROR') {
        console.error('Order creation failed or no PayPal order ID available, aborting Apple Pay session');
        this.loading = false;
        session.abort();
      }
    });
  }
 
  /**
   * Pre-caches basket data for synchronous access in click handler.
   */
  private async cacheBasketData(): Promise<void> {
    this.currentBasket = await firstValueFrom(
      this.checkoutFacade.basket$.pipe(
        whenTruthy(),
        filter(b => !!b),
        take(1)
      )
    );
  }
 
  /**
   * Creates the payment request using current basket data.
   */
  private getPaymentRequest(): ApplePayPaymentRequest {
    return {
      countryCode: this.applePayConfig.countryCode,
      currencyCode: this.currentBasket.totals?.total?.currency || 'USD',
      merchantCapabilities: this.applePayConfig.merchantCapabilities,
      supportedNetworks: this.applePayConfig.supportedNetworks,
      total: {
        label: this.merchantId,
        amount: this.currentBasket.totals?.total?.gross?.toString() || '0',
        type: 'final',
      },
    };
  }
 
  /**
   * Maps the billing contact data from the current basket to the format expected by Apple Pay.
   */
  private mapBillingContactData() {
    const address = this.currentBasket.invoiceToAddress;
    if (address) {
      return {
        givenName: address.firstName,
        familyName: address.lastName,
        emailAddress: address.email,
        phoneNumber: address.phoneHome || address.phoneMobile || address.phoneBusiness,
        addressLines: [address.addressLine1, address.addressLine2, address.addressLine3].filter(Boolean),
        locality: address.city,
        administrativeArea: address.mainDivisionCode || address.mainDivision,
        postalCode: address.postalCode,
        countryCode: address.countryCode,
      };
    }
    return {};
  }
 
  /**
   * Handles the merchant validation callback from Apple Pay.
   * This is called when Apple Pay needs to verify the merchant.
   */
  private async onValidateMerchant(validationURL: string, session: ApplePaySessionInstance) {
    try {
      const payload = await this.paypalApplePay.validateMerchant({
        validationUrl: validationURL,
        domainName: window.location.hostname,
      });
      session.completeMerchantValidation(payload.merchantSession);
    } catch {
      await this.continueICMOrderCreation(this.orderContext.orderId);
      session.abort();
      this.loading = false;
    }
  }
 
  /**
   * Handles the payment authorization callback from Apple Pay.
   */
  private async onPaymentAuthorized(
    event: ApplePayPaymentAuthorizedEvent,
    session: ApplePaySessionInstance
  ): Promise<void> {
    try {
      // Confirm the order with PayPal
      await this.paypalApplePay.confirmOrder({
        orderId: this.orderContext.paypalOrderId,
        token: event.payment.token,
        billingContact: this.mapBillingContactData(),
      });
 
      // Complete the payment
      const result = await this.continueICMOrderCreation(this.orderContext.orderId);
      if (result.status === 'SUCCESS') {
        session.completePayment({ status: ApplePaySession.STATUS_SUCCESS });
      } else E{
        session.completePayment({ status: ApplePaySession.STATUS_FAILURE });
      }
    } catch {
      this.checkoutFacade.processPaypalOrderCreation(this.orderContext.orderId);
      session.completePayment({ status: ApplePaySession.STATUS_FAILURE });
    } finally {
      this.loading = false;
    }
  }
 
  /**
   * ICM order creation needs to be continued after Apple Pay authorization.
   */
  private async continueICMOrderCreation(orderId: string): Promise<{ status: 'SUCCESS' | 'CANCELLED' | 'ERROR' }> {
    const orderContextPromise = firstValueFrom(
      this.paypalDataTransferService.paypalOrder$.pipe(
        filter(order => !!order?.orderStatus),
        take(1)
      )
    );
 
    this.checkoutFacade.processPaypalOrderCreation(orderId);
 
    const orderContext = await orderContextPromise;
 
    return {
      status: orderContext.orderStatus,
    };
  }
}