Skip to content

Stripe Integration & Multi-Currency Deposit Refactor

๐Ÿ Overview

We integrated Stripe as a payment provider for deposits, refactored our multi-currency deposit flow, and improved our exchange rate caching logic. This ensures robust, auditable, and correct handling of all deposit scenarios, including cross-currency and zero-decimal currencies.

๐Ÿ› ๏ธ What We Did

๐Ÿฆ Stripe Payment Provider Integration

  • ๐Ÿš€ Implemented a PaymentProvider interface and a concrete Stripe implementation using the official stripe-go SDK.
  • ๐Ÿ”„ Migrated to the new stripe.Client pattern for future-proofing and better testability.
  • ๐Ÿงฉ Injected the payment provider into the handler chain for clean separation of concerns.

๐Ÿ”— Deposit Flow Refactor

  • ๐Ÿ—๏ธ The service layer now only emits a deposit event; all business logic is handled in the handler chain.
  • โž• Added a PaymentProviderHandler to the chain, which:
  • ๐Ÿ’ณ Initiates payments with Stripe.
  • ๐Ÿ†” Handles payment IDs and errors.
  • ๐Ÿ’ฑ Ensured currency conversion is always performed before crediting the account, using up-to-date exchange rates.

๐Ÿ’ฑ Multi-Currency & Zero-Decimal Currency Handling

  • ๐Ÿž Fixed a critical bug: previously, JPY deposits were multiplied by 100, resulting in 100x overcharging on Stripe.
  • ๐Ÿงฎ Now, the amount sent to Stripe is calculated using currency metadata (e.g., decimals for USD vs. JPY).
  • ๐Ÿ“Š All deposit and conversion logic is fully auditable and testable.

๐Ÿ—ƒ๏ธ Exchange Rate Cache Logic

  • ๐Ÿ”„ Refactored cache lookup to always check both direct and reverse currency pairs, with consistent TTL/freshness logic.
  • ๐Ÿงน Removed backend-specific logic for more predictable and maintainable caching.

๐Ÿž Problems We Faced & Solutions

๐Ÿ’ฑ Currency Conversion Mismatches

  • โŒ Problem: Deposits in a currency different from the accountโ€™s currency were not being converted, leading to accounting errors.
  • โœ… Solution: Enforced conversion in the handler chain, always crediting the account in its own currency.

๐Ÿ’ธ Stripe Amount Calculation for Zero-Decimal Currencies

  • โŒ Problem: JPY and other zero-decimal currencies were incorrectly multiplied by 100, causing 100x overcharging.
  • โœ… Solution: Used currency metadata to determine the correct multiplier for Stripeโ€™s smallest unit.

๐Ÿ—ƒ๏ธ Inconsistent Exchange Rate Caching

  • โŒ Problem: Cache logic was inconsistent, sometimes missing valid rates or using stale data.
  • โœ… Solution: Unified cache lookup logic for both direct and reverse pairs, backend-agnostic.

๐Ÿงฉ Clean Architecture & Testability

  • โŒ Problem: Payment provider logic was mixed into the service layer, making it hard to test and extend.
  • โœ… Solution: Moved all provider logic into a dedicated handler in the chain, improving modularity and testability.

๐Ÿ”ฎ Next Steps

  • ๐Ÿ”” Integrate and test Stripe sandbox webhooks for real-time payment status updates.
  • ๐Ÿ“š Document webhook event handling and reconciliation logic.
  • ๐Ÿงช Continue to add tests and monitoring for all payment and currency flows.

๐Ÿ“š References

  • stripe-go SDK
  • Stripe API Docs
  • Project code: infra/provider/stripe_payment_provider.go, pkg/handler/, pkg/service/account/, infra/provider/exchange_rates.go