| @@ -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 |
| @@ -58,9 +57,8 @@ | ||
| 58 | 57 | $this->version = $version; |
| 59 | 58 | $this->loader = $loader; |
| 60 | 59 | |
| 61 | 60 | $this->loader->add_action( 'plugins_loaded', $this, 'initialize_payment_gateways' ); |
| 62 | - $this->loader->add_action( 'woocommerce_before_checkout_form', $this, 'check_payment_status' ); | |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /** |
| 66 | 64 | * Initialize all payment gateways |
| @@ -67,8 +65,14 @@ | ||
| 67 | 65 | * |
| 68 | 66 | * @since 3.0.0 |
| 69 | 67 | */ |
| 70 | 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 | + | |
| 71 | 75 | //WooCommerce |
| 72 | 76 | new Payment\Gateway\WooCommerce( true ); |
| 73 | 77 | } |
| 74 | 78 | |
| @@ -126,39 +130,13 @@ | ||
| 126 | 130 | |
| 127 | 131 | } |
| 128 | 132 | |
| 129 | 133 | /** |
| 130 | - * Add check for unsuccesful payment redirects | |
| 131 | - * Displays notice if something went wrong | |
| 132 | - * | |
| 133 | - * @since 3.0.12 | |
| 134 | - */ | |
| 135 | - public function check_payment_status() | |
| 136 | - { | |
| 137 | - if ( isset ( $_GET['mc_payment_status'] ) ) { | |
| 138 | - switch( $_GET['mc_payment_status'] ) { | |
| 139 | - case 'failed': | |
| 140 | - wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' ); | |
| 141 | - break; | |
| 142 | - case 'cancelled': | |
| 143 | - wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error' ); | |
| 144 | - break; | |
| 145 | - case 'expired': | |
| 146 | - wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error' ); | |
| 147 | - break; | |
| 148 | - } | |
| 149 | - | |
| 150 | - // Avoid multiple error messages | |
| 151 | - unset($_GET['mc_payment_status']); | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | 134 | * Check payment and process order if completed, canceled |
| 157 | 135 | * |
| 158 | 136 | * @since 3.0.0 |
| 159 | 137 | */ |
| 160 | - public static function check_payment( $settings = array() ) { | |
| 138 | + public static function check_payment() { | |
| 161 | 139 | |
| 162 | 140 | global $woocommerce; |
| 163 | 141 | |
| 164 | 142 | $api = \MakeCommerce::get_api(); |
| @@ -174,28 +152,27 @@ | ||
| 174 | 152 | |
| 175 | 153 | if ( $api->verifyMac( $request ) ) { |
| 176 | 154 | |
| 177 | 155 | $data = $api->extractRequestData( $request ); |
| 178 | - $transactionId = false; | |
| 179 | 156 | $reference = false; |
| 180 | 157 | |
| 181 | 158 | if ( !empty( $data['error'] ) ) { |
| 182 | 159 | |
| 183 | - //add status and send client back to checkout | |
| 184 | - return add_query_arg( 'mc_payment_status', 'failed', wc_get_checkout_url() ); | |
| 160 | + wc_add_notice( __( 'Payment failed', 'wc_makecommerce_domain' ), 'error' ); | |
| 161 | + | |
| 162 | + return $returnUrl; | |
| 185 | 163 | } |
| 186 | 164 | |
| 187 | 165 | if ( !empty( $data['message_type'] ) ) { |
| 188 | 166 | |
| 189 | - if ( $data['message_type'] === 'payment_return' ) { | |
| 167 | + if ( $data['message_type'] == 'payment_return' ) { | |
| 190 | 168 | |
| 191 | 169 | $transactionId = $data['transaction']; |
| 192 | 170 | $reference = $data['reference']; |
| 193 | 171 | $paymentStatus = $data['status']; |
| 194 | - $totalAmount = $data['amount']; | |
| 195 | 172 | } |
| 196 | 173 | |
| 197 | - if ( $data['message_type'] === 'token_return' ) { | |
| 174 | + if ( $data['message_type'] == 'token_return' ) { | |
| 198 | 175 | if ( !empty( $data['transaction']['reference'] ) ) { |
| 199 | 176 | $reference = $data['transaction']['reference']; |
| 200 | 177 | } |
| 201 | 178 | |
| @@ -205,58 +182,21 @@ | ||
| 205 | 182 | } |
| 206 | 183 | } |
| 207 | 184 | |
| 208 | 185 | // if we didn't find reference to an Order, we send user to shop landing-page. TODO: could be improved |
| 209 | - if ( empty( $transactionId ) ) { | |
| 186 | + if ( !$reference ) { | |
| 210 | 187 | return $returnUrl; |
| 211 | 188 | } |
| 212 | 189 | |
| 213 | - //get the post id using transactionid | |
| 214 | - $orderId = self::get_postid_using_metakey( '_makecommerce_transaction_id', $transactionId ); | |
| 215 | - | |
| 216 | - $order = wc_get_order( $orderId ); | |
| 190 | + $order = new \WC_Order( $reference ); | |
| 217 | 191 | |
| 218 | - // if we didn't find the Order, we send user to shop landing-page. TODO: could be improved | |
| 219 | - if ( !$order || !$order->get_id() ) { | |
| 192 | + // if we din't find the Order, we send user to shop landing-page. TODO: could be improved | |
| 193 | + if ( !$order->get_id() ) { | |
| 220 | 194 | return $returnUrl; |
| 221 | 195 | } |
| 222 | 196 | |
| 223 | - $returnUrl = $order->get_meta( '_makecommerce_order_received_url', true ); | |
| 224 | - if ( !$returnUrl ) { | |
| 225 | - $returnUrl = $order->get_checkout_order_received_url(); | |
| 226 | - } | |
| 197 | + $returnUrl = $order->get_checkout_order_received_url(); | |
| 227 | 198 | |
| 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 | 199 | if ( $paymentStatus == 'EXPIRED' || $paymentStatus == 'CANCELLED' || $paymentStatus == 'COMPLETED' ) { |
| 260 | 200 | |
| 261 | 201 | //get transaction to update the payment method. |
| 262 | 202 | $transaction = $api->getTransaction( $transactionId ); |
| @@ -266,87 +206,40 @@ | ||
| 266 | 206 | if ( isset( $transaction->type ) && isset( $transaction->method ) ) { |
| 267 | 207 | $paymentMethod = $transaction->channel; |
| 268 | 208 | } |
| 269 | 209 | |
| 270 | - //update _makecommerce_preselected_method | |
| 271 | - $order->update_meta_data( '_makecommerce_preselected_method', $paymentMethod ); | |
| 272 | - $order->save(); | |
| 210 | + //update _makecommerce_preselected_method | |
| 211 | + update_post_meta( $order->get_id(), '_makecommerce_preselected_method', $paymentMethod ); | |
| 273 | 212 | } |
| 274 | - | |
| 275 | - if ( $paymentStatus == 'CANCELLED' || ( $paymentStatus == 'EXPIRED' && $order->get_status() == "pending" ) ) { | |
| 276 | - //send client back to checkout | |
| 277 | - $returnUrl = wc_get_checkout_url(); | |
| 278 | - } | |
| 279 | - | |
| 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 | - //check if we already processed this status in the past. | |
| 298 | - if ( $check_status && $order->get_meta( '_makecommerce_payment_processed_status', true ) == $paymentStatus ) { | |
| 299 | - return $returnUrl; | |
| 300 | - } | |
| 301 | - | |
| 302 | - if ( $paymentStatus === 'COMPLETED' ) { | |
| 303 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 304 | - $order->save(); | |
| 305 | - } | |
| 306 | - | |
| 213 | + | |
| 307 | 214 | switch( $paymentStatus ) { |
| 308 | 215 | |
| 309 | 216 | case 'CANCELLED': |
| 310 | - // Do not update status when disabled in settings | |
| 311 | - if ( ( $settings['disable_cancelled_payment_update'] ?? '' ) !== 'yes' ) { | |
| 312 | - // Update automatically | |
| 313 | - $order->update_status( 'cancelled' ); | |
| 314 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 315 | - } | |
| 217 | + $order->update_status( 'cancelled' ); | |
| 218 | + wc_add_notice( __( 'Payment transaction cancelled', 'wc_makecommerce_domain' ), 'error'); | |
| 219 | + $returnUrl = wc_get_cart_url(); | |
| 316 | 220 | |
| 317 | - $returnUrl = add_query_arg( 'mc_payment_status', 'cancelled', $returnUrl ); | |
| 318 | - | |
| 319 | 221 | break; |
| 320 | 222 | case 'EXPIRED': |
| 321 | 223 | //only update if order status is pending payment |
| 322 | 224 | if ( $order->get_status() == "pending" ) { |
| 323 | 225 | |
| 324 | - // Do not update order status if disabled in settings | |
| 325 | - if ( ( $settings['disable_expired_payment_update'] ?? '' ) !== 'yes' ) { | |
| 326 | - // Update automatically | |
| 327 | - $order->update_status( 'cancelled' ); | |
| 328 | - $order->update_meta_data( '_makecommerce_payment_processed_status', $paymentStatus ); | |
| 329 | - } | |
| 330 | - | |
| 331 | - $returnUrl = add_query_arg( 'mc_payment_status', 'expired', $returnUrl ); | |
| 226 | + $order->update_status( 'cancelled' ); | |
| 227 | + wc_add_notice( __( 'Payment transaction expired', 'wc_makecommerce_domain' ), 'error'); | |
| 228 | + $returnUrl = wc_get_cart_url(); | |
| 332 | 229 | } |
| 333 | 230 | |
| 334 | 231 | break; |
| 335 | 232 | case 'COMPLETED': |
| 336 | 233 | $orderNote = array(); |
| 337 | - $transactionIdText = __( 'Transaction ID', 'wc_makecommerce_domain' ); | |
| 338 | - $paymentOptionText = __( 'Payment option', 'wc_makecommerce_domain' ); | |
| 339 | - $transactionIdText = \MakeCommerce\i18n::get_string_from_mo( 'Transaction ID', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() ); | |
| 340 | - $paymentOptionText = \MakeCommerce\i18n::get_string_from_mo( 'Payment option', 'wc_makecommerce_domain', \MakeCommerce\i18n::get_site_default_language() ); | |
| 341 | - | |
| 342 | - $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 ); | |
| 234 | + $orderNote[] = __( 'Transaction ID', 'wc_makecommerce_domain' ) . ': <a target=_blank href="'.$api->getEnvUrls()->merchantUrl.'merchant/shop/deals/detail.html?id='. $transactionId .'">'.$transactionId.'</a>'; | |
| 235 | + $orderNote[] = __( 'Payment option', 'wc_makecommerce_domain' ) . ': ' . get_post_meta( $order->get_id(), '_makecommerce_preselected_method', true ); | |
| 344 | 236 | $order->add_order_note( implode( "\r\n", $orderNote ) ); |
| 345 | 237 | |
| 346 | 238 | 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'] ); | |
| 239 | + | |
| 240 | + update_post_meta( $order->get_id(), '_makecommerce_payment_token', $data['token']['id'] ); | |
| 241 | + update_post_meta( $order->get_id(), '_makecommerce_payment_token_valid_until', $data['token']['valid_until'] ); | |
| 349 | 242 | } |
| 350 | 243 | |
| 351 | 244 | if ( isset( $_GET['lang1'] ) ) { |
| 352 | 245 | $order->update_meta_data( 'wpml_language', $_GET["lang1"] ); |
| @@ -357,49 +250,7 @@ | ||
| 357 | 250 | |
| 358 | 251 | break; |
| 359 | 252 | } |
| 360 | 253 | |
| 361 | - // Save all changes to order | |
| 362 | - $order->save(); | |
| 363 | - | |
| 364 | 254 | return $returnUrl; |
| 365 | 255 | } |
| 366 | - | |
| 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; | |
| 374 | - | |
| 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 | - ); | |
| 390 | - | |
| 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 | -} | |
| 256 | +} | |