注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Split Tender
Many of Stripe's merchants rely on Stripe Terminal to accept in-person payments at the point of sale. In physical retail, it's common for a customer to cover a purchase with multiple payment methods. For example, they might cover a $$100$ purchase with a $$40$ gift card combined with a $$60$ credit card. This is called a split-tender transaction, and supporting it is a key requirement for retail merchants on Terminal.
For this multi-part question, we will be writing a system to process split-tender payments. When a transaction occurs, the system first deducts from the customer's available gift card balance, then charges the remaining balance to the customer's credit card. Given a chronological stream of payment events, your task is to calculate the correct credit card charge for each transaction. The question is broken down into four parts, each introducing a new event type: gift card expiration, regional credit card surcharges, and complex refund logic.
Function Description
Complete the functionin the editor below.
Function Parameters
: lines for each event in chronological order. Lines have a format defined by one of three schemas:
- timestamp,ADD_GIFTCARD,customer_id,card_id,amount,valid_before
复制代码- timestamp,CHARGE,charge_id,customer_id,amount,zip_code
复制代码- timestamp,REFUND,charge_id,refund_amount
复制代码- string surcharge_rates[m]
复制代码 : a list of non-overlapping zip code ranges mapped to a surcharge percentage in the format, where start and end are inclusive. Example:(representing $5%$). General Constraints and Assumptions
- Events are in chronological order by timestamp.
- All event lines are well-formed.
- values are unique acrossevents.
- events always reference a priorevent.
- Eachwill receive at most oneevent.
- values fromevents are unique.
- contains non-overlapping, valid zip code ranges.
- values are five-digit numeric strings.
Monetary Representation and Precision
- All monetary amounts in the input and output are represented as non-negative integer numbers of cents.
- For example, $$40.00$ is represented as.
- Surcharge rates are decimal fractions; for example, $0.05$ represents $5%$.
- Each surcharge calculation results in a whole number. You don't need to round.
Returns
: an array of strings, one per input event, in the same order.
- Forevents, return the formatted charge string using the schema
- CC_CHARGE_AMOUNT,GIFT_CARD_ID_1:AMOUNT_USED,GIFT_CARD_ID_2:AMOUNT_USED
复制代码 . - For all other event types, return an empty stringunless a later part specifies otherwise.
- rs, route the money back to the customer in the following priority:
- Real dollars: Refund back to the credit card first, up to the amount that was actually charged to the credit card for the referenced, including surcharge.
- Store credit second: Ifexceeds the amount refundable to the credit card, refund the remaining amount as store credit by creating a new gift card for the original customer. The newmust be formatted as. This gift card has no expiration date.
With $R$ the refund amount and $C$ the credit card amount originally charged (including surcharge): $$\text{CC_REFUND} = \min(R, C), \qquad \text{GC_REFUND} = R - \min(R, C)$$
Refund-created gift cards participate in futureevents like any other gift card. Refund-created gift cards never expire. When choosing gift cards for a future charge, non-expiring gift cards are used after all valid gift cards with finite expiration timestamps.
Output Format for REFUND: Return a flat string containing the amount refunded to the credit card and the amount refunded to the store credit gift card, separated by a single comma:Example Input:- events = [
- "1,ADD_GIFTCARD,cust1,gc1,5000,10",
- "2,CHARGE,chg1,cust1,15000,90210",
- "3,REFUND,chg1,12000"
- ]
-
- surcharge_rates = [
- "90000-99999:0.05"
- ]
复制代码 Example Output:- [
- "",
- "10500,gc1:5000",
- "10500,1500" # CC_REFUND, GC_REFUND
- ]
复制代码 Worked: $\min(12000, 10500) = 10500$ back to the credit card, and $12000 - 10500 = 1500$ as store credit on a new card.
|