Help with Deducting Multiple Credits in Bubble.io During Checkout Process

Hi everyone! :wave:

I’m working on a Bubble.io app where merchants can give credits to their customers, which are then applied to reduce the total cost during checkout. However, I’m struggling to properly deduct credits from each available balance during the transaction process. Here’s how my setup currently works:

On the Checkout Page:

  1. Search for Available Credits:
    I search for the user’s credits, checking if they have any remaining and if they haven’t expired, sort by expiration date.

  2. User Selects “Use Credits”:
    The user clicks a “Use Credits” button, and the total amount of credits to be deducted is saved as a custom state called credit_amount.

  3. On Checkout:
    When the user presses the checkout button, a new data item credit_process is created, storing the remaining_cost (total cost - applied credits).

  4. Trigger Backend API Workflow:
    The workflow runs on the list of credits and passes the credit_process to the backend.


Backend Workflow Logic:

  1. make changes to the 1st credit in the list “credits:item #index
    credit balance = this credit’s credit balance - this credit_process’s remaining cost
    only when credit_process’s remaining cost > 0

  2. make change to credit_process:
    remaining cost = result of step 1 credit balance * -1
    only when step 1 credit balance < 0

  3. make changes to credit_process:
    remaining cost = 0
    only when result of step 1 credit balance ≥ 0

  4. re-run api workflow:
    only when credits:count > index and credit_process’s remaining_cost > 0"


Problem: The logic isn’t working as expected. Either the credits aren’t being deducted properly, or the workflow isn’t correctly looping through the credits until the remaining cost reaches zero.

When I remake the workflow as on the Checkout page, it does work perfectly, but it’s not dynamic like a backend workflow would be.

Does anyone have experience with managing multiple credits in Bubble workflows? Any advice on improving or fixing this logic would be greatly appreciated! :pray:

Thanks in advance!

Frontend Workflow

Backend Workflow

Hey @totalsportshub01,

You should probably filter by expiration date instead of sorting to make sure your search is accounting for non-expired credits

Why do this on a custom state and not directly at checkout?

Why not do this for step 2?


Can you provide a greater picture of this credit system you’re building?

  • Are all credits worth the same? Does a credit represent a dollar? How can credits be spent? Are they deducted by the same amount each time or does it vary?
  • Why can’t you just deduct x number of credits at checkout and if the user doesn’t have enough charge them via Stripe for the remaining balance? Or better yet, use Stripe’s crediting system to handle all of this.

Hi Johnny,

Thanks for getting back to me!

I’m building a sports registration platform where organizations can issue credits to players. These credits act as discounts for registration fees and can be given for tasks like helping with admin, refereeing, or anything else the organization decides.

Here’s how it currently works:

  • Issuing Credits: Organizations specify the amount and expiration date when they issue credits to players.
  • Applying Credits: On the registration page, all available credits for a player (from that specific organization) are summed up and deducted from the total cost of the program they’re registering for. This part is working as expected.

Where I’m stuck is on the backend workflow for deducting the credits used. Here’s a breakdown:

  1. Credits have varying values (they represent specific dollar amounts).
  2. During registration, the credits need to be applied in order of closest expiration date first.
  3. If there’s any leftover credit after deducting from the first credit, the system should move to the next credit and continue deducting until the full amount is used up.
  4. I’m not sure how to set up Stripe’s crediting system for this. Since this is my first app, I’m not entirely sure how to approach it, and I have not seen many tutorials on that.

For example:
Registration cost is $45
User has 4 available credits worth $20 each
$45 worth of credits needs to be taken

Credit 1 will have 0 credits remaining
Credit 2 will have 0 credits remaining
Credit 3 will have 15 credits remaining

Any guidance on how to implement this backend logic would be greatly appreciated!

Thanks!

you can have all of the values in input elements as part of the checkout UI…so an input will have the value of the amount of credit the user selects to apply. This doesn’t need to be saved to a custom state as it is the input value and can be passed to workflow actions.

You will have inputs for total cost, credits applied, and remaining cost…remaining cost is simply the total cost minus the credits applied (again using the inputs values for the expression)…

then when user checksout, just update their credits available data field by simple math, this users credit available balance minus input credits applied value.