Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pt 875 rework credit notes #119

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mondu-buy-now-pay-later.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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) );
Expand Down
4 changes: 2 additions & 2 deletions src/Mondu/Admin/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
62 changes: 62 additions & 0 deletions src/Mondu/Mondu/MonduGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
];
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 5 additions & 24 deletions src/Mondu/Mondu/MonduRequestWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
}


Expand Down
1 change: 0 additions & 1 deletion src/Mondu/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading