简体   繁体   English

如何将数据从服务传递到控制器AngularJS + Stripe

[英]How to pass data from service to controller AngularJS + Stripe

I am implementing the Stripe payment method in ionic app and need to get the token from the service. 我正在离子应用程序中实现Stripe付款方式,需要从服务中获取token

Problem I am facing is that, console.log(token) in StripeService is consoling the token but I need to pass this to my controller to do some extra stuff. 我面临的问题是, StripeService console.log(token)正在安慰令牌,但是我需要将此令牌传递给控制器​​以做一些额外的事情。

I tried return token; 我尝试过return token; in this.open() method and console.log(StripeService.open(amount)) but no luck. this.open()方法和console.log(StripeService.open(amount))但是没有运气。

I am trying this - https://stripe.com/docs/checkout 我正在尝试-https: //stripe.com/docs/checkout

Let me know how could I get the token from service to my controller. 让我知道如何从服务中获取令牌到我的控制器。

Code - 代码-

Following is my AngularJS service code - 以下是我的AngularJS服务代码-

.service('StripeService', function(){

    var handler = StripeCheckout.configure({
    key: 'pk_test_6776Randomkey8990',
    image: '/img/logo.png',
    locale: 'auto'
    });

    this.open = function(amount) {
        return handler.open({
            name: 'mywebsite.com',
            description: 'Pay via stripe',
            amount: amount,
            token: function(token) {
                console.log(token);
            }
        });
    };
});

Following is my method in controller and calling the service - 以下是我在控制器中调用服务的方法-

  $scope.clicked = function(amount) {
      StripeService.open(amount);
  };

You could use promises to return the token. 您可以使用promises返回令牌。

Service: 服务:

.service('StripeService', ['$q', function($q){

    var handler = StripeCheckout.configure({
        key: 'pk_test_6776Randomkey8990',
        image: '/img/logo.png',
        locale: 'auto'
    });

    this.open = function(amount) {
        var deferred = $q.defer();
        handler.open({
            name: 'mywebsite.com',
            description: 'Pay via stripe',
            amount: amount,
            token: function(token) {
                deferred.resolve(token);
            }
        });
        return deferred.promise;
    };
}]);

Controller: 控制器:

 $scope.clicked = function(amount) {
      StripeService.open(amount).then(function(token){
          console.log('token', token);
      });
  };

I didn't tested, but should works! 我没有测试,但应该可以!

As manzapanza says you can use promise, personally I prefer this new notation: 正如manzapanza所说的,您可以使用promise,我个人更喜欢这种新的表示法:

Service: 服务:

.service('StripeService', ['$q', function($q){

    var handler = StripeCheckout.configure({
    key: 'pk_test_6776Randomkey8990',
    image: '/img/logo.png',
    locale: 'auto'
    });

    this.open = function(amount) {
        return $q(function(resolve) {
            handler.open({
                name: 'mywebsite.com',
                description: 'Pay via stripe',
                amount: amount,
                token: function(token) {
                    console.log(token);
                    resolve(token);
                }
            });
        });
    };
}]);

Controller: 控制器:

$scope.clicked = function(amount) {
    StripeService.open(amount).then(function(token){
        console.log('token', token);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM