Inquisitive's avatar

Seeking help to implement Stripe Logic on my use case

I do have a platform where there are parents and their child companies. This platform charges a recurring monthly fee to each of those companies. All these companies used to pay the fee themselves, however, there are certain companies let's say company A wants to pay the monthly fee of their child companies (AA, AB, AC, AD, AE). And, the subscription is the tiered one, so for each company let's say they will be charged 100$ for 6 users and thereafter 25$ per user.

So, I am confused about how I can create a subscription to pay monthly fees to the platform on behalf of child companies. These are some of the options I am considering and the possible problems they might give

Option 1:

  • charge the total fee and from metadata update the subscription status on the database

Problem:

  • it might be not easy because of the tiered pricing, company AA might have 10 users, company AB might have 12, AC might have 6 only, and so on. Because of this, it might be not easy.

Option 2:

Previously I was using stripe session checkout and was thinking of using different line items for different company

Problem:

  • it seems it does not allow to addition same price item in two different lines

Option 3:

  • on for each loop create a subscription for each of those child companies using the parent company payment method

Problem:

  • I might need to create each customer and add payment to each of them. I believe there could be some risk in attaching the payment method of the parent to the child company.
  • additionally, they might have 3DS authorization which might prevent the payment to child companies.

What is the best way to deal with this? Any suggestions would be appreciated.

0 likes
2 replies
LaryAI's avatar
Level 58

Based on the use case you've described, it seems like you need a way to handle complex billing scenarios where a parent company pays for its child companies, and the pricing is tiered based on the number of users. Here's a potential solution using Stripe's API:

Option 4: Use Stripe's Subscription and Invoice APIs with a Custom Logic

You can create a single customer object for the parent company in Stripe and then create subscriptions for each child company with the appropriate tiered pricing. You can use metadata to link each subscription to its respective child company within your platform. Here's a step-by-step approach:

  1. Create a Stripe Customer for the parent company.
  2. For each child company:
    • Calculate the total amount to be charged based on the number of users and the tiered pricing.
    • Create a subscription item with the calculated amount.
    • Use metadata to associate the subscription item with the child company.
  3. Create a single invoice for the parent company customer at the end of the billing cycle with all the subscription items.
  4. Optionally, use webhooks to listen for successful payments or failures and update the subscription status in your database accordingly.

Here's a simplified code example in Python to illustrate the concept:

import stripe

stripe.api_key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"

# Create a Stripe Customer for the parent company
parent_customer = stripe.Customer.create(
    description="Parent Company",
    email="[email protected]"
)

# Define your tiered pricing logic
def calculate_price(num_users):
    base_price = 100
    additional_user_price = 25
    free_users = 6
    if num_users <= free_users:
        return base_price
    else:
        return base_price + (num_users - free_users) * additional_user_price

# Create subscriptions for each child company
child_companies = [
    {"id": "AA", "num_users": 10},
    {"id": "AB", "num_users": 12},
    {"id": "AC", "num_users": 6},
    # Add more child companies as needed
]

for company in child_companies:
    price = calculate_price(company["num_users"])
    subscription = stripe.Subscription.create(
        customer=parent_customer.id,
        items=[{
            'price_data': {
                'currency': 'usd',
                'product': 'prod_XXXXXXXXXXXXXX',  # Replace with your product ID
                'recurring': {'interval': 'month'},
                'unit_amount': price,
            },
        }],
        metadata={'child_company_id': company["id"]}
    )

# Optionally, set up webhook endpoints to handle events such as 'invoice.payment_succeeded'

Remember to replace 'prod_XXXXXXXXXXXXXX' with your actual product ID and 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' with your actual Stripe secret key.

This approach allows you to maintain a single customer for the parent company while tracking each child company's subscription separately. It also avoids the need to attach the parent's payment method to each child company, which could be a security concern.

Please note that this is a simplified example. You'll need to handle potential edge cases, such as subscription cancellations, payment failures, and updates to the number of users for each child company. Additionally, you should implement proper error handling and ensure that your implementation complies with Stripe's best practices and your local regulations regarding payment processing.

martinbean's avatar

@inquisitive It sounds like you need to create some sort of “billing account” model. Each company then has a foreign key pointing to the billing account responsible for that company’s subscription. This way, a parent company and its child company could all share the same billing account; a parent company and its child companies could all have different billing accounts; or a mixture of the two approaches.

Please or to participate in this conversation.