expo / stripe-expo

Use the Stripe HTTP API in Expo without the DOM, node, or native deps
MIT License
159 stars 23 forks source link

`createToken()` fails on Android with error `Unexpected token o in JSON` #19

Closed altwohill closed 7 years ago

altwohill commented 7 years ago

My creditcard form works fine on iOS, however on Android it fails to fetch the token

Here's a snippet of the code

class ConfirmOrderScreen extends Component {
  constructor(props, context) {
    super(props, context);
    this.receiveToken = this.receiveToken.bind(this);
    this.processCreditCard = this.processCreditCard.bind(this);
    this.stripe = stripe(STRIPE_CLIENT_KEY);
  }

  receiveToken(token) {
    const order = Object.assign({}, this.props.order);
    order.paymentToken = token;
    this.props.confirm(order);
  }

  async processCreditCard(card) {
    if (card.valid) {
      const payment = {
        card: {
          number: card.values.number.replace(/\s/g, ''),
          exp_month: card.values.expiry.slice(0, card.values.expiry.indexOf('/')),
          exp_year: card.values.expiry.slice(card.values.expiry.indexOf('/') + 1),
          cvc: card.values.cvc,
        },
      };
      const token = await this.stripe.createToken(payment); //<--- Error on Android only: SyntaxError: Unexpected token o in JSON at position 1
      this.receiveToken(token);
    }
  }
}

It appears to stem from the _parseJSON(token) function, judging from the "Pause on caught exception" snapshot

image

I'm using the following versions

    "expo": "^20.1.3",
    "react": "^16.0.0-rc.3",
    "react-native": "0.47",
    "stripe-client": "^1.1.3",
    "urijs": "^1.18.12"
jeff-da commented 7 years ago

Hey!

I noticed that you had this question answered on the Expo slack, so I'm closing it here. Feel free to reopen the issue if you have any more problems :)

altwohill commented 7 years ago

Ah, I had to fix it by forking and updating the code. I'll submit a PR for feedback. Also I don't seem to be able to reopen the issue...