Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

javascript - specify only one of two parameters in a request

I am integrating Stripe subscriptions on my website and I want to use the same function for two different situation: new customer or existing customer. In case of a new customer I will pass the customer_email parameter, in case of an existing one the customer parameter. I am trying to implement this logic in my actual code where I pass both (and this will lead to an error). I need to remove both key and value of one.

const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    customer: customer, //remove this line if new customer
    customer_email: customerEmail, //remove this line if existing customer
    line_items: [{price: planId, quantity: 1}],
    subscription_data: { 
    trial_period_days: 15
    },
    metadata: {'planId': planId,'product': product},
    success_url: `${domainURL}/index.html?product=${product}&session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${domainURL}/product=${product}&index.html?session_id=cancelled` ,
    mode: 'subscription',
});

The workaround is to duplicate the code but I prefer to keep my code minimal where possible. How can I remove the key:value pair conditionally?

The condition is simply:

if(customer!=''){
    //i have a customer id, so remove the customer_email key:value
}else{
    //new customer, remove the customer key:value
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can convert the values to undefined to omit it.

const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    customer: customerEmail ? undefined : customer, //remove this line if new customer
    customer_email: customerEmail || undefined, //remove this line if existing customer
    line_items: [{price: planId, quantity: 1}],
    subscription_data: { 
    trial_period_days: 15
    },
    metadata: {'planId': planId,'product': product},
    success_url: `${domainURL}/index.html?product=${product}&session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${domainURL}/product=${product}&index.html?session_id=cancelled` ,
    mode: 'subscription',
});

Another solution would be to extend the object conditionally.

const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    line_items: [{price: planId, quantity: 1}],
    subscription_data: { 
    trial_period_days: 15
    },
    metadata: {'planId': planId,'product': product},
    success_url: `${domainURL}/index.html?product=${product}&session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${domainURL}/product=${product}&index.html?session_id=cancelled` ,
    mode: 'subscription',
    ...(customerEmail ? { customer_email: customerEmail } : { customer })
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...