PluginProbe
MakeCommerce for WooCommerce / 3.0.6
MakeCommerce for WooCommerce v3.0.6
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.6, at payment/gateway/gateway.php

280 lines 7.2 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 //initialize form fields for admin
55 $this->initialize_form_fields();
56
57 //set hooks
58 $this->set_hooks();
59
60 //set gateway specific hooks
61 if ( $this->active() ) {
62 $this->set_gateway_hooks();
63 }
64 }
65
66 /**
67 * Initializes basic fields used by all payment gateways.
68 *
69 * @since 3.0.0
70 */
71 public function initialize_form_fields() {
72
73 //Initialize basic form fields
74 $this->initialize_basic_form_fields();
75
76 //Initialize gateway specific form fields
77 $this->initialize_gateway_type_form_fields();
78
79 }
80
81 /**
82 * Check if the gateway is available
83 * Automatically called by \WC_Payment_Gateway
84 *
85 * @since 3.0.0
86 */
87 public function is_available() {
88
89 if ( isset($this->settings['active']) ) {
90 if ( !( $this->settings['active'] == "yes" && \MakeCommerce::is_api_set() ) ) {
91 return false;
92 }
93 }
94
95 return parent::is_available();
96 }
97
98 /**
99 * Sets default settings used by all payment gateways
100 *
101 * @since 3.0.0
102 */
103 final public function set_default_settings() {
104
105 if ( isset( $this->settings['active'] ) ) {
106 $this->enabled = $this->settings['active'];
107 }
108 }
109
110 /**
111 * Checks whether the payment gateway is marked as active
112 *
113 * @since 3.0.0
114 */
115 final public function active() {
116
117 if ( isset( $this->settings['active'] ) ) {
118 if ( $this->settings['active'] == "yes" || empty( $this->settings['active'] ) ) {
119 return true;
120 }
121 }
122
123 return false;
124 }
125
126 /**
127 * Set hooks used by all WooCommerce payment gateways
128 *
129 * @since 3.0.0
130 */
131 final public function set_hooks() {
132
133 add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) );
134 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
135 add_action( 'woocommerce_payment_gateways', array( $this, 'add_payment_gateway' ) );
136
137 global $woocommerce;
138
139 if ( ( double )$woocommerce->version < 2.6 ) {
140 add_action( 'admin_notices', 'old_woocommerce_notice' );
141 }
142
143 // Woocommerce Multilingual overrides
144 if ( \MakeCommerce\i18n::using_language_plugins() ) {
145 add_filter( 'get_post_metadata', array( $this, 'override_payment_method_string' ), 5, 4 );
146 }
147
148 //add slice button to product page
149 add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'product_slice_button' ), 20 );
150 }
151
152 /**
153 * Overrides payment method title
154 *
155 * @since 3.0.0
156 */
157 public function override_payment_method_string( $check, $object_id, $meta_key, $single ) {
158
159 if ( $meta_key == '_payment_method_title' ) {
160
161 $payment_method = get_post_meta( $object_id, '_payment_method', true );
162 if ( $payment_method == 'makecommerce' ) {
163
164 $language_code = \MakeCommerce\i18n::get_two_char_locale();
165
166 $title = "";
167 //default (no language)
168 if ( !empty( $this->settings['ui_widget_title'] ) ) {
169 $title = $this->settings['ui_widget_title'];
170 }
171
172 //if method name setting has been set with current language code
173 if ( !empty( $this->settings['ui_widget_title_' . $language_code] ) ) {
174 $title = $this->settings['ui_widget_title_' . $language_code];
175 }
176
177 if ( $title != "" ) {
178 return $title;
179 }
180 }
181 }
182
183 return $check;
184 }
185
186 /**
187 * Show slice button on product view after add to cart button
188 *
189 * @since 3.0.0
190 */
191 static $sliceShown = false;
192 public function product_slice_button() {
193
194 if ( self::$sliceShown ) {
195 return;
196 }
197
198 if ( get_option('mk_product_slice', 'no') == 'yes' ) {
199
200 global $product;
201 global $wpdb;
202
203 //ignore slice for subscription products
204 if ( class_exists( '\WC_Subscriptions_Product' ) && \WC_Subscriptions_Product::is_subscription( $product ) ) {
205 return;
206 }
207
208 $price_incl_tax = wc_get_price_including_tax( $product );
209
210 $methods = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . MAKECOMMERCE_TABLENAME);
211 $slice = false;
212
213 if ( count($methods) ) {
214 foreach ( $methods as $method ) {
215 if ( $method->name =="slice" ) {
216 if ( $price_incl_tax >= $method->min_amount ) {
217 if ( $price_incl_tax <= $method->max_amount ) {
218 $slice = true;
219 }
220 }
221 }
222 }
223 }
224
225 if ( $slice == true ) {
226 self::$sliceShown = true;
227 echo '
228 <div style="width:100%; margin-top: 20px; margin-bottom: auto;">
229 <img src="'.plugins_url('woocommerce/images/slice_logo_dark_176x62.svg', __FILE__).'" width="88" weight="31"/>&nbsp;&nbsp;'.__("Buy now, pay later in 3 instalments", "wc_makecommerce_domain").' 3 x '.sprintf("%.2f",$price_incl_tax/3).'
230 </div>';
231 }
232 }
233 }
234
235 /**
236 * Adds payment gateway to woocommerce
237 *
238 * @since 3.0.0
239 */
240 final public function add_payment_gateway( $methods ) {
241
242 $methods[] = get_class( $this );
243
244 return $methods;
245 }
246
247 /**
248 * Initializes basic shared form fields
249 * Used by all payment gateways
250 *
251 * since 3.0.0
252 */
253 final public function initialize_basic_form_fields() {
254
255 $this->form_fields = array();
256
257 $this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() );
258 }
259
260 /**
261 * Initializes gateway type form fields
262 *
263 * @since 3.0.0
264 */
265 abstract public function initialize_gateway_type_form_fields();
266
267 /**
268 * Force creating a function for setting gateway specific hooks
269 *
270 * @since 3.0.0
271 */
272 abstract public function set_gateway_hooks();
273
274 /**
275 * Checks if the gateway is enabled
276 *
277 * @since 3.0.0
278 */
279 abstract public function enabled();
280 }