payment-gateway-for-authorize-net-for-woocommerce
/
payment-gateway-for-authorizenet-for-woocommerce.php
payment-gateway-for-authorize-net-for-woocommerce
Last commit date
assets
3 weeks ago
feedback
3 weeks ago
includes
3 weeks ago
languages
3 weeks ago
LICENSE.txt
3 weeks ago
payment-gateway-for-authorizenet-for-woocommerce-admin.php
3 weeks ago
payment-gateway-for-authorizenet-for-woocommerce.php
3 weeks ago
readme.txt
3 weeks ago
uninstall.php
3 weeks ago
payment-gateway-for-authorizenet-for-woocommerce.php
263 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Name: Payment Gateway for Authorize.net for WooCommerce |
| 5 | * Plugin URI: https://wordpress.org/plugins/payment-gateway-for-authorize-net-for-woocommerce/ |
| 6 | * Description: Accept secure credit card payments with Authorize.Net. Supports Subscriptions, Accept.js, Refunds, Saved Cards, and Checkout Blocks. |
| 7 | * Author: easypayment |
| 8 | * Author URI: https://profiles.wordpress.org/easypayment/ |
| 9 | * Version: 1.0.13 |
| 10 | * Requires at least: 5.6 |
| 11 | * Tested up to: 7.0 |
| 12 | * Requires PHP: 7.4 |
| 13 | * Text Domain: payment-gateway-for-authorize-net-for-woocommerce |
| 14 | * Domain Path: /languages/ |
| 15 | * WC requires at least: 6.0 |
| 16 | * WC tested up to: 10.7.0 |
| 17 | * Requires Plugins: woocommerce |
| 18 | * License: GPLv2 or later |
| 19 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 20 | */ |
| 21 | if (!defined('ABSPATH')) |
| 22 | exit; |
| 23 | |
| 24 | if (!defined('EASYAUTHNET_AUTHORIZENET_VERSION')) { |
| 25 | define('EASYAUTHNET_AUTHORIZENET_VERSION', '1.0.13'); |
| 26 | } |
| 27 | define('EASYAUTHNET_AUTHORIZENET_PLUGIN_FILE', __FILE__); |
| 28 | define('EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH', plugin_dir_path(__FILE__)); |
| 29 | define('EASYAUTHNET_AUTHORIZENET_PLUGIN_ASSET_URL', plugin_dir_url(__FILE__)); |
| 30 | if (!defined('EASYAUTHNET_AUTHORIZENET_PLUGIN_DIR')) { |
| 31 | define('EASYAUTHNET_AUTHORIZENET_PLUGIN_DIR', dirname(__FILE__)); |
| 32 | } |
| 33 | if (!defined('EASYAUTHNET_AUTHORIZENET_CUSTOMER_PROFILE_ID')) { |
| 34 | define('EASYAUTHNET_AUTHORIZENET_CUSTOMER_PROFILE_ID', '_easyauthnet_authorizenet_customer_profile_id'); |
| 35 | } |
| 36 | if (!defined('EASYAUTHNET_AUTHORIZENET_BASENAME')) { |
| 37 | define('EASYAUTHNET_AUTHORIZENET_BASENAME', plugin_basename(__FILE__)); |
| 38 | } |
| 39 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'payment-gateway-for-authorizenet-for-woocommerce-admin.php'; |
| 40 | add_action('plugins_loaded', 'easyauthnet_authorizenet_init'); |
| 41 | add_filter('woocommerce_payment_gateways', 'easyauthnet_woocommerce_payment_gateways', 10, 1); |
| 42 | add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'easyauthnet_authorizenet_plugin_action_links'); |
| 43 | add_filter('plugin_row_meta', 'easyauthnet_authorizenet_plugin_meta_links', 10, 2); |
| 44 | add_action('rest_api_init', function () { |
| 45 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-webhook-handler.php'; |
| 46 | register_rest_route('easyauthnet-authorizenet/v1', '/webhook', [ |
| 47 | 'methods' => 'POST', |
| 48 | 'callback' => ['EASYAUTHNET_AuthorizeNet_Webhook_Handler', 'handle'], |
| 49 | // Webhook must be publicly accessible – Authorize.Net does not send authenticated requests. |
| 50 | 'permission_callback' => '__return_true', // Public access |
| 51 | ]); |
| 52 | }); |
| 53 | |
| 54 | function easyauthnet_activate_authorized_for_woocommerce() { |
| 55 | set_transient('easyauthnet_authorized_for_woocommerce_redirect', true, 30); |
| 56 | } |
| 57 | |
| 58 | register_activation_hook(__FILE__, 'easyauthnet_activate_authorized_for_woocommerce'); |
| 59 | |
| 60 | add_filter('woocommerce_order_actions', function ($actions) { |
| 61 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotPrefixed -- This is WC global variable. |
| 62 | global $theorder; |
| 63 | if ($theorder instanceof WC_Order) { |
| 64 | $order = $theorder; |
| 65 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is a read-only check on admin screen; nonce verification happens on action trigger. |
| 66 | } elseif (isset($_REQUEST['post'])) { |
| 67 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Safe usage in filter context. |
| 68 | $order_id = absint($_REQUEST['post']); |
| 69 | $order = wc_get_order($order_id); |
| 70 | } else { |
| 71 | return $actions; |
| 72 | } |
| 73 | if ($order && $order->get_payment_method() === 'easyauthnet_authorizenet' && $order->get_status() === 'on-hold' && $order->get_meta('_easyauthnet_authorize_net_auth_transaction_id')) { |
| 74 | $actions['easyauthnet_capture_authorized_payment'] = __('Capture Authorized Payment', 'payment-gateway-for-authorize-net-for-woocommerce'); |
| 75 | } |
| 76 | return $actions; |
| 77 | }, 20); |
| 78 | |
| 79 | // Init after WooCommerce is loaded |
| 80 | function easyauthnet_authorizenet_init() { |
| 81 | if (!class_exists('WC_Payment_Gateway')) { |
| 82 | return; |
| 83 | } |
| 84 | if (!class_exists('WC_Payment_Gateway_CC')) { |
| 85 | return; |
| 86 | } |
| 87 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-easyauthnet-subscription-helper.php'; |
| 88 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-gateway.php'; |
| 89 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-echeck-gateway.php'; |
| 90 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-googlepay-gateway.php'; |
| 91 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-api-handler.php'; |
| 92 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-preorders-compat.php'; |
| 93 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-funnelkit-compat.php'; |
| 94 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-funnelkit-upsell-authorizenet.php'; |
| 95 | } |
| 96 | |
| 97 | function easyauthnet_woocommerce_payment_gateways($gateways) { |
| 98 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-easyauthnet-subscription-helper.php'; |
| 99 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-gateway.php'; |
| 100 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-echeck-gateway.php'; |
| 101 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-easy-payment-authorizenet-googlepay-gateway.php'; |
| 102 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/class-api-handler.php'; |
| 103 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-preorders-compat.php'; |
| 104 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-funnelkit-compat.php'; |
| 105 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . 'includes/compatibility/class-funnelkit-upsell-authorizenet.php'; |
| 106 | $gateways[] = 'EASYAUTHNET_AuthorizeNet_Gateway'; |
| 107 | $gateways[] = 'EASYAUTHNET_AuthorizeNet_ECheck_Gateway'; |
| 108 | $gateways[] = 'EASYAUTHNET_AuthorizeNet_GooglePay_Gateway'; |
| 109 | $gateways[] = 'EASYAUTHNET_AuthorizeNet_ApplePay_Gateway'; |
| 110 | return $gateways; |
| 111 | } |
| 112 | |
| 113 | add_action('wp_loaded', function () { |
| 114 | if (!function_exists('WFOCU_Core') || !class_exists('WFOCU_Gateways')) { |
| 115 | return; |
| 116 | } |
| 117 | if (!class_exists('My_WFOCU_Gateways_EasyAuthNet')) { |
| 118 | class My_WFOCU_Gateways_EasyAuthNet extends WFOCU_Gateways { |
| 119 | public function get_supported_gateways() { |
| 120 | $filtered = parent::get_supported_gateways(); |
| 121 | unset($filtered['easyauthnet_authorizenet']); |
| 122 | return array_merge(['easyauthnet_authorizenet' => 'EASYAUTHNET_AuthorizeNet_FunnelKit_Upsell'],$filtered); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | $core = WFOCU_Core(); |
| 128 | if (isset($core->gateways) && is_object($core->gateways) && get_class($core->gateways) === 'WFOCU_Gateways') { |
| 129 | $core->gateways = new My_WFOCU_Gateways_EasyAuthNet(); |
| 130 | } |
| 131 | }, 1); |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * |
| 136 | * @param string $active_section Current WC section (gateway id). |
| 137 | * @param string $active_subtab Optional subtab for base gateway. |
| 138 | */ |
| 139 | function easyauthnet_authorizenet_render_settings_tabs($active_section, $active_subtab = '') { |
| 140 | // Keep the current WC tab ("payments" on newer WC, "checkout" on older WC). |
| 141 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen routing. |
| 142 | $tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'payments'; |
| 143 | if (!in_array($tab, array('payments', 'checkout'), true)) { |
| 144 | $tab = 'payments'; |
| 145 | } |
| 146 | |
| 147 | $base = admin_url('admin.php?page=wc-settings&tab=' . $tab); |
| 148 | $tabs = array( |
| 149 | array( |
| 150 | 'label' => __('Authorize.Net Connection', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 151 | 'url' => add_query_arg(array('section' => 'easyauthnet_authorizenet', 'subtab' => 'connection'), $base), |
| 152 | 'active' => ('easyauthnet_authorizenet' === $active_section && 'credit_card' !== $active_subtab), |
| 153 | ), |
| 154 | array( |
| 155 | 'label' => __('Credit Card', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 156 | 'url' => add_query_arg(array('section' => 'easyauthnet_authorizenet', 'subtab' => 'credit_card'), $base), |
| 157 | 'active' => ('easyauthnet_authorizenet' === $active_section && 'credit_card' === $active_subtab), |
| 158 | ), |
| 159 | array( |
| 160 | 'label' => __('eCheck (ACH)', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 161 | 'url' => add_query_arg(array('section' => 'easyauthnet_authorizenet_echeck'), $base), |
| 162 | 'active' => ('easyauthnet_authorizenet_echeck' === $active_section), |
| 163 | ), |
| 164 | array( |
| 165 | 'label' => __('Google Pay', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 166 | 'url' => add_query_arg(array('section' => 'easyauthnet_authorizenet_googlepay'), $base), |
| 167 | 'active' => ('easyauthnet_authorizenet_googlepay' === $active_section), |
| 168 | ), |
| 169 | array( |
| 170 | 'label' => __('Support', 'payment-gateway-for-authorize-net-for-woocommerce'), |
| 171 | 'url' => 'https://wordpress.org/support/plugin/payment-gateway-for-authorize-net-for-woocommerce/', |
| 172 | 'active' => false, |
| 173 | 'target' => '_blank', |
| 174 | ), |
| 175 | ); |
| 176 | |
| 177 | echo '<h2 class="nav-tab-wrapper wc-nav-tab-wrapper">'; |
| 178 | foreach ($tabs as $t) { |
| 179 | $classes = 'nav-tab' . (!empty($t['active']) ? ' nav-tab-active' : ''); |
| 180 | $target_attr = !empty($t['target']) ? ' target="' . esc_attr($t['target']) . '" rel="noopener noreferrer"' : ''; |
| 181 | $support_style = !empty($t['target']) ? ' style="color:#2271b1; text-decoration: underline;font-weight: 504;"' : ''; |
| 182 | |
| 183 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attribute fragments are safely escaped above. |
| 184 | echo '<a' . $support_style . ' class="' . esc_attr($classes) . '" href="' . esc_url($t['url']) . '"' . $target_attr . '>' . esc_html($t['label']) . '</a>'; |
| 185 | } |
| 186 | echo '</h2>'; |
| 187 | } |
| 188 | |
| 189 | add_action('admin_init', 'easyauthnet_authorized_for_woocommerce_redirect_to_settings'); |
| 190 | |
| 191 | function easyauthnet_authorized_for_woocommerce_redirect_to_settings() { |
| 192 | if (get_transient('easyauthnet_authorized_for_woocommerce_redirect')) { |
| 193 | delete_transient('easyauthnet_authorized_for_woocommerce_redirect'); |
| 194 | if (is_admin() && current_user_can('manage_options')) { |
| 195 | wp_safe_redirect(admin_url('admin.php?page=wc-settings&tab=checkout§ion=easyauthnet_authorizenet')); |
| 196 | exit; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function easyauthnet_authorizenet_plugin_action_links($actions) { |
| 202 | $custom_links = array( |
| 203 | 'settings' => sprintf( |
| 204 | '<a href="%s">%s</a>', |
| 205 | admin_url('admin.php?page=wc-settings&tab=checkout§ion=easyauthnet_authorizenet'), |
| 206 | __('Settings', 'payment-gateway-for-authorize-net-for-woocommerce') |
| 207 | ), |
| 208 | ); |
| 209 | return array_merge($custom_links, $actions); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Add plugin meta links under the plugin description. |
| 214 | */ |
| 215 | function easyauthnet_authorizenet_plugin_meta_links($meta, $file) { |
| 216 | if (plugin_basename(__FILE__) === $file) { |
| 217 | $meta[] = '<a href="https://wordpress.org/support/plugin/payment-gateway-for-authorize-net-for-woocommerce/">' . __('Community Support', 'payment-gateway-for-authorize-net-for-woocommerce') . '</a>'; |
| 218 | $meta[] = '<a href="https://wordpress.org/support/plugin/payment-gateway-for-authorize-net-for-woocommerce/reviews/#new-post" target="_blank" rel="noopener noreferrer">' . __('Rate this Plugin', 'payment-gateway-for-authorize-net-for-woocommerce') . '</a>'; |
| 219 | } |
| 220 | return $meta; |
| 221 | } |
| 222 | |
| 223 | add_action('before_woocommerce_init', function () { |
| 224 | if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) { |
| 225 | \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true); |
| 226 | \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true); |
| 227 | } |
| 228 | }); |
| 229 | |
| 230 | add_action('woocommerce_blocks_loaded', function () { |
| 231 | try { |
| 232 | if (!class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) { |
| 233 | return; |
| 234 | } |
| 235 | require_once EASYAUTHNET_AUTHORIZENET_PLUGIN_PATH . '/includes/compatibility/class-block-support.php'; |
| 236 | add_action( |
| 237 | 'woocommerce_blocks_payment_method_type_registration', |
| 238 | function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $integration_registry) { |
| 239 | if ( |
| 240 | method_exists($integration_registry, 'is_registered') && |
| 241 | !$integration_registry->is_registered('easyauthnet_authorizenet') |
| 242 | ) { |
| 243 | $integration_registry->register(new \EASYAUTHNET_AuthorizeNet_Block_Support()); |
| 244 | } |
| 245 | } |
| 246 | ); |
| 247 | } catch (Exception $ex) { |
| 248 | // Optional: log error or handle silently |
| 249 | } |
| 250 | }); |
| 251 | |
| 252 | add_filter('easyauthnet_authorizenet_cc_allowed_currencies', function ($allowed, $gateway) { |
| 253 | return ['USD', 'EUR', 'GBP']; |
| 254 | }, 10, 2); |
| 255 | |
| 256 | add_filter('easyauthnet_authorizenet_googlepay_allowed_currencies', function ($allowed, $gateway) { |
| 257 | return ['USD', 'CAD']; |
| 258 | }, 10, 2); |
| 259 | |
| 260 | add_filter('easyauthnet_authorizenet_echeck_allowed_currencies', function ($allowed, $gateway) { |
| 261 | return ['USD']; // keep |
| 262 | }, 10, 2); |
| 263 |