2023-11-03 10:30:31 +00:00
|
|
|
import ApplepayButton from "./ApplepayButton";
|
2024-06-07 15:00:15 +02:00
|
|
|
import PreviewButton from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButton";
|
|
|
|
import PreviewButtonManager from "../../../ppcp-button/resources/js/modules/Renderer/PreviewButtonManager";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor that creates and returns a single PreviewButtonManager instance.
|
|
|
|
*/
|
|
|
|
const buttonManager = () => {
|
|
|
|
if (!ApplePayPreviewButtonManager.instance) {
|
|
|
|
ApplePayPreviewButtonManager.instance = new ApplePayPreviewButtonManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ApplePayPreviewButtonManager.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages all Apple Pay preview buttons on this page.
|
|
|
|
*/
|
|
|
|
class ApplePayPreviewButtonManager extends PreviewButtonManager {
|
|
|
|
constructor() {
|
|
|
|
const args = {
|
|
|
|
methodName: 'ApplePay',
|
2024-06-11 15:58:43 +02:00
|
|
|
buttonConfig: window.wc_ppcp_applepay_admin
|
2024-06-07 15:00:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
super(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible for fetching and returning the PayPal configuration object for this payment
|
|
|
|
* method.
|
|
|
|
*
|
2024-06-11 15:58:43 +02:00
|
|
|
* @param {{}} payPal - The PayPal SDK object provided by WidgetBuilder.
|
2024-06-07 15:00:15 +02:00
|
|
|
* @return {Promise<{}>}
|
|
|
|
*/
|
2024-06-11 15:58:43 +02:00
|
|
|
async fetchConfig(payPal) {
|
|
|
|
const apiMethod = payPal?.Applepay()?.config
|
2024-06-07 15:00:15 +02:00
|
|
|
|
|
|
|
if (!apiMethod) {
|
|
|
|
this.error('configuration object cannot be retrieved from PayPal');
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return await apiMethod();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is responsible for creating a new PreviewButton instance and returning it.
|
|
|
|
*
|
|
|
|
* @param {string} wrapperId - CSS ID of the wrapper element.
|
|
|
|
* @return {ApplePayPreviewButton}
|
|
|
|
*/
|
2024-06-12 11:27:59 +02:00
|
|
|
createButtonInstance(wrapperId) {
|
2024-06-07 15:00:15 +02:00
|
|
|
return new ApplePayPreviewButton({
|
|
|
|
selector: wrapperId,
|
2024-06-07 21:14:12 +02:00
|
|
|
apiConfig: this.apiConfig
|
2024-06-07 15:00:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:30:31 +00:00
|
|
|
|
2024-06-07 15:00:15 +02:00
|
|
|
/**
|
|
|
|
* A single Apple Pay preview button instance.
|
|
|
|
*/
|
|
|
|
class ApplePayPreviewButton extends PreviewButton {
|
|
|
|
constructor(args) {
|
|
|
|
super(args);
|
|
|
|
|
|
|
|
this.selector = `${args.selector}ApplePay`
|
2024-06-07 21:14:12 +02:00
|
|
|
this.defaultAttributes = {
|
|
|
|
button: {
|
|
|
|
type: 'pay',
|
|
|
|
color: 'black',
|
|
|
|
lang: 'en'
|
|
|
|
}
|
|
|
|
};
|
2024-06-07 15:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createNewWrapper() {
|
|
|
|
const element = super.createNewWrapper();
|
|
|
|
element.addClass('ppcp-button-applepay');
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2024-06-07 21:14:12 +02:00
|
|
|
createButton(buttonConfig) {
|
2024-06-07 15:00:15 +02:00
|
|
|
const button = new ApplepayButton(
|
|
|
|
'preview',
|
|
|
|
null,
|
2024-06-07 21:14:12 +02:00
|
|
|
buttonConfig,
|
2024-06-07 15:00:15 +02:00
|
|
|
this.ppcpConfig,
|
|
|
|
);
|
|
|
|
|
2024-06-07 21:14:12 +02:00
|
|
|
button.init(this.apiConfig);
|
2024-06-07 15:00:15 +02:00
|
|
|
}
|
2024-06-07 15:42:16 +02:00
|
|
|
|
|
|
|
/**
|
2024-06-07 21:14:12 +02:00
|
|
|
* Merge form details into the config object for preview.
|
|
|
|
* Mutates the previewConfig object; no return value.
|
2024-06-07 15:42:16 +02:00
|
|
|
*/
|
2024-06-07 21:14:12 +02:00
|
|
|
dynamicPreviewConfig(buttonConfig, ppcpConfig) {
|
2024-06-07 15:42:16 +02:00
|
|
|
// The Apple Pay button expects the "wrapper" to be an ID without `#` prefix!
|
2024-06-07 21:14:12 +02:00
|
|
|
buttonConfig.button.wrapper = buttonConfig.button.wrapper.replace(/^#/, '');
|
2024-06-07 15:42:16 +02:00
|
|
|
|
2024-06-07 21:14:12 +02:00
|
|
|
// Merge the current form-values into the preview-button configuration.
|
|
|
|
if (ppcpConfig.button) {
|
|
|
|
buttonConfig.button.type = ppcpConfig.button.style.type;
|
|
|
|
buttonConfig.button.color = ppcpConfig.button.style.color;
|
|
|
|
buttonConfig.button.lang = ppcpConfig.button.style?.lang || ppcpConfig.button.style.language;
|
2024-06-07 15:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-07 15:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the preview button manager.
|
|
|
|
buttonManager();
|