richyman28
richyman28

Reputation: 47

How to link parse customer to Stripe customer

I am using Stripe and Parse cloud service to charge a card. Charging is all fine, but I don't know which user made the purchase. My application requires user to log in via Parse user to make the purchase.

I want to know 1. how can I create a Stripe customer using parse cloud code (JS). 2. how can I link that Stripe customer to the customer currently logged into my application (e.g [PFUser currentUser] from parse).

right now I have a vc to add credit card

[[STPAPIClient sharedClient] createTokenWithCard:stripeCard
                                      completion:^(STPToken *token, NSError *error) {
                                          if (error) {
                                              [self handleStripeError:error];
                                          } else {
                                              NSString *myVal = token.tokenId;
                                              NSLog(@"%@",token.tokenId);
                                              [PFCloud callFunctionInBackground:@"createBackendChargeWithToken" withParameters:@{@"token":myVal}
                                                                          block:^(NSString *result, NSError *error) {
                                                                              if (!error) {
                                                                                  NSLog(@"Success: from Cloud Code Res: %@",result);
                                                                                  self.pay.enabled = YES;
                                                                              }
                                                                              else
                                                                              {
                                                                                  NSLog(@"Error: from Cloud Code: %@",error);
                                                                                  self.pay.enabled = YES;
                                                                              }

                                                                          }];
                                                                        }
                                      }];

and my main.js is as follows:

Parse.Cloud.define("createBackendChargeWithToken", function(request, response){
var stripeToken = request.params.token;
    var charge = Stripe.Charges.create({
    amount:1000,
    currency:"usd",
    card: stripeToken,
},{
    success: function(httpResponse){
    response.success("Purchase made!");
    },
    error: function(httpResponse){
    response.error("no good");
    }
})
});

Upvotes: 0

Views: 432

Answers (1)

Jake T.
Jake T.

Reputation: 4378

First off, initialize the Stripe module in your cloud code:

var Stripe = require('stripe');
var STRIPE_SECRET_KEY = '[your secret key]';
var STRIPE_API_BASE_URL = 'api.stripe.com/v1'; //this is used for making http requests for parts of the Stripe API that aren't covered in parse's stripe module
Stripe.initialize( STRIPE_SECRET_KEY );

I create a card token on the client side, and pass that into my cloud code function so I'm never sending sensitive data. The cloud code function is pretty simple:

Parse.Cloud.define("StripeRegisterUser", function(request, response)
{   
    Stripe.Customers.create({
        card: request.params.stripeToken // the token id should be sent from the client
        },{
            success: function(customer) {
                console.log(customer.id);
                response.success(customer.id); 
            },
            error: function(httpResponse) {
                console.log(httpResponse);
                response.error("Uh oh, something went wrong"+httpResponse);
            }
        });
});

I'm actually storing the customer.id, which I'm passing as my response back to my client, to the user object, but that's on my client side code. You could add that code into this cloud function. request.user would be the user who called this cloud code function.

The Stripe module doesn't have the complete Stripe API, so I use that base URL to create custom http requests based off of the curl examples in Stripe's API docs.

Getting the user's card data requires an httpRequest, as the Stripe API doesn't include a method for that:

Parse.Cloud.define("StripeUserCards", function(request, response)
{   
    Parse.Cloud.httpRequest({
        method:"GET",

        url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/customers/" + request.params.customer_id + "/cards",

        success: function(cards) {
            response.success(cards["data"]);
        },
        error: function(httpResponse) {
            response.error('Request failed with response code ' + httpResponse.status);
        }
    });
});

You'll have to figure out how to parse the returned data yourself. It's of type "id" in obj-c.

The Stripe.Charges.create method in Parse requires both a customer id and a card id. Use that user card info on the client side to allow a user to select which card they want to use, send the card id to a method that creates a charge, and pass in that card id along with the customer id attached to the user object.

I paid a guy a lot of money to help me with this stuff, so I'm not gonna be able to give you more specific help.

Upvotes: 1

Related Questions