#jcodog-invoice-settings

1 messages · Page 1 of 1 (latest)

clever quartz
#

Hello! Stripe.js is client-side code, this should never have access to things like invoice_settings that only exist server-side.
Can you try to provide a lot more details

sweet linden
#
const Discord = require('discord.js');

const Stripe = require('stripe');
let key;
if (process.env.env == 'prod') {
    key = process.env.stripelive;
} else {
    key = process.env.stripetest;
}
const stripe = Stripe(key);

module.exports = {
    data: new Discord.SlashCommandBuilder()
        .setName("stripe")
        .setDescription("Get a GDPR request result for your Stripe data. (third party payment provider - No JCN Support)")
        .setDefaultMemberPermissions(Discord.PermissionFlagsBits.SendMessages)
        .setDMPermission(true),

    async execute (interaction) {
        let data;
        data = getStripe(interaction, data);

        if (data != null) {
            let user = {
                "Stripe ID": data.id,
                "Email": data.email,
                "Address": data.address
            }
            let card = {
                "Card": data.invoice_settings.default_payment_method.card,
                "Billing Address": data.invoice_settings.default_payment_method.billing_details.address,
                "Billing Name": data.invoice_settings.default_payment_method.billing_details.name,
                "Billing Email": data.invoice_settings.default_payment_method.billing_details.email,
            }
    
            interaction.reply({
                content: `User: \`\`\`${user}\`\`\`Payment Method: \`\`\`${card}\`\`\`\n\nJCoNet never stores or logs any data returned by Stripe Requests such as this.`,
                ephemeral: true
            });
        } else {
            interaction.reply({
                content: `We are sorry ${interaction.member.user.username}, We were unable to find a customer ID tied to your discord ID of ${interaction.member.id}. This means you have not made a payment on our system and therefore Stripe has no customer related to you.`,
                ephemeral: true
            });
        }
    }
}

async function getStripe(interaction, data) {
    let result = await interaction.client.db.query('SELECT stripeID FROM customers WHERE ID='+interaction.member.id);
    let results = result[0];

    if (results.length > 0) {
        data = await stripe.customers.retrieve(results[0].stripeID, {
            expand: ['default_source', 'invoice_settings.default_payment_method'],
        });
    } else {
        data = null;
    }

    return data;
}```
#

its so a user can view their details and their billing details such as card on an integration I am making. However for some reason the extends invoice settings default_payment_method is not working and says undefined.

#

and this is node sorry, not stripe.js

#

I am so used to node libraries for things like this being the company name.js

clever quartz
#

so what's your question?

#

sorry that's a lot of code, what exact line is the problem?

sweet linden
#

"Card": data.invoice_settings.default_payment_method.card, says data.invoice_settings is unefined so cannot read default_payment_method

clever quartz
#

what is "Card" what is data?

sweet linden
#

data is returned from the getStripe function

clever quartz
#

wgat is getStripe?

sweet linden
#

async function getStripe(interaction, data) {
let result = await interaction.client.db.query('SELECT stripeID FROM customers WHERE ID='+interaction.member.id);
let results = result[0];

if (results.length > 0) {
    data = await stripe.customers.retrieve(results[0].stripeID, {
        expand: ['default_source', 'invoice_settings.default_payment_method'],
    });
} else {
    data = null;
}

return data;

}

clever quartz
#

sorry this is really complex with re-using the same names like data is a param and a returned code

sweet linden
#

i checked... nothing from the customer object shared the name data.... and Card is just akey for my json

clever quartz
#

Like really just step 1 log what is returned?

#

is data = await stripe.customers.retrieve(results[0].stripeID, { expand: ['default_source', 'invoice_settings.default_payment_method'], }); even called? It's in an if for your own code with your own database

#

if it is called, what exactly is returned?

sweet linden
#

the full customer object with default_source and invoice_settings.default_payment_method shown in full.

#

I had issue getting the details to show earlier...

#

This is a repeat issue as no the error is something else... Do I need to be using a restricted key for this request?

#

it works fine on the test key

clever quartz
#

I'm sorry you go way too fast for me

#

Using a Secret API key or a Restricted API key with the right permissions should just work. Why are you talking about a restricted key all of a sudden?

#

Can you please confirm and go step by step first. what exactly is returned by your call? Why do you call the variable datawhen data is a parameter name already. Why not call this customer and return customer and store it in a variable tha tis called customer

#

and then can you please log exactly what you get for that call and what getStripe() returns

sweet linden
#

I think I solved it trying to get the console log...

#

I need to await getStripe(interaction, data)

#

let me test

clever quartz
#

ah yeah since you use await in some cases and not others

sweet linden
#

completely forgot that it returns a promise... It fixxed it. Sorry to waste your time. That was my error with my function.

clever quartz
#

totally fine

#

always add logs before/after calls to debug things like this 🙂

sweet linden
prime coral
#

Hi @sweet linden I'm taking over this thread. Give me a sec to catch up

#

@sweet linden can you tell me what doesn't work? is there an error message?

sweet linden
#

So I am just checking because I swapped to live to test on myself to see if my data pulled, I have a default payment method saved… but it’s saying bull to invoice_settings default_paymetn_method

prime coral
#

OK, did you use the live key when you set the invoice_settings.default_payment_method to the customer?

#

Can you also share with me the customer ID?

sweet linden
#

cus_KhfzKiBmXMI12G

prime coral
#

Thanks, I can see that you've set a default_source to this customer, and the invoice_settings.default_payment_method is null

#

How did you set invoice_settings.default_payment_method to this customer?

sweet linden
#

Yeah... Not sure why...

#

I didnt yet. I added all my details on the dashboard. That is me in that customer

prime coral
#

I see, you need to call the API to set the invoice_settings.default_payment_method to the customer, otherwise it's null.

sweet linden
#

ive chaned my code to accommodate for this.

#

Its working now but its weird it did that.

prime coral
#

When you select a default payment method in Dashboard, it will set it to the customer's default_source, not invoice_settings.default_payment_method

sweet linden
#

I fixed the issue, the live one wasnt working on my prod server

#

had an issue making the command switch to the prod environment variables

prime coral
#

When you switch to prod, make sure you use both live mode secret key and publishable key in your application.

#

What kind of issue are you facing? Let me see if I can help you out.

sweet linden
#

No it was not getting my live key, it was trying to use my test key… because it wasn’t detecting that the environment tag was prod bot test.

#

It’s all resolved now.

prime coral
#

Great to hear that 🙂