Submit an invoice for a Delivery by Amazon order
Learn how to submit an invoice for a Delivery by Amazon order.
Learn how to submit an invoice for a Delivery by Amazon order.
❗Important
The
submitInvoice
operation is in Restricted Availability (RA) status and only available in the Brazil (BR) marketplace.
Prerequisites
To complete this tutorial, you need:
- Authorization from the selling partner for whom you are making calls. For more information, refer to Authorizing Selling Partner API applications.
- The Tax Invoicing (Restricted) role assigned to your developer profile.
- The Tax Invoicing (Restricted) role selected in the app registration page for your application.
- The XML file from an invoice approved on SEFAZ.
- Integrated development environment (IDE) software
Step 1. Convert the XML file to Base64 format
To convert the XML file into Base64 format, use the following Java sample code. The Java sample code takes a single input parameter xmlPath
, which represents the file path, and returns the file's contents encoded in Base64 format. You need the Base64-encoded invoice for Step 3. Submit the invoice.
Sample Java code
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.io.IOException;
public class XMLtoBase64 {
public static String encodeXMLToBase64(String xmlPath) throws IOException {
byte[] xmlBytes = Files.readAllBytes(Paths.get(xmlPath));
return Base64.getEncoder().encodeToString(xmlBytes);
}
}
Step 2. Get the invoice's MD5 hash
To get the invoice's MD5 hash, use the following sample Java code. The Java sample code takes a single input parameter filePath
, which represents the path to the file for which you must calculate the MD5 hash. The code returns a string that contains the Base64-encoded MD5 hash of the file's contents. You need this value for Step 3. Submit the invoice.
Sample Java code
package com.util.hash;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HashGenerator {
private static final Logger LOGGER = Logger.getLogger(HashGenerator.class.getName());
public static String computeMD5HashFromFile(String filePath) {
try (FileInputStream fis = new FileInputStream(filePath)) {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
md.update(buffer, 0, bytesRead);
}
return Base64.encodeBase64String(md.digest());
} catch (IOException | NoSuchAlgorithmException e) {
LOGGER.log(Level.SEVERE, "Error computing MD5 hash for file: " + filePath, e);
throw new RuntimeException("Failed to compute MD5 hash", e);
}
}
}
Step 3. Submit the invoice
Call the submitInvoice
operation. Specify the orderId
, MarketplaceId
, invoiceType
, programType
, invoiceContent
(set to the Base64-encoded invoice) and contentMD5Value
(set to the MD5 hash).
Updated about 3 hours ago