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

[Feature] Refund #16

Merged
merged 2 commits into from
Jun 18, 2024
Merged

[Feature] Refund #16

merged 2 commits into from
Jun 18, 2024

Conversation

Dinwy
Copy link
Collaborator

@Dinwy Dinwy commented Jun 7, 2024

Description

This update enhances the Komoju-Magento plugin with the following key changes and improvements:

  • Addition of Customer Email and Name:
    • Added customer email and name to the hosted page URL parameters to provide better customer context.

image

  • Improved Refund Handling:

    • Implemented refund functionality to handle payment.refunded and payment.refund.created events more effectively.
    • Moved setTotalRefunded method outside of the for-loop block in payment.refund.created event processing to prevent duplication.
  • Code Cleanup and Refactoring:

    • Updated various namespaces and class references for consistency and readability.
    • Replaced deprecated methods

Detailed Changes

WebhookEventProcessor.php:

  • Set $baseTotalPaid = $this->order->getBaseTotalPaid();
    • Reason for Change:
      Without setting baseTotalPaid, the refund logic was throwing errors. This is likely because the validation method validate checks if the base order refund exceeds the base total paid. If baseTotalPaid is not correctly set, this check can fail and cause errors when writing credit memo.

https://github.com/pepe1518/magento2/blob/master/vendor/magento/module-sales/Model/Service/CreditmemoService.php#L213

public function validate($entity)
{
    $messages = [];
    $baseOrderRefund = $this->priceCurrency->round(
        $entity->getOrder()->getBaseTotalRefunded() + $entity->getBaseGrandTotal()
    );
    if ($baseOrderRefund > $this->priceCurrency->round($entity->getOrder()->getBaseTotalPaid())) {
        $baseAvailableRefund = $entity->getOrder()->getBaseTotalPaid()
            - $entity->getOrder()->getBaseTotalRefunded();

        $messages[] = __(
            'The most money available to refund is %1.',
            $baseAvailableRefund
        );
    }

    return $messages;
}
  • Added logic to set the order state to complete if the total amount refunded matches the order's base grand total, which means when it is fully refunded. It is still set as processing based on the adobe commerce's spec.
if ($this->order->getBaseGrandTotal() == $totalAmountRefunded) {
    $this->order->setState(Order::STATE_COMPLETE);
    $this->order->setStatus(Order::STATE_COMPLETE);
}

Quote from: https://experienceleague.adobe.com/en/docs/commerce-admin/stores-sales/order-management/orders/order-status

A partially refunded order remains in Processing status until all ordered items (including refunded items) are shipped. The order status does not change to Complete until every item in the order has been shipped.

  • Set the shipping amount to 0 for the credit memo:
    • This ensures we only write the subtotal since the specific refunded items are unknown, and it avoids potential inaccuracies with shipping refunds.

image

Attachments

How it looks like after processing 3 partial refund

image

image

Please review these changes and merge the PR if everything looks good. Thank you!

@degikko degikko requested a review from wyugue June 10, 2024 09:36
@degikko
Copy link

degikko commented Jun 10, 2024

@wyugue can you help us review this PR, please?

@rramsden
Copy link
Member

@Dinwy Were you able to provide setup instructions to @wyugue

@Dinwy
Copy link
Collaborator Author

Dinwy commented Jun 11, 2024

@wyugue I wrote how to test in the previous PR, #15

However, I don't think it will work on or tested on Linux environment. If you see any error while setting up Magneto then go to Mark Shust's repo and follow the instruction written in there. After that, copy and paste src/app/code/Komoju to the $MAGENTO_INSTALL/app/code in the markshust/magento-php:8.3-fpm-2 image

Copy link

@wyugue wyugue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I have no means of testing this atm

@Dinwy Dinwy merged commit ec907e6 into degica:master Jun 18, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants