Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active August 29, 2015 14:06
Show Gist options
  • Save rayflores/3aba1e28a9d7380ddf90 to your computer and use it in GitHub Desktop.
Save rayflores/3aba1e28a9d7380ddf90 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WooCommerce FIS Gateway
Plugin URI: http://rayflores.com/plugins/fis-gateway
Description: Extends WooCommerce with a <a href="https://paydirect.ca.link2gov.com/SFACArtsSales" target="_blank">FIS</a> gateway. A FIS gateway account, and a server with SSL support and an SSL certificate is required for security reasons.
Author: Ray Flores
Author URI: http://rayflores.com
Version: 1.0.0
Text Domain: wc-gateway-fis
Domain Path: /languages/
*/
/**
* Required functions
*/
if ( ! function_exists( 'woothemes_queue_update' ) )
require_once( 'woo-includes/woo-functions.php' );
add_action('plugins_loaded', 'woocommerce_gateway_fis_init', 0);
function woocommerce_gateway_fis_init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) return;
/**
* Localisation
*/
load_plugin_textdomain( 'wc-gateway-fis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
define('FIS_DIR', WP_PLUGIN_DIR . "/" . plugin_basename( dirname(__FILE__) ) . '/');
/**
* FIS Gateway Class
**/
class WC_Gateway_Fis extends WC_Payment_Gateway {
var $notify_url;
/**
* Constructor for the gateway.
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'fis';
$this->icon = apply_filters( 'woocommerce_fis_icon', WC()->plugin_url() . '/imgs/fis.png' );
$this->has_fields = false;
$this->order_button_text = __( 'Proceed to PayDirect', 'woocommerce' );
$this->liveurl = 'https://paydirect.ca.link2gov.com/SFACArtsSales';
$this->method_title = __( 'FIS', 'woocommerce' );
$this->method_description = __( 'FIS PayDirect works by sending the user to PayDirect to enter their payment information.', 'woocommerce' );
$this->notify_url = WC()->api_request_url( 'WC_Gateway_Fis' );
$this->supports = array( 'products' );
$this->has_fields = false;
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Get setting values
$this->enabled = $this->settings['enabled'];
$this->merchantcode = $this->settings['MerchantCode'];
$this->settlecode = $this->settings['SettleCode'];
$this->debugon = $this->settings['debugon'];
$this->debugrecipient = $this->settings['debugrecipient'];
// Actions
add_action( 'valid-fis-standard-request', array( $this, 'successful_request' ) );
add_action( 'woocommerce_receipt_fis', array( $this, 'receipt_page' ) );
// add_action( 'woocommerce_thankyou_fis', array( $this, 'pdt_return_handler' ) ); // not expecting a responce? no return being used at this time
add_action('woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options'));
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options'));
}
/**
* Initialize Gateway Settings Form Fields
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'wc-gateway-fis' ),
'label' => __( 'Enable FIS Gateway', 'wc-gateway-fis' ),
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'wc-gateway-fis' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'wc-gateway-fis' ),
'default' => __( 'Credit card', 'wc-gateway-fis' )
),
'description' => array(
'title' => __( 'Description', 'wc-gateway-fis' ),
'type' => 'textarea',
'description' => __( 'This controls the description which is displayed to the customer.', 'wc-gateway-fis' ),
'default' => 'Pay with your MasterCard, Visa, Discover or American Express'
),
'MerchantCode' => array(
'title' => __( 'Merchant Code', 'wc-gateway-fis' ),
'type' => 'text',
'description' => __( 'Merchant Code given to you by FIS', 'wc-gateway-fis' ),
'default' => ''
),
'SettleCode' => array(
'title' => __( 'Settle Code', 'wc-gateway-fis' ),
'type' => 'text',
'description' => __( 'Settle Code given to you by FIS', 'wc-gateway-fis' ),
'default' => ''
),
'debugon' => array(
'title' => __( 'Debugging', 'wc-gateway-fis' ),
'label' => __( 'Enable debug emails', 'wc-gateway-fis' ),
'type' => 'checkbox',
'description' => __( 'Receive emails containing the data sent to and from FIS.', 'wc-gateway-fis' ),
'default' => 'no'
),
'debugrecipient' => array(
'title' => __( 'Debugging Email', 'wc-gateway-fis' ),
'type' => 'text',
'description' => __( 'Who should receive the debugging emails.', 'wc-gateway-fis' ),
'default' => get_option('admin_email')
),
);
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*/
public function admin_options() {
?>
<p><a href="http://www.fisglobal.com/products-government-governmentpayments-webpayments/" target="_blank"><img src="<?php echo $this->logo;?>" /></a></p>
<h3><?php _e('FIS', 'wc-gateway-fis'); ?></h3>
<p><?php _e( 'FIS allows customers to checkout using a credit card by adding credit card fields on the checkout page and then sending the details to FIS for verification.', 'wc-gateway-fis' ); ?></p>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table><!--/.form-table-->
<?php
}
/**
* Limit the length of item names
* @param string $item_name
* @return string
*/
public function fis_item_name( $item_name ) {
if ( strlen( $item_name ) > 127 ) {
$item_name = substr( $item_name, 0, 124 ) . '...';
}
return html_entity_decode( $item_name, ENT_NOQUOTES, 'UTF-8' );
}
/**
* Get FIS Args for passing to FIS
*
* @access public
* @param mixed $order
* @return array
*/
function get_fis_args( $order ) {
$order_id = $order->id;
if ( 'yes' == $this->debugon ) {
$this->log = new WC_Logger();
$this->log->add( 'FIS', 'Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $this->notify_url );
}
// FIS Args
$fis_args = array(
'MerchantCode' => $this->merchantcode,
'SettleCode' => $this->settlecode,
'PaymentMethod' => 'CreditOrDebit',
'ReturnUrl' => $this->get_return_url( $order ), // build woo api link ?
'CancelUrl' => $order->get_cancel_order_url(), // cancel link?
// 'LineItems' => $this->get_line_items($order), //'[1*Item 1*Description 1*125.00],[2*Tax**15.50],[3*Shipping**30.00]'
// Billing Address info
'BillingCity' => $order->billing_city,
'BillingState' => $order->billing_state,
'BillingPostalCode' => $order->billing_postcode,
'BillingCountry' => $order->billing_country,
'BillingEmail' => $order->billing_email,
'BillingProvince' => ' ',
'BillingPhone' => $order->billing_phone
);
// Try to send line items, or default to sending the order as a whole
if ( $line_items = $this->get_line_items( $order ) ) {
//$fis_args = array_merge( $fis_args, $line_items );
}
$fis_args = apply_filters( 'woocommerce_fis_args', $fis_args );
return $fis_args;
}
private function get_line_items( $order ) {
$item_loop = 0;
$args = array();
$lineItems_1 = '';
// Products
if ( sizeof( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item ) {
if ( ! $item['qty'] ) {
continue;
}
$item_loop ++;
$product = $order->get_product_from_item( $item );
$item_name = $item['name'];
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
if ( $meta = $item_meta->display( true, true ) ) {
$item_name .= ' ( ' . $meta . ' )';
}
#1 [N*ItemName*SKU*Price]->LOOP ITEMS >0 : [N+1*ItemName*Sku*Price],[N+1*ItemName*Sku*Price]<<END ITEMS : [lastN+1*Tax**TotalTax],[taxN+1*Shipping**TotalShipping]
$lineItems_1 .= '[' . $item_loop . '*' . $this->fis_item_name( $item_name ) . '*' .$product->get_sku() . '*' . $item['line_subtotal'] . '],';
}
#2
$tax_loop = sizeof( $order->get_items() ) + 1;
$tax_items_1 = '[' . $tax_loop . '*Tax**' . $order->get_total_tax() . ']';
#3
$ship_loop = $tax_loop + 1;
$ship_items_1 = '[' . $ship_loop . '*Shipping**' . number_format( $order->get_total_shipping(), 2, '.', '' ) . ']';
$args['LineItems'] = $lineItems_1 . $tax_items_1 . ',' . $ship_items_1; // Array([LineItems] => [1*NAME*Price],[2*Tax**TotalTax],[3*Shipping**TotalShipping]) // woohoo!
$line_items_echo = $lineItems_1 . $tax_items_1 . ',' . $ship_items_1; // [1*NAME*Price],[2*Tax**TotalTax],[3*Shipping**TotalShipping] // just values
}
//return $args;
return $line_items_echo;
}
/**
* Output for the order received page.
*
* @access public
* @return void
*/
public function receipt_page( $order ) {
echo '<p>' . __( 'Thank you - your order is now pending payment. You should be automatically redirected to FIS to make payment.', 'woocommerce' ) . '</p>';
echo $this->generate_fis_form( $order );
}
/**
* Generate the FIS button link
*
* @access public
* @param mixed $order_id
* @return string
*/
function generate_fis_form( $order_id ) {
$order = wc_get_order( $order_id );
$fis_adr = $this->liveurl . '?';
$fis_args = $this->get_fis_args( $order );
//$this->get_line_items( $order );
/* $fis_args_array = array();
foreach ( $fis_args as $key => $value ) {
$fis_args_array[] = '<input type="hidden" name="'.esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
}
*/ $output = '';
$output .= '<form action="' . esc_url( $fis_adr ) . '" method="post" id="fis_payment_form" target="_top">';
$output .= '<input type="hidden" name="MerchantCode" value="' . $this->merchantcode . '" />';
$output .= '<input type="hidden" name="SettleCode" value="' . $this->settlecode . '" />';
$output .= '<input type="hidden" name="PaymentMethod" value="CreditOrDebit" />';
$output .= '<input type="hidden" name="ReturnUrl" value="' . $this->get_return_url( $order ) . '" />'; // build woo api link ?
$output .= '<input type="hidden" name="CancelUrl" value="' . $order->get_cancel_order_url() . '" />'; // cancel link?
$output .= '<input type="hidden" name="LineItems" value="' . $this->get_line_items( $order ) . '" />';//[1*Item 1*Description 1*125.00],[2*Tax**15.50],[3*Shipping**30.00]" />';
$output .= '<input type="hidden" name="BillingCountry" value="' . $order->billing_country . '" />';
$output .= '<input type="hidden" name="BillingCity" value="' . $order->billing_city . '" />';
$output .= '<input type="hidden" name="BillingProvince" value="For International Addresses" />';
$output .= '<input type="hidden" name="BillingState" value="' . $order->billing_state . '" />';
$output .= '<input type="hidden" name="BillingPostalCode" value="' . $order->billing_postcode . '" />';
$output .= '<input type="hidden" name="BillingPhone" value="' . $order->billing_phone . '" />';
$output .= '<input type="hidden" name="BillingEmail" value="' . $order->billing_email . '" />';
wc_enqueue_js( '
$.blockUI({
message: "' . esc_js( __( 'Thank you for your order. We are now redirecting you to PayDirect to make payment.', 'woocommerce' ) ) . '",
baseZ: 99999,
overlayCSS:
{
background: "#fff",
opacity: 0.6
},
css: {
padding: "20px",
zindex: "9999999",
textAlign: "center",
color: "#555",
border: "3px solid #aaa",
backgroundColor:"#fff",
cursor: "wait",
lineHeight: "24px",
}
});
jQuery("#submit_fis_payment_form").click();
' );
/*'<form action="' . esc_url( $fis_adr ) . '" method="post" id="fis_payment_form" target="_top">
'. implode( '', $fis_args_array ) . ' $output . ' */
return $output . '
<!-- Button Fallback -->
<div class="payment_buttons">
<input type="submit" class="button alt" id="submit_fis_payment_form" value="' . __( 'Pay via PayDirect', 'woocommerce' ) . '" /> <a class="button cancel" href="' . esc_url( $order->get_cancel_order_url() ) . '">' . __( 'Cancel order &amp; restore cart', 'woocommerce' ) . '</a>
</div>
<script type="text/javascript">
jQuery(".payment_buttons").hide();
</script>
</form>';
}
/**
* Process the payment and return the result
*
* @access public
* @param int $order_id
* @return array
*/
/**
* Process the payment and return the result
*
* @access public
* @param int $order_id
* @return array
*/
function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
$fis_args = $this->get_fis_args( $order );
$fis_args = http_build_query( $fis_args, '', '&' );
$fis_adr = $this->liveurl . '?';
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}
/**
* Get post data if set
**/
function get_post($name) {
if (isset($_POST[$name])) {
return $_POST[$name];
}
return NULL;
}
/**
* Output a message or error
* @param string $message
* @param string $type
*/
function debug( $message, $type = 'notice' ) {
if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '>=' ) ) {
wc_add_notice( $message, $type );
} else {
global $woocommerce;
$woocommerce->add_message( $message );
}
}
/**
* Send debugging email
**/
function send_debugging_email( $debug ) {
if ($this->debugon!='yes') return; // Debug must be enabled
if (!$this->debugrecipient) return; // Recipient needed
// Send the email
wp_mail( $this->debugrecipient, __('FIS Debug', 'wc-gateway-fis'), $debug );
}
}
/**
* Plugin page links
*/
function wc_fis_plugin_links( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_fis' ) . '">' . __( 'Settings', 'wc-gateway-fis' ) . '</a>',
'<a href="#">' . __( 'Support', 'wc-gateway-fis' ) . '</a>',
'<a href="#">' . __( 'Docs', 'wc-gateway-fis' ) . '</a>',
);
return array_merge( $plugin_links, $links );
}
/**
* Add the gateway to woocommerce
**/
function add_fis_gateway( $methods ) {
$methods[] = 'WC_Gateway_Fis'; return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_fis_gateway' );
}
@growdev
Copy link

growdev commented Sep 18, 2014

A few things:

  1. You can pull this since this isn't a Woo plugin and won't be getting any updates:
    https://gist.github.com/rayflores/3aba1e28a9d7380ddf90#file-fis-gateway-php-L23
  2. May want to put this inside of the woocommerce_gateway_fis_init() function:
    https://gist.github.com/rayflores/3aba1e28a9d7380ddf90#file-fis-gateway-php-L25
  3. Since this is a redirect gateway (sends the customer off site) you don't need an SSL check. You can pull out fis_ssl_check() and the associated action calling it.
  4. This is probably unnecessary since there the umTransaction class is tied to the ChargeItPro gateway.
    https://gist.github.com/rayflores/3aba1e28a9d7380ddf90#file-fis-gateway-php-L286
  5. You can delete this and just have the second unset(). This was for keeping compatibility with WooCommerce 1.x and 2.0.
    https://gist.github.com/rayflores/3aba1e28a9d7380ddf90#file-fis-gateway-php-L421

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment