#How to send and receive function data.
61 messages · Page 1 of 1 (latest)
Here's the code I use to run the function``` @override
FutureEither payCloud(
UserModel user,
String uid,
String cardNumber,
String expMonth,
String expYear,
String cvc,
String name,
String email,
) async {
try {
final response = await _functions.createExecution(
functionId: AppwriteConstants.stripeWebFunctionId,
data: jsonEncode({
'uid': uid,
'email': email,
'cardNumber': cardNumber,
'expMonth': expMonth,
'expYear': expYear,
'cvc': cvc,
'name': name,
}),
);
if (response.statusCode == 200) {
final responseData = jsonDecode(response.response);
final paymentId = responseData['paymentId'];
updateUserData(
user.copyWith(
payment: 'paid subscription',
paymentId: paymentId,
),
);
print('User subscribed successfully');
return right(subscriptionId);
} else {
print('Failed to subscribe user. Error: ${response.statusCode}');
return right(null);
}
} on AppwriteException catch (e, st) {
return left(
Failure(
e.message ?? 'Some unexpected error occurred',
st,
),
);
} catch (e, st) {
return left(Failure(e.toString(), st));
}
}```
And the code to send the paymentId ```final paymentId = await redirectToCheckout(
PriceId: PriceId,
cardName: cardName,
cardNumber: cardNumber,
expMonth: expMonth,
expYear: expYear,
cardCVC: cardCVC,
email: email);
// Assuming the userId is passed as the request payload
print(' created successfully. ID: $paymentId');
res.json({'paymentId': paymentId});
} catch (e) {
res.send('Error creating subscription: $e');
}
}```
Where is the error occurs
Can you attach the stacktrace?
I don't know how to grab the stacktrace but the error occurs when I try to get the data I get from the function
final paymentId = responseData['paymentId'];```
I'm supposed to be using response.body
But since I'm using the appwrite Execution class I can't
} catch (e,st) {
res.send(st);
res.send('Error creating subscription: $e');
}
Okay will run that now
What happened is that the response you're getting doesn't have paymentId as this line
res.send('Error creating subscription: $e');
Is being return
What I suggest is to always return the same object,
For example
{
success:false,
data: {},
errors: []
}
Yes
Okay cool
I keep getting Error creating subscription: type '_OneByteString' is not a subtype of type 'int' of 'index'
Where is stacktrace pointing on?
Stacktrace isn't helping. It keeps sending Error creating subscription: type '_OneByteString' is not a subtype of type 'int' of 'index' but I think its this is maybe where I'm doing things wrong final response = await http.post(url, headers: headers, body: body); if (response.statusCode == 200) { final subscriptionId = jsonDecode(response.body)['id']; return subscriptionId; } else { // Handle subscription creation error // print('Error creating subscription: ${response.body}'); return ''; }
That takes place inside the function
Try to log this
jsonDecode(response.body)
Before getting the ['id']
Okay
To see what you're getting
Ok
I think that you're trying to access undefined map index.
Ohhhh
That's what the error kept saying
Yup you're right
Maybe I can't run this function using appwrite Execution
The response has nothing to do with payment info
Only appwrite
That's why I kept getting wrong answers
What you meant
You're enable to get response from an outside URL?
I think so
What is in the response?
All my appwrite cloud variables
So what are you actually trying to do?
Endpoints, function project ids
I'm trying to get a subscriptionId
Everything works normally but I'm trying to handle payment on the cloud
I see,
As long the payment handling process is just a REST requests, it should work.
Are you getting the results you wants from here?
Nope
Those are only function variables, maybe I need to add some urls to my firewall?
So what you get back?
Are you sure you're not returning those anywhere else in your Function?
I gave up guys. I went back to the in client solution since it works and doesn't complicate things
I don't think so. I think it's a firewall thing
Not sure how a firewall would make you get back Appwrite env vars
Not sure either
How to send and receive function data.
so it sounds like you're getting an error client side, not server side, right?
can you check what response.response before decoding it?
it might be helpful if you shared your full function code and the full (redacted) logs and stderr from the execution
Nope I have a working version that doesn't use functions. When I try to run that same code server side it doesn't complete payment and it didn't give me the subscriptionId