diff --git a/CHANGELOG.md b/CHANGELOG.md index 18c45367..fac1a179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [3.8.3] +- Fix: Unable to refund orders that contain deleted products. +- Avoid unnecessary extra API calls in the daily flow. + ## [3.8.2] - Load plugin CSS and JS only on the checkout page to improve performance. diff --git a/altapay.php b/altapay.php index 354f731d..48d2ee0f 100755 --- a/altapay.php +++ b/altapay.php @@ -7,10 +7,10 @@ * Author URI: https://altapay.com * Text Domain: altapay * Domain Path: /languages - * Version: 3.8.2 + * Version: 3.8.3 * Name: SDM_Altapay * WC requires at least: 3.9.0 - * WC tested up to: 10.0.2 + * WC tested up to: 10.1.2 * * @package Altapay */ @@ -41,7 +41,7 @@ } if ( ! defined( 'ALTAPAY_PLUGIN_VERSION' ) ) { - define( 'ALTAPAY_PLUGIN_VERSION', '3.8.2' ); + define( 'ALTAPAY_PLUGIN_VERSION', '3.8.3' ); } // Include the autoloader, so we can dynamically include the rest of the classes. @@ -301,12 +301,6 @@ function altapay_meta_box_side( $post_or_order_object ) { if ( $txnID || $agreement_id ) { $settings = new Core\AltapaySettings(); - $login = $settings->altapayApiLogin(); - - if ( ! $login || is_wp_error( $login ) ) { - echo '

' . __( 'Could not connect to AltaPay!', 'altapay' ) . '

'; - return; - } if ( ! $txnID ) { $txnID = $agreement_id; @@ -557,14 +551,6 @@ function altapayCaptureCallback() { } if ( $txnID ) { - - $login = $settings->altapayApiLogin(); - if ( ! $login ) { - wp_send_json_error( array( 'error' => 'Could not login to the Merchant API:' ) ); - } elseif ( is_wp_error( $login ) ) { - wp_send_json_error( array( 'error' => wp_kses_post( $login->get_error_message() ) ) ); - } - $postOrderLines = isset( $_POST['orderLines'] ) ? wp_unslash( $_POST['orderLines'] ) : ''; $orderLines = array(); @@ -769,13 +755,6 @@ function altapayRefundPayment( $orderID, $amount, $reason, $isAjax ) { return array( 'error' => 'Invalid order' ); } - $login = $settings->altapayApiLogin(); - if ( ! $login ) { - return array( 'error' => 'Could not login to the Merchant API:' ); - } elseif ( is_wp_error( $login ) ) { - return array( 'error' => wp_kses_post( $login->get_error_message() ) ); - } - $postOrderLines = isset( $_POST['orderLines'] ) ? wp_unslash( $_POST['orderLines'] ) : ''; if ( $postOrderLines ) { $selectedProducts = array( @@ -939,12 +918,6 @@ function altapayReleasePayment() { $reserved = 0; $refunded = 0; - $login = $settings->altapayApiLogin(); - if ( ! $login ) { - wp_send_json_error( array( 'error' => 'Could not login to the Merchant API:' ) ); - } elseif ( is_wp_error( $login ) ) { - wp_send_json_error( array( 'error' => wp_kses_post( $login->get_error_message() ) ) ); - } try { $auth = $settings->getAuth(); $api = new Payments( $auth ); diff --git a/classes/core/AltapayOrderStatus.php b/classes/core/AltapayOrderStatus.php index 1412e85c..8ca764db 100644 --- a/classes/core/AltapayOrderStatus.php +++ b/classes/core/AltapayOrderStatus.php @@ -39,6 +39,10 @@ public function registerHooks() { * @return void|WP_Error */ public function orderStatusChanged( $orderID, $previousStatus, $newStatus, $order ) { + if ( ! in_array( $newStatus, array( 'cancelled', 'completed' ), true ) ) { + return; + } + $txnID = $order->get_transaction_id(); $captured = 0; $reserved = 0; @@ -46,23 +50,15 @@ public function orderStatusChanged( $orderID, $previousStatus, $newStatus, $orde $status = ''; $settings = new Core\AltapaySettings(); - $login = $settings->altapayApiLogin(); - - if ( ! $login || is_wp_error( $login ) ) { - echo '

