PluginProbe
MakeCommerce for WooCommerce / 3.3.1
MakeCommerce for WooCommerce v3.3.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.3.1, at payment/payment.php

337 lines 9.3 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 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod );
246 }
247
248 if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) {
249 //send client back to checkout
250 $returnUrl = wc_get_checkout_url();
251 }
252
253 //check if we already processed this status in the past.
254 if (get_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', true ) == $paymentStatus ) {
255 return $returnUrl;
256 }
257
258 switch( $paymentStatus ) {
259
260 case 'CANCELLED':
261 // Do not update status when disabled in settings
262 if ( ( $settings['disable_cancelled_payment_update'] ?? '' ) !== 'yes' ) {
263 // Update automatically
264 $order->update_status( 'cancelled' );
265 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
266 }
267
268 $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl );
269
270 break;
271 case 'EXPIRED':
272 //only update if order status is pending payment
273 if ( $order->get_status() == "pending" ) {
274
275 // Do not update order status if disabled in settings
276 if ( ( $settings['disable_expired_payment_update'] ?? '' ) !== 'yes' ) {
277 // Update automatically
278 $order->update_status( 'cancelled' );
279 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
280 }
281
282 $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl );
283 }
284
285 break;
286 case 'COMPLETED':
287 $orderNote = array();
288 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
289 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
290 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
291 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
292
293 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
294 $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
295 $order->add_order_note( implode( "\r\n", $orderNote ) );
296
297 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
298
299 update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] );
300 update_post_meta( $order->get_id(), '_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
310 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
311
312 break;
313 }
314
315 return $returnUrl;
316 }
317
318 /**
319 * Returns post_id using transaction id or false if not found
320 *
321 * @since 3.0.4
322 */
323 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
324
325 global $wpdb;
326
327 $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'");
328
329 foreach ( $wpdb->last_result as $row ) {
330 if ( isset( $row->post_id ) ) {
331 return $row->post_id;
332 }
333 }
334
335 return false;
336 }
337 }