Integrating Custom WooCommerce Payment Gateways with WooCommerce Checkout Blocks

In this article, I’ll walk you through the steps to create your own custom WooCommerce payment gateway, complete with compatibility for WooCommerce checkout blocks. Throughout this tutorial, I will guide you through creating a new custom payment gateway for WooCommerce checkout blocks, named “JWC Custom Gateway”.

Let’s initiate the process with an In-depth Guide to Integrating Custom Payment Gateways into WooCommerce Checkout Blocks.

Step 1: Create a payment gateway plugin for “JWC Custom Gateway”.

Creating a plugin is the initial step in implementing a custom WooCommerce payment gateway, ensuring smooth integration into the platform.

/*
Plugin Name: JWC Custom Gateway
Description: A custom payment gateway for WooCommerce.
Version: 1.0.0
Author: Jinesh P.V
Author URI: https://jineshpv.com
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Text Domain: jwc-custom-gateway
Domain Path: /languages
*/

Step 2: Create the Custom Payment Gateway Class

In the directory named ‘jwc-custom-gateway,’ establish a PHP file, such as ‘class-jwc-custom-gateway.php,’ and define your custom gateway class, which must inherit from the WooCommerce-provided WC_Payment_Gateway class.

<?php
class JWC_Custom_Gateway extends WC_Payment_Gateway {
    // Constructor method
    public function __construct () {
        $this->id                 = 'jwc_custom_gateway';
        $this->method_title       = __( 'JWC Custom Gateway', 'jwc-custom-gateway' );
        $this->method_description = __( 'Accept payments through JWC Custom Gateway', 'jwc-custom-gateway' );
        
        // Other initialization code goes here
        $this->init_form_fields();
        $this->init_settings();
        
        add_action ( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ) ;
    }
  
    public function init_form_fields() {
        $this->form_fields = array (
            'enabled' => array (
                'title'   => __( 'Enable/Disable', 'jwc-custom-gateway' ),
                'type'    => 'checkbox',
                'label'   => __( 'Enable JWC Custom Gateway', 'jwc-custom-gateway' ),
                'default' => 'yes',
            ),
        );
    }
    // Process the payment
    public function process_payment( $order_id ) {
        $order = wc_get_order ( $order_id );
        $order->payment_complete();
        
        // Redirect to the thank you page
        return array (
            'result'   => 'success',
            'redirect' => $this->get_return_url( $order ),
        );
    }
}
?>

Step 3: Integrate with WooCommerce

Within your primary plugin file, integrate your custom gateway with WooCommerce by leveraging its designated hooks and actions. This integration seamlessly merges your gateway into the WooCommerce framework, granting it full compatibility and visibility among the platform’s payment methods.

<?php
add_action ( 'plugins_loaded', 'woocommerce_jwc_custom_gateway_loaded', 0 );
function woocommerce_jwc_custom_gateway_loaded() {
    // if the WC payment gateway class 
    if ( ! class_exists ( 'WC_Payment_Gateway' ) )
        return;
    
    include ( plugin_dir_path ( __FILE__ ) . 'class-jwc-custom-gateway.php' );
}

// Add JWC_Custom_Gateway to WooCommerce
add_filter ( 'woocommerce_payment_gateways',  'add_jwc_custom_gateway' );
function add_jwc_custom_gateway( $gateways ) {
    $gateways[] = 'JWC_Custom_Gateway' ;
    return $gateways;
}
?>

Step 4: Declare the plugin support WooCommerce checkout blocks.

With the newly introduced plugin, users can now enjoy a streamlined and more efficient checkout process, enhancing both convenience and speed. Simply integrate it into the plugin’s primary file.

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

// Hook the custom function to the 'before_woocommerce_init' action
add_action ( 'before_woocommerce_init', 'declare_cart_checkout_blocks_compatibility' );
?>

Step 5: Register WooCommerce blocks payment method type registration.

Initiate the registration process for the Woocommerce checkout blocks payment method type to seamlessly integrate it within the platform, guaranteeing compatibility and accessibility for users seeking a range of payment options within the Woocommerce ecosystem.

