Issues while integrating Stripe with Parse

2020-03-31 06:13发布

问题:

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)

What's the problem with my code. I haven't change anything i am doing according to the documentation.Any onle please suggest whats the issue in my code.

回答1:

Hey so I was able to duplicate your error. In the javascript, change var charge = stripe.charges.create({ to var charge = stripe.Charges.create({

Also you can pass the token directly if you want, you don't need to convert it to a string.



回答2:

After spending one night finally I spotted my errors there is Three errors in my code:

Error 1

Replace this

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

with

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

Error 2

stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');

Their is extra space in key after last word i.e 'd'.

Error 3

Remove this

 response.success(request.params.token);

from top

Finally working code is ::

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

});

  });