| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Payment; |
| 4 |
|
| 5 |
/** |
| 6 |
* Creates minimal functionality needed for payment gateways |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
use WC_Payment_Gateway; |
| 12 |
use MakeCommerce\Payment\Gateway\Refund; |
| 13 |
use MakeCommerce\Payment\Gateway\Subscription; |
| 14 |
|
| 15 |
abstract class Gateway extends WC_Payment_Gateway { |
| 16 |
use Refund; |
| 17 |
use Subscription; |
| 18 |
|
| 19 |
public $id; |
| 20 |
public $MK; |
| 21 |
|
| 22 |
/** |
| 23 |
* Defines which WooCommerce options this payment gateway supports |
| 24 |
*/ |
| 25 |
public $supports = array( |
| 26 |
'subscriptions', |
| 27 |
'subscription_cancellation', |
| 28 |
'subscription_suspension', |
| 29 |
'subscription_reactivation', |
| 30 |
'subscription_amount_changes', |
| 31 |
'subscription_date_changes', |
| 32 |
'subscription_payment_method_change_admin', |
| 33 |
'subscription_payment_method_change_customer', |
| 34 |
'products', |
| 35 |
'refunds' |
| 36 |
); |
| 37 |
|
| 38 |
/** |
| 39 |
* Construct payment gateway |
| 40 |
* |
| 41 |
* @since 3.0.0 |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
|
| 45 |
//exit if the gateway is not enabled |
| 46 |
if ( !$this->enabled() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
//Set api |
| 51 |
$this->MK = \MakeCommerce::get_api(); |
| 52 |
|
| 53 |
//get settings from \WC_Payment_Gateway |
| 54 |
$this->init_settings(); |
| 55 |
|
| 56 |
//set default settings used by all shipping methods |
| 57 |
$this->set_default_settings(); |
| 58 |
|
| 59 |
if ( is_admin() ) { |
| 60 |
//initialize form fields for admin |
| 61 |
$this->initialize_form_fields(); |
| 62 |
} |
| 63 |
|
| 64 |
//set hooks |
| 65 |
$this->set_hooks(); |
| 66 |
|
| 67 |
//set gateway specific hooks |
| 68 |
if ( $this->active() ) { |
| 69 |
$this->set_gateway_hooks(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Initializes basic fields used by all payment gateways. |
| 75 |
* |
| 76 |
* @since 3.0.0 |
| 77 |
*/ |
| 78 |
public function initialize_form_fields() { |
| 79 |
|
| 80 |
//Initialize basic form fields |
| 81 |
$this->initialize_basic_form_fields(); |
| 82 |
|
| 83 |
//Initialize gateway specific form fields |
| 84 |
$this->initialize_gateway_type_form_fields(); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if the gateway is available |
| 90 |
* Automatically called by \WC_Payment_Gateway |
| 91 |
* |
| 92 |
* @since 3.0.0 |
| 93 |
*/ |
| 94 |
public function is_available() { |
| 95 |
|
| 96 |
if ( isset($this->settings['active']) ) { |
| 97 |
if ( !( $this->settings['active'] == "yes" && \MakeCommerce::is_api_set() ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return parent::is_available(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Sets default settings used by all payment gateways |
| 107 |
* |
| 108 |
* @since 3.0.0 |
| 109 |
*/ |
| 110 |
final public function set_default_settings() { |
| 111 |
|
| 112 |
if ( isset( $this->settings['active'] ) ) { |
| 113 |
$this->enabled = $this->settings['active']; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Checks whether the payment gateway is marked as active |
| 119 |
* |
| 120 |
* @since 3.0.0 |
| 121 |
*/ |
| 122 |
final public function active() { |
| 123 |
|
| 124 |
if ( isset( $this->settings['active'] ) ) { |
| 125 |
if ( $this->settings['active'] == "yes" || empty( $this->settings['active'] ) ) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set hooks used by all WooCommerce payment gateways |
| 135 |
* |
| 136 |
* @since 3.0.0 |
| 137 |
*/ |
| 138 |
final public function set_hooks() { |
| 139 |
|
| 140 |
add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) ); |
| 141 |
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 142 |
add_action( 'woocommerce_payment_gateways', array( $this, 'add_payment_gateway' ) ); |
| 143 |
|
| 144 |
global $woocommerce; |
| 145 |
|
| 146 |
// Woocommerce Multilingual overrides |
| 147 |
if ( \MakeCommerce\i18n::using_language_plugins() ) { |
| 148 |
add_filter( 'woocommerce_gateway_title', array( $this, 'override_payment_method_string' ), 25, 2 ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Overrides payment method title |
| 154 |
* |
| 155 |
* @since 3.0.0 |
| 156 |
*/ |
| 157 |
public function override_payment_method_string( $title, $gateway_id ) { |
| 158 |
|
| 159 |
if ($gateway_id === 'makecommerce') { |
| 160 |
|
| 161 |
$language_code = \MakeCommerce\i18n::get_two_char_locale(); |
| 162 |
|
| 163 |
//default (no language) |
| 164 |
if ( !empty( $this->settings['ui_widget_title'] ) ) { |
| 165 |
$title = $this->settings['ui_widget_title']; |
| 166 |
} |
| 167 |
|
| 168 |
//if method name setting has been set with current language code |
| 169 |
if ( !empty( $this->settings['ui_widget_title_' . $language_code] ) ) { |
| 170 |
$title = $this->settings['ui_widget_title_' . $language_code]; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $title; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Adds payment gateway to woocommerce |
| 179 |
* |
| 180 |
* @since 3.0.0 |
| 181 |
*/ |
| 182 |
final public function add_payment_gateway( $methods ) { |
| 183 |
|
| 184 |
$methods[] = get_class( $this ); |
| 185 |
|
| 186 |
return $methods; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Initializes basic shared form fields |
| 191 |
* Used by all payment gateways |
| 192 |
* |
| 193 |
* since 3.0.0 |
| 194 |
*/ |
| 195 |
final public function initialize_basic_form_fields() { |
| 196 |
|
| 197 |
$this->form_fields = array(); |
| 198 |
|
| 199 |
$this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Initializes gateway type form fields |
| 204 |
* |
| 205 |
* @since 3.0.0 |
| 206 |
*/ |
| 207 |
abstract public function initialize_gateway_type_form_fields(); |
| 208 |
|
| 209 |
/** |
| 210 |
* Force creating a function for setting gateway specific hooks |
| 211 |
* |
| 212 |
* @since 3.0.0 |
| 213 |
*/ |
| 214 |
abstract public function set_gateway_hooks(); |
| 215 |
|
| 216 |
/** |
| 217 |
* Checks if the gateway is enabled |
| 218 |
* |
| 219 |
* @since 3.0.0 |
| 220 |
*/ |
| 221 |
abstract public function enabled(); |
| 222 |
} |
| 223 |
|