Skip to content

Commit 6ef95c5

Browse files
Pt 875 rework credit notes (#119)
Co-authored-by: Ivan Pugach <[email protected]>
1 parent e666c6f commit 6ef95c5

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: mondu-ai, arthurmmoreira, tikohov20
33
Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b
44
Requires at least: 5.9.0
55
Tested up to: 6.5.3
6-
Stable tag: 3.0.0
6+
Stable tag: 3.0.1
77
Requires PHP: 7.4
88
License: GPLv3
99
License URI: https://www.gnu.org/licenses/gpl-3.0.html

mondu-buy-now-pay-later.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Mondu Buy Now Pay Later
44
* Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases
55
* Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later.
6-
* Version: 3.0.0
6+
* Version: 3.0.1
77
* Author: Mondu
88
* Author URI: https://mondu.ai
99
*
@@ -27,7 +27,7 @@
2727
die( 'Direct access not allowed' );
2828
}
2929

30-
define( 'MONDU_PLUGIN_VERSION', '3.0.0' );
30+
define( 'MONDU_PLUGIN_VERSION', '3.0.1' );
3131
define( 'MONDU_PLUGIN_FILE', __FILE__ );
3232
define( 'MONDU_PLUGIN_PATH', __DIR__ );
3333
define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) );

src/Mondu/Admin/Order.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ function ( $post_or_order_object ) {
6666
$order = ( $post_or_order_object instanceof \WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : $post_or_order_object;
6767

6868
if ( null === $order ) {
69-
return;
69+
return;
7070
}
7171
if ( !in_array( $order->get_payment_method(), Plugin::PAYMENT_METHODS, true ) ) {
72-
return;
72+
return;
7373
}
7474

7575
$this->render_meta_box_content( $order );

src/Mondu/Mondu/MonduGateway.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
namespace Mondu\Mondu;
88

99
use Mondu\Exceptions\ResponseException;
10+
use Mondu\Mondu\Support\OrderData;
1011
use Mondu\Plugin;
1112
use WC_Order;
1213
use WC_Payment_Gateway;
14+
use WP_Error;
1315

1416
/**
1517
* Mondu Gateway
@@ -59,6 +61,12 @@ public function __construct( $register_hooks = true ) {
5961
add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'thankyou_page' ] );
6062
add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 );
6163
}
64+
65+
66+
$this->supports = [
67+
'refunds',
68+
'products',
69+
];
6270
}
6371

6472
/**
@@ -153,6 +161,60 @@ public function process_payment( $order_id ) {
153161
];
154162
}
155163

164+
/**
165+
* @param WC_Order $order
166+
* @return bool
167+
*/
168+
public function can_refund_order( $order ) {
169+
$can_refund_parent = parent::can_refund_order( $order );
170+
171+
if ( !$can_refund_parent ) {
172+
return false;
173+
}
174+
175+
return (bool) $order->get_meta( Plugin::INVOICE_ID_KEY );
176+
}
177+
178+
/**
179+
* @param $order_id
180+
* @param $amount
181+
* @param $reason
182+
* @return bool|WP_Error
183+
*/
184+
public function process_refund( $order_id, $amount = null, $reason = '' ) {
185+
$order = wc_get_order( $order_id );
186+
187+
if ( !$order instanceof WC_Order ) {
188+
return false;
189+
}
190+
191+
$mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY );
192+
193+
if ( !$mondu_invoice_id ) {
194+
return false;
195+
}
196+
197+
$order_refunds = $order->get_refunds();
198+
/** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */
199+
$refund = isset($order_refunds[0]) ? $order_refunds[0] : null;
200+
201+
if ( !$refund ) {
202+
return false;
203+
}
204+
205+
try {
206+
$result = $this->mondu_request_wrapper->create_credit_note($mondu_invoice_id, OrderData::create_credit_note($refund));
207+
} catch ( ResponseException $e ) {
208+
return new WP_Error('error', $e->getMessage() );
209+
}
210+
211+
if ( isset($result['credit_note']) ) {
212+
return true;
213+
}
214+
215+
return false;
216+
}
217+
156218
/**
157219
* Check if Mondu has its credentials validated.
158220
*

src/Mondu/Mondu/MonduRequestWrapper.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,32 +267,13 @@ public function order_status_changed( $order_id, $from_status, $to_status ) {
267267
/**
268268
* Handle Order Refunded
269269
*
270-
* @param $order_id
271-
* @param $refund_id
272-
* @return void
270+
* @param $mondu_invoice_id
271+
* @param $credit_note
272+
* @return array-key|void
273273
* @throws ResponseException
274274
*/
275-
public function order_refunded( $order_id, $refund_id ) {
276-
$order = new WC_Order( $order_id );
277-
if ( !Plugin::order_has_mondu( $order ) ) {
278-
return;
279-
}
280-
281-
$mondu_invoice_id = $order->get_meta( Plugin::INVOICE_ID_KEY );
282-
if ( !$mondu_invoice_id ) {
283-
Helper::log([
284-
'skipping_credit_note_creation' => [
285-
'order' => $order_id,
286-
'refund' => $refund_id,
287-
],
288-
]);
289-
return;
290-
}
291-
292-
$refund = new WC_Order_Refund( $refund_id );
293-
$credit_note = OrderData::create_credit_note( $refund );
294-
295-
$this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] );
275+
public function create_credit_note( $mondu_invoice_id, $credit_note ) {
276+
return $this->wrap_with_mondu_log_event( 'create_credit_note', [ $mondu_invoice_id, $credit_note ] );
296277
}
297278

298279

src/Mondu/Plugin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public function init() {
138138
*/
139139
add_action( 'woocommerce_order_status_changed', [ $this->mondu_request_wrapper, 'order_status_changed' ], 10, 3 );
140140
add_action( 'woocommerce_before_order_object_save', [ $this->mondu_request_wrapper, 'update_order_if_changed_some_fields' ] );
141-
add_action( 'woocommerce_order_refunded', [ $this->mondu_request_wrapper, 'order_refunded' ], 10, 2 );
142141
add_action( 'woocommerce_blocks_loaded', function () {
143142
if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
144143
add_action(

0 commit comments

Comments
 (0)