From 6ef95c5340cfef74e66bac63c5171b18f4272979 Mon Sep 17 00:00:00 2001 From: Tigran <62335544+tikohov20@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:10:51 +0400 Subject: [PATCH] Pt 875 rework credit notes (#119) Co-authored-by: Ivan Pugach --- README.txt | 2 +- mondu-buy-now-pay-later.php | 4 +- src/Mondu/Admin/Order.php | 4 +- src/Mondu/Mondu/MonduGateway.php | 62 +++++++++++++++++++++++++ src/Mondu/Mondu/MonduRequestWrapper.php | 29 ++---------- src/Mondu/Plugin.php | 1 - 6 files changed, 72 insertions(+), 30 deletions(-) diff --git a/README.txt b/README.txt index f84109b..6723444 100644 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: mondu-ai, arthurmmoreira, tikohov20 Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b Requires at least: 5.9.0 Tested up to: 6.5.3 -Stable tag: 3.0.0 +Stable tag: 3.0.1 Requires PHP: 7.4 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/mondu-buy-now-pay-later.php b/mondu-buy-now-pay-later.php index 3aff68a..df286ee 100644 --- a/mondu-buy-now-pay-later.php +++ b/mondu-buy-now-pay-later.php @@ -3,7 +3,7 @@ * Plugin Name: Mondu Buy Now Pay Later * Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases * Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later. - * Version: 3.0.0 + * Version: 3.0.1 * Author: Mondu * Author URI: https://mondu.ai * @@ -27,7 +27,7 @@ die( 'Direct access not allowed' ); } -define( 'MONDU_PLUGIN_VERSION', '3.0.0' ); +define( 'MONDU_PLUGIN_VERSION', '3.0.1' ); define( 'MONDU_PLUGIN_FILE', __FILE__ ); define( 'MONDU_PLUGIN_PATH', __DIR__ ); define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) ); diff --git a/src/Mondu/Admin/Order.php b/src/Mondu/Admin/Order.php index 9925113..6f73ae7 100644 --- a/src/Mondu/Admin/Order.php +++ b/src/Mondu/Admin/Order.php @@ -66,10 +66,10 @@ function ( $post_or_order_object ) { $order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object; if ( null === $order ) { -return; + return; } if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) { -return; + return; } $this->render_meta_box_content( $order ); diff --git a/src/Mondu/Mondu/MonduGateway.php b/src/Mondu/Mondu/MonduGateway.php index a667df8..6b90139 100644 --- a/src/Mondu/Mondu/MonduGateway.php +++ b/src/Mondu/Mondu/MonduGateway.php @@ -7,9 +7,11 @@ namespace Mondu\Mondu; use Mondu\Exceptions\ResponseException; +use Mondu\Mondu\Support\OrderData; use Mondu\Plugin; use WC_Order; use WC_Payment_Gateway; +use WP_Error; /** * Mondu Gateway @@ -59,6 +61,12 @@ public function __construct( $register_hooks = true ) { add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ] ); add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 ); } + + + $this->supports = [ + 'refunds', + 'products', + ]; } /** @@ -153,6 +161,60 @@ public function process_payment( $order_id ) { ]; } + /** + * @param WC_Order $order + * @return bool + */ + public function can_refund_order( $order ) { + $can_refund_parent = parent::can_refund_order( $order ); + + if ( !$can_refund_parent ) { + return false; + } + + return (bool) $order->get_meta( Plugin::INVOICE_ID_KEY ); + } + + /** + * @param $order_id + * @param $amount + * @param $reason + * @return bool|WP_Error + */ + public function process_refund( $order_id, $amount = null, $reason = '' ) { + $order = wc_get_order( $order_id ); + + if ( !$order instanceof WC_Order ) { + return false; + } + + $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); + + if ( !$mondu_invoice_id ) { + return false; + } + + $order_refunds = $order->get_refunds(); + /** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */ + $refund = isset($order_refunds[0]) ? $order_refunds[0] : null; + + if ( !$refund ) { + return false; + } + + try { + $result = $this->mondu_request_wrapper->create_credit_note($mondu_invoice_id, OrderData::create_credit_note($refund)); + } catch ( ResponseException $e ) { + return new WP_Error('error', $e->getMessage() ); + } + + if ( isset($result['credit_note']) ) { + return true; + } + + return false; + } + /** * Check if Mondu has its credentials validated. * diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index 00752bd..58108fd 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -267,32 +267,13 @@ public function order_status_changed( $order_id, $from_status, $to_status ) { /** * Handle Order Refunded * - * @param $order_id - * @param $refund_id - * @return void + * @param $mondu_invoice_id + * @param $credit_note + * @return array-key|void * @throws ResponseException */ - public function order_refunded( $order_id, $refund_id ) { - $order = new WC_Order( $order_id ); - if ( !Plugin::order_has_mondu( $order ) ) { - return; - } - - $mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY ); - if ( !$mondu_invoice_id ) { - Helper::log([ - 'skipping_credit_note_creation' => [ - 'order' => $order_id, - 'refund' => $refund_id, - ], - ]); - return; - } - - $refund = new WC_Order_Refund( $refund_id ); - $credit_note = OrderData::create_credit_note( $refund ); - - $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] ); + public function create_credit_note( $mondu_invoice_id, $credit_note ) { + return $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] ); } diff --git a/src/Mondu/Plugin.php b/src/Mondu/Plugin.php index 613ac85..f599309 100644 --- a/src/Mondu/Plugin.php +++ b/src/Mondu/Plugin.php @@ -138,7 +138,6 @@ public function init() { */ add_action( 'woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3 ); add_action( 'woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ] ); - add_action( 'woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2 ); add_action( 'woocommerce_blocks_loaded', function () { if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) { add_action(