javascript - 如何在向信用卡收费之前开始免费试用期?
问题描述
我试图在用户收费之前为他们提供 7 天的免费试用。我把7天的试用期放在了计划中。但是,它会立即向用户收费。
我也尝试过这样的论点
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");
}
};
这会导致付款失败。然后我在这个函数中调用它。
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 };
}
)
});
这就是我在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);
});
}
解决方案
问题是我trial_end
在几毫秒内发布了论点。在将其除以 1000 并使用它之后,parseInt
它工作得很好。感谢您的建议。
推荐阅读
- python - 为什么这个 os 循环不会打印出所有文件的完整路径而只给出其中的一些?
- ruby-on-rails - 除了使用 cancan 拒绝访问之外,是否可以引发异常?
- ruby - 如何在 Ruby 中使用 OptionParser 从用户那里获取信息?
- html - 使多组图像和段落在列内响应
- c - 如何检测参数是否为仅具有 int 参数的被调用函数中的 char
- python - 在 Windows 10 上,如何配置我的 docker-compose 以允许 Python 连接到 MySql 8?
- html - 到达某个部分时显示我的导航栏#?
- python - 无法在 Windows 10 上的 Python 中运行 Selenium
- python - Python中的方法是实例属性还是类属性?
- python - 如何网页抓取速度量?