| @@ -11,9 +11,8 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | namespace MakeCommerce; |
| 14 | 14 | |
| 15 | -use Automattic\WooCommerce\Utilities\OrderUtil; | |
| 16 | 15 | /** |
| 17 | 16 | * The payment functionality of the plugin. |
| 18 | 17 | * |
| 19 | 18 | * Defines the plugin name, version, and two examples hooks for how to |
| @@ -185,17 +184,16 @@ | ||
| 185 | 184 | } |
| 186 | 185 | |
| 187 | 186 | if ( !empty( $data['message_type'] ) ) { |
| 188 | 187 | |
| 189 | - if ( $data['message_type'] === 'payment_return' ) { | |
| 188 | + if ( $data['message_type'] == 'payment_return' ) { | |
| 190 | 189 | |
| 191 | 190 | $transactionId = $data['transaction']; |
| 192 | 191 | $reference = $data['reference']; |
| 193 | 192 | $paymentStatus = $data['status']; |
| 194 | - $totalAmount = $data['amount']; | |
| 195 | 193 | } |
| 196 | 194 | |
| 197 | - if ( $data['message_type'] === 'token_return' ) { | |
| 195 | + if ( $data['message_type'] == 'token_return' ) { | |
| 198 | 196 | if ( !empty( $data['transaction']['reference'] ) ) { |
| 199 | 197 | $reference = $data['transaction']['reference']; |
| 200 | 198 | } |
| 201 | 199 | |
| @@ -205,9 +203,9 @@ | ||
| 205 | 203 | } |
| 206 | 204 | } |
| 207 | 205 | |
| 208 | 206 | // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved |
| 209 | - if ( empty( $transactionId ) ) { | |
| 207 | + if ( !$transactionId ) { | |
| 210 | 208 | return $returnUrl; |
| 211 | 209 | } |
| 212 | 210 | |
| 213 | 211 | //get the post id using transactionid |
| @@ -212,51 +210,27 @@ | ||
| 212 | 210 | |
| 213 | 211 | //get the post id using transactionid |
| 214 | 212 | $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId ); |
| 215 | 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 | + | |
| 216 | 224 | $order = wc_get_order( $orderId ); |
| 217 | 225 | |
| 218 | 226 | // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved |
| 219 | - if ( !$order || !$order->get_id() ) { | |
| 227 | + if ( !$order->get_id() ) { | |
| 220 | 228 | return $returnUrl; |
| 221 | 229 | } |
| 222 | 230 | |
| 223 | - $returnUrl = $order->get_meta( '_makecommerce_order_received_url', true ); | |
| 224 | - if ( !$returnUrl ) { | |
| 225 | - $returnUrl = $order->get_checkout_order_received_url(); | |
| 226 | - } | |
| 231 | + $returnUrl = $order->get_checkout_order_received_url(); | |
| 227 | 232 | |
| 228 | - // Disregard with subscription method change as amounts do not match (0 vs order total) | |
| 229 | - // Only check sums if $totalAmount is set | |
| 230 | - // Trx total and order total did not match | |
| 231 | - if ( $order->get_status() !== 'active' && isset( $totalAmount ) && $totalAmount != $order->get_total() ) { | |
| 232 | - | |
| 233 | - if ( $paymentStatus == 'COMPLETED' ) { | |
| 234 | - $notes[] = sprintf('%s: %s.', | |
| 235 | - __( 'Order total and transaction amount did not match! Please contact us at', 'wc_makecommerce_domain' ), | |
| 236 | - __( 'support@maksekeskus.ee', 'wc_makecommerce_domain' ) | |
| 237 | - ); | |
| 238 | - $notes[] = sprintf('%s: %s.', | |
| 239 | - __( 'Transaction ID that was assigned to this order', 'wc_makecommerce_domain' ), | |
| 240 | - '<a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>' | |
| 241 | - ); | |
| 242 | - $notes[] = sprintf('%s: %s€', | |
| 243 | - __( 'Amount that was paid', 'wc_makecommerce_domain' ), | |
| 244 | - $totalAmount | |
| 245 | - ); | |
| 246 | - | |
| 247 | - $order->add_order_note( implode( "\r\n", $notes ) ); | |
| 248 | - | |
| 249 | - // Empty the cart so the customer is not tempted to pay again | |
| 250 | - $woocommerce->cart->empty_cart(); | |
| 251 | - | |
| 252 | - return $returnUrl; | |
| 253 | - } | |
| 254 | - | |
| 255 | - // If the payment was not complete then send the customer back to checkout | |
| 256 | - return wc_get_checkout_url(); | |
| 257 | - } | |
| 258 | - | |
| 259 | 233 | if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) { |
| 260 | 234 | |
| 261 | 235 | //get transaction to update the payment method. |
| 262 | 236 | $transaction = $api->getTransaction( $transactionId ); |
| @@ -266,11 +240,10 @@ | ||
| 266 | 240 | if ( isset( $transaction->type ) && isset( $transaction->method ) ) { |
| 267 | 241 | $paymentMethod = $transaction->channel; |
| 268 | 242 | } |
| 269 | 243 | |
| 270 | - //update _makecommerce_preselected_method | |
| 271 | - $order->update_meta_data( '_makecommerce_preselected_method', $paymentMethod ); | |
| 272 | - $order->save(); | |
| 244 | + //update _makecommerce_preselected_method | |
| 245 | + update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod ); | |
| 273 | 246 | } |
| 274 | 247 | |
| 275 | 248 | if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) { |
| 276 | 249 | //send client back to checkout |
| @@ -276,35 +249,13 @@ | ||
| 276 | 249 | //send client back to checkout |
| 277 | 250 | $returnUrl = wc_get_checkout_url(); |
| 278 | 251 | } |
| 279 | 252 | |
| 280 | - // Avoid duplicate method changes for subscriptions | |
| 281 | - $check_status = true; | |
| 282 | - | |
| 283 | - if ( !empty( $data['token']['id'] ) && !empty( $data['token']['multiuse'] ) ) { | |
| 284 | - // Order is a subscription and saved token is different from response token | |
| 285 | - if ( $order->get_meta( '_makecommerce_payment_token', true) !== $data['token']['id'] ) { | |
| 286 | - $check_status = false; | |
| 287 | - } | |
| 288 | - } | |
| 289 | - | |
| 290 | - // Lock processing for this order+status to avoid race conditions between callback and redirect | |
| 291 | - $lock_key = '_makecommerce_payment_lock_status_' . $paymentStatus; | |
| 292 | - $locked = add_post_meta( $orderId, $lock_key, time(), true ); | |
| 293 | - if ( ! $locked ) { | |
| 294 | - return $returnUrl; | |
| 295 | - } | |
| 296 | - | |
| 297 | 253 | //check if we already processed this status in the past. |
| 298 | - if ( $check_status && $order->get_meta( '_makecommerce_payment_processed_status', true ) == $paymentStatus ) { | |
| 254 | + if (get_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', true ) == $paymentStatus ) { | |
| 299 | 255 | return $returnUrl; |
| 300 | 256 | } |
| 301 | 257 | |
| 302 | - if ( $paymentStatus === 'COMPLETED' ) { | |
| 303 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 304 | - $order->save(); | |
| 305 | - } | |
| 306 | - | |
| 307 | 258 | switch( $paymentStatus ) { |
| 308 | 259 | |
| 309 | 260 | case 'CANCELLED': |
| 310 | 261 | // Do not update status when disabled in settings |
| @@ -310,9 +261,9 @@ | ||
| 310 | 261 | // Do not update status when disabled in settings |
| 311 | 262 | if ( ( $settings['disable_cancelled_payment_update'] ?? '' ) !== 'yes' ) { |
| 312 | 263 | // Update automatically |
| 313 | 264 | $order->update_status( 'cancelled' ); |
| 314 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 265 | + update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 315 | 266 | } |
| 316 | 267 | |
| 317 | 268 | $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl ); |
| 318 | 269 | |
| @@ -324,9 +275,9 @@ | ||
| 324 | 275 | // Do not update order status if disabled in settings |
| 325 | 276 | if ( ( $settings['disable_expired_payment_update'] ?? '' ) !== 'yes' ) { |
| 326 | 277 | // Update automatically |
| 327 | 278 | $order->update_status( 'cancelled' ); |
| 328 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 279 | + update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 329 | 280 | } |
| 330 | 281 | |
| 331 | 282 | $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl ); |
| 332 | 283 | } |
| @@ -339,14 +290,15 @@ | ||
| 339 | 290 | $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() ); |
| 340 | 291 | $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() ); |
| 341 | 292 | |
| 342 | 293 | $orderNote[] = $transactionIdText . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>'; |
| 343 | - $orderNote[] = $paymentOptionText . ': ' . $order->get_meta( '_makecommerce_preselected_method', true ); | |
| 294 | + $orderNote[] = $paymentOptionText . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true ); | |
| 344 | 295 | $order->add_order_note( implode( "\r\n", $orderNote ) ); |
| 345 | 296 | |
| 346 | 297 | if ( !empty( $data['token'] ) && !empty( $data['token']['multiuse'] ) ) { |
| 347 | - $order->update_meta_data( '_makecommerce_payment_token', $data['token']['id'] ); | |
| 348 | - $order->update_meta_data( '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] ); | |
| 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'] ); | |
| 349 | 301 | } |
| 350 | 302 | |
| 351 | 303 | if ( isset( $_GET['lang1'] ) ) { |
| 352 | 304 | $order->update_meta_data( 'wpml_language', $_GET["lang1"] ); |
| @@ -353,53 +305,33 @@ | ||
| 353 | 305 | } |
| 354 | 306 | |
| 355 | 307 | $order->payment_complete( $transactionId ); |
| 356 | 308 | $woocommerce->cart->empty_cart(); |
| 309 | + | |
| 310 | + update_post_meta( $order->get_id(), '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 357 | 311 | |
| 358 | 312 | break; |
| 359 | 313 | } |
| 360 | 314 | |
| 361 | - // Save all changes to order | |
| 362 | - $order->save(); | |
| 363 | - | |
| 364 | 315 | return $returnUrl; |
| 365 | 316 | } |
| 366 | 317 | |
| 367 | - /** | |
| 368 | - * Returns post_id using transaction id or false if not found | |
| 369 | - * | |
| 370 | - * @since 3.0.4 | |
| 371 | - */ | |
| 372 | - public static function get_postid_using_metakey( $meta_key, $transactionId ) { | |
| 373 | - global $wpdb; | |
| 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; | |
| 374 | 326 | |
| 375 | - if ( class_exists( OrderUtil::class ) | |
| 376 | - && OrderUtil::custom_orders_table_usage_is_enabled() | |
| 377 | - ) { | |
| 378 | - // HPOS | |
| 379 | - $results = $wpdb->get_results( | |
| 380 | - $wpdb->prepare(" | |
| 381 | - SELECT {$wpdb->prefix}wc_orders.id | |
| 382 | - FROM {$wpdb->prefix}wc_orders | |
| 383 | - INNER JOIN {$wpdb->prefix}wc_orders_meta AS meta ON meta.order_id = {$wpdb->prefix}wc_orders.id AND meta.meta_key = %s | |
| 384 | - WHERE meta.meta_value = %s | |
| 385 | - ", | |
| 386 | - $meta_key, | |
| 387 | - $transactionId | |
| 388 | - ) | |
| 389 | - ); | |
| 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 | + } | |
| 390 | 334 | |
| 391 | - if ( isset( $results[0]->id ) ) { | |
| 392 | - return $results[0]->id; | |
| 393 | - } | |
| 394 | - } else { | |
| 395 | - // Legacy DB | |
| 396 | - $wpdb->query("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` = '". $meta_key ."' AND `meta_value` = '" . $transactionId . "'"); | |
| 397 | - | |
| 398 | - if ( isset( $wpdb->last_result[0]->post_id ) ) { | |
| 399 | - return $wpdb->last_result[0]->post_id; | |
| 400 | - } | |
| 401 | - } | |
| 402 | - | |
| 403 | - return false; | |
| 404 | - } | |
| 405 | -} | |
| 335 | + return false; | |
| 336 | + } | |
| 337 | +} | |