2014-11-21 2 views
0

Я пытаюсь сохранить clientId на Parse backend для пользователей моего приложения.Save Stripe customerId on Parse backend

Что может быть неправильно с помощью следующего кода:

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

Что может быть не так? Буду признателен за любую помощь, спасибо!

+0

Было бы полезно, если бы вы могли войти в HTTPResponse в функции обратного вызова ошибки Stripe.Customers.create, чтобы увидеть, что ответ, вполне может быть, что она содержит полезные советы. –

+0

спасибо @ Björn, я добавлю, что когда я получу шанс – ESG

+0

не смог ли это привести к нарушениям безопасности ??? – SleepsOnNewspapers

ответ

1

Он работает со следующим кодом. Функция Parse была неправильной, и мне пришлось выйти из системы и войти в систему, потому что я не вышел из системы после создания столбца 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"); 
       } 
     }); 
});