仅在存在时在 Stripe Checkout 中添加客户电子邮件

Add Customer Email in Stripe Checkout ONLY if it is exists

如何才能将 'customer_email' 传递到 Stripe Checkout 中(如果存在)。如果它不存在,我根本不想传递电子邮件/属性。

我有一个案例,如果用户登录,然后他们的 customer_email 被传递给函数;否则我不想添加 customer_email 属性.

案例登录:let customerEmail = "example@example.com"; 案例注销:let customerEmail;

customer_email:客户邮箱?客户邮箱:空

本质上,是否可以根据对象的存在动态地将 属性/ 键传入对象?

这可能吗?

let customerEmail;

const session = await stripe.checkout.sessions.create({
      payment_method_types: [
        "card"
      ],
      payment_method_options: {
        wechat_pay: {
          client: "web",
        },
      },
      line_items: [
        {
          price: priceId,
          quantity: 1,
          description: priceName,
        },
      ],
      mode: "payment",
      allow_promotion_codes: true,
      locale: "auto"
      },
      customer_email: customerEmail ? customerEmail: null,
      success_url:
        "http://google.com/session_id={CHECKOUT_SESSION_ID}",
      cancel_url: "http://google.com/",
    });

    res.redirect(303, session.url);
  });

谢谢。

你可以这样做

var strip_param = {
    payment_method_types: [
        "card"
    ],
    payment_method_options: {
        wechat_pay: {
            client: "web",
        },
    },
    line_items: [
        {
            price: priceId,
            quantity: 1,
            description: priceName,
        },
    ],
    mode: "payment",
    allow_promotion_codes: true,
    locale: "auto",
    success_url: "http://google.com/session_id={CHECKOUT_SESSION_ID}",
    cancel_url: "http://google.com/",
};

if(customerEmail){
    strip_param = {...strip_param, customer_email: customerEmail}
}

const session = await stripe.checkout.sessions.create(strip_param);