' . __( 'Could not connect to AltaPay!', 'altapay' ) . '

'; - return; - } - - $auth = $settings->getAuth(); + $auth = $settings->getAuth(); try { $api = new Payments( $auth ); $api->setTransaction( $txnID ); $payments = $api->call(); } catch ( Exception $e ) { - echo '

' . __( 'Could not fetch Payments from AltaPay!', 'altapay' ) . '

'; + $order->add_order_note( $e->getMessage() ); return; } - if ( $payments ) { foreach ( $payments as $pay ) { $reserved += $pay->ReservedAmount; diff --git a/classes/core/AltapaySettings.php b/classes/core/AltapaySettings.php old mode 100755 new mode 100644 index 48d7e0fe..612a55df --- a/classes/core/AltapaySettings.php +++ b/classes/core/AltapaySettings.php @@ -71,13 +71,6 @@ public function altapayOrderStatusCompleted( $orderID ) { return; } - $login = $this->altapayApiLogin(); - if ( ! $login || is_wp_error( $login ) ) { - error_log( 'Could not connect to AltaPay!' ); - - return; - } - try { $auth = $this->getAuth(); $api = new Payments( $auth ); diff --git a/classes/util/UtilMethods.php b/classes/util/UtilMethods.php old mode 100755 new mode 100644 index 6eb4410e..619ecc5d --- a/classes/util/UtilMethods.php +++ b/classes/util/UtilMethods.php @@ -118,15 +118,19 @@ private function getOrderLine( $item, $isSubscription = false ) { $quantity = $item->get_quantity(); // generate line date with all the calculated parameters - $orderLine = new OrderLine( + $orderLine = new OrderLine( $item->get_name(), $item->get_id(), $quantity, round( $item->get_subtotal() / $quantity, 2 ) ); - $orderLine->productUrl = $product->get_permalink(); - $orderLine->imageUrl = wp_get_attachment_url( $product->get_image_id() ); - $orderLine->unitCode = $quantity > 1 ? 'units' : 'unit'; + + if ( $product ) { + $orderLine->productUrl = $product->get_permalink(); + $orderLine->imageUrl = wp_get_attachment_url( $product->get_image_id() ); + } + + $orderLine->unitCode = $quantity > 1 ? 'units' : 'unit'; if ( ! $isSubscription ) { $orderLine->taxAmount = round( $item->get_subtotal_tax(), 2 ); diff --git a/helpers/traits/AltapayMaster.php b/helpers/traits/AltapayMaster.php index 2c0c0e13..1bfe806e 100644 --- a/helpers/traits/AltapayMaster.php +++ b/helpers/traits/AltapayMaster.php @@ -65,12 +65,6 @@ public function scheduledSubscriptionsPayment( $amount, $renewal_order ) { return; } - $login = $this->altapayApiLogin(); - if ( ! $login || is_wp_error( $login ) ) { - echo '

' . __( 'Could not connect to AltaPay!', 'altapay' ) . '

