Skip to content

Commit

Permalink
Merge pull request #116 from degica/fix/pluginConflicts3.1.0
Browse files Browse the repository at this point in the history
Fix plugin conflicts in 3.1.0
  • Loading branch information
rramsden authored May 23, 2024
2 parents d562380 + 2d85668 commit 754b41d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 104 deletions.
2 changes: 1 addition & 1 deletion class-wc-gateway-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @extends WC_Payment_Gateway
*
* @version 3.1.0
* @version 3.1.2
*
* @author Komoju
*/
Expand Down
2 changes: 1 addition & 1 deletion class-wc-settings-page-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @extends WC_Settings_Page
*
* @version 3.1.0
* @version 3.1.2
*
* @author Komoju
*/
Expand Down
190 changes: 99 additions & 91 deletions includes/js/komoju-checkout-blocks.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,113 @@
const { useEffect, useCallback, useRef, createElement } = window.wp.element;
const KomojuPaymentModule = (() => {
const { useEffect, useCallback, useRef, createElement } = window.wp.element;

function registerPaymentMethod(paymentMethod) {
let name = `${paymentMethod.id}`
const settings = window.wc.wcSettings.getSetting(`${name}_data`, {});
function registerPaymentMethod(paymentMethod) {
let name = `${paymentMethod.id}`
const settings = window.wc.wcSettings.getSetting(`${name}_data`, {});

const komojuFields = createElement('komoju-fields', {
'token': '',
'name': 'komoju_payment_token',
'komoju-api': settings.komojuApi,
'publishable-key': settings.publishableKey,
'session': settings.session,
'payment-type': settings.paymentType,
'locale': settings.locale,
style: { display: 'none' },
});
const komojuFields = createElement('komoju-fields', {
'token': '',
'name': 'komoju_payment_token',
'komoju-api': settings.komojuApi,
'publishable-key': settings.publishableKey,
'session': settings.session,
'payment-type': settings.paymentType,
'locale': settings.locale,
style: { display: 'none' },
});

const label = createElement('div', {
style: { display: 'block', alignItems: 'center', justifyContent: 'center', width: '100%' }
},
window.wp.htmlEntities.decodeEntities(settings.title || window.wp.i18n.__('NULL GATEWAY', 'test_komoju_gateway')),
settings.icon ?
createElement('img', {
src: settings.icon,
alt: settings.title || 'Payment Method Icon',
style: { display: 'flex', alignItems: 'center', justifyContent: 'center', marginLeft: '10px' }
}) : null,
komojuFields
);
const label = createElement('div', {
style: { display: 'block', alignItems: 'center', justifyContent: 'center', width: '100%' }
},
window.wp.htmlEntities.decodeEntities(settings.title || window.wp.i18n.__('NULL GATEWAY', 'test_komoju_gateway')),
settings.icon ?
createElement('img', {
src: settings.icon,
alt: settings.title || 'Payment Method Icon',
style: { display: 'flex', alignItems: 'center', justifyContent: 'center', marginLeft: '10px' }
}) : null,
komojuFields
);

const KomojuComponent = ({ activePaymentMethod, emitResponse, eventRegistration }) => {
const { onPaymentSetup } = eventRegistration;
const komojuFieldEnabledMethods = ['komoju_credit_card', 'komoju_konbini', 'komoju_bank_transfer']
const KomojuComponent = ({ activePaymentMethod, emitResponse, eventRegistration }) => {
const { onPaymentSetup } = eventRegistration;
const komojuFieldEnabledMethods = ['komoju_credit_card', 'komoju_konbini', 'komoju_bank_transfer']

useEffect(() => {
const komojuField = document.querySelector(`komoju-fields[payment-type='${paymentMethod.paymentType}']`);
komojuField.style.display = 'block';
useEffect(() => {
const komojuField = document.querySelector(`komoju-fields[payment-type='${paymentMethod.paymentType}']`);
komojuField.style.display = 'block';

const unsubscribe = onPaymentSetup(async () => {
if (paymentMethod.id != activePaymentMethod) return;
if (!komojuFieldEnabledMethods.includes(paymentMethod.id)) return;
const unsubscribe = onPaymentSetup(async () => {
if (paymentMethod.id != activePaymentMethod) return;
if (!komojuFieldEnabledMethods.includes(paymentMethod.id)) return;

if (!(komojuField || typeof komojuField.submit === 'function')) {
return {
type: emitResponse.responseTypes.ERROR,
message: 'There was an error',
};
}
if (!(komojuField || typeof komojuField.submit === 'function')) {
return {
type: emitResponse.responseTypes.ERROR,
message: 'There was an error',
};
}

function submitFields(fields) {
return new Promise(async (resolve, reject) => {
fields.addEventListener("komoju-invalid", reject);
const token = await fields.submit();
fields.removeEventListener("komoju-invalid", reject);
if (token) resolve(token);
});
}
function submitFields(fields) {
return new Promise(async (resolve, reject) => {
fields.addEventListener("komoju-invalid", reject);
const token = await fields.submit();
fields.removeEventListener("komoju-invalid", reject);
if (token) resolve(token);
});
}

try {
const token = await submitFields(komojuField);
return {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
komoju_payment_token: token.id
try {
const token = await submitFields(komojuField);
return {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
komoju_payment_token: token.id
},
},
},
};
} catch (e) {
return {
type: emitResponse.responseTypes.ERROR,
message: e.detail.errors[0].message,
};
}
});
};
} catch (e) {
return {
type: emitResponse.responseTypes.ERROR,
message: e.detail.errors[0].message,
};
}
});

return () => {
komojuField.style.display = 'none';
unsubscribe();
};
}, [
activePaymentMethod,
emitResponse.responseTypes.ERROR,
emitResponse.responseTypes.SUCCESS
]);
};
return () => {
komojuField.style.display = 'none';
unsubscribe();
};
}, [
activePaymentMethod,
emitResponse.responseTypes.ERROR,
emitResponse.responseTypes.SUCCESS
]);
};

const Block_Gateway = {
name: name,
label: label,
content: createElement(KomojuComponent, null),
edit: createElement(KomojuComponent, null),
canMakePayment: () => true,
ariaLabel: settings.title || 'Payment Method',
supports: {
features: settings.supports || ['products'],
},
const Block_Gateway = {
name: name,
label: label,
content: createElement(KomojuComponent, null),
edit: createElement(KomojuComponent, null),
canMakePayment: () => true,
ariaLabel: settings.title || 'Payment Method',
supports: {
features: settings.supports || ['products'],
},
};
window.wc.wcBlocksRegistry.registerPaymentMethod(Block_Gateway);
}

return {
init: () => {
const paymentMethodData = window.wc.wcSettings.getSetting('paymentMethodData', {});
Object.values(paymentMethodData).forEach((value) => {
registerPaymentMethod(value);
});
}
};
window.wc.wcBlocksRegistry.registerPaymentMethod(Block_Gateway);
}
})();

