Skip to content

Instantly share code, notes, and snippets.

@swaters2858
Created January 16, 2018 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swaters2858/4a7a69acac3677c0826aaad0ff2cdfd7 to your computer and use it in GitHub Desktop.
Save swaters2858/4a7a69acac3677c0826aaad0ff2cdfd7 to your computer and use it in GitHub Desktop.
wc-custom-payment-gateways-by-sam
WC Custom Payment Gateways by Sam - Version 1.2- - by Samantha Waters
- Added support for WooCommerce Subscriptions cancellations plus all other subscription options.
WC Custom Payment Gateways by Sam - Version 1.1- - by Samantha Waters
General Usage Notes
- This plugin only integrates only with WooCommerce by Automattic and Woocommerce Subscriptions by Prospress Inc..
- Custom payment gateways marks orders as paid, though they do not require online payment.
Version Notes
- Added complete support for WooCommerce Subscriptions version 2.2.12 by Prospress Inc.; can now use custom payment gateways to accept payments for all subscription sign ups and changes.
WC Custom Payment Gateways by Sam - Version 1.0 - by Samantha Waters
General Usage Notes
- This plugin only integrates only with WooCommerce.
- Payment gateways marks orders as paid, though the payment gateway does not require payment.
Version Notes
- This allows two basic custom payment gateways that does not require any form of online payment.
- Settings for custom payment gateways can be found in woocommerce settings, in the checkout tab, in the same list as all other standard WooCommerce payment gateways.
- Custom payment gateway fields shown in WooCommerce settings include enable/disable checkbox, title and description (customer message). Both fields include a tooltip/desciption.
<?php
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;
//Adds Child Gateway Class
add_action( 'plugins_loaded', 'init_call_for_payment_info' );
function init_call_for_payment_info() {
class WC_Gateway_Call_for_Payment_Info extends WC_Payment_Gateway {
//Construct Gateway
function __construct() {
$this->id = 'call_for_payment_info';
$this->icon =
$this->has_fields = false;
$this->method_title = 'Call for Payment Information';
$this->method_description = 'Have customers to checkout without paying, but request a call for payment options.';
//define and load settings fields
$this->init_form_fields();
$this->init_settings();
//get the settings and load them into variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
//add a save hook for your settings
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_custom', array( $this, 'thankyou_page' ) );
//options shown in admin on gateway settings page
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Call for Payment Information', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Call for Payment Information', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
'default' => __( 'Please verify your profile has the correct phone number.', 'woocommerce' ),
'desc_tip' => true,
),
);
}
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
//mark order as paid
$order->payment_complete();
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
}
function add_call_for_payment_info( $methods ) {
$methods[] = 'WC_Gateway_Call_for_Payment_Info';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_call_for_payment_info' );
?>
<?php
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;
//Adds Child Gateway Class
add_action( 'plugins_loaded', 'init_debit_account_on_file' );
function init_debit_account_on_file() {
class WC_Gateway_Debit_Account extends WC_Payment_Gateway {
//Construct Gateway
function __construct() {
$this->id = 'debit_account_on_file';
$this->icon =
$this->has_fields = false;
$this->method_title = 'Debit Account On File';
$this->method_description = 'Have customers checkout without paying, but agree to allow their account on file to be charged.';
//define and load settings fields
$this->init_form_fields();
$this->init_settings();
//get the settings and load them into variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
//supports all subscription features
$this->supports = array(
'subscriptions',
'products',
'subscription_cancellation',
'subscription_reactivation',
'subscription_suspension',
'subscription_amount_changes',
'subscription_payment_method_change',
'subscription_date_changes',
);
//add a save hook for your settings
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_custom', array( $this, 'thankyou_page' ) );
//options shown in admin on gateway settings page
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Debit Account On File', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Debit Account On File', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
'default' => __( 'Your payment type on file will be used.', 'woocommerce' ),
'desc_tip' => true,
),
);
}
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
//mark order as paid
$order->payment_complete();
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
}
function add_debit_account_on_file( $methods ) {
$methods[] = 'WC_Gateway_Debit_Account';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_debit_account_on_file' );
?>
<?php
/*
Plugin Name: WC Custom Payment Gateway by Sam
Plugin URI:
Description: Creates a custom payment gateway to debit the customer's account on file, and a gateway to call for payment options.
Version: 1.2
Author: Samantha Waters
Author URI: https://swatersdesign.com/
*/
include_once('debit-account-on-file.php');
include_once('call-for-payment-information.php');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment