DEV Community

Felipe Jansen
Felipe Jansen

Posted on

How we can create a Pix Qrcode using Java ☕ and Spring 🍃

Hey everyone! Good morning, good afternoon, and good evening!!! Let’s talk about money! #letstalkaboutmoney

In this post, I'll show you how to create a Pix code and then generate a QR code from it. Let’s dive in! 🚀📲

Image description

Creating a Pix code is pretty straightforward, but it’s worth checking out BACEN’s documentation. Since they’re behind Pix, it’s a good idea to follow their guidelines. 😉📋

For that, here’s the guide we’ll be using: https://www.bcb.gov.br/content/estabilidadefinanceira/pix/Regulamento_Pix/II_ManualdePadroesparaIniciacaodoPix.pdf. Check it out! 📚

If you’re panicking about your feature’s deadline and can’t squeeze in time for the documentation, don’t worry—I’ll break down the key points for you right here… 😉

Image description

Below is an example of a Pix code generated from the table above. If you look closely, you'll see it's pretty straightforward—just follow the info from the table. Also, keep in mind that the most likely things to change are the Pix key and the amount.

Image description

Without further ado, let’s get to the point.

The format is ID + Tam + Valor, and we’ll be concatenating the information. Additionally, some fields require you to include both the information and the string length. This applies specifically to Merchant Account Information, Transaction Currency, and Merchant City.

PS.: The details that change most often are the key (which is the Pix key itself), Transaction Currency, Merchant Name, and Merchant City. You’ll need to replace these with the Pix key, the amount, the name of the recipient, and the recipient's city.

PS. 2: After completing the procedure, we need to make sure the code is valid. To do this, we’ll run a Cyclic Redundancy Check (CRC) on it (https://en.wikipedia.org/wiki/Cyclic_redundancy_check) and append the generated CRC code to the end of the Pix code. This will validate the Pix code. The Java code for this is provided below.

PS 3 .: The txid field refers to the Pix description field—the message field provided by BACEN that lets us send messages with our Pix transactions.

private static String CRC16CCITT(String payload) {
        int crc = 0xFFFF;          // initial value
        int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12)

        byte[] bytes = payload.getBytes(Charset.defaultCharset());

        for (byte b : bytes) {
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b   >> (7-i) & 1) == 1);
                boolean c15 = ((crc >> 15    & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit) crc ^= polynomial;
            }
        }

        crc &= 0xffff;
        return Integer.toHexString(crc);
    }
Enter fullscreen mode Exit fullscreen mode

That’s it—this is how you create a Pix code. To generate the QR code, just take the text and plug it into your preferred QR code generator. I like using QR Code Generator.

Image description

Useful Links:

Follow me on Linkedin for more: https://www.linkedin.com/in/felipe-neiva-jansen/

Top comments (0)