0

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.

Sid
  • 79
  • 11

2 Answers2

3

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.

CoolestNerdIII
  • 770
  • 4
  • 10
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);

});

  });
Sid
  • 79
  • 11
  • Sid, I tried using your code and unfortunately got the following error:[Error]: TypeError: Object [object Object] has no method 'isString' at request (stripe.js:49:25) at post (stripe.js:117:12) at Object.module.exports.Charges.create (stripe.js:157:16) at main.js:17:31 (Code: 141, Version: 1.8.3). Any help would be greatly appreciated. – Ibdakine Sep 12 '15 at 22:55