Open
Description
Describe the bug
I am trying to collect credit card information from user using stripe package flutter_stripe: ^11.5.0 when I try to store credit card on stripe server it send below exception, my publish key is 100% correct.
StripeException(error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: There was an unexpected error -- try again in a few seconds, message: Invalid API Key provided: pk_test_***********************************************************************************************fyDZ, stripeErrorCode: , declineCode: null, type: invalid_request_error))
To Reproduce
Steps to reproduce the behavior:
- Add the stripe package and mention your key in main function as showing below
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Stripe.publishableKey = 'pk_test_your_key' ;
await Stripe.instance.applySettings();
runApp(const MyApp());
}
- here is my code to help in details
const AddCardScreen({super.key});
@override
State<AddCardScreen> createState() => _AddCardScreenState();
}
class _AddCardScreenState extends State<AddCardScreen> {
CardFieldInputDetails? _cardFieldInputDetails;
bool _loading = false;
void _createPaymentMethod() async {
if (_cardFieldInputDetails == null || !_cardFieldInputDetails!.complete) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Please enter complete card details")),
);
return;
}
setState(() => _loading = true);
try {
final paymentMethod = await Stripe.instance.createPaymentMethod(
params: PaymentMethodParams.card(
paymentMethodData: PaymentMethodData(billingDetails: BillingDetails(
name: 'Asif Taj',
email: '[email protected]',
)),
),
);
setState(() => _loading = false);
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text("Payment Method Created"),
content: Text("ID: ${paymentMethod.id}"),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text("OK"),
)
],
),
);
// Optional: send to your backend for saving
// await sendPaymentMethodToServer(paymentMethod.id);
} catch (e) {
print(e);
setState(() => _loading = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Error: $e")),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Stripe Save Card')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
const Text("Enter your card details:"),
const SizedBox(height: 16),
CardField(
onCardChanged: (card) {
setState(() => _cardFieldInputDetails = card);
},
decoration: const InputDecoration(border: OutlineInputBorder()),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _loading ? null : _createPaymentMethod,
child: _loading
? const CircularProgressIndicator()
: const Text("Save Card"),
),
],
),
),
);
}
}```
4. Observe a failure with exception (including the part of the stack trace, belonging to this package):
`StripeException(error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: There was an unexpected error -- try again in a few seconds, message: Invalid API Key provided: pk_test_***********************************************************************************************fyDZ, stripeErrorCode: , declineCode: null, type: invalid_request_error))`
**Expected behavior**
Expected behavior, it should have been returning me **paymentMethod.id**
**Smartphone / tablet**
- Heppening on all phone