diff --git a/src/Mondu/Mondu/Api.php b/src/Mondu/Mondu/Api.php index 55498b78..68e1ef7d 100644 --- a/src/Mondu/Mondu/Api.php +++ b/src/Mondu/Mondu/Api.php @@ -120,6 +120,18 @@ public function get_invoices($mondu_uuid) { return json_decode($result['body'], true); } + /** + * @param $mondu_order_uuid + * @param $mondu_invoice_uuid + * @return string + * @throws MonduException + * @throws ResponseException + */ + public function get_invoice($mondu_order_uuid, $mondu_invoice_uuid) { + $result = $this->get(sprintf('/orders/%s/invoices/%s', $mondu_order_uuid, $mondu_invoice_uuid), null); + return json_decode($result['body'], true); + } + /** * @return string * @throws MonduException @@ -172,6 +184,18 @@ public function cancel_invoice($mondu_uuid, $mondu_invoice_uuid) { return json_decode($result['body'], true); } + /** + * @param $mondu_invoice_uuid + * @param array $credit_note + * @return mixed + * @throws MonduException + * @throws ResponseException + */ + public function create_credit_note($mondu_invoice_uuid, array $credit_note) { + $result = $this->post(sprintf('/invoices/%s/credit_notes', $mondu_invoice_uuid), $credit_note); + return json_decode($result['body'], true); + } + /** * @param $path * @param array|string|null $body diff --git a/src/Mondu/Mondu/Gateway.php b/src/Mondu/Mondu/Gateway.php index dd65e306..e68e8add 100644 --- a/src/Mondu/Mondu/Gateway.php +++ b/src/Mondu/Mondu/Gateway.php @@ -173,21 +173,26 @@ public function order_status_changed($order_id, $from_status, $to_status) { * @throws MonduException * @throws ResponseException */ - // public function order_refunded($order_id, $refund_id) { - // $order = new WC_Order($order_id); - // $refund = new WC_Order_Refund($refund_id); - // $mondu_order_id = get_post_meta($order->get_id(), Plugin::ORDER_ID_KEY, true); - - // $order_total = $order->get_total(); - // $order_total_tax = $order->get_total_tax(); - // $refund_total = $refund->get_total(); - // $refund_total_tax = $refund->get_total_tax(); - // $new_amount = [ - // 'net' => ($order_total - $order_total_tax) + ($refund_total - $refund_total_tax), - // 'gross' => ($order_total) + ($refund_total), - // 'tax' => ($order_total_tax) + ($refund_total_tax), - // ]; - - // $this->api->updateOrder($mondu_order_id, ['amount' => $new_amount]); - // } + public function order_refunded($order_id, $refund_id) { + $order = new WC_Order($order_id); + + if ($order->get_payment_method() !== 'mondu') { + return; + } + + $refund = new WC_Order_Refund($refund_id); + $mondu_invoice_id = get_post_meta($order->get_id(), PLUGIN::INVOICE_ID_KEY, true); + + if(!$mondu_invoice_id) { + throw new ResponseException('Mondu: Can\'t create a credit note without an invoice'); + } + + $refund_total = $refund->get_total(); + $credit_note = [ + 'gross_amount_cents' => abs(round ((float) $refund_total * 100)), + 'external_reference_id' => (string) $refund->get_id() + ]; + + $this->api->create_credit_note($mondu_invoice_id, $credit_note); + } } diff --git a/src/Mondu/Mondu/MonduRequestWrapper.php b/src/Mondu/Mondu/MonduRequestWrapper.php index 1f2ee191..eb18a4d8 100644 --- a/src/Mondu/Mondu/MonduRequestWrapper.php +++ b/src/Mondu/Mondu/MonduRequestWrapper.php @@ -144,4 +144,24 @@ public function get_invoices($order_id) { $response = $this->api->get_invoices($mondu_order_id); return $response['invoices']; } + + /** + * @param $invoice_id + * @return mixed | void + * @throws MonduException + * @throws ResponseException + */ + public function get_invoice($order_id) { + $order = new WC_Order($order_id); + if ($order->get_payment_method() !== 'mondu') { + return; + } + + $mondu_order_id = get_post_meta($order_id, Plugin::ORDER_ID_KEY, true); + $mondu_invoice_id = get_post_meta($order_id, Plugin::INVOICE_ID_KEY, true); + + $response = $this->api->get_invoice($mondu_order_id, $mondu_invoice_id); + + return @$response['invoice']; + } } diff --git a/src/Mondu/Mondu/Presenters/PaymentInfo.php b/src/Mondu/Mondu/Presenters/PaymentInfo.php index 760d58d3..ec1fc18a 100644 --- a/src/Mondu/Mondu/Presenters/PaymentInfo.php +++ b/src/Mondu/Mondu/Presenters/PaymentInfo.php @@ -107,32 +107,56 @@ public function get_mondu_section_html() { return ob_get_clean(); } - public function get_mondu_invoice_html($orderId = null) { - $invoice_data = $this->get_invoices(); + public function get_mondu_invoice_html($mondu_order_id = null) { + $invoice_data = $this->get_invoice(); ob_start(); - foreach ($invoice_data as $key => $invoice_data) { + if($invoice_data) { + $currency = $invoice_data['order']['currency']; ?>

- +

- +

- Total - + +

- +

- mondu_request_wrapper->get_invoice($this->order->get_id()); + } catch (ResponseException $e) { + return false; + } + } + private function get_order() { try { return $this->mondu_request_wrapper->get_order($this->order->get_id()); diff --git a/src/Mondu/Plugin.php b/src/Mondu/Plugin.php index 1960c4bc..35c75a6d 100644 --- a/src/Mondu/Plugin.php +++ b/src/Mondu/Plugin.php @@ -68,7 +68,7 @@ public function init() { add_action('woocommerce_before_order_object_save', [new Gateway(), 'update_order_if_changed_some_fields'], 10, 2); - // add_action('woocommerce_order_refunded', [new Gateway(), 'order_refunded'], 10, 2); + add_action('woocommerce_order_refunded', [new Gateway(), 'order_refunded'], 10, 2); add_action('rest_api_init', function () { $orders = new OrdersController();