Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block-based checkout support #109

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM wordpress:6.1.1
FROM wordpress:6.4.2

ARG woocommerce_version

Expand Down
33 changes: 32 additions & 1 deletion class-wc-gateway-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
* @class WC_Gateway_Komoju
* @extends WC_Payment_Gateway
*
* @version 3.0.8
* @version 3.1.0
*
* @author Komoju
*/
require_once dirname(__FILE__) . '/komoju-php/komoju-php/lib/komoju.php';

class WC_Gateway_Komoju extends WC_Payment_Gateway
{

/** Singleton instance */
private static $instance = null;

/** @var array Array of locales */
public $locale;

Expand All @@ -28,6 +32,25 @@ class WC_Gateway_Komoju extends WC_Payment_Gateway
/** @var WC_Logger Logger instance */
public static $log;

protected $debug;
protected $invoice_prefix;
protected $secretKey;
protected $webhookSecretToken;
protected $komoju_api;
protected $instructions;
protected $useOnHold;

/**
* Constructor for the gateway.
*/
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new WC_Gateway_Komoju();
}

return self::$instance;
}

/**
* Constructor for the gateway.
*/
Expand All @@ -52,10 +75,12 @@ public function __construct()
$this->description = $this->get_option('description');
$this->instructions = $this->get_option('instructions', $this->description);
$this->useOnHold = $this->get_option('useOnHold');
$this->supports[] = 'blocks';

// Filters
// Actions
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
add_action('init', [$this, 'woocommerce_komoju_declare_checkout_blocks_compatibility']);

if ($this->id === 'komoju') {
include_once 'includes/class-wc-gateway-komoju-ipn-handler.php';
Expand All @@ -70,6 +95,12 @@ public function __construct()
}
}

function woocommerce_komoju_declare_checkout_blocks_compatibility() {
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true);
}
}

