Skip to content

Commit

Permalink
Creating credit note on woocommerce refund. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tikohov20 authored Jul 26, 2022
1 parent c1dc25d commit eb14130
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
24 changes: 24 additions & 0 deletions src/Mondu/Mondu/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions src/Mondu/Mondu/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/Mondu/Mondu/MonduRequestWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
52 changes: 42 additions & 10 deletions src/Mondu/Mondu/Presenters/PaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
?>
<hr>
<p>
<span><strong><?php printf(__('Invoice state:', 'mondu')); ?></strong></span>
<span><strong><?php printf(__('Invoice state:', 'mondu')); ?></strong></span>
<?php printf($invoice_data['state']) ?>
</p>
<p>
<span><strong><?php printf(__('Invoice number:', 'mondu')); ?></strong></span>
<span><strong><?php printf(__('Invoice number:', 'mondu')); ?></strong></span>
<?php printf($invoice_data['invoice_number']) ?>
</p>
<p>
<span><strong>Total</strong></span>
<?php printf($invoice_data['gross_amount_cents']/100) ?>
<span><strong><?php printf(__('Total:', 'mondu')); ?></strong></span>
<?php printf('%s %s', ($invoice_data['gross_amount_cents'] / 100), $currency) ?>
</p>
<p>
<span><strong><?php printf(__('Paid out:', 'mondu')); ?></strong></span>
<span><strong><?php printf(__('Paid out:', 'mondu')); ?></strong></span>
<?php printf($invoice_data['paid_out'] ? 'Yes' : 'No') ?>
</p>
<button <?php $invoice_data['state'] === 'canceled' ? printf('disabled') : ''?> data-mondu='<?php echo(json_encode(['invoice_id' => $invoice_data['uuid'], 'order_id' => $orderId])) ?>' id="mondu-cancel-invoice-button" type="submit" class="button grant_access">
Cancel Invoice
<div>
<?php
if($invoice_data['credit_notes']) {
printf('<hr>');
}
foreach ($invoice_data['credit_notes'] as $note) {
?>
<p>
<span>
<strong><?php printf(__('Credit Note number', 'mondu')); ?></strong>
<?php printf($note['external_reference_id']) ?>
</span>
</p>
<p>
<span>
<strong><?php printf(__('Total: ', 'mondu')); ?></strong>
<?php printf('%s %s', ($note['gross_amount_cents'] / 100), $currency) ?>
</span>
</p>
<?php
}
?>
</div>
<button <?php $invoice_data['state'] === 'canceled' ? printf('disabled') : ''?> data-mondu='<?php echo(json_encode(['invoice_id' => $invoice_data['uuid'], 'order_id' => $mondu_order_id])) ?>' id="mondu-cancel-invoice-button" type="submit" class="button grant_access">
<?php printf(__('Cancel Invoice', 'mondu')); ?>
</button>
<?php
}
Expand All @@ -147,6 +171,14 @@ private function get_invoices() {
}
}

private function get_invoice() {
try {
return $this->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());
Expand Down
2 changes: 1 addition & 1 deletion src/Mondu/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit eb14130

Please sign in to comment.