PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0.1 4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 All 258 releases
give / src / DonationForms / resources / registrars / gateways / index.ts

index.ts in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonationForms/resources/registrars/gateways/index.ts

72 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {Gateway, RegisteredGateway} from '@givewp/forms/types';
2
3 const {registeredGateways} = window.givewpDonationFormExports;
4
5 /**
6 * @since 3.0.0
7 */
8 interface GatewayRegistrar {
9 register(gateway: Gateway): void;
10
11 getAll(): Gateway[];
12
13 get(id: string): Gateway | undefined;
14 }
15
16 /**
17 * @since 3.0.0
18 */
19 export default class Registrar implements GatewayRegistrar {
20 /**
21 * @since 3.0.0
22 */
23 private gateways: Gateway[] = [];
24
25 /**
26 * @since 3.0.0
27 */
28 public get(id: string): Gateway | undefined {
29 return this.gateways.find((gateway) => gateway.id === id);
30 }
31
32 /**
33 * @since 3.0.0
34 */
35 public getAll(): Gateway[] {
36 return this.gateways;
37 }
38
39 /**
40 * @since 3.0.0
41 */
42 public register(gateway: Gateway): void {
43 const registeredGateway = registeredGateways?.find(({id}) => id === gateway.id);
44
45 this.mapRegisteredGatewayToClientGateway(registeredGateway, gateway);
46
47 if (gateway.hasOwnProperty('initialize')) {
48 try {
49 gateway.initialize();
50 } catch (e) {
51 console.error(`Error initializing ${gateway.id} gateway:`, e);
52 // TODO: decide what to do if a gateway fails to initialize
53 // we can hide the fields from the list or display an error message.
54 // for now we will just display the error message, but in the future
55 // it might be better to hide the fields all together by returning early here.
56 //return;
57 }
58 }
59
60 this.gateways.push(gateway);
61 }
62
63 /**
64 * @since 3.0.0
65 */
66 private mapRegisteredGatewayToClientGateway(registeredGateway: RegisteredGateway, clientGateway: Gateway): void {
67 for (const [key, value] of Object.entries(registeredGateway)) {
68 clientGateway[key] = value;
69 }
70 }
71 }
72