From 5df609b4022447aed4b220719b5262f50d2d24ec Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 31 Mar 2015 19:16:34 -0400 Subject: [PATCH] Completed initial framework for WooCommerce Payment Gateway --- activate.php | 42 +----- classes/glmMesGateway.php | 218 ++++++++++++++++++++++++++++++++ classes/glmMesPluginSupport.php | 26 ++++ classes/glmPluginSupport.php | 118 ----------------- controllers/controller.php | 23 +++- deactivate.php | 38 +----- defines.php | 2 +- index.php | 164 +----------------------- readme.txt | 2 +- uninstall.php | 31 +---- views/paymentForm.html | 78 ++++++++++++ 11 files changed, 360 insertions(+), 382 deletions(-) create mode 100644 classes/glmMesGateway.php create mode 100644 classes/glmMesPluginSupport.php delete mode 100644 classes/glmPluginSupport.php create mode 100644 views/paymentForm.html diff --git a/activate.php b/activate.php index 0bc007b..2ff2a6e 100644 --- a/activate.php +++ b/activate.php @@ -1,7 +1,7 @@ addNotice("Interesting, you don't have permission to activate plugins."); @@ -64,41 +63,8 @@ class glmMesPluginActivate extends glmPluginSupport // Save plugin configuration object $this->config = $config; - - } - - /* - * Add a role capability to all current roles - * - * @param string $capability Name of capability to add - * @param string $default Whether capability should be on by default - * - * @return void - * @access private - */ - private function addRoleCapability ($capability, $default) - { - // Get list of role objects - $roleObjects = $GLOBALS['wp_roles']->role_objects; - - // Get list of roles we can edit - $roles = get_editable_roles(); - - // For each role object - foreach ($roleObjects as $key => $role) { - - // Check if the role exists in list of editable roles and capability - // does not exist - if (isset($roles[$key]) && ! isset($role->capabilities[$capability])) { - - // Add the role - $role->add_cap($capability, $default); - } - } } - - } ?> \ No newline at end of file diff --git a/classes/glmMesGateway.php b/classes/glmMesGateway.php new file mode 100644 index 0000000..b2e141d --- /dev/null +++ b/classes/glmMesGateway.php @@ -0,0 +1,218 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release classes/glmMesGateway.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/* + * MES Gateway class + * + * Extends the WooCommerce base gateway class. See the following URL. + * http://docs.woothemes.com/wc-apidocs/class-WC_Payment_Gateway.html + * + */ +class GlmMesGateway extends WC_Payment_Gateway +{ + + /** + * Gateway ID + * + * @var $id + * @access public + */ + public $id = 'merchant_e_solutions'; + /** + * Gateway Icon URL + * + * @var $icon + * @access public + */ + public $icon = false; + /** + * Show Payment Fields + * + * @var $has_fields + * @access public + */ + public $has_fields = true; + /** + * Title of this Payment Gateway + * + * @var $method_title + * @access public + */ + public $method_title = 'Merchant e-Solutions'; + /** + * Payment Gateway Description + * + * @var $method_description + * @access public + */ + public $method_description = 'Processes credit card payments through the Merchant e-Solutions Payment Gateway'; + + + public function __construct () + { + + // Add filter for our gateway + add_filter( 'woocommerce_payment_gateways', array( $this, 'glmMesAddGateway') ); + + // Setup Fields and Settings + $this->glmMesInitFormFields(); + $this->glmMesInitSettings(); + + // Get any settings + $this->title = $this->get_option('title'); + + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'glmMesProcessAdminOptions' ) ); + + } + + // Tell WooCommerce that we exist + public function glmMesAddGateway( $methods ) + { + $methods[] = 'GlmMesGateway'; + return $methods; + } + + /* + * Initialize Form Fields + * + * Add form fields to the WooCommerce Checkout page for this gateway + */ + + + public function glmMesInitFormFields() + { + + $this->form_fields = array( + + // Enable gateway chackbox + 'enabled' => array( + 'title' => __( 'Enable/Disable', 'woocommerce' ), + 'type' => 'checkbox', + 'label' => __( 'Enable Merchant e-Solutions Gateway', 'woocommerce' ), + 'default' => 'yes' + ), + + // Title displayed to user for this payment method + 'title' => array( + 'title' => __( 'Title', 'woocommerce' ), + 'type' => 'text', + 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), + 'default' => __( 'Credit Card Payment', 'woocommerce' ), + 'desc_tip' => true, + ), + + // Description to customer at checkout + 'description' => array( + 'title' => __( 'Customer Message', 'woocommerce' ), + 'type' => 'textarea', + 'default' => '' + ) + ); + + } + + // Initialize Settings + public function glmMesInitSettings() + { + + } + + // Init Form Fields + public function glmMesProcessAdminOptions() + { + + } + + // Process Admin Options + public function glmProcessAdminOptions() + { + + } + + // Set Payment Fields + public function payment_fields() + { + + require_once (GLM_MES_PLUGIN_PATH . '/views/paymentForm.html'); + + } + + // Validate Payment Fields + public function validate_fields() + { + + } + + /* + * Process order with MES Gateway + */ + public function process_payment( $order_id ) { + + global $woocommerce; + $order = new WC_Order( $order_id ); + + /* + * Process order here + * + * Returns various status states + */ + $paymentState = 'complete'; + + // Take action based on payment state + switch ($paymentState) { + + case 'complete': + + // Mark order as complete + $order->payment_complete(); + + // Return thank you page redirect + return array( + 'result' => 'success', + 'redirect' => $this->get_return_url( $order ) + ); + + break; + + case 'call_for_approval': + + // Mark as on-hold (use if payment delayed) + $order->update_status('on-hold', __( 'Awaiting authorization of Credit Card payment. Please call your Credit Card provider.', 'woocommerce' )); + return; + + break; + + case 'fail': + + // Tell the user we had a payment failure + wc_add_notice( __('Payment error:', 'woothemes') . $error_message, 'error' ); + return; + + break; + + default: + + // Need to figure this out + + break; + + } + + } + +} + +?> \ No newline at end of file diff --git a/classes/glmMesPluginSupport.php b/classes/glmMesPluginSupport.php new file mode 100644 index 0000000..d38a172 --- /dev/null +++ b/classes/glmMesPluginSupport.php @@ -0,0 +1,26 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release classes/glmMesPluginSupport.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/* + * This class provides some standard methods used by this plugin + */ +class GlmMesPluginSupport +{ + + +} + +?> \ No newline at end of file diff --git a/classes/glmPluginSupport.php b/classes/glmPluginSupport.php deleted file mode 100644 index 7be41a5..0000000 --- a/classes/glmPluginSupport.php +++ /dev/null @@ -1,118 +0,0 @@ - - * @license http://www.gaslightmedia.com Gaslightmedia - * @release glmPluginSupport.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ - * @link http://dev.gaslightmedia.com/ - */ - -/* - * This class provides some standard methods used by this plugin - */ -class GlmPluginSupport -{ - - /* - * Add a message to the 'glmMembersAdminNotices' option for output later. - * - * Messages that don't have a $type specified are displayed in - * the admin WordPress message area. - * - * Messages that have a $type are for the debug window and are categorized - * as follows and in this order in the debug window - * Alert Placed sequentially in the Alerts list - * Process Placed sequentially in the process list - * DataBlock Placed in a colored data block. Requires a $title - * - * @param string $message - * @param string $type - * @param string $title - * - * @return void - * @access public - */ - public static function addNotice ($message, $type = false, $title = false) - { - - switch($type) { - - case 'Alert'; - $alerts = get_option('glmMembersAdminNoticeAlerts'); - $alerts[] = $message; - update_option('glmMembersAdminNoticeAlerts', $alerts); - break; - - case 'Process'; - $process = get_option('glmMembersAdminNoticeProcess'); - $process[] = $message; - update_option('glmMembersAdminNoticeProcess', $process); - break; - - case 'DataBlock'; - $dataBlocks = get_option('glmMembersAdminNoticeDataBlocks'); - $dataBlocks[] = array('data' => $message, 'title' => $title); - update_option('glmMembersAdminNoticeDataBlocks', $dataBlocks); - break; - - default; - $notices = get_option('glmMembersAdminNotices'); - $notices[] = $message; - update_option('glmMembersAdminNotices', $notices); - break; - - } - - } - - /* - * Check if this is a second call to the activation hook by WordPress to activate this plugin. - * - * (Yea, I know that's stupid behavior, but there's nothing I can do about it.) - * It tells us this is happening by setting the 'action' GET parameter to 'error_scrape', - * which is also stupid. - * - * In this case, we don't want to try again, so output any saved notices from the first pass - * and then exit to tell WordPress the plugin didn't install. - * - * @return void - * @access public - */ - public function checkErrorScrape() - { - - // Check for 'action' = 'error_scrape', which indicates a second call from WordPress due to an error. - if (isset($_GET['action']) && $_GET['action'] == 'error_scrape') { - - // Sleep for a bit to make sure the glmMembersAdminNotices option is set. (seems to need this) - sleep(1); - - // if we have pending messages, display those - if (get_option('glmMembersAdminNotices')) { - - glmMembersAdminNotices(); - - // Otherwise, there must have been some other error. - } else { - - echo 'There has been an unknown error installing the Gaslight Media Members Database plugin.'; - - } - - // Quit here so Wordpress doesn't mark plugin as activated or deactivated - exit; - - } - - } - -} - -?> \ No newline at end of file diff --git a/controllers/controller.php b/controllers/controller.php index fb2aeef..6d91a15 100644 --- a/controllers/controller.php +++ b/controllers/controller.php @@ -1,7 +1,7 @@ config = $config; + add_action( 'plugins_loaded', array( $this, 'initMesGateway') ); } + /** + * Initialize MES Gateway + * + * @return void + * @access public + */ + public function initMesGateway() + { + require_once (GLM_MES_PLUGIN_PATH . '/classes/glmMesGateway.php'); + new glmMesGateway(); + + } + + /** * Controller * diff --git a/deactivate.php b/deactivate.php index f2de196..ed8deb3 100644 --- a/deactivate.php +++ b/deactivate.php @@ -1,7 +1,7 @@ config = $config; - - } - - /* - * Remove a role capability from all current roles - * - * @param string $capability - * - * @return void - * @access public - */ - public function removeRoleCapability ($capability) - { - // Get list of role objects - $roleObjects = $GLOBALS['wp_roles']->role_objects; - - // Get list of roles we can edit - $roles = get_editable_roles(); - - // For each role object - foreach ($roleObjects as $key => $role) { - // Check if the role exists in list of editable roles and capability - // does not exist - if (isset($roles[$key]) && isset($role->capabilities[$capability])) { - - // Remove role - $role->remove_cap($capability); - } - } } } diff --git a/defines.php b/defines.php index 0f7233c..454a5ff 100644 --- a/defines.php +++ b/defines.php @@ -1,6 +1,6 @@ - - - - - -
'.date('m/d/Y G:i:s A').'
-
'.GLM_MES_PLUGIN_NAME.' - Debug Data
- '; - - // Display alerts - $alerts = get_option('glmMesAdminNoticeAlerts'); - if(is_array($alerts)) { - $output .= '

