0
votes

I am looking to create stripe token in parse cloud code..

I dont want to create token in client side HTML page. My complete web application is in HTML + Javascript so dont want to expose my Stripe.setPublishableKey('pk_test_xxxxxxx');

Because of this reason interest to define function in cloud code.

Parse.Cloud.define("addCreditCard", function(request, response) {
    var token;
    var group;

    var Stripe = require('https://js.stripe.com/v2/');
    Stripe.setPublishableKey('pk_test_xxxxxxxxx');

    Stripe.card.createToken({
        number : request.params.number,
        cvc : request.params.cvc,
        exp_month : request.params.month,
        exp_year : request.params.year
    }, {
        sucsess: function(result) { response.success("Ok"); },
        error : function(error) { response.error(error); }
    });
});

Here parse cloud unable to call var Stripe = require('https://js.stripe.com/v2/');

If so many place suggested use parse cloud stripe module var Stripe = require('stripe'); var STRIPE_SECRET_KEY = 'sk_test_xxxxxxxxxx';

But here the function Stripe.card.createToken is not define

1
try using Stripe.tokens.create instead of Stripe.cart.createToken? stripe.com/docs/api#token_object - Alex
No such method createToken i am refering parse.com/docs/js/symbols/Stripe.Tokens.html - Neelabh
Why don't you want to expose your publishable key? It's totally safe as explained here: support.stripe.com/questions/… Doing what you plan will require the card details to reach your server and you would have to be PCI compliant on your own which is a lot of work - koopajah
I want to minimize the client side things and plan most of the things in cloud function.. Because client side source code is exposed - Neelabh

1 Answers

1
votes

Finally my research is over and I got the solution:

Parse.Cloud.httpRequest({
    method : 'POST',
    url : 'https://api.stripe.com/v1/tokens',
    headers : {
        'Authorization' : 'Bearer sk_test_xxxxxxxxxxxxxx'
    },
    body : {
        "card[number]" : request.params.number,
        "card[exp_month]" : request.params.month,
        "card[exp_year]" : request.params.year,
        "card[cvc]" : request.params.cvc
    },
    success : function(httpResponse) {
        token = httpResponse.data.id; // Its token which required for create payment/charge
    },
    error : function(httpResponse) {
        // Error
    }
})

The above code can be used in any cloud function which are written in main.js