Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions assets/data/payment_methods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"ARS": ["Mercado Pago", "MODO", "CVU", "Belo", "Lemon", "CBU", "Efectivo"],
"AUD": ["PayID", "Alipay", "Cash deposit", "Revolut", "Cash"],
"BOB": ["QR", "Transferencia Bancaria", "Efectivo"],
"BRL": ["PIX", "TED", "PicPay", "Depósito", "Cash"],
"CAD": ["Interac e-Transfer", "Any national bank", "Wise", "Revolut", "Cash"],
"CHF": ["TWINT", "Cash"],
"CLP": ["MACH", "Cuenta RUT", "Mercado Pago", "Transferencia bancaria", "Efectivo"],
"CRC": ["Bank transfer (IBAN)", "SINPE Móvil", "Cash"],
"COP": ["Nequi", "Daviplata", "PSE", "Llaves BRE-B", "Transferencia bancaria", "Efectivo"],
"CUP": ["Transfermovil", "EnZona", "Tarjeta Clásica", "Saldo móvil", "MiTransfer", "Efectivo"],
"EUR": ["Revolut", "HalCash", "Bank Transfer", "Wise", "SEPA instant", "Bizum", "Payoneer", "Cash"],
"GBP": ["Revolut", "Monzo", "Wise", "Any national bank", "Bank Transfer", "Cash"],
"JPY": ["PayPay", "Bank Transfer", "Cash"],
"KES": ["M-PESA", "Equity Bank", "NCBA Bank", "Airtel Money", "Co-operative Bank", "Standard Chartered", "Ecobank", "GTBank", "Pochi la Biashara", "Cash"],
"MLC": ["Transfermovil", "EnZona"],
"MWK": ["Airtel Money", "TNM Mpamba", "Bank Transfer", "Cash"],
"MXN": ["SPEI", "CoDi", "Retiro Cajero BBVA", "Efectivo"],
"MZN": ["M-Pesa", "e-Mola", "Millennium bim", "BCI", "Cash"],
"NGN": ["OPay", "PalmPay", "Moniepoint", "GTBank", "Access Bank", "UBA", "Zenith Bank", "Bank Transfer", "Cash"],
"PEN": ["Yape", "Plin", "QR Yape", "Transferencia bancaria", "Cash"],
"PHP": ["Any national bank", "GCash", "Cash"],
"PYG": ["SIPAP", "Transferencia bancaria", "Efectivo"],
"TZS": ["M-Pesa", "Mixx by Yas (Tigo Pesa)", "Airtel Money", "Halopesa", "CRDB Bank", "Cash"],
"UGX": ["MTN MoMo", "Airtel Money", "Chipper Cash", "Cash"],
"USD": ["Cash App", "Venmo", "Zelle", "PayPal", "Wise", "Payoneer", "Strike", "Revolut", "N1CO", "Transfer365", "Cash"],
"VES": ["Pago Móvil", "Binance P2P", "Transferencia bancaria", "Efectivo"],
"ZAR": ["Capitec", "FNB", "Absa", "Nedbank", "Standard Bank", "FNB eWallet", "MTN MoMo", "Cash"],
"ZMW": ["Airtel Money", "MTN MoMo", "Zamtel Kwacha", "FNB eWallet", "Stanbic Bank", "SPENN", "Cash"],
"default": ["Bank Transfer", "Cash in person"]
Comment thread
Catrya marked this conversation as resolved.
}
36 changes: 36 additions & 0 deletions lib/features/order/providers/payment_methods_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// The default payment methods used when a currency has no specific list.
const _fallbackMethods = <String>['Bank Transfer', 'Cash in person'];

/// Loads the currency → payment-methods map from the bundled asset once.
final paymentMethodsDataProvider =
FutureProvider<Map<String, List<String>>>((ref) async {
final raw =
await rootBundle.loadString('assets/data/payment_methods.json');
final decoded = jsonDecode(raw) as Map<String, dynamic>;
return decoded.map(
(code, methods) => MapEntry(
code,
(methods as List<dynamic>).cast<String>(),
),
);
});

/// The suggested payment methods for [currencyCode], falling back to the
/// `default` list (and then a hardcoded fallback) for unknown currencies.
///
/// Returns an empty list while the asset is still loading so the section can
/// render its custom field without flashing placeholder chips.
final paymentMethodsForCurrencyProvider =
Provider.family<List<String>, String>((ref, currencyCode) {
final data = ref.watch(paymentMethodsDataProvider);
return data.maybeWhen(
data: (map) =>
map[currencyCode] ?? map['default'] ?? _fallbackMethods,
orElse: () => const <String>[],
);
});
38 changes: 21 additions & 17 deletions lib/features/order/widgets/payment_method_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:mostro/core/app_theme.dart';
import 'package:mostro/features/order/providers/payment_methods_provider.dart';
import 'package:mostro/features/order/widgets/currency_section.dart';
import 'package:mostro/l10n/app_localizations.dart';

