2017-08-18 60 views
0

我試圖在我的網站上設置條紋以獲取信用卡付款。 我成功設法帶工資的工作是這樣的:使用Apple/Android支付的AngularJs網站上的條紋

function directive() { 
    return { 
     restrict: 'A', 
     controller: 'StripePayController', 
     controllerAs: 'controller', 
     bindToController: true, 
     templateUrl: 'app/directives/stripe-pay/stripe-pay.html' 
    }; 
}; 

function controller(stripePayService) { 
    var self = this; 

    // Method binding 
    self.checkout = stripePayService.checkout; 

    init(); 

    ////////////////////////////////////////////////// 

    function init() { 
     stripePayService.configure(); 
    }; 
}; 


function service($rootScope, $localStorage, $q, $document, paymentService) { 
    var handler; 
    return { 
     configure: configure, 
     checkout: checkout 
    }; 

    ////////////////////////////////////////////////// 

    function configure() { 

     // Load our script 
     loadScript().then(function() {     
      handler = StripeCheckout.configure({ 
       key: 'my-code', 
       locale: 'auto', 
       token: function (token) { 
        token.transaction = transaction(); 
        paymentService.create({ token: token.id }).then(function (response) { 
         var authorizationData = $rootScope.authorizationData; 
         authorizationData.stripeCardId = response.stripeCardId; 
         authorizationData.stripeCustomerId = response.stripeCustomerId; 
         authorizationData.stripeSubscriptionId = response.stripeSubscriptionId; 
         $localStorage.set('authorizationData', angular.toJson(authorizationData)); 
        }); 
       } 
      }); 
     }); 
    }; 

    function checkout(e) { 
     handler.open(transaction()); 
     e.preventDefault(); 
    }; 

    function transaction() { 
     var email = $rootScope.authorizationData.userName; 
     return { 
      name: 'acme Ltd', 
      description: 'Results', 
      email: email, 
      zipCode: false, 
      currency: 'gbp', 
      amount: 199 
     }; 
    }; 

    function loadScript() { 

     // Create our script 
     var deferred = $q.defer(); 
     var doc = $document[0]; 
     var script = doc.createElement('script'); 

     // Set the url 
     script.src = 'https://checkout.stripe.com/checkout.js'; 

     // Bind our methods 
     script.onload = function() { 
      deferred.resolve(); 
     }; 

     // Bind our methods 
     script.onreadystatechange = function() { 
      var rs = this.readyState; 
      if (rs === 'loaded' || rs === 'complete') 
       deferred.resolve(); 
     }; 

     // Bind our methods 
     script.onerror = function() { 
      deferred.reject(new Error('Unable to load checkout.js')); 
     }; 

     // Get the head 
     var container = doc.getElementsByTagName('head')[0]; 

     // Append our script to our page 
     container.appendChild(script); 

     // Return our promise 
     return deferred.promise; 
    }; 
}; 

這似乎很好地工作。 但是設置應用工資似乎更加複雜。有沒有人有之前使用過的任何angularjs代碼可以幫助我?

回答