PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
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.2, at payment/gateway/gateway.php

263 lines 6.6 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 private 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 private 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 //dont know if this is needed:
144 // Woocommerce Multilingual overrides
145 if ( \MakeCommerce\i18n::using_language_plugins() ) {
146 add_filter( 'get_post_metadata', array( $this, 'override_payment_method_string' ), 5, 4 );
147 }
148
149 //add slice button to product page
150 add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'product_slice_button' ), 20 );
151 }
152
153 /**
154 * Overrides payment method title
155 *
156 * @since 3.0.0
157 */
158 public function override_payment_method_string( $check, $object_id, $meta_key, $single ) {
159
160 if ( $meta_key == '_payment_method_title' ) {
161
162 $payment_method = get_post_meta( $object_id, '_payment_method', true );
163
164 if ( $payment_method == 'makecommerce' ) {
165 $payment_gateways = WC()->payment_gateways->payment_gateways();
166
167 if ( isset( $payment_gateways[$payment_method] ) ) {
168 $title = $payment_gateways[$payment_method]->title;
169
170 return $title;
171 }
172 }
173 }
174
175 return $check;
176 }
177
178 /**
179 * Show slice button on product view after add to cart button
180 *
181 * @since 3.0.0
182 */
183 public function product_slice_button() {
184
185 if ( get_option('mk_product_slice', 'no') == 'yes' ) {
186
187 global $product;
188 global $wpdb;
189
190 //ignore slice for subscription products
191 if ( class_exists( '\WC_Subscriptions_Product' ) && \WC_Subscriptions_Product::is_subscription( $product ) ) {
192 return;
193 }
194
195 $price_incl_tax = wc_get_price_including_tax( $product );
196
197 $methods = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . MAKECOMMERCE_TABLENAME);
198 $slice = false;
199
200 if ( count($methods) ) {
201 foreach ( $methods as $method ) {
202 if ( $method->name =="slice" ) {
203 if ( $price_incl_tax >= $method->min_amount ) {
204 if ( $price_incl_tax <= $method->max_amount ) {
205 $slice = true;
206 }
207 }
208 }
209 }
210 }
211
212 if ( $slice == true ) {
213 echo '<div style="width:100%; margin-top: 20px; margin-bottom: auto;"><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).' €</div>';
214 }
215 }
216 }
217
218 /**
219 * Adds payment gateway to woocommerce
220 *
221 * @since 3.0.0
222 */
223 final public function add_payment_gateway( $methods ) {
224
225 $methods[] = get_class($this);
226
227 return $methods;
228 }
229
230 /**
231 * Initializes basic shared form fields
232 * Used by all payment gateways
233 *
234 * since 3.0.0
235 */
236 final private function initialize_basic_form_fields() {
237
238 $this->form_fields = array();
239
240 $this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() );
241 }
242
243 /**
244 * Initializes gateway type form fields
245 *
246 * @since 3.0.0
247 */
248 abstract public function initialize_gateway_type_form_fields();
249
250 /**
251 * Force creating a function for setting gateway specific hooks
252 *
253 * @since 3.0.0
254 */
255 abstract public function set_gateway_hooks();
256
257 /**
258 * Checks if the gateway is enabled
259 *
260 * @since 3.0.0
261 */
262 abstract public function enabled();
263 }