jsetting32
jsetting32

Reputation: 1632

Stripe and Parse SDK to store credit card

I am attempting to store a users credit card into stripe. I have a form all setup using Payment Kits PTKView. Then once the user taps the save button, I create a STPToken which is required by Stripe. Once the token is made, I attempt to save the token with the users objectId to Stripe as a customer. I am sure the problem lies within the server code.

A bit of background, I am using Parse SDK's cloud code as my server, so there I'm dealing with javascript :(.

Here I take the information from the form, create a STPCard, then generate a token.

PTKCard* card = self.cardView.card; // self.cardView is the PTKView
STPCard *stpcard = [[STPCard alloc] init];
stpcard.number = card.number;
stpcard.expMonth = card.expMonth;
stpcard.expYear = card.expYear;
stpcard.cvc = card.cvc;

[[STPAPIClient sharedClient] createTokenWithCard:stpcard
                                      completion:^(STPToken *token, NSError *error) {
                                          if (error) {
                                              NSLog(@"error");
                                          } else {
                                              [self createBackendChargeWithToken:token];
                                          }
                                      }];

Once the token is made, I send it to Parse where I implemented the server side code to handle creating the customer and saving it to Stripe.

- (void)createBackendChargeWithToken:(STPToken *)token
{

    NSDictionary *productInfo = @{@"cardToken": [NSString stringWithFormat:@"%@",token],
                                  @"objectId": [[PFUser currentUser] objectId]};


    [PFCloud callFunctionInBackground:@"saveCardInformation"
                       withParameters:productInfo
                                block:^(id object, NSError *error) {
                                    if (error) {
                                        NSLog(@"error");
                                        return ;
                                    }

                                    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Success", @"Success")
                                                                message:nil
                                                               delegate:nil
                                                      cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                                      otherButtonTitles:nil] show];

                                }];
}

Now I handle the token passed within the cloud code:

var Stripe = require('stripe');
Stripe.initialize(my_key);


Parse.Cloud.define("saveCardInformation", function(request, response) {

    Stripe.Customers.create({
      source: request.params.cardToken,
    },{
      success: function(httpResponse) {
        response.success("Customer created!");
      },
      error: function(httpResponse) {
        response.error(httpResponse.message);
      }
    });

});

I then get a response back saying this:

Error: There is no token with ID tok_15afWSDOYfVZdgZvh6qSvmmm (test mode). (Code: 141, Version: 1.6.0)

Why is it saying this? To my understanding, I am just creating a user so of course there is no token with the specified id. I understand it is mainly in the javascript cloud code but I am unsure how to handle this... Anyone got any ideas?

Upvotes: 2

Views: 1150

Answers (1)

jsetting32
jsetting32

Reputation: 1632

Okay... I made a small mistake... Apparently I needed to access the STPTokens tokenId property....

NSDictionary *productInfo = @{@"cardToken": token.tokenId,
                              @"objectId": [[PFUser currentUser] objectId]};

Need to really read my code thoroughly.

Upvotes: 2

Related Questions