| 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', |
| 33 |
'products', |
| 34 |
'refunds' |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Construct payment gateway |
| 39 |
* |
| 40 |
* @since 3.0.0 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
|
| 44 |
//exit if the gateway is not enabled |
| 45 |
if ( !$this->enabled() ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
//Set api |
| 50 |
$this->MK = \MakeCommerce::get_api(); |
| 51 |
|
| 52 |
//get settings from \WC_Payment_Gateway |
| 53 |
$this->init_settings(); |
| 54 |
|
| 55 |
//set default settings used by all shipping methods |
| 56 |
$this->set_default_settings(); |
| 57 |
|
| 58 |
if ( is_admin() ) { |
| 59 |
//initialize form fields for admin |
| 60 |
$this->initialize_form_fields(); |
| 61 |
} |
| 62 |
|
| 63 |
//set hooks |
| 64 |
$this->set_hooks(); |
| 65 |
|
| 66 |
//set gateway specific hooks |
| 67 |
if ( $this->active() ) { |
| 68 |
$this->set_gateway_hooks(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Initializes basic fields used by all payment gateways. |
| 74 |
* |
| 75 |
* @since 3.0.0 |
| 76 |
*/ |
| 77 |
public function initialize_form_fields() { |
| 78 |
|
| 79 |
//Initialize basic form fields |
| 80 |
$this->initialize_basic_form_fields(); |
| 81 |
|
| 82 |
//Initialize gateway specific form fields |
| 83 |
$this->initialize_gateway_type_form_fields(); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if the gateway is available |
| 89 |
* Automatically called by \WC_Payment_Gateway |
| 90 |
* |
| 91 |
* @since 3.0.0 |
| 92 |
*/ |
| 93 |
public function is_available() { |
| 94 |
|
| 95 |
if ( isset($this->settings['active']) ) { |
| 96 |
if ( !( $this->settings['active'] == "yes" && \MakeCommerce::is_api_set() ) ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return parent::is_available(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Sets default settings used by all payment gateways |
| 106 |
* |
| 107 |
* @since 3.0.0 |
| 108 |
*/ |
| 109 |
final public function set_default_settings() { |
| 110 |
|
| 111 |
if ( isset( $this->settings['active'] ) ) { |
| 112 |
$this->enabled = $this->settings['active']; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks whether the payment gateway is marked as active |
| 118 |
* |
| 119 |
* @since 3.0.0 |
| 120 |
*/ |
| 121 |
final public function active() { |
| 122 |
|
| 123 |
if ( isset( $this->settings['active'] ) ) { |
| 124 |
if ( $this->settings['active'] == "yes" || empty( $this->settings['active'] ) ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Set hooks used by all WooCommerce payment gateways |
| 134 |
* |
| 135 |
* @since 3.0.0 |
| 136 |
*/ |
| 137 |
final public function set_hooks() { |
| 138 |
|
| 139 |
add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) ); |
| 140 |
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
| 141 |
add_action( 'woocommerce_payment_gateways', array( $this, 'add_payment_gateway' ) ); |
| 142 |
|
| 143 |
global $woocommerce; |
| 144 |
|
| 145 |
// Woocommerce Multilingual overrides |
| 146 |
if ( \MakeCommerce\i18n::using_language_plugins() ) { |
| 147 |
add_filter( 'get_post_metadata', array( $this, 'override_payment_method_string' ), 5, 4 ); |
| 148 |
} |
| 149 |
|
| 150 |
//add pay later block to product |
| 151 |
if ( isset( $this->settings["pl_show_on_product_pages"] ) ) { |
| 152 |
if ( $this->settings["pl_show_on_product_pages"] == "yes" && !empty( $this->settings["pl_show_methods"] )) { |
| 153 |
add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'add_pay_later_block_to_product' ), 10, 0 ); |
| 154 |
add_action( 'woocommerce_add_to_cart_redirect', array( $this, 'added_to_cart_via_paylater_redirect' ), 10, 0 ); |
| 155 |
add_action( 'woocommerce_after_checkout_form', array( $this, 'autoselect_chosen_paylater_method' ), 10, 1 ); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Overrides payment method title |
| 162 |
* |
| 163 |
* @since 3.0.0 |
| 164 |
*/ |
| 165 |
public function override_payment_method_string( $check, $object_id, $meta_key, $single ) { |
| 166 |
|
| 167 |
if ( $meta_key == '_payment_method_title' ) { |
| 168 |
|
| 169 |
$payment_method = get_post_meta( $object_id, '_payment_method', true ); |
| 170 |
if ( $payment_method == 'makecommerce' ) { |
| 171 |
|
| 172 |
$language_code = \MakeCommerce\i18n::get_two_char_locale(); |
| 173 |
|
| 174 |
$title = ""; |
| 175 |
//default (no language) |
| 176 |
if ( !empty( $this->settings['ui_widget_title'] ) ) { |
| 177 |
$title = $this->settings['ui_widget_title']; |
| 178 |
} |
| 179 |
|
| 180 |
//if method name setting has been set with current language code |
| 181 |
if ( !empty( $this->settings['ui_widget_title_' . $language_code] ) ) { |
| 182 |
$title = $this->settings['ui_widget_title_' . $language_code]; |
| 183 |
} |
| 184 |
|
| 185 |
if ( $title != "" ) { |
| 186 |
return $title; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return $check; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Adds payment gateway to woocommerce |
| 196 |
* |
| 197 |
* @since 3.0.0 |
| 198 |
*/ |
| 199 |
final public function add_payment_gateway( $methods ) { |
| 200 |
|
| 201 |
$methods[] = get_class( $this ); |
| 202 |
|
| 203 |
return $methods; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Initializes basic shared form fields |
| 208 |
* Used by all payment gateways |
| 209 |
* |
| 210 |
* since 3.0.0 |
| 211 |
*/ |
| 212 |
final public function initialize_basic_form_fields() { |
| 213 |
|
| 214 |
$this->form_fields = array(); |
| 215 |
|
| 216 |
$this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Initializes gateway type form fields |
| 221 |
* |
| 222 |
* @since 3.0.0 |
| 223 |
*/ |
| 224 |
abstract public function initialize_gateway_type_form_fields(); |
| 225 |
|
| 226 |
/** |
| 227 |
* Force creating a function for setting gateway specific hooks |
| 228 |
* |
| 229 |
* @since 3.0.0 |
| 230 |
*/ |
| 231 |
abstract public function set_gateway_hooks(); |
| 232 |
|
| 233 |
/** |
| 234 |
* Checks if the gateway is enabled |
| 235 |
* |
| 236 |
* @since 3.0.0 |
| 237 |
*/ |
| 238 |
abstract public function enabled(); |
| 239 |
} |