anchor.diff:  -H "Content-Type: application/json" \
   -H 'X-Killbill-CreatedBy: demo' \
   -X POST \
  ---data-binary '{"CONFIGURE_ACTION":"ACTION_RETURN_PLUGIN_STATUS_ERROR", "METHODS":"purchase_payment"}' \
  +--data-binary '{"CONFIGURE_ACTION":"ACTION_RETURN_PLUGIN_STATUS_ERROR", "METHODS":"purchasePayment"}' \
    -v 'http://127.0.0.1:8080/plugins/killbill-payment-test/configure'
   ----
   
  -You can then refer to the  https://github.com/killbill/killbill-payment-test-plugin/blob/master/README.md[plugin documentation (section Global State Configuration)] to configure the plugin and trigger failures. 
  -
  -
  -[start = 6]
  -6. Move the clock to the next month and observe first failed payment
  +You can then refer to the  https://github.com/killbill/killbill-payment-test-plugin#global-state-configuration[Payment Test Plugin Global State Configuration] to understand this better. 
  +
  +
  +[start = 7]
  +. Move the clock to the next month (2021-09-06) and observe first failed payment
   
   
   [source,bash]
  
positive.code: 
  * Email/Notify user about the new state
  * Take action to modify the experience (based on the state name): for instance to degrade the service, modify the login flow to prompt for payment, ...
  
