PluginProbe
MakeCommerce for WooCommerce / 3.0.12
MakeCommerce for WooCommerce v3.0.12
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.12, at payment/gateway/gateway.php

273 lines 7.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 //check if payment has been unsuccesful
160 add_action( 'woocommerce_before_checkout_form', array( $this, 'check_payment_status' ) );
161 }
162
163 /**
164 * Add check for unsuccesful payment redirects
165 * Displays notice if something went wrong
166 *
167 * @since 3.0.12
168 */
169 public function check_payment_status() {
170
171 if ( isset ( $_GET['mc_payment_status'] ) ) {
172
173 switch( $_GET['mc_payment_status'] ) {
174
175 case 'failed':
176 wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' );
177
178 break;
179 case 'cancelled':
180 wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error' );
181
182 break;
183 case 'expired':
184 wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error' );
185
186 break;
187 }
188
189 // Avoid multiple error messages
190 unset($_GET['mc_payment_status']);
191 }
192 }
193
194 /**
195 * Overrides payment method title
196 *
197 * @since 3.0.0
198 */
199 public function override_payment_method_string( $check, $object_id, $meta_key, $single ) {
200
201 if ( $meta_key == '_payment_method_title' ) {
202
203 $payment_method = get_post_meta( $object_id, '_payment_method', true );
204 if ( $payment_method == 'makecommerce' ) {
205
206 $language_code = \MakeCommerce\i18n::get_two_char_locale();
207
208 $title = "";
209 //default (no language)
210 if ( !empty( $this->settings['ui_widget_title'] ) ) {
211 $title = $this->settings['ui_widget_title'];
212 }
213
214 //if method name setting has been set with current language code
215 if ( !empty( $this->settings['ui_widget_title_' . $language_code] ) ) {
216 $title = $this->settings['ui_widget_title_' . $language_code];
217 }
218
219 if ( $title != "" ) {
220 return $title;
221 }
222 }
223 }
224
225 return $check;
226 }
227
228 /**
229 * Adds payment gateway to woocommerce
230 *
231 * @since 3.0.0
232 */
233 final public function add_payment_gateway( $methods ) {
234
235 $methods[] = get_class( $this );
236
237 return $methods;
238 }
239
240 /**
241 * Initializes basic shared form fields
242 * Used by all payment gateways
243 *
244 * since 3.0.0
245 */
246 final public function initialize_basic_form_fields() {
247
248 $this->form_fields = array();
249
250 $this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() );
251 }
252
253 /**
254 * Initializes gateway type form fields
255 *
256 * @since 3.0.0
257 */
258 abstract public function initialize_gateway_type_form_fields();
259
260 /**
261 * Force creating a function for setting gateway specific hooks
262 *
263 * @since 3.0.0
264 */
265 abstract public function set_gateway_hooks();
266
267 /**
268 * Checks if the gateway is enabled
269 *
270 * @since 3.0.0
271 */
272 abstract public function enabled();
273 }