将 Stripe 与 Parse 集成时出现的问题

Issues while integrating Stripe with Parse

Code to make Token

- (IBAction)save:(id)sender
{
STPCard *card = [[STPCard alloc] init];
card.number = self.paymentView.card.number;
card.expMonth = self.paymentView.card.expMonth;
card.expYear = self.paymentView.card.expYear;
card.cvc = self.paymentView.card.cvc;
[[STPAPIClient sharedClient] createTokenWithCard:card
                                      completion:^(STPToken *token, NSError *error) {
                                          if (error) {
                                              NSLog(@"%@",error);
                                          } else {
                                              NSString *myVal = [NSString stringWithFormat:@"%@",token];
                                              NSLog(@"%@",token);
                                              [PFCloud callFunctionInBackground:@"chargeMoney"
                                                                 withParameters:@{@"token":myVal}
                                                                          block:^(NSString *result, NSError *error) {
                                                                              if (!error) {
                                                                                  NSLog(@"from Cloud Code: %@",result);
                                                                              }
                                                                          }];

                                          };
                                      }];


}

Code to Charge

Parse.Cloud.define("chargeMoney", function(request, response) {
  response.success(request.params.token);
var stripe = require('stripe');
stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');

var stripeToken = request.params.token;

  var charge = stripe.charges.create({
  amount: 1000, // amount in cents, again
  currency: "usd",
   source: stripeToken,
  description: "payinguser@example.com"
}, function(err, charge) {
  if (err && err.type === 'StripeCardError') {
    // The card has been declined
 }
   });

});

Error I am getting

  [Error]: TypeError: Cannot call method 'create' of undefined
   at main.js:11:31 (Code: 141, Version: 1.7.1)

我的代码有什么问题。我没有根据 documentation.Any 更改我正在做的任何事情,请建议我的代码中的问题是什么。

嘿,所以我能够复制你的错误。在 javascript 中,将 var charge = stripe.charges.create({ 更改为 var charge = stripe.Charges.create({

也可以直接传token,不需要转成字符串

花了一晚上终于发现了我的错误,我的代码中有三个错误:

Error 1

替换此

   NSString *myVal = [NSString stringWithFormat:@"%@",token];

  NSString *myVal = [NSString stringWithFormat:@"%@",token.tokenId];

Error 2

stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');

它们在最后一个词后的键中是额外的 space,即 'd'。

Error 3

删除此

 response.success(request.params.token);

自上而下

最终的工作代码是 ::

Create token

- (IBAction)save:(id)sender
{
STPCard *card = [[STPCard alloc] init];
card.number = self.paymentView.card.number;
card.expMonth = self.paymentView.card.expMonth;
card.expYear = self.paymentView.card.expYear;
card.cvc = self.paymentView.card.cvc;
[[STPAPIClient sharedClient] createTokenWithCard:card
                                      completion:^(STPToken *token, NSError *error) {
                                          if (error) {
                                              NSLog(@"%@",error);
                                          } else {
                                              NSString *myVal = token.tokenId;
                                              NSLog(@"%@",token);
                                              [PFCloud callFunctionInBackground:@"hello" withParameters:@{@"token":myVal}
                                                                          block:^(NSString *result, NSError *error) {
                                                                              if (!error) {
                                                                                  NSLog(@"from Cloud Code Res: %@",result);
                                                                              }
                                                                              else
                                                                              {
                                                                               NSLog(@"from Cloud Code: %@",error);
                                                                              }

                                                                          }];

                                          };
                                      }];


}

Parse Cloud code (main.js)

// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
 var stripe = require('stripe');
 stripe.initialize('sk_test_lweGasdaqwMKnZRndg5123G');
Parse.Cloud.define("hello", function(request, response) {



var stripeToken = request.params.token;

  var charge = stripe.Charges.create({
  amount: 1000, // express dollars in cents 
  currency: 'usd',
  card: stripeToken
}).then(null, function(error) {
  console.log('Charging with stripe failed. Error: ' + error);

});

  });