在Parse后端保存条带customerId

时间:2014-11-21 13:40:33

标签: ios parse-platform stripe-payments

我正在尝试在Parse后端为我的应用用户保存customerId。

以下代码可能出现问题:

    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");
                }
        });
});

IOS代码:

- (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

使用此代码我可以在Stripe中创建一个客户。但是,它不会将其保存在Parse用户表中。

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

可能出现什么问题?我将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

它适用于以下代码。解析函数不对,我必须注销并登录用户,因为我在创建StripeCustomerId列后没有注销用户。

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");
                }
        });
});