/// Common payment methods available for selection.
const _commonMethods = [
'Mercado Pago',
'Bank Transfer',
'Pix',
'Zelle',
'Wise',
'SEPA',
'Revolut',
'Cash',
'PayPal',
'Nequi',
];

/// Selected payment methods for the create-order form.
final selectedPaymentMethodsProvider =
StateProvider<List<String>>((_) => []);
Expand Down Expand Up @@ -57,6 +45,17 @@ class _PaymentMethodSectionState extends ConsumerState<PaymentMethodSection> {
final colors = theme.extension<AppColors>();
final green = colors?.mostroGreen ?? const Color(0xFF8CC63F);
final inputBg = colors?.backgroundInput ?? const Color(0xFF252A3A);
// When the currency changes, drop any selected methods that are not valid
// for the new currency (the custom free-text entry is left untouched).
ref.listen<String>(selectedFiatCodeProvider, (_, next) {
final valid = ref.read(paymentMethodsForCurrencyProvider(next)).toSet();
final current = ref.read(selectedPaymentMethodsProvider);
final pruned = current.where(valid.contains).toList();
if (pruned.length != current.length) {
ref.read(selectedPaymentMethodsProvider.notifier).state = pruned;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});

final selected = ref.watch(selectedPaymentMethodsProvider);
final custom = ref.watch(customPaymentMethodProvider);
final l10n = AppLocalizations.of(context);
Expand Down Expand Up @@ -146,12 +145,15 @@ class _PaymentMethodSectionState extends ConsumerState<PaymentMethodSection> {
void _showMethodPicker(BuildContext context) {
final selected = ref.read(selectedPaymentMethodsProvider);

final fiatCode = ref.read(selectedFiatCodeProvider);
final methods = ref.read(paymentMethodsForCurrencyProvider(fiatCode));
showDialog<void>(
context: context,
builder: (dialogContext) => _MethodPickerDialog(
selected: selected,
onDone: (methods) {
ref.read(selectedPaymentMethodsProvider.notifier).state = methods;
methods: methods,
onDone: (chosen) {
ref.read(selectedPaymentMethodsProvider.notifier).state = chosen;
Navigator.pop(dialogContext);
},
),
Expand All @@ -162,10 +164,12 @@ class _PaymentMethodSectionState extends ConsumerState<PaymentMethodSection> {
class _MethodPickerDialog extends StatefulWidget {
const _MethodPickerDialog({
required this.selected,
required this.methods,
required this.onDone,
});

final List<String> selected;
final List<String> methods;
final ValueChanged<List<String>> onDone;

@override
Expand Down Expand Up @@ -202,7 +206,7 @@ class _MethodPickerDialogState extends State<_MethodPickerDialog> {
Wrap(
spacing: AppSpacing.sm,
runSpacing: AppSpacing.xs,
children: _commonMethods.map((method) {
children: widget.methods.map((method) {
final isSelected = _selected.contains(method);
return FilterChip(
label: Text(method, style: const TextStyle(fontSize: 12)),
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ flutter:

assets:
- assets/data/fiat.json
- assets/data/payment_methods.json
- assets/images/
42 changes: 42 additions & 0 deletions test/features/order/providers/payment_methods_provider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';

Map<String, List<String>> _loadShipped() {
final raw = File('assets/data/payment_methods.json').readAsStringSync();
final decoded = jsonDecode(raw) as Map<String, dynamic>;
return decoded.map(
(k, v) => MapEntry(k, (v as List<dynamic>).cast<String>()),
);
}

List<String> _forCurrency(Map<String, List<String>> data, String code) =>
data[code] ?? data['default'] ?? const ['Bank Transfer', 'Cash in person'];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

void main() {
late Map<String, List<String>> data;
setUpAll(() => data = _loadShipped());

group('payment_methods.json currency contract', () {
test('ships a default fallback plus many currencies', () {
expect(data.containsKey('default'), isTrue);
expect(data.length, greaterThan(20));
});
test('known currency resolves to its list', () {
final ars = _forCurrency(data, 'ARS');
expect(ars, contains('Mercado Pago'));
expect(ars, contains('CVU'));
expect(ars, isNot(contains('Zelle')));
});
test('African currencies present', () {
expect(_forCurrency(data, 'MWK'), contains('Airtel Money'));
expect(_forCurrency(data, 'KES'), contains('M-PESA'));
});
test('unknown currency falls back to default', () {
final unknown = _forCurrency(data, 'XXX');
expect(unknown, contains('Bank Transfer'));
expect(unknown, contains('Cash in person'));
});
});
}
Loading