All files / src/app/pages/checkout-payment/payment-payone-creditcard payment-payone-creditcard.component.ts

58.82% Statements 30/51
60.71% Branches 17/28
50% Functions 7/14
60% Lines 30/50

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 1861x 1x                             1x 1x     1x 1x                       1x 6x   6x     6x 6x 6x                     6x   6x 6x   6x         6x                         4x       4x                                         6x                                                                         1x                                             2x 1x                 2x       12x 2x 2x   2x 1x     11x      
import { DOCUMENT } from '@angular/common';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  DestroyRef,
  EventEmitter,
  Inject,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  Renderer2,
  inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
 
import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { PaymentMethod } from 'ish-core/models/payment-method/payment-method.model';
import { ScriptLoaderService } from 'ish-core/utils/script-loader/script-loader.service';
 
// spell-checker: disable
// allows access to Payone js functionality
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let Payone: any;
 
@Component({
  selector: 'ish-payment-payone-creditcard',
  templateUrl: './payment-payone-creditcard.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class PaymentPayoneCreditcardComponent implements OnChanges, OnDestroy, OnInit {
  payoneCreditCardForm = new FormGroup({});
 
  private renderer = inject(Renderer2);
 
  constructor(
    protected scriptLoader: ScriptLoaderService,
    protected cd: ChangeDetectorRef,
    @Inject(DOCUMENT) private document: Document
  ) {}
 
  /**
   * payone payment method, needed to get configuration parameters
   */
  @Input({ required: true }) paymentMethod: PaymentMethod;
 
  /**
   * should be set to true by the parent, if component is visible
   */
  @Input() activated = false;
 
  @Output() cancelPayment = new EventEmitter<void>();
  @Output() submitPayment = new EventEmitter<{ parameters: Attribute<string>[]; saveAllowed: boolean }>();
 
  private destroyRef = inject(DestroyRef);
 
  /**
   * flag to make sure that the init script is executed only once
   */
  private scriptLoaded = false;
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private iframes: any;
 
  /**
   *  field for generic error message
   * */
  generalErrorMessage: string;
 
  ngOnInit() {
    // keep reference for payone cc component
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const thisComp = this;
 
    // register helper function to call the callback function of this component
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (window as any).payoneCreditCardCallback = function (response: {
      status: string;
      pseudocardpan: string;
      truncatedcardpan: string;
    }) {
      thisComp.submitCallback(response);
    };
  }
 
  /**
   * load payone script if component is shown
   */
  ngOnChanges() {
    Iif (this.paymentMethod) {
      this.loadScript();
    }
  }
 
  ngOnDestroy() {
    // unregister helper function again
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (window as any).payoneCreditCardCallback = undefined;
  }
 
  private getParamValue(name: string, errorMessage: string): string {
    const parameter = this.paymentMethod.hostedPaymentPageParameters.find(param => param.name === name);
    Iif (!parameter?.value) {
      this.generalErrorMessage = errorMessage;
      return;
    }
    return parameter.value;
  }
 
  private loadScript() {
    // load script only once if component becomes visible
    Iif (this.activated && !this.scriptLoaded) {
      const request = JSON.parse(this.getParamValue('request', 'checkout.credit_card.config.error.notFound'));
      const config = JSON.parse(this.getParamValue('config', 'checkout.credit_card.config.error.notFound'));
 
      this.scriptLoaded = true;
      this.scriptLoader
        .load('https://secure.pay1.de/client-api/js/v1/payone_hosted_min.js')
        .pipe(takeUntilDestroyed(this.destroyRef))
        .subscribe(() => {
          // append localization language: Payone.ClientApi.Language.en, Language to display error-messages (default:Payone.ClientApi.Language.en)
          // eslint-disable-next-line no-eval
          config.language = eval(this.getParamValue('language', ''));
 
          // setup
          this.iframes = new Payone.ClientApi.HostedIFrames(config, request);
        });
    }
  }
 
  /**
   * cancel new payment instrument and hides the form
   */
  cancelNewPaymentInstrument() {
    this.cancelPayment.emit();
  }
 
  /**
   * submit payone payment form
   */
  submitNewPaymentInstrument() {
    // reset error message
    this.generalErrorMessage = undefined;
 
    this.renderer.setProperty(this.document.getElementById('error'), 'innerHTML', '');
 
    if (this.iframes.isComplete()) {
      // Perform "CreditCardCheck" to create and get a PseudoCardPan; then call your function "payCallback"
      this.iframes.creditCardCheck('payoneCreditCardCallback');
    } else {
      // set general error message for incomplete form
      this.generalErrorMessage = 'checkout.payment.form.incomplete';
    }
  }
 
  // visible-for-testing
  submitCallback(response: { status: string; pseudocardpan: string; truncatedcardpan: string }) {
    if (response.status === 'VALID' && !this.payoneCreditCardForm.invalid) {
      this.submitPayment.emit({
        parameters: [
          { name: 'pseudocardpan', value: response.pseudocardpan },
          { name: 'truncatedcardpan', value: response.truncatedcardpan },
        ],
        saveAllowed: this.paymentMethod.saveAllowed && this.payoneCreditCardForm.get('saveForLater').value,
      });
    }
    // else: error message is shown in the error div
    this.cd.detectChanges();
  }
 
  isSecurityCodeCheckRequired() {
    if (this.paymentMethod.hostedPaymentPageParameters) {
      const isSecurityCodeCheckRequired = this.paymentMethod.hostedPaymentPageParameters.find(
        param => param.name === 'isSecurityCheckCodeRequired'
      );
      if (isSecurityCodeCheckRequired && String(isSecurityCodeCheckRequired.value).toLowerCase() === 'true') {
        return true;
      }
    }
    return false;
  }
}