Failed To Parse Ephemeral Key.. CustomerSession.initCustomerSession Stripe_sdk Package
I'm a bit confused abut how to initialise the package properly given the Stripe terminology.
App => my platform , which has an AccountId and a publishable key. Account => any seller connected to my platform. Customer => any buyer paying to my sellers.
As I first understood, I should initialise Stripe with my platform publishable key and my platform AccountId
as I saw the stripeAccount
optional parameter, but the I saw the doc hint acc_
so it was clear to initialise Stripe
with a CustomerId
contrarily to the AccountId
.
But then I initialise the CustomerSession with api version ( I upgraded in console to the latest ) and customerId (same as above) but it trows an Unhandled Exception: Failed to parse Ephemeral Key, Please return the response as it is as you received from stripe server
which is not picked by the catch statement.
Can you please help me clarify this? I 've looked around but haven't seen any explanation. Many thanks.
Here's my inits, what am I doing wrong?:
try {
Stripe.init(Environment.stripePublishableKey,
stripeAccount: state.user.stripeId);
CustomerSession.initCustomerSession((apiVersion) async {
print('apiVersion is : $apiVersion');
return apiVersion;
}, apiVersion: '2020-08-27', stripeAccount: state.user.stripeId);
} catch (error) {
print('Stripe error: $error');
}
these are the prints from console:
I/flutter (26067): apiVersion is : 2020-03-02
E/flutter (26067): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Failed to parse Ephemeral Key, Please return the response as it is as you received from stripe server
E/flutter (26067): #0 EphemeralKeyManager.retrieveEphemeralKey (package:stripe_sdk/src/ephemeral_key_manager.dart:101:9)
E/flutter (26067): <asynchronous suspension>
E/flutter (26067):
[log] FormatException: Unexpected character (at character 5)
2020-03-02
^
``
Answer
Finally after a better look at the method description
void initCustomerSession(
Future<String> Function(String) provider,
{ String apiVersion = defaultApiVersion,
String stripeAccount,
bool prefetchKey = true,
})
I saw that I needed to create it using the Stipe api from my Node server:
stripe.ephemeralKeys.create({customer: customer}, {apiVersion: apiVersion})
.then((result) => {
console.log('Mongoose findUser customer && stripe_version ephemeralKey is: ', result);
if(result != null) {
res.status(200).send({message: 'Ephemeral key created successfully.', data : result});
} else {
res.status(400).send({message: 'Ephemeral key not created'});
}
}).catch((err) => {
console.log('Mongoose findUser customer && stripe_version ephemeralKey error: ', err);
res.status(503).send({message: 'Internal error, Ephemeral key not created'});
});
retrieve it with a method in my repository that hits the endpoint:
Future<String> getEphemeralKey({String apiVersion, String stripeId}) async {
String ephemeralKey;
await _firebaseAuth.currentUser.getIdToken().then((idToken) {
headers = {
'Content-Type': 'application/json',
'api_key': Environment.dbApiKey,
'AuthToken': idToken
};
});
var params = {'customer': stripeId, 'apiVersion': apiVersion};
final Uri uri = Uri.http(Environment.dbUrl, 'api/users', params);
await get(uri, headers: headers).then((resp) {
if (resp.statusCode == 200) {
ephemeralKey = jsonEncode(jsonDecode(resp.body)['data']);
}
}).catchError((e) {
print('PaymentMongoRepository getEphemeralKey error: $e');
});
return ephemeralKey;
}
and finally I Initialise customerSession:
Future<String> createCustomerEphemeralKey(
{String apiVersion, String customerId}) async {
String ephemeralKey;
await _mongoRepository
.getEphemeralKey(
apiVersion: apiVersion, stripeId: state.user.stripeId)
.then((key) {
ephemeralKey = key;
}).catchError((e) {
print(
'∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞ AccountConnectionManager getEphemeralKey key error: $e');
});
return ephemeralKey;
}
CustomerSession.initCustomerSession(
(apiVersion) => createCustomerEphemeralKey(
apiVersion: '2020-08-27',
customerId: state.user.stripeId),
stripeAccount: state.user.stripeId);
The method is expecting an Ephemeral key in a json formatted string (which is not specified anywhere except in the error:
Unhandled Exception: Failed to parse Ephemeral Key, `Please return the response as it is as you received from stripe server`
I discovered it looking at the actual package code for ÈphimeralKey .. Hope this helps others.. Cheers
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?