-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enabler): splitting creation of the component into 2 steps via b…
…uilder
- Loading branch information
1 parent
c708a58
commit 223cebb
Showing
9 changed files
with
241 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,87 @@ | ||
import Core from '@adyen/adyen-web/dist/types/core/core'; | ||
import { ComponentOptions, PaymentComponent, PaymentMethod } from '../payment-enabler/payment-enabler'; | ||
import ApplePay from '@adyen/adyen-web/dist/types/components/ApplePay'; | ||
import GooglePay from '@adyen/adyen-web/dist/types/components/GooglePay'; | ||
import RedirectElement from '@adyen/adyen-web/dist/types/components/Redirect/Redirect'; | ||
import ApplePay from "@adyen/adyen-web/dist/types/components/ApplePay"; | ||
import GooglePay from "@adyen/adyen-web/dist/types/components/GooglePay"; | ||
import RedirectElement from "@adyen/adyen-web/dist/types/components/Redirect/Redirect"; | ||
import Core from "@adyen/adyen-web/dist/types/core/core"; | ||
import { | ||
ComponentOptions, | ||
PaymentComponent, | ||
PaymentComponentBuilder, | ||
PaymentMethod, | ||
} from "../payment-enabler/payment-enabler"; | ||
|
||
export type BaseOptions = { | ||
adyenCheckout: typeof Core; | ||
} | ||
}; | ||
|
||
/** | ||
* Base Web Component | ||
*/ | ||
export abstract class BaseComponent implements PaymentComponent { | ||
export abstract class AdyenBaseComponentBuilder | ||
implements PaymentComponentBuilder | ||
{ | ||
public componentHasSubmit = true; | ||
|
||
protected paymentMethod: PaymentMethod; | ||
protected adyenCheckout: typeof Core; | ||
protected config: ComponentOptions['config']; | ||
protected component: typeof ApplePay | typeof GooglePay | typeof RedirectElement; | ||
|
||
constructor(paymentMethod: PaymentMethod, baseOptions: BaseOptions, componentOptions: ComponentOptions) { | ||
constructor(paymentMethod: PaymentMethod, baseOptions: BaseOptions) { | ||
this.paymentMethod = paymentMethod; | ||
this.adyenCheckout = baseOptions.adyenCheckout; | ||
this.config = componentOptions.config; | ||
this.component = this._create(); | ||
} | ||
|
||
protected _create(): typeof ApplePay | typeof GooglePay | typeof RedirectElement { | ||
return this.adyenCheckout.create(this.paymentMethod, this.config); | ||
build(config: ComponentOptions): PaymentComponent { | ||
const component = new DefaultAdyenComponent( | ||
this.paymentMethod, | ||
this.adyenCheckout, | ||
config | ||
); | ||
component.init(); | ||
return component; | ||
} | ||
} | ||
|
||
submit() { | ||
export class DefaultAdyenComponent implements PaymentComponent { | ||
protected component: | ||
| typeof ApplePay | ||
| typeof GooglePay | ||
| typeof RedirectElement; | ||
protected paymentMethod: PaymentMethod; | ||
protected adyenCheckout: typeof Core; | ||
protected componentOptions: ComponentOptions; | ||
|
||
constructor( | ||
paymentMethod: PaymentMethod, | ||
adyenCheckout: typeof Core, | ||
componentOptions: ComponentOptions | ||
) { | ||
this.paymentMethod = paymentMethod; | ||
this.adyenCheckout = adyenCheckout; | ||
this.componentOptions = componentOptions; | ||
} | ||
// This is an internal method | ||
init() { | ||
this.component = this.adyenCheckout.create( | ||
this.paymentMethod, | ||
this.componentOptions | ||
); | ||
} | ||
|
||
submit() { | ||
this.component.submit(); | ||
}; | ||
} | ||
|
||
mount(selector: string) { | ||
if ('isAvailable' in this.component) { | ||
this.component.isAvailable() | ||
if ("isAvailable" in this.component) { | ||
this.component | ||
.isAvailable() | ||
.then(() => { | ||
this.component.mount(selector); | ||
}) | ||
.catch((e: unknown) => { | ||
console.log(`${this.paymentMethod } is not available`, e); | ||
console.log(`${this.paymentMethod} is not available`, e); | ||
}); | ||
} else { | ||
this.component.mount(selector); | ||
} | ||
} | ||
|
||
showValidation?(): void; | ||
isValid?(): boolean; | ||
getState?(): { | ||
card?: { | ||
endDigits?: string; | ||
brand?: string; | ||
expiryDate? : string; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { BaseComponent, BaseOptions } from '../base'; | ||
import { ComponentOptions, PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import { PaymentMethod } from "../../payment-enabler/payment-enabler"; | ||
import { AdyenBaseComponentBuilder, BaseOptions } from "../base"; | ||
|
||
/** | ||
* Apple pay component | ||
* | ||
* Configuration options: | ||
* https://docs.adyen.com/payment-methods/apple-pay/web-component/ | ||
*/ | ||
export class Applepay extends BaseComponent { | ||
constructor(baseOptions: BaseOptions, componentOptions: ComponentOptions) { | ||
super(PaymentMethod.applepay, baseOptions, componentOptions); | ||
export class ApplepayBuilder extends AdyenBaseComponentBuilder { | ||
public componentHasSubmit = false; | ||
constructor(baseOptions: BaseOptions) { | ||
super(PaymentMethod.applepay, baseOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import { BaseComponent, BaseOptions } from '../base'; | ||
import { ComponentOptions, PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import { PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import { AdyenBaseComponentBuilder, BaseOptions } from '../base'; | ||
|
||
/** | ||
* Google pay component | ||
* | ||
* Configuration options: | ||
* https://docs.adyen.com/payment-methods/google-pay/web-component/ | ||
*/ | ||
export class Googlepay extends BaseComponent { | ||
constructor(baseOptions: BaseOptions, componentOptions: ComponentOptions) { | ||
super(PaymentMethod.googlepay, baseOptions, componentOptions); | ||
export class GooglepayBuilder extends AdyenBaseComponentBuilder { | ||
public componentHasSubmit = false; | ||
|
||
constructor(baseOptions: BaseOptions) { | ||
super(PaymentMethod.googlepay, baseOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { BaseComponent, BaseOptions } from '../base'; | ||
import { ComponentOptions, PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import { PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import { AdyenBaseComponentBuilder, BaseOptions } from '../base'; | ||
|
||
/** | ||
* Klarna component | ||
* | ||
* Configuration options: | ||
* https://docs.adyen.com/payment-methods/klarna/web-component/ | ||
*/ | ||
export class Klarna extends BaseComponent { | ||
constructor(baseOptions: BaseOptions, componentOptions: ComponentOptions) { | ||
export class KlarnaBuilder extends AdyenBaseComponentBuilder { | ||
constructor(baseOptions: BaseOptions) { | ||
/* todo: | ||
pass locale | ||
*/ | ||
super(PaymentMethod.klarna, baseOptions, componentOptions); | ||
super(PaymentMethod.klarna, baseOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.