#How to send and receive function data.

61 messages · Page 1 of 1 (latest)

celest solar
#

Not sure what I'm doing wrong but I set up a payment system that works client side and now I want to make it a cloud function. The problem is I need the function to return a paymentId. I get this error put in the paymentId variable Error creating subscription: type '_OneByteString' is not a subtype of type 'int' of 'index'

#

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');
}
}```

olive kettle
#

Where is the error occurs
Can you attach the stacktrace?

celest solar
#

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

olive kettle
#
 } catch (e,st) {
    res.send(st);
    res.send('Error creating subscription: $e');
  }
celest solar
#

Okay will run that now

olive kettle
#

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

celest solar
#

Ohhh

#

Yeah while running the stacktrace I noticed yours is different

olive kettle
#

What I suggest is to always return the same object,
For example

 {
    success:false,
    data: {},
    errors: []
 }
celest solar
#

How do I use the object in my normal code

#

Response.response?

olive kettle
#

Yes

celest solar
#

Okay cool

#

I keep getting Error creating subscription: type '_OneByteString' is not a subtype of type 'int' of 'index'

olive kettle
#

Where is stacktrace pointing on?

celest solar
#

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

olive kettle
#

Try to log this

jsonDecode(response.body)

Before getting the ['id']

celest solar
#

Okay

olive kettle
#

To see what you're getting

celest solar
#

Ok

olive kettle
#

I think that you're trying to access undefined map index.

celest solar
#

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

olive kettle
#

What you meant
You're enable to get response from an outside URL?

celest solar
#

I think so

olive kettle
#

What is in the response?

celest solar
#

All my appwrite cloud variables

olive kettle
#

So what are you actually trying to do?

celest solar
#

Endpoints, function project ids

#

I'm trying to get a subscriptionId

#

Everything works normally but I'm trying to handle payment on the cloud

olive kettle
#

I see,
As long the payment handling process is just a REST requests, it should work.

celest solar
#

Weird

#

Not sure what to do

olive kettle
celest solar
#

Nope

#

Those are only function variables, maybe I need to add some urls to my firewall?

olive kettle
#

So what you get back?

celest solar
#

I get my function variables

#

Like api key, endpoint, project id

willow shuttle
#

Are you sure you're not returning those anywhere else in your Function?

celest solar
#

I gave up guys. I went back to the in client solution since it works and doesn't complicate things

celest solar
willow shuttle
#

Not sure how a firewall would make you get back Appwrite env vars

celest solar
#

Not sure either

pastel charm
#

How to send and receive function data.

pastel charm
pastel charm
pastel charm
celest solar