Quantcast
Channel: Square Developer Forums - Latest topics
Viewing all articles
Browse latest Browse all 1325

Create invoice attachment - filename in the response includes url parameters

$
0
0

I have been able to attach a pdf file to an invoice. However, the file name of the attachment gets URL parameters in it and looks bad on the invoice:
Please see it in this image:

I download my pdf file from a Firebase Storage public URL and pass it as a readable stream to the FileWrapper which I then pass to the createInvoiceAttachment API call. In the description field, I add the name of the file which I want to show on the invoice but it gets shown with URL parameters. Here’s my code:

// 6. Upload the attachment
   async function createSquareInvoiceAttachment(invoiceId, fileUrl) {
    try {     
          // 1. Create a readable stream from the public URL
          const fileStream = new Promise((resolve, reject) => {
            https.get(fileUrl, (response) => {
              resolve(response);
            }).on('error', (error) => {
              reject(error);
            });
          });
      
          // 2. Create a FileWrapper with the file stream AND filename
          const fileWrapper = new FileWrapper(await fileStream); 
      
          // 3. Create the invoice attachment
          const response = await client.invoicesApi.createInvoiceAttachment(
            invoiceId,
            {
              idempotencyKey: require("crypto").randomBytes(22).toString("hex"),
              description: 'Service terms',
            },
            fileWrapper
          );
      
          console.log(response.result);
      
  
    } catch (error) {
      console.error(error);
    }
  };
  
  const fileUrl = 'https://firebasestorage.googleapis.com/v0/b/dt-translations.appspot.com/o/000000000000%2FService%20terms.pdf?alt=media&token=cebf7331-cb9d-42dd-a9ae-d96f16d8c294';
  await createSquareInvoiceAttachment(invoiceId, fileUrl);

Here’s the response I get:

{
  attachment: {
    id: 'inva:0-ChD8Ar3v9wyY_6u2XbB2P5s-',
    filename: 'Service terms.pdf?alt=media&token=cebf7331-cb9d-42dd-a9ae-d96f16d8c294',
    description: 'Service terms',
    filesize: 238896,
    hash: 'cdded65e5503a2bf94cb05328ef582d4',
    mimeType: 'application/pdf',
    uploadedAt: '2024-10-15T00:49:55Z'
  }
}

I have tried various solutions and troubleshooting steps for many hours but none of them worked. I even tried it with the data-form npm package and downloading the file to a temporary location on the server side to try to bypass the URL being used to derive the file name from. However, the above code is the only code that actually attaches the file to an invoice. The rest of the codes I tried did not attach the file. It’s simply missing with the other solutions I tried. I have consulted various sources including AI technologies like Gemini and Chat GPT but nothing worked. I am using Node JS, JS on the client-side, Firebase Cloud functions and other Firebase technologies.

I would appreciate some guidance on how I can resolve the issue of the file name including URL parameters.

Kind regards

Thank you.

Don

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1325

Trending Articles