For All Intents And Purposes Part 1 - Initializing the System ποΈ
π Hi there! The description you are currently reading is just 1st 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 π³
Background
Stripe processes billions of dollars of payments to businesses every day through dozens of different payment methods like cards, bank debits, and even papaer cheques.
We don't want merchants (the businesses who use Stripe to accept peyments) to have to worry about the details for each specific payment method. For example, for many payment methods, payments are not completed instantly and can take a few days to process and be confirmed or fail, while others are processed much more quickly. Stripe abstracts this payment flow with a simple state machine object called a Payment Intent.
Today, imagine you are an engineer working at Stripe in its early days. You will implement a system that handles the creation and management of these Payment Intens, helping merchants easily setup and accept payments from customers.
Please read all instructions for each part before you begin coding for that part.
What is a Payment Intent?
A Payment Intent tracks a payment through its flow from initilization to processing to confirmation. We model this flow as a state machine: an abstract object that can exist in one of a number of states and transition between states.
A Payment Intent stores information like the merchant receiving the peyment and the monetary amount. It also should keep track of its state, which can be one of three values
π The initial state of a Payment Intent upon creation. Can transition to PROCESSING.
π The customer has attempted to pay but the attempt has not yet succeeded or failed. Can transition to either REQUIRES_ACTION or COMPLETED
π The attempt to pay succeeded and the Payment Intent amount was added to the merchant's balance.
You Task
You will implement a function that executes a chronologically ordered list of commands to create and manage Payment Intents for different merchants and then returns the account balances for each merchant after executing all commands.
Input
You will receive a chronologically ordered list of commands in the form ['INIT m1 0', 'CREATE p1 m1 100'] where each command is a single string. You will also receive the part number, corresponding to the pairs in the problem description. The commands you need to support are decribed below.
Output
A list of merchant balances in the form ['m1 100', 'm2 200'] representing the balance for each merchant after executing all commands. The list should be sorted by merchant ID in ascending alphabetical order.
Part 1: Good Intentions
To build the initial version of our system, we will support a few basic commands for initializing a merchant, creating a Payment Intent, attempting a Payment Intent, and succeeding a Payment Intent.
Commands
For this part, your system should handle the following commands -
INIT <merchant_id> <starting_balance>
CREATE <payment_init_id> <merchant_id> <amount>
ATTEMPT <payment_init_id>
SUCCEED <payment_init_id>
π³βqΛββ ππ«§πΌ ΛΒ°Credit to Rachelα₯«α‘.
1Example 1
Constraints
Limits and guarantees your solution can rely on.
π«Ά