<?php
// Hook the custom function to the 'woocommerce_blocks_loaded' action
add_action ( 'woocommerce_blocks_loaded', 'jwc_register_order_approval_payment_method_type' );

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

    require_once plugin_dir_path ( __FILE__ ) . 'class-jwc-gateway-block.php';
    add_action ( woocommerce_blocks_payment_method_type_registration',
        function ( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
            $payment_method_registry->register ( new My_JWC_Custom_Gateway_Blocks );
        }
    );
}
?>

Step 6: Develop a block-based Payment Gateway class for WooCommerce Checkout.

<?php
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
final class JWC_Custom_Gateway_Blocks extends AbstractPaymentMethodType {
    private $gateway;

    protected $name = 'jwc_custom_gateway';

    public function initialize() {
        $this->settings = get_option ( 'woocommerce_jwc_custom_gateway_settings', [] );
        $this->gateway = new JWC_Custom_Gateway();
    }

    public function is_active() {
        return $this->gateway->is_available();
    }

    public function get_payment_method_script_handles() {
        wp_register_script (
            'jwc_custom_gateway-blocks-integration',
            plugin_dir_url ( __FILE__ ) . 'checkout.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 ( 'jwc_custom_gateway-blocks-integration');
            
        }
        return [ 'jwc_custom_gateway-blocks-integration' ];
    }

    public function get_payment_method_data() {
        return [
            'title' => $this->gateway->title,
            'description' => $this->gateway->description,
        ];
    }
}
?>

Step 7: Create js files for the block.

Create JavaScript scripts specialized for the checkout block, designed to elevate its functionality and facilitate engaging user interactions within the specified application environment.

