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 / payment.php

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

306 lines 8.0 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 }
62
63 /**
64 * Initialize all payment gateways
65 *
66 * @since 3.0.0
67 */
68 public function initialize_payment_gateways() {
69
70 //Simplecheckout
71 if ( get_option( 'mk_checkout_sco', 'no' ) == "yes" ) {
72 new Payment\Gateway\Simplecheckout();
73 }
74
75 //WooCommerce
76 new Payment\Gateway\WooCommerce( true );
77 }
78
79 /**
80 * Returns price excluding taxes
81 *
82 * @since 3.0.0
83 */
84 public static function get_rate_without_taxes( $method, $rate ) {
85
86 if ( empty( $rate[$method] ) ) {
87 return 0;
88 }
89
90 $rate = $rate[$method];
91
92 return $rate->cost;
93 }
94
95 /**
96 * Returns price including taxes
97 *
98 * @since 3.0.0
99 */
100 public static function get_rate_with_taxes( $method, $rate ) {
101
102 if ( empty( $rate[$method] ) ) {
103 return 0;
104 }
105
106 $rate = $rate[$method];
107 $price = $rate->cost;
108
109 if ( empty( $rate->taxes ) ) {
110 return $price;
111 }
112
113 foreach ( $rate->taxes as $tax ) {
114 $price += $tax;
115 }
116
117 return $price;
118 }
119
120 /**
121 * Update banklinks
122 *
123 * @since 3.0.0
124 */
125 public static function update_banklinks() {
126
127 $obj = new Payment\Gateway\WooCommerce( true );
128
129 $obj->mc_banklinks_reload( true );
130
131 }
132
133 /**
134 * Check payment and process order if completed, canceled
135 *
136 * @since 3.0.0
137 */
138 public static function check_payment() {
139
140 global $woocommerce;
141
142 $api = \MakeCommerce::get_api();
143
144 //set the correct language
145 if ( isset( $_GET["lang1"] ) ) {
146 \MakeCommerce\i18n::switch_language( $_GET["lang1"] );
147 }
148
149 $returnUrl = home_url();
150
151 $request = stripslashes_deep( $_POST );
152
153 if ( $api->verifyMac( $request ) ) {
154
155 $data = $api->extractRequestData( $request );
156 $transactionId = false;
157 $reference = false;
158
159 if ( !empty( $data['error'] ) ) {
160
161 //add status and send client back to checkout
162 return add_query_arg( 'mc_payment_status', 'failed', wc_get_checkout_url() );
163 }
164
165 if ( !empty( $data['message_type'] ) ) {
166
167 if ( $data['message_type'] == 'payment_return' ) {
168
169 $transactionId = $data['transaction'];
170 $reference = $data['reference'];
171 $paymentStatus = $data['status'];
172 }
173
174 if ( $data['message_type'] == 'token_return' ) {
175 if ( !empty( $data['transaction']['reference'] ) ) {
176 $reference = $data['transaction']['reference'];
177 }
178
179 $paymentStatus = $data['transaction']['status'];
180 $transactionId = $data['transaction']['id'];
181 }
182 }
183 }
184
185 // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved
186 if ( !$transactionId ) {
187 return $returnUrl;
188 }
189
190 //get the post id using transactionid
191 $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId );
192
193 //still nothing. Lets try matching _order_number (SCO doesnt seem to have transaction id, transaction is created via SCO instead)
194 if ( $orderId == null ) {
195 $orderId = self::get_postid_using_metakey( '_order_number', $reference );
196 }
197
198 //try using reference as orderId
199 if ( !$orderId ) {
200 $orderId = $reference;
201 }
202
203 $order = new \WC_Order( $orderId );
204
205 // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved
206 if ( !$order->get_id() ) {
207 return $returnUrl;
208 }
209
210 $returnUrl = $order->get_checkout_order_received_url();
211
212 if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) {
213
214 //get transaction to update the payment method.
215 $transaction = $api->getTransaction( $transactionId );
216
217 //make sure the payment method is the same for simplecheckout and normal version.
218 $paymentMethod = "";
219 if ( isset( $transaction->type ) && isset( $transaction->method ) ) {
220 $paymentMethod = $transaction->channel;
221 }
222
223 //update _makecommerce_preselected_method
224 update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod );
225 }
226
227 if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) {
228 //send client back to checkout
229 $returnUrl = wc_get_checkout_url();
230 }
231
232 //check if we already processed this status in the past.
233 if (get_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', true ) == $paymentStatus ) {
234 return $returnUrl;
235 }
236
237 switch( $paymentStatus ) {
238
239 case 'CANCELLED':
240 $order->update_status( 'cancelled' );
241 $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl );
242 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
243
244 break;
245 case 'EXPIRED':
246 //only update if order status is pending payment
247 if ( $order->get_status() == "pending" ) {
248
249 $order->update_status( 'cancelled' );
250 $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl );
251 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
252 }
253
254 break;
255 case 'COMPLETED':
256 $orderNote = array();
257 $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' );
258 $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' );
259 $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
260 $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() );
261
262 $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>';
263 $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true );
264 $order->add_order_note( implode( "\r\n", $orderNote ) );
265
266 if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) {
267
268 update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] );
269 update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] );
270 }
271
272 if ( isset( $_GET['lang1'] ) ) {
273 $order->update_meta_data( 'wpml_language', $_GET["lang1"] );
274 }
275
276 $order->payment_complete( $transactionId );
277 $woocommerce->cart->empty_cart();
278
279 update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus );
280
281 break;
282 }
283
284 return $returnUrl;
285 }
286
287 /**
288 * Returns post_id using transaction id or false if not found
289 *
290 * @since 3.0.4
291 */
292 public static function get_postid_using_metakey( $meta_key, $transactionId ) {
293
294 global $wpdb;
295
296 $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'");
297
298 foreach ( $wpdb->last_result as $row ) {
299 if ( isset( $row->post_id ) ) {
300 return $row->post_id;
301 }
302 }
303
304 return false;
305 }
306 }