#puya_17680

1 messages · Page 1 of 1 (latest)

nocturne pondBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

soft jewel
#

It should just work AFAIK? Does it not? Is there an error?

sturdy yew
#

the tax_id_collection it's working yes

#

but I need to add a custom field just when it's enabled

#

how I can do it?

soft jewel
#

Then you'd add some logic to your code to only add the custom_fields parameter when you're passing tax_id_collection

sturdy yew
#

is there any example?

soft jewel
#

Not really no. Would depend on what language you're using

sturdy yew
#

i'm using nextjs

soft jewel
#

Ok, so Node.js. So you likely want to conditionally add the custom_field parameter depending on some state or something:

...(addCustomFields && {
  custom_fields: [...]
})
sturdy yew
#

return from(stripe.checkout.sessions.create({
client_reference_id: ulid(),
payment_method_types: ['card', 'paypal'],
custom_fields: [
{
key: 'codice_fiscale',
label: {type: 'custom', custom: 'Codice Fiscale'},
type: 'text'
}
],
tax_id_collection: {
enabled: true,
},
}))

#

so now I have this one, right?

#

the tax_ic_collection tha is enabled

#

so I want to add a new field call VAT2 just when is enabled

#

and remove when is disabled

soft jewel
#

Share you full route handler code please

sturdy yew
#

const session$ = defer(() => product$.pipe(
concatMap((product) => {
return from(stripe.checkout.sessions.create({
client_reference_id: ulid(),
payment_method_types: ['card', 'paypal'],
line_items: [
{
price_data: {
product_data: {name: Custom Patch ${product.id}},
currency: "eur",
unit_amount:
parseFloat((product.price as number * 100).toFixed(2)),
},
quantity: 1,

                },
            ],
            custom_fields: [
                {
                    key: 'codice_fiscale',
                    label: {type: 'custom', custom: 'Codice Fiscale'},
                    type: 'text'
                }
            ],
            tax_id_collection: {
                enabled: true,
            },
            shipping_address_collection: {allowed_countries: ['IT', 'GB']},
            mode: "payment",
            success_url: `${process.env.FRONTEND_URL}/payment?success=true`,
            cancel_url: `${process.env.FRONTEND_URL}/payment?canceled=true`,
        }))
    })
))
soft jewel
#

Yes, but how are you invoking this server-side call from your React front-end?

sturdy yew
#

const product$ = defer(() => formData$.pipe(
concatMap((formData) => {
jwt = formData.get('jwt') as string;
return from(
getProduct(
request.url.replace("/payment", "").split("/").pop() as any,
jwt
)
).pipe(map((product) => {
productId = product.id as string;
return product
}))
})
))

#

const updateProperties$ = defer(() =>
product$.pipe(
concatMap((product) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { patchType: _, font: __, ...data } = payload;

      for (const key in data)
        if (data[key] === 'null') data[key] = undefined;

      for (const key of ['patchWidth', 'patchHeight', 'quantity'])
        if (payload[key]) payload[key] = parseFloat(payload[key]);
      Object.assign(product, data);
      if (image) product.image = image.filename;

      return of(product);
    }),
    concatMap((product) => {
      const { patchType } = payload;
      if (
        patchType !== 'null' &&
        typeof patchType !== 'undefined' &&
        patchType !== product?.patchType?.id
      )
        return from(
          this.patchTypeRepo.findOneOrFail({
            id: patchType,
          }),
        ).pipe(
          map((patchType) => {
            product.patchType = patchType;
            return product;
          }),
          catchError(() => {
            throw new NotFoundException('Patch type not found');
          }),
        );
      return of(product);
    }),
    concatMap((product) => {
      const { font } = payload;
      if (
        font !== 'null' &&
        typeof font !== 'undefined' &&
        font !== product?.font?.id
      )
        return from(this.fontRepo.findOneOrFail({ id: font })).pipe(
          concatMap((font) => {
            product.font = font;
            return of(product);
          }),
          catchError(() => {
            return throwError(
              () => new NotFoundException('Font not found'),
            );
          }),
        );
      return of(product);
    }),
    catchError((e) => throwError(() => e)),
  ),
);
#

and here there is the logic to call it

soft jewel
#

Yeah that is hard for me to grok. In any case you need to pass a field/parameter in the body of your request to that server-side function that creates the Checkout Session. Then you can use that to determine whether to pass tax_id_collection and conditionally add custom_fields