Vladimir Djukic
Vladimir Djukic

Reputation: 2072

How to create charges with Stripe and Node.js

I have problem with Stripe I don't get anything inside callback...

Here is my server side:

Stripe.charges.create(
                        {
                            amount: totalCostUSD,
                            currency: 'usd',
                            source: req.body.stripeToken,
                            description: 'Example Charge',
                            metadata: {'order_id': '6735'}
                        }, function (err, charge) {
                    if (err && err.type === "StripeCardError") {
                        return res
                                .status(status.BAD_REQUEST)
                                .json({error: err.toString()});
                    }

                    if (err) {
                        console.log(err);
                        return res
                                .status(status.INTERNAL_SERVER_ERROR)
                                .json({error: err.toString()});
                    }

                    req.user.data.cart = [];

                    req.user.save(function () {
                        return res.json({id: charge.id});
                    });
                });

From my client side I try to send request like this:

superagent
   .post(url)
   .send({
   stripeToken: {
            number: '4242424242424242',
            exp_month: 12,
            exp_year: 2017,
            cvc: '123'
               }
                 })
   .end(function (error, res) {
         console.log(error);
        console.log(res.text);
     });

I tried to log inside my callback on server and client side but it doesn't return anything... Anyone know what is problem?

Upvotes: 0

Views: 613

Answers (2)

Vladimir Djukic
Vladimir Djukic

Reputation: 2072

I found what is the problem, my unit test is limited to execute in 2000ms and after that it fails...

But to for hiting Strip api it need more time to execute...

So to extend the period of execution I put this inside test:

it("Can chackout", function (done) {

        this.timeout(15000);
        setTimeout(done, 15000);

Upvotes: 0

korben
korben

Reputation: 1146

Are you able to make any logs at all? I would try throwing some logging in, to see exactly what could be going wrong.

console.log("Ready to create a charge!");

Stripe.charges.create({
    amount: totalCostUSD,
    currency: 'usd',
    source: req.body.stripeToken,
    description: 'Example Charge',
    metadata: {'order_id': '6735'}
  }, function (err, charge) {

    console.log("Entered ChargeCreate Callback Function.");

    if (err && err.type === "StripeCardError") {
      console.log("Encountered Stripe error.");
      return res.status(status.BAD_REQUEST).json({
        error: err.toString()
      });
    }

    if (err) {
      console.log("Encountered unknown error.");
      return res.status(status.INTERNAL_SERVER_ERROR).json({
        error: err.toString()
      });
    }

    req.user.data.cart = [];

    req.user.save(function () {
      console.log("Entered UserSave Callback Function.");
      return res.json({id: charge.id});
    });
  }
);

If you're not seeing any logs, is it possible that you're not able to view the stdOut from your Node.js server? Are you running this locally?

Upvotes: 1

Related Questions