diff --git a/README.txt b/README.txt index 215e54f..35ddcff 100644 --- a/README.txt +++ b/README.txt @@ -4,7 +4,7 @@ Donate link: https://exn.rs Tags: comments, spam Requires at least: 5.2 Tested up to: 6.5 -Stable tag: 1.0.9 +Stable tag: 1.0.10 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -125,3 +125,9 @@ Automatic updates should work smoothly, but we still recommend you back up your **ArkPay** * Fix - Removed the JSON_UNESCAPED_SLASHES option from the signature creation in the payment request. + += 1.0.10 2024-03-06 = + +**ArkPay** + +* Fix - Cart - Checking if there is already an active transaction before proceeding. diff --git a/arkpay.php b/arkpay.php index dae3e0a..e2661fd 100644 --- a/arkpay.php +++ b/arkpay.php @@ -16,7 +16,7 @@ * Plugin Name: Arkpay * Plugin URI: https://arkpay.com * Description: The Smartest, Fastest & Most Secure Payment Processor. - * Version: 1.0.9 + * Version: 1.0.10 * Author: Arkpay * Author URI: https://arkpay.com/ * License: GPL-2.0+ @@ -35,7 +35,7 @@ * Start at version 1.0.0 and use SemVer - https://semver.org * Rename this for your plugin and update it as you release new versions. */ -define( 'ARKPAY_VERSION', '1.0.9' ); +define( 'ARKPAY_VERSION', '1.0.10' ); /** * The code that runs during plugin activation. diff --git a/includes/class-arkpay-cart-button.php b/includes/class-arkpay-cart-button.php index 802af37..a21f2cd 100644 --- a/includes/class-arkpay-cart-button.php +++ b/includes/class-arkpay-cart-button.php @@ -35,9 +35,14 @@ function add_arkpay_cart_pay_button() { url: '', data: cartData, success: function (response) { + if ( 200 !== response.data['code'] ) { + location.reload(); + return; + } + $(e.target).addClass('disabled'); window.open( - response.data, + response.data['redirect_url'], '_blank', ); }, @@ -82,6 +87,35 @@ function arkpay_save_draft_order() { 'handlePayment' => false, ); + $cart_contents = WC()->cart->get_cart(); + + if ( empty( $cart_contents ) ) { + wc_add_notice( __( 'ArkPay: Your cart is empty.', 'arkpay-payment' ), 'error' ); + wp_send_json_error( array( + 'code' => 400, + 'message' => 'Cart is empty.' + ) ); + wp_die(); + } + + $session = WC()->session->get_session_cookie(); + $session_id = implode( '', $session ); + $cart_contents_serialized = serialize( $cart_contents ); + $cart_hash = WC()->cart->get_cart_hash(); + $cart_identifier = md5( $session_id . $cart_hash . $cart_contents_serialized ); + + $transaction_url = $arkpay_gateway->get_draft_order_by_cart_identifier( $cart_identifier ); + if ( ! empty( $transaction_url ) ) { + // Clear cart + WC()->cart->empty_cart(); + + wp_send_json_success( array( + 'code' => 200, + 'redirect_url' => $transaction_url + ) ); + wp_die(); + } + $transaction = $arkpay_gateway->create_arkpay_transaction( $order_data ); if ( $transaction ) { @@ -96,7 +130,7 @@ function arkpay_save_draft_order() { $session_instance = WC()->session; $shipping_method_id = $session_instance->get('chosen_shipping_methods')[0]; - + $chosen_shipping_methods = $session_instance->get('shipping_for_package_0')['rates']; $shipping_method_cost = 0; foreach ( $chosen_shipping_methods as $method_id => $rate ) { @@ -117,7 +151,9 @@ function arkpay_save_draft_order() { $draft_order_data = array( 'transaction_id' => $transaction->transaction->id, 'transaction_status' => $transaction->transaction->status, + 'transaction_url' => $transaction->redirectUrl, 'cart_items' => wp_json_encode( $items ), + 'cart_identifier' => $cart_identifier, 'order_id' => null, 'order_key' => null, 'shipping' => wp_json_encode( $shipping ), @@ -130,7 +166,10 @@ function arkpay_save_draft_order() { // Clear cart WC()->cart->empty_cart(); - wp_send_json_success( $redirect_url ); + wp_send_json_success( array( + 'code' => 200, + 'redirect_url' => $redirect_url + ) ); } } catch ( Exception $error ) { wp_send_json_error( array( 'error_message' => $error->getMessage() ) ); diff --git a/includes/class-wc-gateway-arkpay.php b/includes/class-wc-gateway-arkpay.php index 0cb6d80..a4a2f86 100644 --- a/includes/class-wc-gateway-arkpay.php +++ b/includes/class-wc-gateway-arkpay.php @@ -146,9 +146,11 @@ private function create_arkpay_draft_order_table() { $sql_order = "CREATE TABLE $table_order ( transaction_id VARCHAR(255) NOT NULL, transaction_status VARCHAR(50), + transaction_url LONGTEXT, cart_items LONGTEXT, order_id VARCHAR(255), order_key VARCHAR(255), + cart_identifier VARCHAR(255), shipping LONGTEXT, PRIMARY KEY (transaction_id) ) $charset_collate;"; @@ -670,6 +672,26 @@ public function create_arkpay_transaction( $order ) { return json_decode( wp_remote_retrieve_body( $response ) ); } + /** + * Retrieves the draft order's transaction URL based on the cart identifier. + * + * This function queries the database to find a draft order associated with the provided cart identifier + * and with a transaction status of 'NOT_STARTED'. + * + * @param string $cart_identifier The identifier of the cart to search for. + * @return string|null The transaction URL of the draft order if found, or null if no matching draft order is found. + */ + public function get_draft_order_by_cart_identifier( $cart_identifier ) { + global $wpdb; + + $table_name = $wpdb->prefix . 'arkpay_draft_order'; + $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE cart_identifier=%s AND transaction_status=%s", $cart_identifier, 'NOT_STARTED' ) ); + if ( ! empty( $results ) ) { + $transaction_url = $results[0]->transaction_url; + return $transaction_url; + } + } + /** * Save draft order data to a arkpay_draft_order table in the database. * @@ -678,9 +700,12 @@ public function create_arkpay_transaction( $order ) { * * @type string $transaction_id The transaction ID. * @type string $transaction_status The transaction status. + * @type string $transaction_url The transaction URL. * @type string $cart_items The serialized cart items. + * @type string $cart_identifier The cart identifier. * @type int $order_id The order ID. * @type string $order_key The order key. + * @type string $shipping The serialized shipping data. * } * * @global wpdb $wpdb WordPress database class. @@ -692,7 +717,9 @@ public function save_draft_order( $order_data ) { $table_order = $wpdb->prefix . 'arkpay_draft_order'; $transaction_id = $order_data['transaction_id']; $transaction_status = $order_data['transaction_status']; + $transaction_url = $order_data['transaction_url']; $cart_items = $order_data['cart_items']; + $cart_identifier = $order_data['cart_identifier']; $order_id = $order_data['order_id']; $order_key = $order_data['order_key']; $shipping = $order_data['shipping']; @@ -702,7 +729,9 @@ public function save_draft_order( $order_data ) { array( 'transaction_id' => $transaction_id, 'transaction_status' => $transaction_status, + 'transaction_url' => $transaction_url, 'cart_items' => $cart_items, + 'cart_identifier' => $cart_identifier, 'order_id' => $order_id, 'order_key' => $order_key, 'shipping' => $shipping,