当用户/电子邮件存在于数据库中时,用于创建Stripe客户的Meteor调用方法

时间:2016-05-12 09:28:42

标签: mongodb meteor stripe-payments

提交订阅计划表单,该表单在客户端上运行jquery验证,然后调用createCustomer方法。在该方法中,查找用户名和电子邮件(来自小写的表单)并执行findOne,如果mongo中不存在用户名和电子邮件,请调用stripeCreateCustomer和stripeCreateSubscription方法。

问题是,它仍然会通过付款将用户注册到条带信息中心。即使我收到错误消息,说明已使用用户名,或收到电子邮件。为什么stripeCreateCustomer和stripeCreateSubscription方法仍在运行?

除此之外,我相信它运作良好。我只需要电子邮件验证..如果有人也可以帮助我,那真的很好。感谢。

~/server/signup.js

import Future from 'fibers/future';

Meteor.methods({
  createCustomer(customer) {
    check(customer, {
      username: String,
      email: String,
      password: String,
      plan: String,
      token: String
    });

    const usernameRegEx = new RegExp(customer.username, 'i');
    const emailRegEx = new RegExp(customer.email, 'i');

    const lookupCustomerUsername = Meteor.users.findOne({'username': usernameRegEx});
    const lookupCustomerEmail = Meteor.users.findOne({'email': emailRegEx});

    if(!lookupCustomerUsername) {
      if(!lookupCustomerEmail) {
        const newCustomer = new Future();

        Meteor.call('stripeCreateCustomer', customer.token, customer.email, function(error, stripeCustomer) {
          if(error) {
            console.log(error)
          } else {
            const customerId = stripeCustomer.id,
                  plan       = customer.plan;

            Meteor.call('stripeCreateSubscription', customerId, plan, function(error, response) {
              if(error) {
                console.log(error)
              } else {
                try {
                  const user = Accounts.createUser({
                    username: customer.username,
                    email: customer.email,
                    password: customer.password
                  });

                  const subscription = {
                    customerId: customerId,
                    subscription: {
                      plan: customer.plan,
                      payment: {
                        card: {
                          type: stripeCustomer.sources.data[0].brand,
                          lastFour: stripeCustomer.sources.data[0].last4
                        },
                        nextPaymentDue: response.current_period_end
                      }
                    }
                  }

                  Meteor.users.update(user, {
                    $set: subscription
                  }, function(error, response) {
                    if(error) {
                      console.log(error)
                    } else {
                      newCustomer.return(user)
                    }
                  });
                } catch(exception) {
                  newCustomer.return(exception);
                }
              }
            });
          }
        });
        return newCustomer.wait();
      } else {
        throw new Meteor.Error('customer-exists', 'email address is already taken')
      }
    } else {
      throw new Meteor.Error('customer-exists', 'username is already taken')
    }
  }
});
~/server/stripe.js

import Future from 'fibers/future';

const secret = Meteor.settings.private.stripe.testSecretKey;
const Stripe = StripeAPI(secret);

Meteor.methods({
  stripeCreateCustomer(token, email) {
    check(token, String);
    check(email, String);

    const stripeCustomer = new Future();

    Stripe.customers.create({
      source: token,
      email: email
    }, function(error, customer) {
      if(error) {
        stripeCustomer.return(error)
      } else {
        stripeCustomer.return(customer)
      }
    });

    return stripeCustomer.wait();
  },

  stripeCreateSubscription(customer, plan) {
    check(customer, String);
    check(plan, String);

    const stripeSubscription = new Future();

    Stripe.customers.createSubscription(customer, {
      plan: plan
    }, function(error, subscription) {
      if(error) {
        stripeSubscription.return(error)
      } else {
        stripeSubscription.return(subscription)
      }
    });

    return stripeSubscription.wait();
  }
});

对于MasterAM: 很抱歉继续询问并感谢您坚持下去。我对此很新...我提供了一个示例用户文档的findOne。提交表单后,(如果db中不存在用户名或电子邮件),这就是添加的内容:

{
  "_id" : "WyWmdyZenEJkgAmJv",
  "createdAt" : ISODate("2016-05-12T07:27:16.459Z"),
  "services" : {
    "password" : {
      "bcrypt" : "$2a$10$u8hyzWPu6Dnda1r8j7GkBuvQiF2iGFa5DdjEoD/CkHhT0jU.IsHhu"
    }
  },
  "username" : "test",
  "emails" : [
    {
      "address" : "test@test.com",
      "verified" : false
    }
  ],
  "customerId" : "cus_8R6QC2ENNJR13A",
  "subscription" : {
    "plan" : "basic_monthly",
    "payment" : {
      "card" : {
        "type" : "Visa",
        "lastFour" : "4242"
      },
      "nextPaymentDue" : 1465716435
    }
  }
}

0 个答案:

没有答案