omise / omise-woocommerce

Omise WooCommerce Plugin
https://docs.opn.ooo/woocommerce-plugin
MIT License
47 stars 27 forks source link

Code refactoring, simplifying the 'process_refund' method #157

Closed guzzilar closed 4 years ago

guzzilar commented 4 years ago

1. Objective

The main concept of this refactoring is about implementing the refund feature for Alipay and Installment. However, looking at the current code and found that the process_refund method has been implemented separately in Credit Card and TrueMoney Wallet payment methods.

Therefore this pull request is submitted to reduce the duplicated code so we can easily implement the refund feature to those 2 payment methods without producing any more duplicated.

Related information: Related issue(s): T19965 (internal ticket)

2. Description of change

3. Quality assurance

🔧 Environments:

✏️ Details:

To make sure that this refactoring will not break any current feature, there are 3 main points we should cover. 1. Making sure we can make a refund using TrueMoney Wallet payment method. 1.1. After placed an order, at WooCommerce Order page, you can create a refund here.

truemoney-refund-step-01

1.2. If succeed, there will be a note that the order has been refunded.

truemoney-refund-step-02

1.3. Double check at Omise Dashboard to make sure that the refund has been done properly.

truemoney-refund-step-03

2. Making sure we can make a refund using Credit Card payment method.

card-refund-step1 card-refund-step2 card-refund-step3

3. Making sure we can make a refund using Credit Card payment method and the plugin can detect and add a proper note accordingly if that refund is being treated as voided.

card-void-step1 card-void-step2 card-void-step3

Also, testing and making sure that the plugin can handle failure cases properly.

4. Making sure that the plugin will be raising a proper failure message in case if it cannot retrieve an order object from a given order ID.

Note, this case is an extremely rare case, as the only chance that this case would happen is when the database gets corrupted.

Also note, to test this case, you will need to modify code, at file: includes/gateway/class-omise-payment.php Line: 240. Add a random string after an order id.

Screen Shot 2563-03-19 at 23 51 15

By doing that, when you try to create a refund, the plugin will raise an error that, it cannot retrieve order from the given order ID.

refund-failed-1

5. You mat try modify another part of the process_refund code to see different cases of failure message. In this case, when the Omise Charge object cannot be retrieve. At the same file as the case [4], but adding a random string at the line 250.

Screen Shot 2563-03-19 at 23 59 51

You will get an error message saying that charge cannot be retrieve.

refund-failed-2

6. Or, at the line 252 of the same file, you may add a random amount that make the refund failed.

Screen Shot 2563-03-20 at 00 01 49

By trying to create a refund with amount that is higher than its charge's amount, you will receive a message saying that refund failed.

refund-failed-3

7. Also, making sure that those orders that are placed using payment methods that don't support for the refund feature, won't be able to create a refund via Omise. Placing an order using Internet Banking payment method will not show "Refund via Omise" button at the order detail page.

refund-no-support

4. Impact of the change

Nothing

5. Priority of change

Normal

6. Additional Notes

This PR is focusing on relocating the process_refund method. There will be 3 more PRs coming.

  1. Make use of $reason variable that is in the 3rd argument of process_refund. (in WooCommerce, you can add a reason of refunding, which we can pass that reason to the metadata parameter when creating a new Refund object)

  2. Adding refund feature to Alipay and Installment payment methods.

  3. As from the internal ticket that WooCommerce cannot sync up refund status from Omise Dashboard. One more dedicated PR will be coming to handle this issue.

guzzilar commented 4 years ago

Here is a use case after this refactoring. https://github.com/omise/omise-woocommerce/pull/158/files#diff-3f1dd1d765734b8bbc1ab3abf4d36ff6R12

By adding this line, it will enable payment methods to be able to create a refund on WooCommerce user interface.

guzzilar commented 4 years ago

@jonrandy thanks for pointing out 👍 Fixed here https://github.com/omise/omise-woocommerce/pull/157/commits/287dcb6e629deda65d0b38e4c4935ede0c198369