woocommerce-plugin
woocommerce-plugin copied to clipboard
Coupon implementation on underpayment
#282 Phase 1 - as discussed here on issue #282
Add automatically generated coupon on confirmed underpaid callback. Discount amount is calculated in 68c8396
Coupon menu in merchant dashboard

Trying to add same coupon on another checkout throws usage limit error:

Now, if the order status is set to failed or cancelled, this coupon's usage/limit changes to 0/1, so it can be used one more time. If we want to prevent that, there are 2 ways:
- We can set auto-expiry to 1 hour (time after which woocommerce itself cancels the order), or
- We can write an action hook that will see if the order failed/cancelled had a coupon code starting with 'bck_' with total length = 10. - done in https://github.com/blockonomics/woocommerce-plugin/pull/291/commits/1f63c70c829088a1c6e2d01d79f109c231830af9 -- reverted in 04d696c as per discussion below.
Customer now sees the order with discount applied

Now, if the order status is set to failed or cancelled, this coupon's usage/limit changes to 0/1
This is not a big deal really and we can safely ignore. Few things:
- Does the customer ever even see the coupon code to be able to use the exploit , no I guess ! I am not seeing it anywhere in the customer UI/emails
- Logically if the order got cancelled, the customer is atleast entitled to the coupon for the BTC he spent
Update on Phase 2:
I have added a new field in blockonomics_orders table - timestamp. For a new order, timestamp records the time of order creation. Now, after confirmed underpaid callbacks:
- Coupon is applied to the order equal to amount received in the callback
- After applying coupon, new address is generated and a new row is added in the
blockonomics_orderstable. So on each underpaid callback the time is recorded on callback itself. - Now customer can refresh the same checkout page to get updated information i.e. new address and remaining amount to be paid.
- It is applicable for any number of underpaid callbacks.
Here is such an order which is underpaid thrice:

There are some issues/enhancement in this upon which I will update further:
- Order id is skipped as per the number of underpaid callbacks. For ex. in above Order id 108 is underpaid 3 times, so next order created has order id = 112
- If same Callback is hit again & again, it'll keep adding coupons.
- Pending changes in custom fields, as suggested here, i.e. remove
paid_btc,expected_btcand add new custom fieldpaid_order_amount. - Multiple random string coupons can be confusing, so adding some details for the coupons in order notes.
- Automatically emailing the updated invoice to customer on underpayment.
Finally, here is a video of consecutive underpayments
https://user-images.githubusercontent.com/97018228/198191942-09fd0bea-76cd-4758-adee-f3f7b9b59165.mp4
3 possible changes to DB discussed here: https://docs.google.com/document/d/1m2lX_ZMCupZHDvkjdauBEkb4fGhA5EWXTrvflDjuhhg/edit?usp=sharing
Update: https://github.com/blockonomics/woocommerce-plugin/pull/291/commits/09db6e7cbb3d1e57d79ef4a6b9fe05f534bfb09c - Now customer automatically receives email on confirmed underpayment, with a link to pay for remaining amount.
Email screenshot:

https://user-images.githubusercontent.com/97018228/202644241-e46f0d1c-b24b-4624-b876-3eeeba3eea1f.mp4
Not implementing . Closing in favour of https://github.com/blockonomics/woocommerce-plugin/pull/335