Help with workflows for a wallet system

Each user can buy credits at different prices to get discounts when they buy services.

But when they buy products the price they pay should be the same as the value of the product.

(Eg. The customer buys 10 credits for $1 each. The customer buys 10 credits for 5$ each. In total customer has 20 credits but the value is $60)

So I’ve built a wallet system with the price per credit the number of credits and who bought them.

But my question is, how do I build a workflow that takes money from one of the wallets based on the price per credit and the value of the product, and then that wallet is empty it takes money from the next wallet?

It would be something like:

When buy is clicked: Make changes to the oldest wallet: Credits minus product’s cost. If the wallet is empty search for the next oldest wallet. Until the value of the product has been subtracted from wallets.

How are you first taking money from the User who is buying the ‘credits’?

With Stripe, subscriptions with X amount of credits per month or individual packages with credits

Then you will need to setup API calls to transfer money from one user to the other inside of Stripe. And ensure that all users are connected through you platform to a Stripe account.