本文介绍了将 Stripe 与 Parse 集成时的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成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完成:^(STPToken *令牌,NSError *错误){如果(错误){NSLog(@"%@",error);} 别的 {NSString *myVal = [NSString stringWithFormat:@"%@",token];NSLog(@"%@",token);[PFCloud callFunctionInBackground:@"chargeMoney"withParameters:@{@"token":myVal}块:^(NSString *result,NSError *error){如果(!错误){NSLog(@"来自云代码:%@",result);}}];};}];}

充电代码

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,//再次以美分为单位货币:美元",来源:stripeToken,描述:[email protected]"},功能(错误,收费){if (err && err.type === 'StripeCardError') {//该卡已被拒绝}});});

我遇到的错误

 [错误]:类型错误:无法调用未定义的方法创建"在 main.js:11:31(代码:141,版本:1.7.1)

我的代码有什么问题.我没有根据文档更改我正在做的任何事情.请建议我的代码中有什么问题.

解决方案

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

错误 1

替换这个

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

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

错误 2

stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd');

他们在最后一个单词后的键中有额外的空格,即d".

错误 3

删除这个

 response.success(request.params.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完成:^(STPToken *令牌,NSError *错误){如果(错误){NSLog(@"%@",error);} 别的 {NSString *myVal = token.tokenId;NSLog(@"%@",token);[PFCloud callFunctionInBackground:@"hello" withParameters:@{@"token":myVal}块:^(NSString *result,NSError *error){如果(!错误){NSLog(@"from Cloud Code Res: %@",result);}别的{NSLog(@"来自云代码:%@",error);}}];};}];}

解析云代码(main.js)

//使用 Parse.Cloud.define 定义任意数量的云函数.//例如:var stripe = require('stripe');stripe.initialize('sk_test_lweGasdaqwMKnZRndg5123G');Parse.Cloud.define(你好",函数(请求,响应){var stripeToken = request.params.token;var Charge = stripe.Charges.create({金额:1000,//以美分表示美元货币:美元',卡:stripeToken}).then(null, function(error) {console.log('用条带充电失败.错误:' + 错误);});});
- (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);
                                                                              }
                                                                          }];

                                          };
                                      }];


}
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: "[email protected]"
}, function(err, charge) {
  if (err && err.type === 'StripeCardError') {
    // The card has been declined
 }
   });

});
  [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.

解决方案

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

Replace this

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

with

  NSString *myVal = [NSString stringWithFormat:@"%@",token.tokenId];
stripe.initialize('sk_test_ldqwdqwdqwdqwdwqdqwd ');

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

Remove this

 response.success(request.params.token);

from top

Finally working code is ::

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

                                                                          }];

                                          };
                                      }];


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

});

  });

这篇关于将 Stripe 与 Parse 集成时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 03:50