const settings = window.wc.wcSettings.getSetting( 'jwc_custom_gateway_data', {} );
const label = window.wp.htmlEntities.decodeEntities( settings.title ) || window.wp.i18n.__( 'JWC Custom Gateway', 'jwc_custom_gateway' );
const Content = () => {
    return window.wp.htmlEntities.decodeEntities( settings.description || '' );
};
const JWC_Block_Gateway = {
    name: 'jwc_custom_gateway',
    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( JWC_Block_Gateway );

Within the scope of this article, I’ll provide comprehensive guidance on establishing a bespoke WooCommerce checkout payment gateway that seamlessly integrates with WooCommerce checkout blocks. Nonetheless, there are moments where a customized resolution is essential to cater to specific business prerequisites


Comments

77 responses to “Integrating Custom WooCommerce Payment Gateways with WooCommerce Checkout Blocks”

  1. Total game changer! The variety of games here is insane and the graphics are stunning. I spend way too much time on iny1games these days but it is totally worth it.

  2. Finally a site that doesn’t crash every five minutes. The winning streaks I’ve had here are legendary. Big thanks to phtaya789!

  3. The login process is super quick and I had no issues setting up my account. Everything feels very professional and secure. Give it a go at jilibet020comloginregister

  4. Really convenient app for anyone on the go. The layout is clean and everything works exactly as it should. Download nutristarapp and see for yourself.

  5. Finally found a platform that actually pays out fast. The game selection is top notch and the bonuses are legit. Check out ph50jili if you want a real gaming experience.

  6. The customer support is top notch. They solved my account issue in under five minutes. Great service at 3fcassinosuporte!

  7. The graphics are top-notch and the overall feel is very professional. It feels like a real luxury casino from home. winnacasino is simply the best.

  8. The game variety is just insane and the interface is super easy to navigate. Really enjoying the vibes here. Highly recommend phjilico to everyone!

  9. The login process is seamless and the community here is awesome. If you’re looking for a solid gaming hub this is it. clubpkrlogin

  10. The community here is great and the promotions are always exciting. It feels like a premium experience every time I log in to phjilipark22.

  11. The login process is super fast and I didnt run into any glitches. It makes the whole experience so much better. 3taya365login is definitely my go-to now.

  12. Hands down one of the best gaming experiences I have had in a while. The graphics are crisp and the gameplay is fair. Check out vnnoci88 if you want some real action.

  13. Really impressed with the variety of games here. I had a few questions and the support team sorted me out in minutes. vn88xxplus is the way to go.

  14. Getting into my account is always seamless. No annoying errors or long wait times. The access via dino69login is the smoothest way to get back into the action.

  15. Solid performance and great variety of options. It is my go-to site whenever I want to relax and play. Cheers to bdq444.

  16. The atmosphere is electric and the games are truly engaging. I’ve had a few big wins here and the withdrawal process was a breeze. phfierycasino is the real deal!

  17. The VIP perks are actually worth it. I feel like a high roller even with modest bets. If you want a premium feel then cassinovip222 is where it is at.

  18. If you are looking for a place with a huge range of betting options, this is it. Very transparent and easy to navigate. Visit miliarbetlogin.

  19. Love the local vibe of this site. It feels tailored for us and the customer support is actually helpful. Glad I found phauronplay.

  20. Great rewards and even better customer support. I had a small issue with my account and they sorted it out in minutes. Big thanks to vntc88.

  21. Was struggling to find the official download link but this site made it so easy. The app is lightweight and runs perfectly on my old phone. Visit ok777betappdownload to get started.

  22. I love the rewards system on this platform. It keeps things exciting and motivating. I am really enjoying my time at 777acom.

  23. Quick deposits and even quicker withdrawals. That is all I really care about and they nailed it. Trust idhura500 for your wins.

  24. The app is very intuitive and the game selection is massive. It runs perfectly on my phone without any glitches. Definitely download jljl333apps if you’re a serious player.

  25. Love the variety of games available here. It keeps things exciting and there is always something new to try. Highly recommend qq8bet

  26. Best slot games in the Philippines for sure. The jackpot hits are real and the loading speed is impressive. Try your luck at phjilihot

  27. Really impressed with the security and transparency of this platform. It feels safe and the withdrawal process is seamless. Give idmpoasia88 a try

  28. I love the slot selection and the bonuses for new members are generous. Such a fun way to spend the weekend at poneclubslot.

  29. The interface is very user friendly and the bonuses are actually fair. I had a blast playing last night. Highly recommend arushokilogin to everyone.

  30. Finally found an app that doesn’t lag during big wins. The bonus offers are actually fair and easy to claim. Give pkb9app a try guys, it’s a game changer.

  31. Finally found a platform that doesn’t lag during peak hours. The interface is super smooth and easy to navigate. Highly recommend krikialogin to everyone.

  32. This platform has everything I was looking for. The layout is user-friendly and the features are top-notch. Definitely spending more time on 777ggplataforma from now on.

  33. Finally found a place where the bonuses actually help you win. The customer support at 55win1 is top notch and very helpful.

  34. Finally found a place where the bonuses actually make sense. Had a great run last night and the interface is very user-friendly. Check out g7777casino if you want some real action.

  35. I really love the interface here, very smooth and easy to navigate. The bonuses for new players are actually generous. If you are in the Philippines, give phslotmaxvip a try, it is legit!

  36. The app version is just fantastic. I can play anywhere and the graphics are crisp. Definitely the best way to access vnhitclubapp on the go.

  37. Finally found a site that actually works without any lag. The interface is super clean and getting started was a breeze. Definitely checking out yolo47login more often!

  38. I’ve tried a few sites lately but this one is definitely smoother. The login process is quick and I didn’t run into any lag while playing. Highly recommend oxibet88login for anyone looking for a stable platform.

  39. The variety of games here is insane. I’m really enjoying the vibe on brtigrinhobet, definitely worth a visit.

  40. Signing up was a breeze. I was able to start playing within minutes without any hassle. Go to spintimephsignup to get started.

  41. The app version is so much more convenient than the browser. Everything is just a tap away and it runs perfectly on my phone. Love 89batapp

  42. I’ve been trying a few apps lately and this one is actually smooth. The payouts are fast and the games are super addictive. Definitely check out phjilli777 if you want some real fun.

  43. Solid platform with a great vibe. I managed to hit a decent win yesterday and the withdrawal was fast. Cheers to brvx777 for the awesome service.

  44. Great experience so far. The customer support is actually helpful and doesn’t just give canned responses. Love the energy at auronplaycasino.

  45. Everything about this site feels premium. From the customer service to the actual gaming experience, it is just flawless. you7777

  46. Really liking the vibe of this platform. The registration process was quick and I was playing in minutes. rusia777 is definitely my new favorite hangout.

  47. Finally found a site that doesn’t lag during peak hours. The login process is seamless and the game variety is top notch. Highly recommended for local players. Visit phskycomlogin now.

  48. The community here is great and the odds are very competitive. I’ve had a few big wins already this week. Check out idmamajitu for a solid gaming experience.

  49. Quick deposits and even quicker withdrawals. That is all I really care about and they deliver every time. bd777u is honestly the most trustworthy site I have used recently.

  50. Fast registration and even faster gameplay. I love the variety of options available on this platform. It’s very refreshing. Visit idhulk123

  51. Love how intuitive the layout is. I didn’t have to spend any time figuring out where everything was. Everything is just right at ph798ph.

  52. Quick access and no downtime. I appreciate the simplicity of the login process and the overall speed of the platform. kk77login is a winner.

  53. Best gaming experience I’ve had in a while. The graphics are crisp and the payouts are legit. Give phroyalbet a try!

  54. The game variety is insane! I spent the whole weekend exploring different titles and I am hooked. Give br777games a try if you love gaming.

  55. Super friendly vibe and very easy to navigate. I had a small issue with my deposit but support fixed it in minutes. lisboa77 is legit and trustworthy.

  56. Best gaming experience I have had in a while. The bonuses are generous and the site is very stable. Visit pky88888

  57. The login process is seamless and I didn’t encounter any lags during my session. Great experience overall with cwdbetlogin. Keep it up guys!

  58. Super easy registration process and the site loads quickly on my phone. It is just a straightforward and reliable place to play. Glad I found idmpobos

  59. The app version is just fantastic. I can play anywhere and the graphics are crisp. Definitely the best way to access vnhitclubapp on the go.

  60. Finally found an APK that actually works without any lag. The interface is very user friendly and the payouts are legit. Love using peryaplusapk on my phone

  61. I’ve tried a few sites lately but this one is surprisingly smooth. The payouts are quick and the interface is easy to navigate. Definitely worth a visit vx7775

  62. Really impressed with the variety of games available here. It keeps things exciting and the customer support is top notch. Cheers to okbetgamesonline

  63. Best experience I’ve had in a while. Everything from the graphics to the payouts is top notch. Glad I found phjljl5.

  64. Honestly one of the most intuitive sites I have encountered. I really enjoy the overall experience on bdqq7777.

  65. Love the variety of games available here. Everything loads quickly and I haven’t faced any lag during my sessions. Give inzeta777 a try, you won’t regret it.

  66. pkgb777 is a gem. I appreciate how transparent they are with their rules and promotions. It makes the whole gaming experience much more relaxing and trustworthy.

  67. Solid experience so far. The customer support responded to my query within minutes which is rare these days. Big thumbs up for phskycomph.

  68. The registration was a breeze and I started playing almost immediately. It is refreshing to find a site that values the users time. idsinar777

  69. Solid platform with a great community vibe. It is always exciting to see who is winning today. I am really enjoying bdtak999.

  70. Just joined last week and I am already impressed by the payout speed. It is rare to find such a transparent platform these days. Cheers to bddaka88

  71. Finally found the official source for the app. The installation was a breeze and the performance is top notch. If you are wondering where to get it, just head over to jljl5download.

  72. I had a great run yesterday and won big! The graphics are sleek and the gameplay is fair. I will definitely be coming back to korwin39

  73. The login process is super fast and secure. I appreciate how straightforward the dashboard is on 77bbbetlogin. Makes gaming much more enjoyable.

  74. Very reliable platform with a wide range of payment methods. Everything is transparent and fast. I trust rupiahbetlogin completely.

  75. Finally found a site that actually cares about user experience. The bonuses are generous and the support team is helpful. Love phskycomcasinologin.

  76. Always good to have a working alternative link. This one is stable and gets me right where I need to be without any lag. linkalternatifjasabola

  77. Really loving the interface here. The loading speed is fast and the games are super smooth. Definitely my new favorite spot to hang out. Check out dreamjili2login

Leave a Reply

Your email address will not be published. Required fields are marked *