【问题标题】:Get access to Stripe Portal访问 Stripe 门户
【发布时间】:2020-10-22 18:24:53
【问题描述】:

我已经设置了一个节点服务器来管理订阅,现在我正在尝试添加到 Stripe 客户门户的连接(这样我就可以在创建后将订阅管理外包给他们)。

说明书说:

创建一个供用户点击的按钮:

<form method="POST" action="/create_customer_portal_session">
    <button type="submit">Manage billing</button>
</form>

然后为此添加端点:

// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
const stripe = require('stripe')('sk_test_yvBluFKhx8Xg1vjZVoulY8NO003U0HnRoA');

var session = await stripe.billingPortal.sessions.create({
  customer: 'cus_IETxeMQvgvY05a',
  return_url: 'https://example.com/account',
});

所以我采用了当前的服务器实现并尝试对其进行更新以包含此事件:

/* Get your Stripe publishable key to initialize Stripe.js */
fetch("https://***.com:4343/setup")
  .then(function(result) {
    return result.json();
  })
  .then(function(json) {
    var publicKey = json.publicKey;
    var basicPlanId = json.basicPlan;
    var proPlanId = json.proPlan;

    var stripe = Stripe(publicKey);
    // Setup event handler to create a Checkout Session when button is clicked
    document
      .getElementById("basic-plan-btn")
      .addEventListener("click", function(evt) {
        customerEmail=document.querySelector('[name="username"]').value;
        createCheckoutSession(basicPlanId).then(function(data) {
          // Call Stripe.js method to redirect to the new Checkout page
          stripe
            .redirectToCheckout({
                  sessionId: data.sessionId,
            })
            .then(handleResult);
        });
      });

    // Setup event handler to create a Checkout Session when button is clicked
    document
      .getElementById("pro-plan-btn")
      .addEventListener("click", function(evt) {
        customerEmail=document.querySelector('[name="username"]').value;
        createCheckoutSession(proPlanId,customerEmail).then(function(data) {
          // Call Stripe.js method to redirect to the new Checkout page
          stripe
            .redirectToCheckout({
              sessionId: data.sessionId
            })
            .then(handleResult);
        });
      });
    
    //PART  ADDED TO CALL THE USER PORTAL
    //Setup event handler to create a Portal Sesssion when button is clicked
    document
        .getElementById("portal")
        .addEventListener("click",function(evt){
            var customerId = 'cus_IETxeMQvgvY05a';
            stripe.billingPortal.sessions.create({
                customer: 'cus_IETxeMQvgvY05a',
                return_url: 'https://***.com/account',
            });
        });
    });

但是当我尝试单击按钮时,我发现 Stripe.BillinPortal 未定义。

其他两个请求工作顺利:对于每个请求,我都有一个这样定义的函数:

var createCheckoutSession = function(planId,customerEmail) {
  return fetch("https://***.com:4343/create-checkout-session", {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      planId: planId,
      customerEmail: customerEmail
    })
  }).then(function(result) {
    return result.json();
  });
};

节点服务器上对应的处理程序是这样的:

app.post("/create-checkout-session", async (req, res) => {
  const domainURL = process.env.DOMAIN;
  const { planId,customerEmail } = req.body;
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    customer_email: customerEmail,
    line_items: [{price: planId, quantity: 1}],
    subscription_data: { 
        //items: [{ plan: planId }]
        trial_period_days: 15
    },
    metadata: {'planId': planId},
    success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${domainURL}/canceled.html` ,
    mode: 'subscription',
  });

  res.send({
    sessionId: session.id
  });
});

我不知道如何对客户门户做同样的事情。

【问题讨论】:

    标签: javascript node.js stripe-payments


    【解决方案1】:

    您试图在您的前端代码中使用stripe.billingPortal.sessions,但这是行不通的。 (//PART ADDED TO CALL THE USER PORTAL的代码)

    会话应改为在您的后端服务器上创建。然后,您将 URL 返回到前端,并可以将 window.location 设置为例如。这类似于你已经实现了一个后端路由来获取 CheckoutSession,只是在这里你正在获取 Billing Session 的 url 字段,你的前端代码需要做的就是直接重定向到它。(你不需要像使用 redirectToCheckout 一样使用 Stripe 前端库。

    【讨论】:

    • 为什么客户门户没有类似redirectToCheckout的方法?如果客户可以更新他们的付款信息,那么为什么没有以与结帐相同的方式创建某种安全会话?
    • 请注意,redirectToCheckout 也不必再使用,您可以从后端访问并重定向到 url。无论如何,想法是您的 后端需要对客户进行身份验证,并且只有在您信任他们并且当前浏览器会话就是该客户时才为他们生成一个 CustomerPortal 链接。例如,在基于 RoR 会话(HTTP 会话 cookie)的 github.com/stripe-samples/developer-office-hours/tree/master/… 中,您可以将其绑定到您网站中的身份验证系统中。
    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多