PluginProbe
MakeCommerce for WooCommerce / 3.0.9
MakeCommerce for WooCommerce v3.0.9
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.9, at payment/gateway/gateway.php

237 lines 6.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 pay later block to product
149 if ( isset( $this->settings["pl_show_on_product_pages"] ) ) {
150 if ( $this->settings["pl_show_on_product_pages"] == "yes" && !empty( $this->settings["pl_show_methods"] )) {
151 add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'add_pay_later_block_to_product' ), 10, 0 );
152 add_action( 'woocommerce_add_to_cart_redirect', array( $this, 'added_to_cart_via_paylater_redirect' ), 10, 0 );
153 add_action( 'woocommerce_after_checkout_form', array( $this, 'autoselect_chosen_paylater_method' ), 10, 1 );
154 }
155 }
156 }
157
158 /**
159 * Overrides payment method title
160 *
161 * @since 3.0.0
162 */
163 public function override_payment_method_string( $check, $object_id, $meta_key, $single ) {
164
165 if ( $meta_key == '_payment_method_title' ) {
166
167 $payment_method = get_post_meta( $object_id, '_payment_method', true );
168 if ( $payment_method == 'makecommerce' ) {
169
170 $language_code = \MakeCommerce\i18n::get_two_char_locale();
171
172 $title = "";
173 //default (no language)
174 if ( !empty( $this->settings['ui_widget_title'] ) ) {
175 $title = $this->settings['ui_widget_title'];
176 }
177
178 //if method name setting has been set with current language code
179 if ( !empty( $this->settings['ui_widget_title_' . $language_code] ) ) {
180 $title = $this->settings['ui_widget_title_' . $language_code];
181 }
182
183 if ( $title != "" ) {
184 return $title;
185 }
186 }
187 }
188
189 return $check;
190 }
191
192 /**
193 * Adds payment gateway to woocommerce
194 *
195 * @since 3.0.0
196 */
197 final public function add_payment_gateway( $methods ) {
198
199 $methods[] = get_class( $this );
200
201 return $methods;
202 }
203
204 /**
205 * Initializes basic shared form fields
206 * Used by all payment gateways
207 *
208 * since 3.0.0
209 */
210 final public function initialize_basic_form_fields() {
211
212 $this->form_fields = array();
213
214 $this->form_fields['logo'] = array( 'type' => 'title', 'description' => \MakeCommerce::get_logo_html() );
215 }
216
217 /**
218 * Initializes gateway type form fields
219 *
220 * @since 3.0.0
221 */
222 abstract public function initialize_gateway_type_form_fields();
223
224 /**
225 * Force creating a function for setting gateway specific hooks
226 *
227 * @since 3.0.0
228 */
229 abstract public function set_gateway_hooks();
230
231 /**
232 * Checks if the gateway is enabled
233 *
234 * @since 3.0.0
235 */
236 abstract public function enabled();
237 }