/*
* This shows a link to komoju on order pages that were paid with this gateway.
*/
Expand Down
4 changes: 2 additions & 2 deletions class-wc-settings-page-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ private function fetch_all_payment_methods()
{
$api = new KomojuApi($this->secret_key());

if (!$api->secretKey || strlen($api->secretKey) === 0) {
return null;
if (!$api->getSecretKey() || strlen($api->getSecretKey()) === 0) {
return null;
}

try {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
context: .
dockerfile: Dockerfile
args:
woocommerce_version: 6.3.1
woocommerce_version: 8.5.1
ports:
- "8000:80"
restart: always
Expand Down
50 changes: 50 additions & 0 deletions includes/class-wc-gateway-komoju-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

class WC_Gateway_Komoju_Blocks extends AbstractPaymentMethodType {
protected $payment_method;
protected $name;

public function __construct($payment_method) {
$this->payment_method = $payment_method;
$this->name = $payment_method->id;
}

public function initialize() {
$this->settings = $this->payment_method->settings;
}

public function is_active() {
return $this->settings['enabled'];
}

// We enqueue the komoju-fields js in woocommerce_komoju_load_scripts in index.php. Register
// the script here.
public function get_payment_method_script_handles() {
$komoju_fields_js = plugins_url('js/komoju-fields-blocks.js', __FILE__);
wp_register_script('komoju-fields-blocks', $komoju_fields_js, [], null, true);

return ['komoju-fields-blocks'];
}

public function get_payment_method_data() {
// Trying to pass the html from WC_Gateway_Komoju_Single_Slug::payment_fields to the
// frontend, without success.
ob_start();
$this->payment_method->payment_fields();
$paymentFields = ob_get_clean();

return [
'title' => $this->name,
'description' => $this->payment_method->method_description,
'supports' => array_filter($this->payment_method->supports, array($this->payment_method, 'supports')),
'paymentFields' => $paymentFields,
// 'blocks' => [
// 'editor' => 'woocommerce/checkout',
// 'frontend' => 'woocommerce/checkout',
// ],
];
}
}
?>
11 changes: 11 additions & 0 deletions includes/class-wc-gateway-komoju-single-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
*/
class WC_Gateway_Komoju_Single_Slug extends WC_Gateway_Komoju
{
public static $instances = [];
protected $publishableKey;
protected $payment_method;
protected $debug;
protected $invoice_prefix;
protected $secretKey;
protected $webhookSecretToken;
protected $komoju_api;
protected $instructions;
protected $useOnHold;

public function __construct($payment_method)
{
$slug = $payment_method['type_slug'];
Expand Down
34 changes: 34 additions & 0 deletions includes/js/komoju-fields-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function registerPaymentMethod(paymentMethod) {
const settings = window.wc.wcSettings.getSetting(`${paymentMethod.title}_data`, {});
const label = window.wp.htmlEntities.decodeEntities(settings.title) || window.wp.i18n.__(paymentMethod.title, paymentMethod.title);

// Need to rework WC_Gateway_Komoju_Single_Slug::payment_fields to get the komoju-fields element to show up
const createContent = () => {
return '';
};

// Need to rework WC_Gateway_Komoju_Single_Slug::payment_fields to get the komoju-fields element to show up
const createEdit = () => {
return '';
};

const Block_Gateway = {
name: paymentMethod.title,
label: label,
content: createContent(),
edit: createEdit(),
canMakePayment: () => true,
ariaLabel: label,
supports: {
features: settings.supports,
},
};
window.wc.wcBlocksRegistry.registerPaymentMethod(Block_Gateway);
}

window.addEventListener('load', () => {
paymentMethodData = window.wc.wcSettings.getSetting('paymentMethodData', {});
Object.values(paymentMethodData).forEach((value) => {
registerPaymentMethod(value);
});
});
70 changes: 61 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
Plugin Name: KOMOJU Payments
Plugin URI: https://github.com/komoju/komoju-woocommerce
Description: Extends WooCommerce with KOMOJU gateway.
Version: 3.0.8
Version: 3.1.0
Author: KOMOJU
Author URI: https://komoju.com
WC tested up to: 8.5
*/

add_action('plugins_loaded', 'woocommerce_komoju_init', 0);
Expand All @@ -20,22 +21,35 @@ function woocommerce_komoju_init()
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_komoju_gateway($methods)
{
function get_komoju_payment_methods() {
require_once 'class-wc-gateway-komoju.php';
require_once 'includes/class-wc-gateway-komoju-single-slug.php';
$methods[] = new WC_Gateway_Komoju();

$komoju_payment_methods = get_option('komoju_woocommerce_payment_methods');
if (gettype($komoju_payment_methods) == 'array') {
foreach ($komoju_payment_methods as $payment_method) {
$methods[] = new WC_Gateway_Komoju_Single_Slug($payment_method);
$methods[] = WC_Gateway_Komoju::getInstance();

// If single slug instances don't exist, instantiate them
if (empty(WC_Gateway_Komoju_Single_Slug::$instances)) {
$komoju_payment_methods = get_option('komoju_woocommerce_payment_methods');

if (gettype($komoju_payment_methods) == 'array') {
foreach ($komoju_payment_methods as $payment_method) {
$method = new WC_Gateway_Komoju_Single_Slug($payment_method);
WC_Gateway_Komoju_Single_Slug::$instances[] = $method;
}
}
}

if (!empty(WC_Gateway_Komoju_Single_Slug::$instances)) {
$methods = array_merge($methods, WC_Gateway_Komoju_Single_Slug::$instances);
}

return $methods;
}

function woocommerce_add_komoju_gateway($methods) {
return get_komoju_payment_methods();
}

/**
* Add the KOMOJU settings page to WooCommerce
**/
Expand Down Expand Up @@ -64,6 +78,7 @@ function woocommerce_komoju_load_scripts()

wp_enqueue_script('komoju-fields', $komoju_fields_js);
}

function woocommerce_komoju_load_script_as_module($tag, $handle, $src)
{
if ($handle !== 'komoju-fields') {
Expand Down Expand Up @@ -92,10 +107,47 @@ function woocommerce_komoju_handle_http_request()
}
}

/**
* Custom function to declare compatibility with cart_checkout_blocks feature
*/
function woocommerce_komoju_declare_checkout_blocks_compatibility() {
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true);
}
}

/**
* Custom function to register a payment method type
*/
function woocommerce_komoju_register_order_approval_payment_method_type() {
if (!class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
return;
}

// Include the custom Blocks Checkout class
require_once 'includes/class-wc-gateway-komoju-block.php';

add_action(
'woocommerce_blocks_payment_method_type_registration',
function(Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) {
$komoju_payment_methods = get_komoju_payment_methods();

foreach ($komoju_payment_methods as $payment_method) {
$payment_method_registry->register(new WC_Gateway_Komoju_Blocks($payment_method));
}
}
);
}

add_filter('woocommerce_payment_gateways', 'woocommerce_add_komoju_gateway');

// Block setup actions
add_action('before_woocommerce_init', 'woocommerce_komoju_declare_checkout_blocks_compatibility');
add_action('woocommerce_blocks_loaded', 'woocommerce_komoju_register_order_approval_payment_method_type');

add_filter('woocommerce_get_settings_pages', 'woocommerce_add_komoju_settings_page');
add_action('woocommerce_api_wc_gateway_komoju', 'woocommerce_komoju_handle_http_request');

add_action('wp_enqueue_scripts', 'woocommerce_komoju_load_scripts');
add_filter('script_loader_tag', 'woocommerce_komoju_load_script_as_module', 10, 3);
}
}
9 changes: 9 additions & 0 deletions komoju-php/komoju-php/lib/komoju/KomojuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class KomojuApi
{
protected $endpoint;
protected $via;
protected $secretKey;

public static function defaultEndpoint()
{
return 'https://komoju.com';
Expand All @@ -17,6 +21,11 @@ public static function endpoint()
return $endpoint;
}

public function getSecretKey()
{
return $this->secretKey;
}

public function __construct($secretKey)
{
$this->endpoint = self::endpoint();
Expand Down
Loading