Alerts

'; - foreach($alerts as $a) { - $output .= $a.'
'; - } - } else { - $output .= '

Alerts

'; - } - $output .= '

'; - delete_option('glmMesAdminNoticeAlerts'); - - // Display process messages - $process = get_option('glmMesAdminNoticeProcess'); - $output .= '

Processing

'; - if(is_array($process)) { - foreach($process as $p) { - $output .= $p.'
'; - } - } - $output .= '

'; - delete_option('glmMesAdminNoticeProcess'); - - // Display data blocks table of contents then the data blocks - $dataBlocks = get_option('glmMesAdminNoticeDataBlocks'); - $output .= '

Data Blocks

    '; - $n = 0; - if (is_array($dataBlocks)) { - foreach($dataBlocks as $d) { - $output .= '
  • '.$d['title'].'
  • '; - } - } - $output .= '

'; - if (is_array($dataBlocks)) { - reset($dataBlocks); - $n = 0; - foreach($dataBlocks as $d) { - $output .= ' -
-
[Top]
- -
'.print_r($d['data'],1).'
-
- '; - } - } - delete_option('glmMesAdminNoticeDataBlocks'); - - echo $output.' - - - '; - - // Otherwise we're outputting data to the message block in the WordPress admin area - } else { - - // Start with div class to output in standard admin notice block - $output .= '

'; - - // Get the notice texts - $notices = get_option('glmMesAdminNotices'); - - if (is_array($notices) && count($notices) > 0) { - - // For each notice retrieved - $br = ''; - foreach($notices as $n) { - - // Add the notice to the output - $output .= $br.$n; - $br = '
'; - } - - } - - echo $output.'

'; - - // Delete the option containing the notices to indicate they have been sent - delete_option('glmMesAdminNotices'); - - } - -} - -/* - * Check if there's any startup notices in this file. (stuff that happens before we get all setup) - * - * If there is, have the message displayed at the top of the wp-admin content area. - */ -function glmMesStartupNotices() { - global $startupNotices; - echo '

'.GLM_MES_PLUGIN_NAME.' Plugin Warning

'.$startupNotices.'

'; -} -if ($startupNotices != '') { - add_action('admin_notices','glmMesStartupNotices'); -} - -/* - * Check if there's any debug information or other notices that need to be displayed - * - * If there is, display as a separate window. - * - * NOTE: Need to break out notices that should be displayed in the wp-admin content area. To - * do that I need to modify the glmMesAdminNotices function above and the - * addNotice() function in the classes/glmPluginSupport.php file. Should have addNotice() - * function be able to add to another option specifically for doing this. - */ -$notices = get_option('glmMesAdminNotices'); -if (is_admin() && $notices && !GLM_MES_PLUGIN_ADMIN_DEBUG) { - - // Add action to output the notices - add_action('admin_notices','glmMesAdminNotices'); +require_once (GLM_MES_PLUGIN_PATH . '/controllers/controller.php'); +new glmMesController($wpdb, $config); -} ?> \ No newline at end of file diff --git a/readme.txt b/readme.txt index a553734..d7cf95e 100644 --- a/readme.txt +++ b/readme.txt @@ -1,4 +1,4 @@ -=== Gaslight Media Member Database === +=== GLM WooCommerce Merchant e-Solutions Gateway === Contributors: cscott@gaslightmedia.com Donate link: http://www.gaslightmedia.com Tags: Gaslight Media,Plugin,WooCommerce,Merchant e-Solutions,Mes Payment Gateway diff --git a/uninstall.php b/uninstall.php index 168bbdd..cd8e9bd 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,16 +1,16 @@ * @license http://www.gaslightmedia.com Gaslightmedia - * @release admin.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @release uninstall.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ * @link http://dev.gaslightmedia.com/ */ @@ -22,30 +22,9 @@ if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) exit(); // Delete options -delete_option('glmMembersDatbasePluginVersion'); -delete_option('glmMembersDatabaseDbVersion'); -delete_option('glmMembersAdminNotices'); +delete_option('glmMesPluginVersion'); // Same for Multi-site -delete_site_option('glmMembersDatbasePluginVersion'); -delete_site_option('glmMembersDatabaseDbVersion'); -delete_site_option('glmMembersAdminNotices'); - -/* - * Drop database tables - */ -// Set current database version -$dbVersion = GLM_MEMBERS_PLUGIN_DB_VERSION; - -// Read in Database deletion script - assumes the current db version. -$sqlFile = GLM_MEMBERS_PLUGIN_DB_SCRIPTS.'/drop_database_V'.$dbVersion.'.sql'; -$sql = file_get_contents($sqlFile); - -// Replace {prefix} with table name prefix -$sql = str_replace('{prefix}', GLM_MEMBERS_PLUGIN_DB_PREFIX, $sql); - -// Removing the tables using the script -global $wpdb; -$wpdb->query($sql); +delete_site_option('glmMesPluginVersion'); ?> \ No newline at end of file diff --git a/views/paymentForm.html b/views/paymentForm.html new file mode 100644 index 0000000..4448b20 --- /dev/null +++ b/views/paymentForm.html @@ -0,0 +1,78 @@ + + + +
+

Safe and Secure Credit Card Payment.

+

TEST MODE ENABLED

+ +
+
+

+ + +

+ +

+ + + +

+
+ +

+ + +

+
+
+
+
+ \ No newline at end of file -- 2.17.1