UNPKG

@apicart/store-sdk

Version:

Apicart SDK for integrating store into any web application

222 lines (180 loc) 4.92 kB
import Apicart from '@apicart/core-sdk'; import PaymentMethodParameter from './PaymentMethodParameter'; export interface PaymentMethodSerializedStructureInterface { id: number; uid: string | null; name: string; description: string; image: string | null; price: number; sort: number; enabled: boolean; parameters: any; } export default class PaymentMethod { public static TYPE_APICART_PAYMENT_ADYEN = 'apicart-payment-adyen'; public static TYPE_APICART_PAYMENT_BRAINTREE = 'apicart-payment-braintree'; public static TYPE_APICART_PAYMENT_PAY_U = 'apicart-payment-pay_u'; public static TYPE_APICART_PAYMENT_STRIPE = 'apicart-payment-stripe'; public static TYPE_BANK_TRANSFER = 'bank-transfer'; public static TYPE_CASH = 'cash'; public static TYPE_CASH_ON_DELIVERY = 'cash-on-delivery'; public static TYPE_CUSTOM = 'custom'; private _id: number; private _name: string; private _description: string | null private _image: string | null; private _type: string; private _price: number; private _sort: number | null; private _uid: string | null = null; private _enabled = false; private _parameters: PaymentMethodParameter[] = []; constructor( id: number, name: string, description: string | null, image: string | null, type: string, price: number, sort: number | null, enabled: boolean, uid: string | null = null, parameters: PaymentMethodParameter[] = [] ) { this._id = id; this._uid = uid; this._name = name; this._description = description; this._image = image; this._type = type; this._price = price; this._sort = sort; this._enabled = enabled; Apicart.Utils.Loops.forEach(parameters, (paymentMethodParameter: PaymentMethodParameter): void => { this.addParameter(paymentMethodParameter); }); } public getId(): number { return this._id; } public getUid(): string | null { return this._uid; } public getName(): string { return this._name; } public getDescription(): string | null { return this._description; } public getImage(): string | null { return this._image; } public getType(): string { return this._type; } public getPrice(): number { return this._price; } public getSort(): number | null { return this._sort; } public isEnabled(): boolean { return this._enabled; } public getParameters(): PaymentMethodParameter[] { return this._parameters; } public addParameter(paymentMethodParameter: PaymentMethodParameter): void { let parameterUpdated = false; Apicart.Utils.Loops.forEach(this.getParameters(), (existingParameter: PaymentMethodParameter) => { if (existingParameter.getKey() === paymentMethodParameter.getKey()) { existingParameter.setValue(paymentMethodParameter.getValue()); parameterUpdated = true; return false; } }); if (!parameterUpdated) { this._parameters.push(paymentMethodParameter); } } public removeParameter(key: string): void { Apicart.Utils.Loops.forEach( this.getParameters(), (paymentMethodParameter: PaymentMethodParameter, index: string | number): boolean => { if (paymentMethodParameter.getKey() === key) { delete this._parameters[index]; return false; } } ); } public getParameterValue(key: string, defaultValue: any = null): any { let parameterValue = defaultValue; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: PaymentMethodParameter): boolean => { if (parameter.getKey() === key) { parameterValue = parameter.getValue(); return false; } }); return parameterValue; } public serialize(): PaymentMethodSerializedStructureInterface { const structure = { id: this.getId(), uid: this.getUid(), name: this.getName(), description: this.getDescription(), image: this.getImage(), type: this.getType(), price: this.getPrice(), sort: this.getSort(), enabled: this.isEnabled(), parameters: [] }; Apicart.Utils.Loops.forEach(this.getParameters(), (parameter: PaymentMethodParameter): void => { structure.parameters.push({ key: parameter.getKey(), value: parameter.getValue() }); }); return structure; } public static deserialize(data: any): PaymentMethod { Apicart.Utils.Loops.forEach( ['id', 'name', 'image', 'type', 'price', 'sort', 'enabled'], (key: string): void => { if (!(key in data)) { throw 'PaymentMethod: Missing required parameter: "' + key + '".'; } } ); const paymentMethod = new PaymentMethod( data.id, data.name, data.description, data.image, data.type, data.price, data.sort, data.enabled, data.uid ); if ('parameters' in data && Array.isArray(data.parameters)) { Apicart.Utils.Loops.forEach(data.parameters, (parameter: any): void => { if ('key' in parameter) { paymentMethod.addParameter(new PaymentMethodParameter(parameter.key, parameter.value || null)); } }); } return paymentMethod; } }