Inspiration

In Indian mandis, small farmers sell through middlemen who set the price after delivery. Quality is argued about with no proof, and payment can take weeks. Studying agriculture at GB Pant University, I've seen how much farmers lose in that gap. We wanted the farmer to arrive with proof of quality, a fair price and a payment link, and to have a neutral referee if the buyer disputes it.

What it does

MandiPay is an AI selling agent for small farmers.

  1. Photo → grade: the farmer uploads a photo of the produce and the quantity. AI grades it A, B or C and explains why (size, colour, spots).
  2. Grade → fair price: the agent suggests a price per kg from the grade and a market-price table.
  3. Price → PayPal link: the agent creates a PayPal order and sends the buyer a payment link, with the photo and grade as proof of quality.
  4. Buyer pays: the payment is captured and the order is marked paid.
  5. Dispute → AI referee: if the buyer reports bad quality, they upload a photo. The AI compares it with the original and suggests full payment, a partial refund or a full refund. The farmer approves before any refund is sent, and the agent then issues the refund through PayPal.

How we built it

  • App: Streamlit (Python)
  • AI: a Groq-hosted Qwen vision model for photo grading and the dispute referee, with automatic failover to Google Gemini if a provider is busy. Both return structured JSON (grade, reasons, defects, refund_percent) at temperature 0.
  • Payments: PayPal Sandbox: Orders API v2 to create and capture payments (captures are safe to retry using PayPal-Request-Id), Refunds API for full or partial refunds
  • Data: SQLite for orders, grades and photos
  • Hosting: Render
  • Security: API keys only in environment variables, never in the repo

Challenges we ran into

  • Getting a full PayPal loop working end to end: create order → buyer approval → capture → partial refund, all in the Sandbox with a separate test buyer account.
  • AI provider outages: in the final days our first AI provider kept returning "high demand" errors and rate limits. We added retries, a second provider and friendly error messages instead of crashes.
  • Payment return flow: a wrong return URL sent buyers back to localhost, and a reloaded page caused an "already captured" error. We fixed the URL and made capture safe to retry.
  • Sandbox regions: payments to a test merchant in one region were declined, so we moved to a US Sandbox merchant.
  • Getting consistent grades from photos taken in different light and backgrounds.
  • Deciding how much the AI should do on its own. We let it suggest refunds but never send one without the farmer's approval.

Accomplishments that we're proud of

  • A complete flow tested live on the deployed app: AI grade → PayPal payment → AI dispute → PayPal refund, ending with the order marked refunded.
  • Human-in-the-loop by design: the agent never moves money back without the farmer saying yes.

What we learned

  • How PayPal's Orders and Refunds APIs work, and how to wrap them as simple tools an AI agent can call.
  • Build in AI provider failover from day one, and make every payment step safe to retry.
  • Grading is a business decision, not just an AI output. Clear rules and a human check are what make people trust it.

What's next for MandiPay

  • Local languages and voice input, so farmers can use it without typing.
  • Live mandi price feeds instead of a fixed price table.
  • A pilot with a farmer cooperative to test it on real sales.

Built With

Share this project:

Updates

Submission history