0

我在离子应用程序中实施Stripe支付方法,需要从服务中获取token如何将数据从服务传递到控制器AngularJS + Stripe

问题我面临的是,console.log(token)StripeService是安慰令牌,但我需要通过这个给我的控制器做一些额外的东西。

我试过return token;this.open()方法和console.log(StripeService.open(amount))但没有运气。

我想这一点 - https://stripe.com/docs/checkout

让我知道我能得到的服务令牌到我的控制器。

码 -

以下是我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); 
      } 
     }); 
    }; 
}); 

以下是我在控制方法和调用服务 -

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

回答

1

你可以使用promises返回令牌。

服务:

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

控制器:

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

我没有测试,但应该工作!

+0

真棒非常感谢:) –

+0

我怎么能让这个在手机上工作?在移动分条签出打开一个新的选项卡,只有真正的onclick事件打开一个新的选项卡,所以ng-click的handler.open不打开表单 –

1

由于manzapanza说,你可以使用的承诺,我个人更喜欢这个新的符号:

服务:

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

控制器:

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