negative.code: -H "Content-Type: text/plain" \
  -H 'X-Killbill-CreatedBy: stephane' \
  -X POST \
  --data-binary '{"org.killbill.payment.retry.days":"1,1,8,6"}' \
  "http://127.0.0.1:8080/1.0/kb/tenants/uploadPerTenantConfig"
  ----
  
  Additionally, if you need more granularity in how you want to retry payments, you can implement your own logic in a https://docs.killbill.io/latest/payment_control_plugin.html[Payment Control plugin] (which goes beyond the scope of this tutorial).
  
  [[customers]]
  == Some Customer Flows
  
  Let us now take a look at some typical customer flows. 
  
  === Flow 1 - Good Customer, No issue with payment
  
  Let us first understand what happens when there is no issue with the payment:
  
  image:https://github.com/killbill/killbill-docs/raw/v3/userguide/assets/img/overdue/overdue-flow1.png[align=center]
  
  
  Immediately after the subscription is created, a $0 invoice is created to indicate the customer is in TRIAL. 10 days later, the customer moves out of TRIAL and the system generates a $10 invoice for the month. At this point, the system attempts to make a payment which is successful. 
  
  So, in this case, the payment retry system DOES NOT kick-in.
  
  
  === Flow 2 - Good Customer, fixes payment issue during retry flow
  
  Next, suppose the customer's credit card does not have enough funds to cover the payment initially but the customer fixes this issue later on:
  
  image:https://github.com/killbill/killbill-docs/raw/v3/userguide/assets/img/overdue/overdue-flow2.png[align=center]
  
  As before, the system attempts a payment after the trial period. Since this payment fails, the payment retry system is activated. It first retries a payment *1* day later (on `2021-08-07`) and again retries a payment another day later( on `2021-08-08`). The customer then fixes his/her credit card issue on `2021-08-15`. The payment retry system which is scheduled to run again *8* days later run on `2021-08-16`. At this point, the payment is successful, so no more payments are attempted.
  
  === Flow 3 - Good Customer, fixes payment issue during overdue flow
  
  In the next scenario, let us see what happens when the customer fixes his/her credit card issue after the overdue flow has kicked in:
  
  image:https://github.com/killbill/killbill-docs/raw/v3/userguide/assets/img/overdue/overdue-flow3.png[align=center]
  
  As before, the system attempts a payment after the trial period which fails. The payment retry system then attempts the payment after *1* day(on `2021-08-07`), again after *1* day(`2021-08-08`) and again after *8* days(on `2021-08-16`). At this point, 10 days have elapsed since the first unpaid invoice. So, the overdue system is activated which moves the account to the `WARNING` state.  After 4 days (that is on `2021-08-20`), the Overdue system runs again and moves the account to the `BLOCKED` state. The customer then fixes his/her credit card issue on 2021-08-21. The payment retry system (which has already made 3 payment attempts), is scheduled to make one last payment attempt 6 days after the previous payment attempt. The previous payment attempt was on `2021-08-16` so the payment retry system attempts a payment on `2021-08-22`. Since the payment issue is now fixed, the payment is successful. So, the account is moved back to the `CLEAR` state.
  
  === Flow 4 - Bad Customer, does not fix payment issue
  
  Let us now consider what happens when the customer is a bad customer and does not fix the payment issue:
  
  image:https://github.com/killbill/killbill-docs/raw/v3/userguide/assets/img/overdue/overdue-flow4.png[align=center]
  
  This flow is quite similar to the previous flow. The Overdue System has already moved the account to the `BLOCKED` state on `2021-08-20`. Since the customer does not fix the payment issue, the payment fails when the payment retry system attempt it the 4th time on `2021-08-22`. The Overdue system is configured to run one more time after 7 day after the previous run. Since the Overdue system was previously run on `2021-08-20`, it runs on `2021-08-27` and moves the account to the `CANCELLATION` state. 
  
  [[testing]]
  == Configuring the Payment Test Plugin
  
  In order to test the system, one must be able to make payments fail. For that purpose, we have created a https://github.com/killbill/killbill-payment-test-plugin[payment test plugin] that can be configured through an api  call to make payments fail. 
  
  The plugin can be installed using any of the methods specified in the https://docs.killbill.io/latest/plugin_development.html#_plugin_installation[plugin installation instructions].
  
  For example, to install the plugin via https://github.com/killbill/killbill-cloud/tree/master/kpm[kpm], you need to run the following command:
  
  [source,bash]
  ----
  kpm install_java_plugin 'payment-test' --destination="<path_to_install_plugin>"
  ----
  
  == Testing the System
  
  Now that we have everything ready, we can test the system. We will be simulating the following flow:
  
  image:https://github.com/killbill/killbill-docs/raw/v3/userguide/assets/img/overdue/overdue-test-flow.png[align=center]
  
  So, we will be doing the following:
  
  * Create an account
  * Add default payment matching our test payment plugin
  * Create a subscription
  * Move the clock after the trial and observe first successful payment
  * Configure payment plugin to fail payments
  * Move the clock a month later and observe first failed payment
  * Move clock +1 day and observe first payment retry
  * Move clock +1 day and observe second payment retry
  * Move clock +8 day and observe third payment retry and first overdue state `WARNING`
  * Move clock +4 day and observe second overdue state `BLOCKED`
  * Move clock +2 day (6 days from last payment attempt) and observe fourth payment retry
  * Move clock +5 day (7 days from last overdue run) and observe second overdue state `CANCELLATION` and verify subscription has been automatically cancelled by the system
  
  Notes: For simplicity, we are using dates (e.g `2021-01-10`) when manipulating the Kill Bill clock instead of fully qualified datetimes (`2021-01-10T01:43:23.000Z`). Passing such a date will end up moving the Kill Bill clock to a given point in time and that point in time may end up before the exact time of the event we are trying to trigger. In such case, retry moving the clock by one day and that should trigger it. An alternative is to specify the exact datetime when moving the clock, but that requires looking into Kill Bill internal tables to understand what is the exact trigger time. This is more rigorous but less convenient.
  
  [start = 1]
  . Set the date to `2021-07-26` (This is not mandatory, but would make the flow in sync with the diagram above):
  [source,bash]
  curl -v \
  -u admin:password \
  -H "X-Killbill-ApiKey: bob" \
  -H "X-Killbill-ApiSecret: lazar" \
  -H "Content-Type: application/json" \
  -H 'X-Killbill-CreatedBy: demo' \
  -X POST \
  "http://127.0.0.1:8080/1.0/kb/test/clock?requestedDate=2021-07-26"
  
  [start = 2]
  . Create your account:
  
  [source,bash]
  ----
  