Reputation: 1074
I am new to React Native and Stripe API and have been trying to integrate the two for my payments module. So initially when I used the code as node.js docs described I'm getting an error because of HTTP module that is not available in React Native.
Until I encountered this blog describing a workaround using the Fetch API instead of the usual Stripe implementation. But when I run my code I am getting a 401 error which means Unauthorized or the key is invalid. Considering that my implementation and key is correct, what could be causing this error? Here's a sample code:
fetch('https://api.stripe.com/v1/customers', {
method: 'post',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer MY_TEST_KEY'
},
body: JSON.stringify({
firstName: 'John',
lastName: 'Doe'
})
})
Thanks in advance!
Upvotes: 1
Views: 685
Reputation: 25652
Unfortunately, it's not possible to do any of this in your mobile application as those calls require your Secret API key. You should never have the Secret API key in your mobile application otherwise an attacker could get his hands on it and then create charges, refunds or transfers on your behalf.
What you need to do here is create a card token first in your mobile application. You then send it to your server where you will create the charge or the customer using your Secret key.
Upvotes: 1