Description
Solutions
For All Intents And Purposes Part 3 - Accepting Failure 😌
🀘 INTERNπŸ‘©β€πŸŽ“ NEW GRAD

🐝 Hi there! The description you are currently reading is just 3rd part of the problem set. It is highly recommended to read ALL THE PARTS before coding as parts may build on top of each other 🐳

  • For All Intents And Purposes Part 1 - Initilizing the System πŸ—οΈ
  • For All Intents And Purposes Part 2 - Change Is Good! πŸ‘
  • For All Intents And Purposes Part 3 - Accepting Failure 😌
  • For All Intents And Purposes Part 4 - Timing Matters ⏰
  • The rest of the team is thrilled with our progress building Stripe's new Payment Intent processor, but they pointed out a few cases we should handle before releasing the system to the public πŸ‘₯

    First, payments don't alway succeed and can fail for a variety of reasons like a card network declining a transaction or a bank account not having sufficient funds to be debited. Second, even after payments have succeeded, customers need to be able to request refunds from merchant.

    Let's update our implementation to handle two new commands: FAIL and REFUND πŸ₯Ή.

    Commands

    FAIL <payment_intent_id>

  • Fails a payment intent in the PROCESSING state, transitioning it from PROCESSING to REQUIRES_ACTION. This occurs when the customer's payment was declined.
  • If no Payment Intent with the given identifier exists, or if the Payment Intent is not in the PROCESSING state, this command should do nothing.
  • REFUND <payment_intent_id>

  • Processes a refund for a previously successful payment intent in the COMPLETED state. This should decrement the merchant's balance by the amount of the Payment Intent in order to return the refund to the customer
  • If no Payment Intent with the given identifier exists, or if the Payment Intent is not in the COMPLETED state, or if the Payment Intent has already been refunded, this command should do nothing.
  • ⊹ ΰ£ͺ οΉπ“ŠοΉπ“‚οΉCredit to Rachel 🐳

    Example 1:

    Input:  commands = ["INIT m1 0", "CREATE p1 m1 50",  "ATTEMPT p1",  "FAIL p1",  "ATTEMPT p1",  "SUCCEED p1",   "CREATE p2 m1 100",  "ATTEMPT p2", "SUCCEED p2", "REFUND p2"]
    Output: ["m1 50"]
    Explanation:
    The Payment Intent p1 was created with an amount of 50, then attempted, then failed which transitioned it back to the REQUIRES_ACTION, then attempted, then succeeded. At that point, m1's balance was 50. The Payment Intent p2 was created with a amount of 100, then attempted, then succeeded. At that point, m1's balance was 150. Then p2 was refunded, so m1's balance after executing all commands is 50 :)
    Constraints:
      🫢
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: