PluginProbe
MakeCommerce for WooCommerce / 3.0.11
MakeCommerce for WooCommerce v3.0.11
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / payment / gateway / gateway.php

gateway.php in MakeCommerce for WooCommerce 3.0.11, at payment/gateway/gateway.php

239 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 abstract class Gateway extends \WC_Payment_Gateway {
12
13 use Gateway\Refund, Gateway\Subscription;
14
15 public $id;
16 public $MK;
17
18 /**
19 * Defines which WooCommerce options this payment gateway supports
20 */
21 public $supports = array(
22 'subscriptions',
23 'subscription_cancellation',
24 'subscription_suspension',
25 'subscription_reactivation',
26 'subscription_amount_changes',
27 'subscription_date_changes',
28 'subscription_payment_method_change',
29 'products',
30 'refunds'
31 );
32
33 /**
34 * Construct payment gateway
35 *
36 * @since 3.0.0
37 */
38 public function __construct() {
39
40 //exit if the gateway is not enabled
41 if ( !$this->enabled() ) {
42 return;
43 }
44
45 //Set api
46 $this->MK = \MakeCommerce::get_api();
47
48 //get settings from \WC_Payment_Gateway
49 $this->init_settings();
50
51 //set default settings used by all shipping methods
52 $this->set_default_settings();
53
54 if ( is_admin() ) {
55 //initialize form fields for admin
56 $this->initialize_form_fields();
57 }
58
59 //set hooks
60 $this->set_hooks();
61
62 //set gateway specific hooks
63 if ( $this->active() ) {
64 $this->set_gateway_hooks();
65 }
66 }
67
68 /**
69 * Initializes basic fields used by all payment gateways.
70 *
71 * @since 3.0.0
72 */
73 public function initialize_form_fields() {
74
75 //Initialize basic form fields
76 $this->initialize_basic_form_fields();
77
78 //Initialize gateway specific form fields
79 $this->initialize_gateway_type_form_fields();
80
81 }
82
83 /**
84 * Check if the gateway is available
85 * Automatically called by \WC_Payment_Gateway
86 *
87 * @since 3.0.0
88 */
89 public function is_available() {
90
91 if ( isset($this->settings['active']) ) {
92 if ( !( $this->settings['active'] == "yes" && \MakeCommerce::is_api_set() ) ) {
93 return false;
94 }
95 }
96
97 return parent::is_available();
98 }
99
100 /**
101 * Sets default settings used by all payment gateways
102 *
103 * @since 3.0.0
104 */
105 final public function set_default_settings() {
106
107 if ( isset( $this->settings['active'] ) ) {
108 $this->enabled = $this->settings['active'];
109 }
110 }
111
112 /**
113 * Checks whether the payment gateway is marked as active
114 *
115 * @since 3.0.0
116 */
117 final public function active() {
118
119 if ( isset( $this->settings['active'] ) ) {
120 if ( $this->settings['active'] == "yes" || empty( $this->settings['active'] ) ) {
121 return true;
122 }
123 }
124
125 return false;
126 }
127
128 /**
129 * Set hooks used by all WooCommerce payment gateways
130 *
131 * @since 3.0.0
132 */
133 final public function set_hooks() {
134
135 add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) );
136 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
137 add_action( 'woocommerce_payment_gateways', array( $this, 'add_payment_gateway' ) );
138
139 global $woocommerce;
140
141 if ( ( double )$woocommerce->version < 2.6 ) {
142 add_action( 'admin_notices', 'old_woocommerce_notice' );
143 }
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 }