paymentMethodData = window.wc.wcSettings.getSetting('paymentMethodData', {});
Object.values(paymentMethodData).forEach((value) => {
registerPaymentMethod(value);
});
KomojuPaymentModule.init();
21 changes: 10 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: KOMOJU Payments
Plugin URI: https://github.com/komoju/komoju-woocommerce
Description: Extends WooCommerce with KOMOJU gateway.
Version: 3.1.0
Version: 3.1.2
Author: KOMOJU
Author URI: https://komoju.com
*/
Expand Down Expand Up @@ -119,19 +119,18 @@ function register_komoju_payment_method_type()
return;
}

$gateways = WC()->payment_gateways->payment_gateways();
add_action(
'woocommerce_blocks_payment_method_type_registration',
function ($payment_method_registry) {
$gateways = WC()->payment_gateways()->payment_gateways();

if ($gateways) {
foreach ($gateways as $gateway) {
if ($gateway->enabled == 'yes' && $gateway instanceof WC_Gateway_Komoju_Single_Slug) {
add_action(
'woocommerce_blocks_payment_method_type_registration',
function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) use ($gateway) {
if ($gateways) {
foreach ($gateways as $gateway) {
if ($gateway->enabled == 'yes' && $gateway instanceof WC_Gateway_Komoju_Single_Slug) {
$payment_method_registry->register(new WC_Gateway_Komoju_Blocks($gateway));
}
);
}
}
}
}
});
}
}
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Go back to your Wordpress instance and set the "Webhook Secret Token" value on t

== Changelog ==

= 3.1.2 =

Fix plugin conflicts

= 3.1.0 =
Updated to use WooCommerce version 8.8.3.
Adds a user editable description field.
Expand Down

0 comments on commit 754b41d

Please sign in to comment.