Skip to content

Commit

Permalink
Support for checkout blocks (#534)
Browse files Browse the repository at this point in the history
* Added support for checkout blocks
  • Loading branch information
yashgit891 authored Feb 22, 2024
1 parent 5cb0bc1 commit 790f631
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
45 changes: 45 additions & 0 deletions checkout-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

final class WC_Razorpay_Blocks extends AbstractPaymentMethodType
{
protected $name = 'razorpay';

public function initialize()
{
$this->settings = get_option('woocommerce_razorpay_settings', []);
}

public function get_payment_method_script_handles()
{
wp_register_script(
'razorpay-blocks-integration',
plugin_dir_url(__FILE__) . 'checkout_block.js',
[
'wc-blocks-registry',
'wc-settings',
'wp-element',
'wp-html-entities',
'wp-i18n',
],
null,
true
);

if (function_exists('wp_set_script_translations'))
{
wp_set_script_translations('razorpay-blocks-integration');
}

return ['razorpay-blocks-integration'];
}

public function get_payment_method_data()
{
return [
'title' => 'Pay by Razorpay',
'description' => $this->settings['description'],
];
}
}
18 changes: 18 additions & 0 deletions checkout_block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const settings = window.wc.wcSettings.getSetting('razorpay_data', {});
const label = window.wp.htmlEntities.decodeEntities(settings.title) || window.wp.i18n.__('Razorpay for woocommerce', 'razorpay');
const Content = () => {
return window.wp.htmlEntities.decodeEntities(settings.description || '');
};
const Block_Gateway = {
name: 'razorpay',
label: label,
content: Object(window.wp.element.createElement)(Content, null ),
edit: Object(window.wp.element.createElement)(Content, null ),
canMakePayment: () => true,
ariaLabel: label,
supports: {
features: settings.supports,
},
};
window.wc.wcBlocksRegistry.registerPaymentMethod( Block_Gateway );

33 changes: 33 additions & 0 deletions woo-razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Razorpay\Api\Api;
use Razorpay\Api\Errors;
use Automattic\WooCommerce\Utilities\OrderUtil;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;

add_action('plugins_loaded', 'woocommerce_razorpay_init', 0);
add_action('admin_post_nopriv_rzp_wc_webhook', 'razorpay_webhook_init', 10);
Expand All @@ -47,6 +48,38 @@
}
});

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

add_action('woocommerce_blocks_loaded', 'razorpay_woocommerce_block_support');

function razorpay_woocommerce_block_support()
{
if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType'))
{
require_once dirname( __FILE__ ) . '/checkout-block.php';

add_action(
'woocommerce_blocks_payment_method_type_registration',
function(PaymentMethodRegistry $payment_method_registry) {
$container = Automattic\WooCommerce\Blocks\Package::container();
$container->register(
WC_Razorpay_Blocks::class,
function() {
return new WC_Razorpay_Blocks();
}
);
$payment_method_registry->register($container->get(WC_Razorpay_Blocks::class));
},
5
);
}
}

function woocommerce_razorpay_init()
{
add_action("woocommerce_update_options_advanced", 'hposInstrumentation');
Expand Down

0 comments on commit 790f631

Please sign in to comment.