GeneratorsWooCommercePayment Gateway

Payment Gateway Generator

A WC_Payment_Gateway subclass — offline (mark on-hold, confirm by hand) or a redirect-to-processor stub with a webhook — using the exact result/redirect contract process_payment() must return.

class-acme-gateway-gateway.php
Saved just now
The gateway
Acme_Gateway_Gateway
Instructions
Show instructions after checkout
Printed on the order-received page and in the order emails, the way Cheque/BACS do it in core.
Marks the order on-hold until you confirm payment by hand — the shape of Cheque/BACS.
<?php
// Generated with WP CodeKit — powered by GrowQuest (https://growquest.io).
/**
 * Plugin Name:       Acme Bank Transfer
 * Description:       Registers the acme_gateway payment gateway.
 * Version:           1.0.0
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Text Domain:       acme
 */

defined( 'ABSPATH' ) || exit;

/**
 * Declare the class once WooCommerce's own gateway base class exists.
 */
function acme_gateway_init() {
	if ( class_exists( 'Acme_Gateway_Gateway' ) ) {
		return;
	}

	class Acme_Gateway_Gateway extends WC_Payment_Gateway {

	public function __construct() {
		$this->id                 = 'acme_gateway';
		$this->icon               = '';
		$this->has_fields         = false;
		$this->method_title       = __( 'Acme Bank Transfer', 'acme' );
		$this->method_description = __( 'Accepts manual bank transfers, confirmed by the store owner.', 'acme' );
		$this->supports           = array( 'products' );

		$this->init_form_fields();
		$this->init_settings();

		$this->title       = $this->get_option( 'title' );
		$this->description = $this->get_option( 'description' );
		$this->enabled     = $this->get_option( 'enabled' );
		$this->instructions = $this->get_option( 'instructions' );
		add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
		add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );

		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
	}

	/**
	 * The settings fields, keyed by option name.
	 */
	public function init_form_fields() {
		$this->form_fields = array(
		'enabled' => array(
			'title'   => __( 'Enable/Disable', 'acme' ),
			'type'    => 'checkbox',
			'label'   => __( 'Enable this payment method', 'acme' ),
			'default' => 'no',
		),

		'title' => array(
			'title'       => __( 'Title', 'acme' ),
			'type'        => 'text',
			'description' => __( 'Shown to the customer during checkout.', 'acme' ),
			'default'     => __( 'Pay by bank transfer', 'acme' ),
			'desc_tip'    => true,
		),

		'description' => array(
			'title'   => __( 'Description', 'acme' ),
			'type'    => 'textarea',
			'default' => __( 'Make your payment directly into our bank account. Your order will ship once funds have cleared.', 'acme' ),
		),

		'instructions' => array(
			'title'       => __( 'Instructions', 'acme' ),
			'type'        => 'textarea',
			'description' => __( 'Shown on the order-received page and in the order emails.', 'acme' ),
			'default'     => __( 'Our bank details are: Acme Ltd, Sort code 00-00-00, Account 00000000. Use your order number as the payment reference.', 'acme' ),
			'desc_tip'    => true,
		),
		);
	}

	/**
	 * Handle the order once the customer confirms payment.
	 *
	 * @param int $order_id Order ID.
	 * @return array
	 */
	public function process_payment( $order_id ) {
		$order = wc_get_order( $order_id );

		$order->update_status( 'on-hold', __( 'Awaiting manual payment confirmation.', 'acme' ) );

		wc_reduce_stock_levels( $order_id );
		WC()->cart->empty_cart();

		return array(
			'result'   => 'success',
			'redirect' => $this->get_return_url( $order ),
		);
	}

	/**
	 * Print the instructions on the order-received page.
	 */
	public function thankyou_page() {
		if ( $this->instructions ) {
			echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
		}
	}

	/**
	 * Print the instructions in the order emails, while the order is on-hold.
	 *
	 * @param WC_Order $order         The order.
	 * @param bool     $sent_to_admin Whether this copy goes to the store admin.
	 * @param bool     $plain_text    Whether the email is plain text.
	 */
	public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
		if ( $this->instructions && ! $sent_to_admin && 'acme_gateway' === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) {
			echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) ) . PHP_EOL;
		}
	}
	}
}
add_action( 'plugins_loaded', 'acme_gateway_init' );

/**
 * Register it so it appears under WooCommerce → Settings → Payments.
 *
 * @param array $gateways Existing gateways.
 * @return array
 */
function acme_add_gateway( $gateways ) {
	$gateways[] = 'Acme_Gateway_Gateway';

	return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'acme_add_gateway' );