'use strict';

var angular = require('angular');
var Stripe  = require('stripe');

var internals = {};

internals.promisify = function (receiver, method) {
  var $q = internals.$q;
  return  function (data) {
    var deferred = $q.defer();
    receiver[method](data, function (status, response) {
      if (response.error) return deferred.reject(response.error);
      return deferred.resolve(response);
    });
    return deferred.promise;
  };
};

internals.wrap = function (source, options) {
  var angularStripe = {};
  angular.forEach(options, function (methods, receiver) {
    var destination = angularStripe[receiver] = {};
    receiver = Stripe[receiver];
    /* istanbul ignore else */
    if (methods.promisify) angular.forEach(methods.promisify, function (method) {
      destination[method] = internals.promisify(receiver, method);
    });
    if (methods.reference) angular.forEach(methods.reference, function (method) {
      destination[method] = receiver[method];
    });
  });
  return angularStripe;
};

module.exports = [
  '$q',
  function ($q) {
    internals.$q = $q;

    return internals.wrap(Stripe, {
      card: {
        reference: ['validateCardNumber', 'validateExpiry', 'validateCVC', 'cardType'],
        promisify: ['createToken']
      },
      bankAccount: {
        reference: ['validateRoutingNumber', 'validateAccountNumber'],
        promisify: ['createToken']
      }
    });
  }
];

@@ -1036,20 +1036,8 @@
 ts =
- %5B%0A  '$q',%0A 
  fun
@@ -1049,18 +1049,16 @@
  ($q) %7B%0A
-  
   intern
@@ -1070,19 +1070,16 @@
 q = $q;%0A
-%0A  
   return
@@ -1112,18 +1112,16 @@
     
- 
- 
 card: %7B%0A
     
@@ -1112,26 +1112,24 @@
     card: %7B%0A
-  
       refere
@@ -1200,34 +1200,32 @@
 rdType'%5D,%0A      
-  
 promisify: %5B'cre
@@ -1243,15 +1243,11 @@
     
-  
 %7D,%0A
-  
     
@@ -1263,26 +1263,24 @@
 nt: %7B%0A      
-  
 reference: %5B
@@ -1332,26 +1332,24 @@
 er'%5D,%0A      
-  
 promisify: %5B
@@ -1371,23 +1371,49 @@
     
-  %7D%0A    %7D);%0A  %7D%0A
+%7D%0A  %7D);%0A%7D;%0A%0Amodule.exports.$inject = %5B'$q'
 %5D;%0A

Use $inject DI syntax