Custom WooCommerce payment gateway

/**
 * Creates custom payment gateway.
 *
 * @see https://wpcodebook.com/custom-woocommerce-payment-gateway/
 */
add_action( 'plugins_loaded', function () {

	if (
		! class_exists( 'WC_Payment_Gateway' ) ||
		class_exists( 'WPCodebook_WC_Payment_Gateway' )
	) {
		return;
	}

	/**
	 * WPCodebook_WC_Payment_Gateway class.
	 *
	 * @see https://woocommerce.github.io/code-reference/classes/WC-Payment-Gateway.html
	 */
	class WPCodebook_WC_Payment_Gateway extends WC_Payment_Gateway {

		/**
		 * Constructor.
		 */
		function __construct() {
			$this->id                 = 'wpcodebook_gateway';
			$this->method_title       = esc_html__( 'My Payment Gateway' );  // Back-end title
			$this->method_description = esc_html__( 'My payment gateway.' ); // Back-end description
			$this->title              = esc_html__( 'My Payment Gateway' );  // Front-end title
		}

		/**
		 * Process payment.
		 */
		function process_payment( $order_id ) {
			return array(
				'result'   => 'success',
				'redirect' => $this->get_return_url( wc_get_order( $order_id ) ),
			);
		}

	}

	/**
	 * Loads the gateway.
	 *
	 * @see https://github.com/woocommerce/woocommerce/blob/9.3.3/plugins/woocommerce/includes/class-wc-payment-gateways.php#L94
	 */
	add_filter( 'woocommerce_payment_gateways', function ( $gateways ) {
		$gateways[] = 'WPCodebook_WC_Payment_Gateway';
		return $gateways;
	} );

} );

Leave a Comment