-
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.
Merge pull request #48 from commercetools/ddeliziact/enabler-refactoring
feat(enabler): splitting creation of the component into 2 steps via builder
- Loading branch information
Showing
9 changed files
with
318 additions
and
204 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 was deleted.
Oops, something went wrong.
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,27 +1,50 @@ | ||
import { BaseComponent, BaseOptions } from '../base'; | ||
import { ComponentOptions, PaymentMethod } from '../../payment-enabler/payment-enabler'; | ||
import Core from "@adyen/adyen-web/dist/types/core/core"; | ||
import { | ||
ComponentOptions, | ||
PaymentComponent, | ||
PaymentMethod | ||
} from "../../payment-enabler/payment-enabler"; | ||
import { | ||
AdyenBaseComponentBuilder, | ||
DefaultAdyenComponent, | ||
BaseOptions, | ||
} from "../base"; | ||
|
||
/** | ||
* Paypal component | ||
* | ||
* Configuration options: | ||
* https://docs.adyen.com/payment-methods/paypal/web-component/ | ||
*/ | ||
export class Paypal extends BaseComponent { | ||
constructor(baseOptions: BaseOptions, componentOptions: ComponentOptions) { | ||
// TODO: | ||
|
||
/* | ||
Hide Venmo | ||
If you and your shopper are both located in the US, Venmo is shown in the PayPal Component by default. To hide Venmo in the PayPal Component, set blockPayPalVenmoButton to true. | ||
export class PaypalBuilder extends AdyenBaseComponentBuilder { | ||
public componentHasSubmit = false; | ||
|
||
constructor(baseOptions: BaseOptions) { | ||
super(PaymentMethod.paypal, baseOptions); | ||
} | ||
|
||
Use the create method of your AdyenCheckout instance, in this case checkout, to create an instance of the Component. Add the configuration object if you created one. | ||
build(config: ComponentOptions): PaymentComponent { | ||
const paypalComponent = new PaypalComponent(this.paymentMethod, this.adyenCheckout, config); | ||
paypalComponent.init(); | ||
return paypalComponent; | ||
} | ||
} | ||
|
||
export class PaypalComponent extends DefaultAdyenComponent { | ||
constructor( | ||
paymentMethod: PaymentMethod, | ||
adyenCheckout: typeof Core, | ||
componentOptions: ComponentOptions | ||
) { | ||
super(paymentMethod, adyenCheckout, componentOptions); | ||
} | ||
|
||
const paypalComponent = checkout.create('paypal', paypalConfiguration).mount('#paypal-container'); | ||
*/ | ||
super(PaymentMethod.paypal, baseOptions, componentOptions); | ||
init() { | ||
this.component = this.adyenCheckout.create(this.paymentMethod, { | ||
...this.componentOptions, | ||
blockPayPalCreditButton: true, | ||
blockPayPalPayLaterButton: true, | ||
blockPayPalVenmoButton: true, | ||
}); | ||
} | ||
} |
Oops, something went wrong.