Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Methods: Signup
  3. * Methods for signing users up and adding them to our database.
  4. */
  5.  
  6. var Future = Npm.require('fibers/future');
  7.  
  8. Meteor.methods({
  9.   createTrialCustomer: function(customer){
  10.     // Check our customer object against our expected pattern.
  11.     check(customer, {
  12.       name: String,
  13.       emailAddress: String,
  14.       password: String,
  15.       plan: String,
  16.       creditCard: {
  17.         number: String,
  18.         cvv: String,
  19.         expirationMonth: String,
  20.         expirationYear: String,
  21.         billingAddress: {
  22.           postalCode: String
  23.         }
  24.       }
  25.     });
  26.  
  27.     // Before we send anything to Braintree, we should verify that our user doesn't
  28.     // exist in our database. We do this here because we technically won't create
  29.     // our user until AFTER we've created them on Braintree. To avoid duplicate customers
  30.     // on Braintree, we can check to see if they exist in our database first. If they
  31.     // don't, we know that they don't exist on Braintree either. Make sure to use a
  32.     // RegExp (regular expression) object to match any case.
  33.     var emailRegex     = new RegExp(customer.emailAddress, "i");
  34.     var lookupCustomer = Meteor.users.findOne({"emails.address": emailRegex});
  35.  
  36.     if ( !lookupCustomer ) {
  37.       // Create a Future that we can use to confirm successful account creation.
  38.       var newCustomer = new Future();
  39.  
  40.       // Create our customer.
  41.       Meteor.call('btCreateCustomer', customer.creditCard, customer.emailAddress, customer.name, function(error, btCustomer){
  42.         if (error) {
  43.           console.log(error);
  44.         } else {
  45.           var customerId = btCustomer.customer.id,
  46.               plan       = customer.plan;
  47.  
  48.           // Setup a subscription for our new customer.
  49.           Meteor.call('btCreateSubscription', customerId, plan, function(error, response){
  50.             if (error) {
  51.               console.log(error);
  52.             } else {
  53.               // Once Braintree is all setup, create our user in the application, adding all
  54.               // of the Braintree data we just received. Note: the third parameter being passed
  55.               // is the "profile" data we want to set for the customer. Note: here we're using
  56.               // a try/catch statement because Accounts.createUser does NOT accept a callback
  57.               // on the server. This was if we run into an error, we can still grab it and
  58.               // return it to the client.
  59.               try {
  60.                 var user = Accounts.createUser({
  61.                   email: customer.emailAddress,
  62.                   password: customer.password,
  63.                   profile: {
  64.                     name: customer.name,
  65.                   }
  66.                 });
  67.  
  68.                 // Before we return our user to the client, we need to perform a Meteor.users.update
  69.                 // on our user. This is done because we need to add our subscription data to our
  70.                 // customer, however, we don't want to store it in our customer's profile object.
  71.                 // Unfortunately, the only way to do this is to wait until the user exists and
  72.                 // *then* add the subscription data.
  73.  
  74.                 var customerSubscription = {
  75.                   customerId: customerId,
  76.                   subscription: {
  77.                     plan: {
  78.                       name: customer.plan,
  79.                       used: 0
  80.                     },
  81.                     payment: {
  82.                       card: {
  83.                         type: btCustomer.customer.paymentMethods[0].cardType,
  84.                         lastFour: btCustomer.customer.paymentMethods[0].last4
  85.                       },
  86.                       nextPaymentDue: response.subscription.nextBillingDate
  87.                     },
  88.                     status: response.subscription.status,
  89.                     ends: response.subscription.nextBillingDate
  90.                   }
  91.                 }
  92.  
  93.                 // Perform an update on our new user.
  94.                 Meteor.users.update(user, {
  95.                   $set: customerSubscription
  96.                 }, function(error, response){
  97.                   if (error){
  98.                     console.log(error);
  99.                   } else {
  100.                     // Once the subscription data has been added, return to our Future.
  101.                     newCustomer.return(user);
  102.                   }
  103.                 });
  104.               } catch(exception) {
  105.                 newCustomer.return(exception);
  106.               }
  107.             }
  108.           });
  109.         }
  110.       });
  111.       // Return our newCustomer Future.
  112.       return newCustomer.wait();
  113.     } else {
  114.       throw new Meteor.Error('customer-exists', 'Sorry, that customer email already exists!');
  115.     }
  116.   }
  117. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement