EsmaGo
EsmaGo

Reputation: 161

Save Stripe customerId on Parse backend

I am trying to save a customerId on Parse backend for users of my app.

What can be wrong with the following code:

    var Stripe = require("stripe");
    Stripe.initialize('sk_test_----------');

    Parse.Cloud.define("saveStripeCustomerId", function (request, response) {
    Stripe.Customers.create(
{ card: request.params.token
    }, {
                success: function(httpResponse) {
                        response.success("Purchase made!");


                var Usr = Parse.User.current();
                            Usr.set("StripeCustomerId",request.params.objectId );
                            Usr.save(null, {
                                success: function(newUsr) {
                                // Execute any logic that should take place after the object is saved.
                               alert('New object created with objectId: ' + newUsr.id);

                                },
                                error: function(newUsr, error) {
                                // Execute any logic that should take place if the save fails.
                                // error is a Parse.Error with an error code and message.
                                alert('Failed to create new customer , with error code: ' + error.message);
                                }

                            });

                },
                error: function(httpResponse) {
                        response.error("Error ...oh no");
                }
        });
});

The IOS code:

- (IBAction)save:(id)sender
{
    PTKCard* card = self.paymentView.card;

    NSLog(@"Card last4: %@", card.last4);
    NSLog(@"Card expiry: %lu/%lu", (unsigned long)card.expMonth, (unsigned long)card.expYear);
    NSLog(@"Card cvc: %@", card.cvc);

    [[NSUserDefaults standardUserDefaults] setValue:card.last4 forKey:@"card.last4"];
    [self.navigationController popViewControllerAnimated:YES];

    STPCard* stpcard = [[STPCard alloc] init];
    stpcard.number = card.number;
    stpcard.expMonth = card.expMonth;
    stpcard.expYear = card.expYear;
    stpcard.cvc = card.cvc;


    [Stripe createTokenWithCard:stpcard completion:^(STPToken *token, NSError *error) {
        if (error) {

            //[self handleError:error];


        } else {
            //[self createBackendChargeWithToken:token];


            [PFCloud callFunctionInBackground:@"saveStripeCustomerId"
                               withParameters:[NSDictionary dictionaryWithObjectsAndKeys:token.tokenId, @"token", nil]
                                        block:^(id object, NSError *error) {

                                            if(error == nil)
                                            {
                                                [[[UIAlertView alloc] initWithTitle:@"Stripe Customer Id saved!"
                                                                            message:@"Your stripe cust id has been saved!"
                                                                           delegate:nil
                                                                  cancelButtonTitle:@"Ok"
                                                                  otherButtonTitles:nil, nil] show];
                                            }
                                        }];

        }
    }];   
}

@end

with this code I can create a customer in Stripe. However, it does not save it in Parse User Table.

Parse Cloud Log:
I2014-11-21T15:55:10.887Z] v46: Ran cloud function saveStripeCustomerId for user GBkeqCcOcU with:
  Input: {"token":"tok_-----------"}
  Result: Purchase made!

What could be going wrong? I will appreciate any help, thanks!

Upvotes: 0

Views: 1001

Answers (1)

EsmaGo
EsmaGo

Reputation: 161

It works with the following code. Parse function was not right and I had to log out and login the user because i did not log out the user after creating StripeCustomerId column.

Parse.Cloud.define("saveStripeCustomerId", function (request, response) {
        Stripe.Customers.create(
    { card: request.params.token
        }, {
                success: function(customer) {

                            //response.success("Purchase made!");


                var Usr = request.user;
                            Usr.set("StripeCustomerId",customer.id );
                            Usr.save(null, {
                                success: function(customer) {
                                // Execute any logic that should take place after the object is saved.
                                //alert('New object created with objectId: ' + newUsr.id);

                response.success("customer saved to parse = " + Usr.get("username"));
                                },
                                error: function(customer, error) {
                                // Execute any logic that should take place if the save fails.
                                // error is a Parse.Error with an error code and message.
                                //alert('Failed to create new customer , with error code: ' + error.message);
                response.error("oh uh non oooo failed to saved customer id to parse");
                                }

                            });

                },
                error: function(httpResponse) {
                        response.error("Error ...oh no");
                }
        });
});

Upvotes: 1

Related Questions