'; - return; - } - $payment_method = wc_get_payment_gateway_by_order( $renewal_order ); if ( $payment_method && isset( $payment_method->settings ) && is_array( $payment_method->settings ) ) { $settings = $payment_method->settings; @@ -111,8 +105,7 @@ public function scheduledSubscriptionsPayment( $amount, $renewal_order ) { $transaction = $jsonToArray[ $latest_transaction ]; $transaction_id = $transaction['TransactionId']; - $renewal_order->update_meta_data( '_transaction_id', $transaction_id ); - $renewal_order->save(); + $renewal_order->set_transaction_id( $transaction_id ); if ( $response->Result === 'Success' ) { $reconciliation = new Core\AltapayReconciliation(); diff --git a/readme.txt b/readme.txt index 3731a128..b3e20aa4 100644 --- a/readme.txt +++ b/readme.txt @@ -4,10 +4,10 @@ Tags: AltaPay, Gateway, Payments, WooCommerce, Payment Card Industry Requires PHP: 7.4 Requires at least: 5.0 Tested up to: 6.8.2 -Stable tag: 3.8.2 +Stable tag: 3.8.3 License: MIT WC requires at least: 3.9.0 -WC tested up to: 10.0.2 +WC tested up to: 10.1.2 License URI: http://www.gnu.org/licenses/gpl-2.0.html A plugin that integrates your WooCommerce web shop to the AltaPay payments gateway. @@ -39,6 +39,10 @@ AltaPay's Payment Gateway for WooCommerce provides merchants with access to a fu == Changelog == += 3.8.3 = +* Fix: Unable to refund orders that contain deleted products. +* Avoid unnecessary extra API calls in the daily flow. + = 3.8.2 = * Load plugin CSS and JS only on the checkout page to improve performance. diff --git a/views/paymentClass.tpl b/views/paymentClass.tpl old mode 100755 new mode 100644 index 6365450a..0bbdde78 --- a/views/paymentClass.tpl +++ b/views/paymentClass.tpl @@ -147,11 +147,6 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { global $wpdb; $utilMethods = new Util\UtilMethods; $altapayHelpers = new Helpers\AltapayHelpers; - // Create form request etc. - $login = $this->altapayApiLogin(); - if ( ! $login || is_wp_error( $login ) ) { - throw new Exception( 'Could not connect to AltaPay!' ); - } // Create payment request $order = new WC_Order( $order_id ); @@ -160,7 +155,7 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { $amount = $this->getOrderAmount( $order ); $currency = $order->get_currency(); $customerInfo = $this->setCustomer( $order ); - $cookie = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : ''; + $cookie = $_SERVER['HTTP_COOKIE'] ?? ''; $language = 'en'; $languages = array( 'ca', @@ -427,6 +422,7 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { $lastFourDigits = $transaction['CardInformation']['LastFourDigits'] ?? ''; $ccExpiryDate = isset( $transaction['CreditCardExpiry'] ) ? ( $transaction['CreditCardExpiry']['Month'] . '/' . $transaction['CreditCardExpiry']['Year'] ) : ''; $reservedAmount = $transaction['ReservedAmount'] ?? 0; + $capturedAmount = $transaction['CapturedAmount'] ?? 0; $surchargeAmount = $transaction['SurchargeAmount'] ?? 0; if ( $this->apply_surcharge === 'yes' && $surchargeAmount > 0 ) { @@ -551,7 +547,6 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { if ( $transaction['AuthType'] === 'subscription_payment' and $transaction['TransactionStatus'] === 'pending' ) { $order->update_status( 'on-hold', 'The payment is pending an update from the payment provider.' ); } else { - $order->add_order_note( __( 'Callback completed', 'altapay' ) ); $order->payment_complete(); } @@ -607,12 +602,6 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { // Redirect to Order Confirmation Page if ( $type === 'paymentAndCapture' && $requireCapture === 'true' && $callback_type == 'ok' ) { - $login = $this->altapayApiLogin(); - if ( ! $login || is_wp_error( $login ) ) { - error_log( 'Could not connect to AltaPay!' ); - return; - } - $api = new CaptureReservation( $this->getAuth() ); $api->setAmount( round( $amount, 2 ) ); $api->setTransaction( $txnId ); @@ -637,7 +626,11 @@ class WC_Gateway_{key} extends WC_Payment_Gateway { } catch ( \Exception $e ) { error_log( 'Exception ' . $e->getMessage() ); } + } elseif ( $type === 'paymentAndCapture' && $capturedAmount == $this->getOrderAmount( $order ) ) { + $order->update_meta_data( '_captured', true ); + $order->save(); } + $redirect = $this->get_return_url( $order ); wp_redirect( $redirect ); exit; diff --git a/wiki.md b/wiki.md index 52919974..03c9392b 100644 --- a/wiki.md +++ b/wiki.md @@ -299,13 +299,13 @@ The new credentials can now be used as the API Username and API Password in your Minimum system requirements are: - WordPress min. 5.0 – max. 6.8.2 -- WooCommerce min. 3.9.0 – max. 10.0.2 +- WooCommerce min. 3.9.0 – max. 10.1.2 - PHP 7.4 and above - PHP-bcmath library installed. - PHP-curl MUST be enabled. The latest tested version is: -- WordPress 6.8.2, WooCommerce 10.0.2 and PHP 8.2 +- WordPress 6.8.2, WooCommerce 10.1.2 and PHP 8.2 ## Troubleshooting