Build a production-grade backend service that intelligently selects the optimal payment source (or multi-source payment stack) for every transaction a user makes with their mCard. The system evaluates all of a user's connected financial accounts in real time, scores each eligible funding source against a weighted model, and returns a recommended payment stack that maximizes the user's financial benefit while respecting their chosen optimization strategy. An AI adaptive mode dynamically selects the best strategy per transaction based on real-time context.
WalletOptimizationEngineThe engine is invoked at the point of transaction authorization (or pre-authorization). It receives transaction context and user profile, evaluates all eligible funding sources, and returns an optimized payment stack.
Each eligible funding source receives a composite score using this weighted formula:
score =
rewardValue × 0.30 +
discountValue × 0.25 +
expiryUrgency × 0.15 +
preferenceFit × 0.15 −
feeCost × 0.10 −
creditRisk × 0.05
| Dimension | Weight | Description |
|---|---|---|
| rewardValue | +0.30 | Cash-equivalent value of rewards, points, or cashback earned by using this source for this transaction. Factor in category multipliers (e.g., 3x on travel), point valuations, and caps. |
| discountValue | +0.25 | Immediate monetary discount applied by using this source (promo credits, loyalty balances, coupon redemptions, merchant-specific offers). |
| expiryUrgency | +0.15 | Urgency of using expiring value. If a loyalty credit or promo balance expires soon, this source gets a higher score to prevent value loss. Scale: 0 = no expiry, 1.0 = expires today. |
| preferenceFit | +0.15 | How well this source aligns with the user's selected optimization strategy and risk profile. A business card scores high when optimization_goal = business_expense; a no-fee account scores high when optimization_goal = min_fees. |
| feeCost | −0.10 | Total fees incurred by using this source: FX fees, cash advance fees, wire fees, convenience surcharges, network fees. 0 = no fees, 1.0 = maximum fee scenario. |
| creditRisk | −0.05 | Negative impact on the user's credit health or debt exposure. Using a credit card near its utilization limit scores high; using checking scores 0. Factor in current utilization, days until statement closes, and whether the user's risk profile is debt-averse. |
Each source's final score is a value between 0 and 1 (clamped). The highest-scoring source(s) are selected for the payment stack. When scores are close (within 0.05), the engine may split the transaction to capture benefits from multiple sources.
Each strategy modifies the scoring weights, eligibility rules, and post-processing logic. Users select one as their default, or enable AI Adaptive Mode.
prefer_highest_reward_rate: true, use_expiring_rewards_first: true, credit_utilization_limit: 0.45flag_business_mcc: true, prefer_business_card: true, receipt_tracking: true, credit_utilization_limit: 0.40apply_promo_credits: true, apply_loyalty_credits: true, apply_gift_balances: true, apply_coupon_discounts: true, bank_credit_as_last_resort: truecredit_utilization_limit: 0.30, ask_before_credit_above: 300, cash_preservation: trueprefer_credit_over_debit: true, defer_to_statement_date: true, credit_utilization_limit: 0.35avoid_cash_advance: true, avoid_fx_fees: true, avoid_convenience_fees: true, prefer_no_fee_accounts: truecharge_credit_then_instant_payoff: true, auto_pay_credit_from_checking: true, ensure_zero_statement_balance: true, maximize_credit_utilization_cycling: true, target_utilization: 0.08, rewards_on_credit_spend: true, credit_bureau_reporting: monthly_positiveWhen AI Adaptive Mode is enabled, the engine does NOT use a single fixed strategy. Instead, for each transaction it:
The AI considers these factors when selecting a strategy per transaction:
For each transaction, the AI runs all 7 strategies through the scoring engine and compares the resulting benefit projections. It selects the strategy that produces the highest net benefit (rewards + discounts + credit-building value − fees − risk-adjusted debt cost). The AI outputs:
selected_strategy: the strategy ID and namereason: a plain-language explanation (e.g., "Expiring loyalty credits detected — highest value recovery")confidence: 0–1 score indicating how clearly this strategy wonalternatives_considered: ranked list of other strategies with their scoresThe AI improves over time by tracking:
The model adjusts its scoring weights and strategy selection heuristics based on outcomes, becoming more personalized with each transaction.
A single transaction may be split across multiple funding sources. The engine constructs an ordered payment stack:
Input: transaction = { merchant, amount, category, mcc, location }
userProfile = { optimization_goal, risk_profile, credit_limits, ... }
connectedAccounts = [ { source, type, balance, reward_rate, fees, expiry, ... } ]
Output: paymentStack = [
{ source: "Sponsor Free Money", amount: 10.00, reason: "Eligible grocery promo — applied first" },
{ source: "Loyalty Credit", amount: 18.00, reason: "Expiring in 5 days — used before lost" },
{ source: "Grocery Rewards Card", amount: 58.42, reason: "5% cashback on remaining balance" },
]
POST /wallet/optimizeRequest:
{
"user_id": "string",
"transaction": {
"merchant": "string",
"amount": "number",
"category": "string",
"mcc": "string",
"location": { "country": "string", "currency": "string" }
},
"ai_mode": "boolean"
}
Response:
{
"selected_strategy": {
"id": "string",
"name": "string",
"reason": "string"
},
"payment_stack": [
{
"source": "string",
"amount": "number",
"reason": "string"
}
],
"projected_benefits": {
"reward_earned": "string",
"fees_avoided": "string",
"credit_impact": "string",
"cash_preserved": "string"
},
"ai_analysis": {
"confidence": "number",
"alternatives_considered": [
{ "strategy_id": "string", "score": "number", "summary": "string" }
]
},
"eligible_accounts_scored": [
{
"source": "string",
"scores": {
"reward": "number",
"discount": "number",
"expiry": "number",
"fit": "number",
"fee": "number",
"risk": "number"
},
"composite_score": "number",
"selected": "boolean"
}
]
}
{
"user_id": "string",
"optimization_goal": "max_rewards | business_expense | free_money_first | balanced | preserve_cash | min_fees | build_credit | ai_adaptive",
"risk_profile": "rewards_priority | tax_optimized | value_preservation | debt_averse | cash_conservative | fee_sensitive | credit_builder",
"cash_preservation": "boolean",
"credit_utilization_limit": "number (0-1)",
"ask_before_credit_above": "number (dollar threshold)",
"prefer_highest_reward_rate": "boolean",
"use_expiring_rewards_first": "boolean",
"apply_promo_credits": "boolean",
"apply_loyalty_credits": "boolean",
"apply_gift_balances": "boolean",
"apply_coupon_discounts": "boolean",
"bank_credit_as_last_resort": "boolean",
"avoid_cash_advance": "boolean",
"avoid_fx_fees": "boolean",
"prefer_credit_over_debit": "boolean",
"defer_to_statement_date": "boolean",
"charge_credit_then_instant_payoff": "boolean",
"auto_pay_credit_from_checking": "boolean",
"ensure_zero_statement_balance": "boolean",
"maximize_credit_utilization_cycling": "boolean",
"target_utilization": "number (0-1)",
"ai_learning_enabled": "boolean"
}
{
"account_id": "string",
"source_name": "string",
"type": "credit_card | debit_card | checking | savings | crypto_wallet | bnpl | loyalty | promo_credit | gift_balance",
"balance": "number",
"reward_rate": "number",
"reward_categories": [{ "category": "string", "multiplier": "number" }],
"fees": {
"fx_fee": "number (0-1)",
"cash_advance_fee": "number (0-1)",
"wire_fee": "number",
"convenience_fee": "number (0-1)"
},
"credit_limit": "number (nullable)",
"current_balance": "number (nullable, for credit cards)",
"statement_close_date": "date (nullable)",
"payment_due_date": "date (nullable)",
"expiry_date": "date (nullable, for promo/loyalty balances)",
"reports_to_bureaus": "boolean",
"is_frozen": "boolean",
"supported_mccs": ["string"]
}
avoid_* preferences are set.