PluginProbe
MakeCommerce for WooCommerce / 3.4.1
MakeCommerce for WooCommerce v3.4.1
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 / payment.php

payment.php in MakeCommerce for WooCommerce 3.4.1, at payment/payment.php

347 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The payment functionality of the plugin.
5 *
6 * @link https://makecommerce.net/
7 * @since 3.0.0
8 *
9 * @package Makecommerce
10 * @subpackage Makecommerce/admin
11 */
12
13 namespace MakeCommerce;
14
15 /**
16 * The payment functionality of the plugin.
17 *
18 * Defines the plugin name, version, and two examples hooks for how to
19 * enqueue the admin-specific stylesheet and JavaScript.
20 *
21 * @package Makecommerce
22 * @subpackage Makecommerce/payment
23 * @author Maksekeskus AS <support@maksekeskus.ee>
24 */
25 class Payment {
26
27 /**
28 * The ID of this plugin.
29 *
30 * @since 3.0.0
31 * @access private
32 * @var string $plugin_name The ID of this plugin.
33 */
34 private $plugin_name;
35
36 /**
37 * The version of this plugin.
38 *
39 * @since 3.0.0
40 * @access private
41 * @var string $version The current version of this plugin.
42 */
43 private $version;
44
45 private Loader $loader;
46
47 /**
48 * Initialize the class and set its properties.
49 *
50 * @since 3.0.0
51 * @param string $plugin_name The name of this plugin.
52 * @param string $version The version of this plugin.
53 */
54 public function __construct( $plugin_name, $version, Loader $loader ) {
55
56 $this->plugin_name = $plugin_name;
57 $this->version = $version;
58 $this->loader = $loader;
59
60 $this->loader->add_action( 'plugins_loaded', $this, 'initialize_payment_gateways' );
61 $this->loader->add_action( 'woocommerce_before_checkout_form', $this, 'check_payment_status' );
62 }
63
64 /**
65 * Initialize all payment gateways
66 *
67 * @since 3.0.0
68 */
69 public function initialize_payment_gateways() {
70 //WooCommerce
71 new Payment\Gateway\WooCommerce( true );
72 }
73
74 /**
75 * Returns price excluding taxes
76 *
77 * @since 3.0.0
78 */
79 public static function get_rate_without_taxes( $method, $rate ) {
80
81 if ( empty( $rate[$method] ) ) {
82 return 0;
83 }
84
85 $rate = $rate[$method];
86
87 return $rate->cost;
88 }
89
90 /**
91 * Returns price including taxes
92 *
93 * @since 3.0.0
94 */
95 public static function get_rate_with_taxes( $method, $rate ) {
96
97 if ( empty( $rate[$method] ) ) {
98 return 0;
99 }
100
101 $rate = $rate[$method];
102 $price = $rate->cost;
103
104 if ( empty( $rate->taxes ) ) {
105 return $price;
106 }
107
108 foreach ( $rate->taxes as $tax ) {
109 $price += $tax;
110 }
111
112 return $price;
113 }
114
115 /**
116 * Update banklinks
117 *
118 * @since 3.0.0
119 */
120 public static function update_banklinks() {
121
122 $obj = new Payment\Gateway\WooCommerce( true );
123
124 $obj->mc_banklinks_reload( true );
125
126 }
127
128 /**
129 * Add check for unsuccesful payment redirects
130 * Displays notice if something went wrong
131 *
132 * @since 3.0.12
133 */
134 public function check_payment_status()
135 {
136 if ( isset ( $_GET['mc_payment_status'] ) ) {
137 switch( $_GET['mc_payment_status'] ) {
138 case 'failed':
139 wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' );
140 break;
141 case 'cancelled':
142 wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error' );
143 break;
144 case 'expired':
145 wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error' );
146 break;
147 }
148
149 // Avoid multiple error messages
150 unset($_GET['mc_payment_status']);
151 }
152 }
153
154 /**
155 * Check payment and process order if completed, canceled
156 *
157 * @since 3.0.0
158 */
159 public static function check_payment( $settings = array() ) {
160
161 global $woocommerce;
162
163 $api = \MakeCommerce::get_api();
164
165 //set the correct language
166 if ( isset( $_GET["lang1"] ) ) {
167 \MakeCommerce\i18n::switch_language( $_GET["lang1"] );
168 }
169
170 $returnUrl = home_url();
171
172 $request = stripslashes_deep( $_POST );
173
174 if ( $api->verifyMac( $request ) ) {
175
176 $data = $api->extractRequestData( $request );
177 $transactionId = false;
178 $reference = false;
179
180 if ( !empty( $data['error'] ) ) {
181
182 //add status and send client back to checkout
183 return add_query_arg( 'mc_payment_status', 'failed', wc_get_checkout_url() );
184 }
185
186 if ( !empty( $data['message_type'] ) ) {
187
188 if ( $data['message_type'] == 'payment_return' ) {
189
190 $transactionId = $data['transaction'];
191 $reference = $data['reference'];
192 $paymentStatus = $data['status'];
193 }
194
195 if ( $data['message_type'] == 'token_return' ) {
196 if ( !empty( $data['transaction']['reference'] ) ) {
197 $reference = $data['transaction']['reference'];
198 }
199
200 $paymentStatus = $data['transaction']['status'];
201 $transactionId = $data['transaction']['id'];
202 }
203 }
204 }
205
206 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
207 if ( !$transactionId ) {
208 return $returnUrl;
209 }
210
211 //get the post id using transactionid
212 $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId );
213
214 //still nothing. Lets try matching _order_number (SCO doesnt seem to have transaction id, transaction is created via SCO instead)
215 if ( $orderId == null ) {
216 $orderId = self::get_postid_using_metakey( '_order_number', $reference );
217 }
218
219 //try using reference as orderId
220 if ( !$orderId ) {
221 $orderId = $reference;
222 }
223
224 $order = wc_get_order( $orderId );
225
226 // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved
227 if ( !$order->get_id() ) {
228 return $returnUrl;
229 }
230
231 $returnUrl = $order->get_checkout_order_received_url();
232
233 if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) {
234
235 //get transaction to update the payment method.
236 $transaction = $api->getTransaction( $transactionId );
237
238 //make sure the payment method is the same for simplecheckout and normal version.
239 $paymentMethod = "";
240 if ( isset( $transaction->type ) && isset( $transaction->method ) ) {
241 $paymentMethod = $transaction->channel;
242 }
243
244 //update _makecommerce_preselected_method
245 $order->update_meta_data( '_makecommerce_preselected_method', $paymentMethod );
246 $order->save();
247 }
248
249 if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) {
250 //send client back to checkout
251 $returnUrl = wc_get_checkout_url();
252 }
253
254 //check if we already processed this status in the past.
255 if ( $order->get_meta( '_makecommerce_payment_processed_status', true ) == $paymentStatus ) {
256 return $returnUrl;
257 }
258
259 switch( $paymentStatus ) {
260
261 case 'CANCELLED':
262 // Do not update status when disabled in settings
263 if ( ( $settings['disable_cancelled_payment_update'] ?? '' ) !== 'yes' ) {
264 // Update automatically
265 $order->update_status( 'cancelled' );
266 $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus );
267 }
268
269 $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl );
270
271 break;
272 case 'EXPIRED':
273 //only update if order status is pending payment
274 if ( $order->get_status() == "pending" ) {
275
276 // Do not update order status if disabled in settings
277 if ( ( $settings['disable_expired_payment_update'] ?? '' ) !== 'yes' ) {
278 // Update automatically
279 $order->update_status( 'cancelled' );
280 $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus );
281 }
282
283 $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl );
284 }
285
286 break;
287 case 'COMPLETED':
288 $orderNote = array();
289 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
290 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
291 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
292 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
293
294 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
295 $orderNote[] = $paymentOptionText . ': ' . $order->get_meta( '_makecommerce_preselected_method', true );
296 $order->add_order_note( implode( "\r\n", $orderNote ) );
297
298 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
299 $order->update_meta_data( '_makecommerce_payment_token', $data['token']['id'] );
300 $order->update_meta_data( '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] );
301 }
302
303 if ( isset( $_GET['lang1'] ) ) {
304 $order->update_meta_data( 'wpml_language', $_GET["lang1"] );
305 }
306
307 $order->payment_complete( $transactionId );
308 $woocommerce->cart->empty_cart();
309 $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus );
310
311 break;
312 }
313
314 // Save all changes to order
315 $order->save();
316
317 return $returnUrl;
318 }
319
320 /**
321 * Returns post_id using transaction id or false if not found
322 *
323 * @since 3.0.4
324 */
325 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
326
327 $orders = wc_get_orders(
328 [
329 'meta_query' => [
330 [
331 'meta_key' => $meta_key,
332 'meta_value' => $transactionId
333 ]
334 ]
335 ]
336 );
337
338 foreach ( $orders as $order ) {
339 $id = $order->get_id();
340 if ( isset( $id ) ) {
341 return $id;
342 }
343 }
344
345 return false;
346 }
347 }