All files / src/app/core/models/payment-method payment-method.mapper.ts

76.78% Statements 43/56
67.64% Branches 23/34
80.95% Functions 17/21
79.62% Lines 43/54

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      25x                   25x   4x       4x   4x 1x   3x 3x 3x                         3x                                 3x       3x 2x     1x     1x 1x             2x 2x             1x 1x               2x     2x 2x 1x               3x     3x 3x                     3x 2x   1x 4x                                           4x 2x 2x 2x   2x 1x 1x 1x           2x 1x 1x           4x                                                    
import { FormlyFieldConfig } from '@ngx-formly/core';
 
import { PaymentInstrumentData } from 'ish-core/models/payment-instrument/payment-instrument.interface';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
 
import {
  PaymentMethodBaseData,
  PaymentMethodData,
  PaymentMethodOptionsDataType,
  PaymentMethodParameterType,
} from './payment-method.interface';
import { PaymentMethod } from './payment-method.model';
 
export class PaymentMethodMapper {
  static fromData(body: PaymentMethodData): PaymentMethod[] {
    Iif (!body?.data) {
      throw new Error(`'paymentMethodData' is required`);
    }
 
    const included = body.included;
 
    if (!body.data.length) {
      return [];
    }
    return body.data
      .filter(data => PaymentMethodMapper.isPaymentMethodValid(data))
      .map(data => ({
        id: data.id,
        serviceId: data.serviceID,
        displayName: data.displayName,
        description: data.description,
        capabilities: data.capabilities,
        isRestricted: data.restricted,
        saveAllowed: data.saveAllowed && !!data.parameterDefinitions && !!data.parameterDefinitions.length,
        restrictionCauses: data.restrictions,
        paymentCosts: PriceItemMapper.fromPriceItem(data.paymentCosts),
        paymentCostsThreshold: PriceItemMapper.fromPriceItem(data.paymentCostsThreshold),
        paymentInstruments:
          included?.paymentInstruments && data.paymentInstruments
            ? data.paymentInstruments.map(id => included.paymentInstruments[id])
            : undefined,
        parameters: PaymentMethodMapper.mapParameter(data.parameterDefinitions),
        hostedPaymentPageParameters:
          data.serviceID === 'Concardis_DirectDebit'
            ? PaymentMethodMapper.mapSEPAMandateInformation(data.hostedPaymentPageParameters)
            : data.hostedPaymentPageParameters,
      }));
  }
 
  /**
   * get a user's eligible payment methods
   */
  static fromOptions(options: {
    methods: PaymentMethodOptionsDataType[];
    instruments: PaymentInstrumentData[];
  }): PaymentMethod[] {
    Iif (!options) {
      throw new Error(`'paymentMethodOptions' are required`);
    }
 
    if (!options.methods?.length) {
      return [];
    }
 
    const pmBlacklist = ['ISH_FASTPAY', 'ISH_INVOICE_TOTAL_ZERO'];
 
    // return only payment methods that have either payment instruments or no parameters
    return options.methods[0].payments
      .map(pm => ({
        id: pm.id,
        serviceId: pm.id, // is missing
        displayName: pm.displayName,
        restrictionCauses: pm.restrictions,
        hasParameters: !!pm.paymentParameters?.length,
        paymentInstruments: options.instruments
          .filter(i => i.name === pm.id)
          .map(i => ({
            id: i.id,
            parameters: i.attributes,
            accountIdentifier: PaymentMethodMapper.determineAccountIdentifier(i),
            paymentMethod: pm.id,
          })),
      }))
      .filter(pm => !pmBlacklist.includes(pm.serviceId))
      .filter(pm => !pm.hasParameters || pm.paymentInstruments?.length);
  }
 
  /**
   * returns the payment instrument's account identifier if present, else it returns all the attributes
   * add further special functionality here, if needed
   */
  private static determineAccountIdentifier(pi: PaymentInstrumentData): string {
    return pi.accountIdentifier
      ? pi.accountIdentifier
      : pi.attributes
          ?.filter(attr => attr.name !== 'paymentInstrumentId')
          .map(attr => attr.value)
          .reduce((acc, val) => (val ? `${acc}  ${val}` : acc));
  }
 
  /**
   * determines if payment method is valid and is available
   * valid: payment methods without capabilities or which have no capabilities given in the list below
   */
  private static isPaymentMethodValid(paymentData: PaymentMethodBaseData): boolean {
    const invalidCapabilities = ['LimitedTender'];
 
    // without capabilities
    if (!paymentData.capabilities?.length) {
      return true;
    }
 
    // excluded by the invalidCapabilities list
    return !paymentData.capabilities.some(data => invalidCapabilities.includes(data));
  }
 
  /**
   * maps form parameter if there are some (like credit card or direct debit)
   */
  private static mapParameter(parametersData: PaymentMethodParameterType[]): FormlyFieldConfig[] {
    if (!parametersData) {
      return;
    }
    return parametersData.map(p => {
      const param: FormlyFieldConfig = {
        key: p.name,
        type: p.options ? 'ish-select-field' : 'ish-text-input-field',
        props: {
          labelClass: 'col-md-4',
          fieldClass: 'col-sm-6',
          type: p.type === 'string' ? 'text' : (p.type as string).toLowerCase(),
          label: p.displayName,
          placeholder: p.description,
          defaultValue: '',
          options:
            p.options && p.options.length > 0
              ? p.options.map(option => ({ label: option.displayName, value: option.id }))
              : undefined,
          attributes: {},
        },
        validation: {
          messages: {},
        },
        hide: p.hidden,
      };
 
      if (p.constraints) {
        if (p.constraints.required) {
          param.props.required = true;
          param.validation.messages = { ...param.validation.messages, required: p.constraints.required.message };
        }
        if (p.constraints.size) {
          param.props.minLength = p.constraints.size.min;
          param.props.maxLength = p.constraints.size.max;
          param.validation.messages = {
            ...param.validation.messages,
            minLength: p.constraints.size.message,
            maxLength: p.constraints.size.message,
          };
        }
        if (p.constraints.pattern) {
          param.props.pattern = p.constraints.pattern.regexp;
          param.validation.messages = {
            ...param.validation.messages,
            pattern: p.constraints.pattern.message,
          };
        }
      }
      return param;
    });
  }
 
  /**
   * convenience method to restructure concardis sepa mandate hosted payment page parameter
   */
  private static mapSEPAMandateInformation(
    hostedPaymentPageParameters: { name: string; value: string }[]
  ): { name: string; value: string }[] {
    const mandateEntry = hostedPaymentPageParameters.find(p => p.name === 'Concardis_SEPA_Mandate');
 
    Iif (typeof mandateEntry.value !== 'string') {
      const sepaMandateArray = mandateEntry.value as {
        mandateId: string;
        mandateText: string;
        directDebitType: string;
      };
      hostedPaymentPageParameters.push({ name: 'mandateId', value: sepaMandateArray.mandateId });
      hostedPaymentPageParameters.push({ name: 'mandateText', value: sepaMandateArray.mandateText });
      hostedPaymentPageParameters.push({ name: 'directDebitType', value: sepaMandateArray.directDebitType });
    }
 
    return hostedPaymentPageParameters;
  }
}