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

69.86% Statements 51/73
39.39% Branches 13/33
54.54% Functions 6/11
70.42% Lines 50/71

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 2175x                       5x       5x 5x                     5x 16x                 16x   16x 16x         16x         16x             16x 16x         16x                     16x                       3x       12x                                                                     3x                                                                 3x 3x       3x             7x 7x 7x 7x 7x   7x 7x 7x 7x   7x 7x 7x 7x   7x 7x 7x 7x   7x 7x 7x 7x   7x 7x 7x 7x               2x 2x     2x 2x      
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  DestroyRef,
  EventEmitter,
  Input,
  OnChanges,
  OnInit,
  Output,
  inject,
} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
 
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';
 
export type ConcardisErrorMessageType =
  | { properties: { key: string; code: number; message: string; messageKey: string }[] }
  | string;
 
@Component({
  selector: 'ish-payment-concardis',
  template: ' ',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class PaymentConcardisComponent implements OnInit, OnChanges {
  constructor(protected scriptLoader: ScriptLoaderService, protected cd: ChangeDetectorRef) {}
  /**
   * concardis 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 }>();
 
  /**
   * flag to make sure that the init script is executed only once
   */
  scriptLoaded = false;
 
  /**
   * flag for displaying error messages after form submit
   */
  formSubmitted = false;
 
  /**
   * form for parameters which don't come form payment host
   */
  fieldConfig: FormlyFieldConfig[];
  parameterForm: FormGroup;
  model = {};
  options: FormlyFormOptions = {};
 
  /**
   * error messages from host
   */
  errorMessage = {
    general: { message: '' },
    iban: { messageKey: '', message: '', code: 0 },
    bic: { messageKey: '', message: '', code: 0 },
    accountholder: { messageKey: '', message: '', code: 0 },
    cardNumber: { messageKey: '', message: '', code: 0 },
    cvc: { messageKey: '', message: '', code: 0 },
    expiryMonth: { messageKey: '', message: '', code: 0 },
  };
 
  // eslint-disable-next-line ish-custom-rules/private-destroyRef-field
  protected destroyRef = inject(DestroyRef);
 
  getPayEngineURL() {
    return this.getParamValue('ConcardisPaymentService.Environment', '') === 'LIVE'
      ? 'https://pp.payengine.de/bridge/1.0/payengine.min.js'
      : 'https://pptest.payengine.de/bridge/1.0/payengine.min.js';
  }
 
  /**
   * initialize parameter form on init
   */
  ngOnInit() {
    this.formInit();
  }
 
  formInit() {
    this.parameterForm = new FormGroup({});
  }
 
  /**
   * load concardis script if component is shown
   */
  ngOnChanges() {
    Iif (this.paymentMethod) {
      this.loadScript();
    }
  }
 
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  loadScript() {}
 
  /**
   * gets a parameter value from payment method
   * sets the general error message (key) if the parameter is not available
   */
  protected getParamValue(name: string, errorMessage: string): string {
    const parameter = this.paymentMethod.hostedPaymentPageParameters.find(param => param.name === name);
    Iif (!parameter?.value) {
      this.errorMessage.general.message = errorMessage;
      return;
    }
    return parameter.value;
  }
 
  /**
   * determine errorMessages on the basis of the error code
   */
  // eslint-disable-next-line complexity
  getErrorMessage(code: number, paymentMethod: string, fieldType: string, defaultMessage: string): string {
    let messageKey: string;
 
    switch (code) {
      case 4121:
      case 4122: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.default`;
        break;
      }
      case 4123: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.notNumber`;
        break;
      }
      case 4124: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.notAlphanumeric`;
        break;
      }
      case 4126:
      case 4128:
      case 41214:
      case 41216: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.length`;
        break;
      }
      case 4127:
      case 4129:
      case 41215: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.invalid`;
        break;
      }
      case 41213:
      case 41217: {
        messageKey = `checkout.${paymentMethod}.${fieldType}.error.countryNotSupported`;
        break;
      }
      default: {
        messageKey = defaultMessage;
        break;
      }
    }
 
    return messageKey;
  }
 
  /**
   * reset errorMessages
   */
  resetErrors() {
    this.errorMessage.general.message = undefined;
    if (this.errorMessage.accountholder) {
      this.errorMessage.accountholder.message = undefined;
      this.errorMessage.accountholder.messageKey = undefined;
      this.errorMessage.accountholder.code = undefined;
    }
    if (this.errorMessage.iban) {
      this.errorMessage.iban.message = undefined;
      this.errorMessage.iban.messageKey = undefined;
      this.errorMessage.iban.code = undefined;
    }
    if (this.errorMessage.bic) {
      this.errorMessage.bic.message = undefined;
      this.errorMessage.bic.messageKey = undefined;
      this.errorMessage.bic.code = undefined;
    }
    if (this.errorMessage.cardNumber) {
      this.errorMessage.cardNumber.message = undefined;
      this.errorMessage.cardNumber.messageKey = undefined;
      this.errorMessage.cardNumber.code = undefined;
    }
    if (this.errorMessage.cvc) {
      this.errorMessage.cvc.message = undefined;
      this.errorMessage.cvc.messageKey = undefined;
      this.errorMessage.cvc.code = undefined;
    }
    if (this.errorMessage.expiryMonth) {
      this.errorMessage.expiryMonth.message = undefined;
      this.errorMessage.expiryMonth.messageKey = undefined;
      this.errorMessage.expiryMonth.code = undefined;
    }
  }
 
  /**
   * cancel new payment instrument, hides and resets the parameter form
   */
  cancelNewPaymentInstrument() {
    this.parameterForm.reset();
    Iif (this.parameterForm.get('saveForLater')) {
      this.parameterForm.get('saveForLater').setValue(true);
    }
    this.resetErrors();
    this.cancelPayment.emit();
  }
}