'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 = function ($q) {
  internals.$q = $q;
  return internals.wrap(Stripe, {
    card: {
      reference: ['validateCardNumber', 'validateExpiry', 'validateCVC', 'cardType'],
      promisify: ['createToken']
    },
    bankAccount: {
      reference: ['validateRoutingNumber', 'validateAccountNumber'],
      promisify: ['createToken']
    }
  });
};

module.exports.$inject = ['$q'];

@@ -80,106 +80,86 @@
 );%0A%0A
-var internals = %7B%7D;%0A%0Ainternals.promisify = function (receiver, method) %7B%0A  var $q = internals.$q;%0A
+module.exports = function ($q) %7B%0A%0A  function promisify (receiver, method) %7B%0A  
   re
@@ -178,24 +178,26 @@
 on (data) %7B%0A
+  
     var defe
@@ -215,24 +215,26 @@
 efer();%0A    
+ 
+ 
 receiver%5Bmet
@@ -270,24 +270,26 @@
 response) %7B%0A
+  
       if (re
@@ -344,24 +344,26 @@
 ror);%0A      
+ 
+ 
 return defer
@@ -385,28 +385,32 @@
 ponse);%0A    
+  
 %7D);%0A
+  
     return d
@@ -430,33 +430,21 @@
 se;%0A
+  
   %7D;%0A
-%7D;%0A%0Ainternals.wrap =
+  %7D%0A%0A 
  fun
@@ -449,16 +449,21 @@
 unction 
+wrap 
 (source,
@@ -476,16 +476,18 @@
 ns) %7B%0A  
+ 
+ 
 var angu
@@ -502,16 +502,18 @@
 e = %7B%7D;%0A
+  
   angula
@@ -558,24 +558,26 @@
 receiver) %7B%0A
+  
     var dest
@@ -616,24 +616,26 @@
 %5D = %7B%7D;%0A    
+  
 receiver = S
@@ -659,61 +659,9 @@
     
-/* istanbul ignore else */%0A    if (methods.promisify)
+ 
  ang
@@ -710,32 +710,34 @@
 method) %7B%0A      
+  
 destination%5Bmeth
@@ -742,26 +742,16 @@
 thod%5D = 
-internals.
 promisif
@@ -775,24 +775,26 @@
 d);%0A    
+  
 %7D);%0A    
 if (meth
@@ -789,30 +789,9 @@
     
-if (methods.reference)
+ 
  ang
@@ -844,24 +844,26 @@
 od) %7B%0A      
+ 
+ 
 destination%5B
@@ -898,18 +898,24 @@
     
+  
 %7D);%0A  
+ 
+ 
 %7D);%0A
+  
   re
@@ -938,85 +938,22 @@
 pe;%0A
-%7D;%0A%0Amodule.exports = function ($q) %7B%0A  internals.$q = $q;%0A  return internals.
+  %7D%0A%0A  return 
 wrap

Refactor service for better readability