StockSwap — Rescue the Order, Not Just the Payment

An AI-powered checkout recovery system that finds compatible product replacements, obtains explicit buyer consent, and captures the exact approved amount through PayPal.

Inspiration

Imagine ordering a product online, authorizing payment, and then discovering that the item is out of stock.

The merchant faces a difficult choice: cancel the order, ask the customer to start over, or suggest a replacement. But a replacement cannot be selected just because an AI model thinks it looks similar.

Does it actually fit? Is it available? Does it meet the buyer's requirements? Is the price acceptable? And most importantly, did the buyer explicitly authorize that exact replacement?

We built StockSwap to explore how AI could help recover these interrupted transactions without compromising payment integrity or customer control.

Our central principle is simple: AI can recommend a replacement, but only the buyer can approve it, and only a verified payment workflow can settle it.

What It Does

StockSwap demonstrates an end-to-end out-of-stock order recovery workflow using real PayPal Sandbox transactions.

  1. Authorize the original purchase: A buyer approves a checkout through PayPal, creating an authorization without immediately capturing funds.
  2. Detect an inventory problem: The original product becomes unavailable, triggering the replacement workflow.
  3. Find eligible replacements: Deterministic rules filter candidate products by compatibility, performance, stock availability, and price constraints.
  4. Generate an AI recommendation: Qwen3 evaluates only the eligible alternatives and proposes a suitable replacement with an explanation.
  5. Request explicit buyer consent: The buyer accepts a specific SKU, quantity, and amount. Their acceptance is bound to the payment authorization and the relevant inventory and proposal versions.
  6. Capture or void: A separate merchant action captures the approved amount through PayPal. If the replacement is declined, the authorization can instead be voided.
  7. Verify the outcome: StockSwap retrieves authoritative PayPal payment details and maintains a receipt trail that can be exported as JSON.

If the inventory, proposed terms, or relevant versions change, previous consent is invalidated.

The AI never receives authority to capture funds or approve a purchase on behalf of the buyer.

How We Built It

StockSwap uses a Node.js 24 backend with a JavaScript, HTML, and CSS frontend.

PayPal Payment Integration

We integrated PayPal's Sandbox APIs to implement actual payment authorization and settlement workflows:

  • Orders v2 API for creating orders and authorizing checkout.
  • Payments v2 API for capturing authorizations, voiding authorizations, and retrieving payment state.
  • Webhooks API for verifying notification signatures and reconciling matching payment sessions.

Unlike a simulation that merely changes a local payment status, StockSwap performs real API operations against PayPal Sandbox and checks their authoritative results.

AI Recommendation Engine

StockSwap integrates Qwen3-30B-A3B-Instruct-2507 through an OpenAI-compatible inference endpoint.

We deliberately separated AI reasoning from business-rule enforcement.

The application first establishes which replacement products are eligible. The model can recommend among those candidates, but its output is validated against the allowed selection before it becomes a proposal.

Compatibility, inventory, pricing, buyer consent, and settlement remain governed by deterministic application logic.

Infrastructure and Persistence

The application is deployed to Google Cloud Run over HTTPS.

Private Google Cloud Storage objects preserve payment intent, transaction receipts, and session state across deployments. Generation-checked writes help prevent conflicting state updates. SQLite supports local development.

Google Cloud Secret Manager protects credentials, while dedicated runtime identity and private storage limit access to payment-session data.

Challenges We Faced

1. Handling uncertain payment outcomes

One of the hardest challenges was ensuring that a payment operation could not accidentally be repeated simply because its network response was missing.

A payment provider may successfully process a capture even when the application never receives the response.

StockSwap records operation intent and a stable request identifier before making a payment-changing request. When the outcome is uncertain, it reconciles the operation against authoritative PayPal records rather than blindly initiating another capture.

The recovered transaction must match the expected authorization, amount, and currency.

2. Making buyer consent precise

A cheaper replacement is not automatically an authorized replacement.

We designed consent to bind a specific SKU, quantity, amount, authorization, and relevant state versions. A changed proposal invalidates the previous acceptance.

This prevents an AI recommendation or outdated buyer approval from silently changing payment terms.

3. Keeping AI useful without giving it financial authority

LLMs can explain alternatives and rank eligible products, but their responses should not directly authorize financial operations.

StockSwap treats model output as a recommendation requiring validation, not as an executable payment instruction.

4. Reconciling asynchronous notifications

Payment webhooks can arrive after local actions, and duplicate deliveries are possible.

We implemented signature verification and session matching, with automated tests covering duplicate-delivery behavior and reconciliation.

Accomplishments We're Proud Of

We validated the core workflow through actual deployed-browser and PayPal Sandbox execution.

Verified replacement transaction:

  • Original payment authorization: $12.99
  • AI-proposed compatible replacement: $11.99
  • Buyer explicitly accepted the replacement and amount.
  • The merchant captured exactly $11.99.
  • Authoritative PayPal retrieval confirmed a completed capture associated with the expected authorization.

Verified cancellation transaction:

A separate authorization was voided, and PayPal confirmed its VOIDED status with zero captures.

We also successfully processed six genuine signed PayPal notifications across the test transactions, covering approval, authorization, capture, and void events.

Saved state and receipt records survived a deployed application update and browser reload.

Automated coverage addresses consent integrity, uncertain payment responses, session isolation, CSRF, webhook signatures, storage conflicts, and sensitive-file access.

What We Learned

The biggest lesson was that reliable AI-powered commerce requires more than a capable model and a working payment API.

The difficult part lies in the boundaries between recommendation, authorization, consent, settlement, and reconciliation.

We learned how to design payment workflows around authoritative provider evidence, preserve buyer approval across asynchronous operations, and constrain AI to decisions it can safely support.

What's Next

StockSwap is currently a working hackathon prototype using fictional products and genuine PayPal Sandbox payment operations.

Potential next steps include integrating real inventory systems, introducing separately authenticated buyer and merchant accounts, supporting merchant-configurable substitution policies, and expanding recovery workflows for production commerce environments.

The longer-term opportunity is to help merchants recover otherwise lost orders while ensuring that customers remain in control of what they purchase and what they pay.

StockSwap demonstrates that AI can help save a sale without taking control of the payment.


Demo scope: All payments use PayPal Sandbox; no real money is charged. The merchant and buyer interfaces are demonstration controls within a private session, not separate authenticated accounts. Live AI inference is operator-controlled and subject to a limited allowance. This prototype does not claim measured revenue improvements or production readiness.

Development disclosure: Built using AI-assisted coding, with ChatGPT supporting research and technical discussions.

Built With

Share this project:

Updates

Submission history