Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ewallz/04f961803e0b86075f81e71f0a2cc5cb to your computer and use it in GitHub Desktop.
Save ewallz/04f961803e0b86075f81e71f0a2cc5cb to your computer and use it in GitHub Desktop.
Woo Custom Payment Gateway
<?php
/**
* Plugin Name: WooCommerce Custom Payment Gateway
* Version: 1.0.0
**/
class WC_Other_Payment_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'other_payment';
$this->method_title = __('Custom Payment', 'woocommerce-other-payment-gateway');
$this->title = __('Custom Payment', 'woocommerce-other-payment-gateway');
$this->has_fields = true;
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option('enabled');
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce-other-payment-gateway'),
'type' => 'checkbox',
'label' => __('Enable Custom Payment', 'woocommerce-other-payment-gateway'),
'default' => 'yes'
),
'title' => array(
'title' => __('Method Title', 'woocommerce-other-payment-gateway'),
'type' => 'text',
'description' => __('This controls the title', 'woocommerce-other-payment-gateway'),
'default' => __('Custom Payment', 'woocommerce-other-payment-gateway'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Customer Message', 'woocommerce-other-payment-gateway'),
'type' => 'textarea',
'css' => 'width:500px;',
'default' => 'None of the other payment options are suitable for you? please drop us a note about your favourable payment option and we will contact you as soon as possible.',
'description' => __('The message which you want it to appear to the customer in the checkout page.', 'woocommerce-other-payment-gateway'),
)
);
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*
* @since 1.0.0
* @return void
*/
public function admin_options() {
?>
<h3><?php _e('Custom Payment Settings', 'woocommerce-other-payment-gateway'); ?></h3>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table><!--/.form-table-->
</div>
</div>
<div class="clear"></div>
<style type="text/css">
.wpruby_button{
background-color:#4CAF50 !important;
border-color:#4CAF50 !important;
color:#ffffff !important;
width:100%;
padding:5px !important;
text-align:center;
height:35px !important;
font-size:12pt !important;
}
</style>
<?php
}
public function process_payment($order_id) {
global $woocommerce;
$order = new WC_Order($order_id);
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __('Awaiting payment', 'woocommerce-other-payment-gateway'));
// Reduce stock levels
$order->reduce_order_stock();
if (isset($_POST[$this->id . '-admin-note']) && trim($_POST[$this->id . '-admin-note']) != '') {
$order->add_order_note(esc_html($_POST[$this->id . '-admin-note']), 1);
}
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order)
);
}
public function payment_fields() {
?>
<div class="lm_payment-method-form">
<p class="form-row form-row form-row-wide validate-required">
<label for="wc-authorize-net-cim-credit-card-account-number" class="">
Card Number
<abbr class="required" title="required">*</abbr>
</label>
<input type="text" class="input-text" name="lm_card_number" id="lm_card_number" placeholder="•••• •••• •••• ••••" maxlength="20" value="" autocomplete="cc-number">
</p>
<div class="form-row form-row form-row-wide validate-required">
Expiration
<abbr class="required" title="required">
*
</abbr>
<div class="col2-set">
<div class="col-1">
<select name="lm_cardexp_m" id="lm_cardexp_m" class="select">
<option value="0">Select Month</option>
<?php
for ($i = 1; $i <= 12; $i++) {
$j = $i < 10 ? "0" . $i : $i;
echo "<option value='$j'>$j - " . date('F', mktime(0, 0, 0, $i, 10)) . "</option>";
}
?>
</select>
</div>
<div class="col-2">
<select name="lm_cardexp_y" id="lm_cardexp_m" class="select">
<option value="0">Select Year</option>
<?php
$start = date("Y");
for ($i = $start; $i <= $start + 15; $i++) {
$j = substr($i, 2);
echo "<option value='$j'>$i</option>";
}
?>
</select>
</div>
</div>
<div class="col2-set">
<div class="col-1">
<input type="text" class="input-text cvv" name="lm_card_csc" id="lm_card_csc" placeholder="CSC" maxlength="3" value="" autocomplete="off">
</div>
<div class="col-2">
<select name="lm_card_type" id="lm_card_type" class="select">
<option value="visa">Visa</option>
<option value="master">Master Card</option>
<option value="discover">Discover</option>
<option value="amex">American Express</option>
<option value="maestro">Maestro</option>
</select>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('.card_expiry').mask('00/00');
$('.cvv').mask('000');
})
</script>
<?php
}
}
$active_plugins = apply_filters('active_plugins', get_option('active_plugins'));
if(in_array('woocommerce/woocommerce.php', $active_plugins)){
add_filter('woocommerce_payment_gateways', 'add_other_payment_gateway');
function add_other_payment_gateway( $gateways ){
$gateways[] = 'WC_Other_Payment_Gateway';
return $gateways;
}
add_action('plugins_loaded', 'init_other_payment_gateway');
function init_other_payment_gateway(){
require 'class-woocommerce-other-payment-gateway.php';
}
add_action( 'plugins_loaded', 'other_payment_load_plugin_textdomain' );
function other_payment_load_plugin_textdomain() {
load_plugin_textdomain( 'woocommerce-other-payment-gateway', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment