1

I have been researching this in Node JS and Swift. I have a "form" in my iOS app that gives me the following 3 options:

  • update the expiration date of credit card
  • update the expiration year of credit card
  • update the default credit card entirely

There may be instances where the user is only updating the default card, only updating one of the dates or both and not the default card. both dates.etc, any possibly scenario for updating.

My question is, how can I allow this function to accept blank or null input? I know I can set the CONST variable as null to begin with, but how to I prevent that from being submitted to stripe as blank and either wiping out the data or error out? For example, if const newdefault is blank, how can I make sure that default_source is not updated with blank data?

Would I use the filter command and put these into an array? These are already in JSON strings, so I am not sure an array would work. Additionally, if all 3 are blank (meaning, no one is updating the expire month or year, or selecting default, then they will not be able to hit the function, I have a null checker in my Swift code)

See code below with comments::

exports.updateCard = functions.https.onCall((data, context) => {
    const stripeId = data.stripeId;  // needed as users stripe ID
    const cardId = data.cardId;      // the current card they are updating, comes from the app
    const month = data.expmonth;    // this may or may not be blank
    const year = data.expyear;      // this may or may not be blank
    const newdefault = data.newdefault;  //this may or may not be blank

    return stripe.customers.updateSource(stripeId, cardId,
        {
            exp_month: month,  <--- how can I make sure this is not updated if month is blank?
            exp_year: year  <--- how can I make sure this is not updated if year is blank?
        }
    ).then(() => {
        return stripe.customers.update(stripeId,
            {
                default_source: newdefault <--- how can I make sure this is not updated if newdefault is blank?
            }
        ).then(() => {
            console.log(card);
            return { myReturn: card };
        }).catch((err) => {
            console.log(err);
        });
    });
});

In my research I have found that there is a command called array.filter that can be used. However, these are not arrays, but JSON strings. Here is the example I found on StackOverflow along with the link.
StackOverflow article on .filter

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];

var filtered = array.filter(function (el) {
  return el != null;
});

console.log(filtered);

Can someone please advise if I can use this method, if not, provide an example of a method I can use to achieve only allowing submission to stripe if the variables have data. Possibly a switch statement?

Greg
  • 67
  • 7
  • Is the old default_source represented somewhere or do you have access to it from the updateCard function? – ruby_newbie Nov 18 '19 at 19:42
  • You can validate the data with falsie values, is it blank it false and set your default data, or you can set default values to your parameter, or if they send it in a form validate the required fields on the form to prevent empty values. – Luis Louis Nov 18 '19 at 19:45
  • @ruby_newbie The old default source is already in stripe. I am only updating it if the user selects that option in my app. I have another function that pulls down the current_source and saves into a struct array in my Swift app. – Greg Nov 18 '19 at 19:45
  • @llCastro that is already what I have done. Say they are ONLY updating the default_source, when the app sends to this function its will ONLY fill the CONST of default_source. How can be sure that the other variables won't wipe out or enter blank data? I would assume they couldn't since it doesn't know what card ID to update – Greg Nov 18 '19 at 19:47
  • It looks like the stripe API will handle the logic if you send the fields in as null. For example if newDefault is null, the stripe API will not attempt to update that value: "If you need to update only some card details, like the billing address or expiration date, you can do so without having to re-enter the full card details." – ruby_newbie Nov 18 '19 at 19:57
  • @ruby_newbie Ok, thank you for that information, I must have missed that. This helps! So I don't need to use filter? – Greg Nov 18 '19 at 20:09
  • I don't think you need it but you should verify that because I have not used the Stripe API for some time but that is what I would infer from their docs. – ruby_newbie Nov 20 '19 at 19:48
  • I don't think you need it but you should verify that because I have not used the Stripe API for some time but that is what I would infer from their docs. – ruby_newbie Nov 20 '19 at 19:48

1 Answers1

2

It will probably be more straightforward if you just populate an object with a property based on the explicit condition that you want:

const obj = {}
if (thing) {
    obj.thing = thing
}
if (otherthing) {
    obj.otherthing = otherthing
}

stripe.customers.updateSource(obj)

In other words, there's no need to specify the entire object at once when you call any API. Just build up the object based on the conditions you want.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you for that information, I will re-structure the code that way. – Greg Nov 18 '19 at 20:11
  • I am strongly opposed to relying on the falsey-ness of a value for control flow in Javascript especially. I am pretty easy going about code standards as long as the team is on the same page but not using falsey for control flow is a hill I will die on every time. so basically use `if(thing == undefined){...}` rather than `if(thing){...} – ruby_newbie Nov 20 '19 at 19:47
  • @ruby_newbie `thing` is just a placeholder for whatever boolean you want to use to describe the condition that determines if the property should be populated. It doesn't have to be truthy or falsy - it just has to be correct. – Doug Stevenson Nov 20 '19 at 19:49
  • @DougStevenson Yeah I get it and I know what you meant(I upvoted your answer) but it seems Greg is a little earlier in his JS journey and wanted to spread advice that I has saved me a ton of headaches and really hard to diagnose bugs. – ruby_newbie Nov 20 '19 at 21:31