简体   繁体   English

如何在信用卡收费之前开始条纹免费试用期?

[英]How to start a stripe free trial period before charging the credit card?

I am trying to offer the user a 7 day free trial before they get charged.我正在尝试为用户提供 7 天免费试用,然后再收费。 I put the trial period of 7 days in the plan.我把7天的试用期放在了计划中。 However, it charges the user instantly.但是,它会立即向用户收费。

I also tried passing in the argument like this我也试过像这样传入参数

  const stripe = require("stripe")("sk_test_key");
  if (planType === "monthly") {
    stripe.subscriptions.create(
      {
        customer: customer,
        items: [
          {
            plan: "plan...."
          }
        ],
        coupon: coupon
        trial_end : 1579313395
      },
      function(err, subscription) {
        addDataToUserProfile(uid, "subscription", subscription);
      }
    );
  } else if (planType === "1year") {
    stripe.subscriptions.create(
      {
        customer: customer,
        items: [
          {
            plan: "plan......."
          }
        ],
        coupon: coupon
      },
      function(err, subscription) {
        addDataToUserProfile(uid, "subscription", subscription);
      }
    );
  } else {
    console.log("invalid plan type selected");
  }
};

This results in payment failure.这导致付款失败。 Then i call it in this function.然后我在这个函数中调用它。

exports.addStripeID = functions.https.onCall((data, context) => {
  const stripe = require("stripe")("sk_test_key");
  const uid = data.uid;
  const source = data.source;
  const email = data.email;
  const planType = data.planType;
  const coupon = data.coupon;
  return stripe.customers
    .create({
      source: source,
      email: email
    })
    .then(
      customer => {
        addCustomerToPlan(customer.id, uid, planType, coupon);
        addData(uid, "stripeID", customer.id);
        return { success: true };
      },
      err => {
        console.log("the error", err);
        return { error: err, message: "there was an error", success: false };
      }
    )
});

This is how i call it in firebase.这就是我在firebase中的称呼。

doAddStripeID(source, email, uid, planType, coupon) {
  console.log("do add stripe ID local function called");

  //change to this.functions
  var addData = app.functions().httpsCallable("addStripeID");

  addData({ source: source, email: email, uid: uid, planType: planType, coupon: coupon})
    .then(function (result) {
      console.log("add stripe result is ", result);
      // this.doAddDataToUser(uid, result.key, result.value)
      if (result.data.success) {
        console.log("it works");

        //this can probably be fixed with a reload instead of a interval
        setInterval(() => { window.location.assign("/payment-success") }, 3000);
      } else {
        console.log("nope");
        setInterval(() => { window.location.assign("/payment-failure") }, 3000);
      }
    })
    .catch(err => {
      console.log(err.code);
    });
}

The problem was that i was posting the trial_end argument in mili seconds.问题是我在几毫秒内发布了trial_end参数。 After dividing it by a 1000 and using parseInt it worked just fine.在除以 1000 并使用parseInt它工作得很好。 Thank you for the suggestions.感谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM