In my previous article, I discussed the general architecture of Virtual Account Payment.
If you take a look at the article, you'll see that I drew something, e.g., “Virtual Account Generator”. When I drafted it, I was thinking that I could probably share this concept, at least my experience with trying to improve a virtual account generator in one of the Payment Gateways that I was involved in building.
In this article, I will focus on that component, drawing particularly from my experience of how some concepts that I have seen are used to generate a virtual account number in the Indonesian payment ecosystem.
Intro!
One of my experiences building a payment system with the Virtual Account (VA) concept was when I was working on a payment gateway system in Singapore and Indonesia. I joined a team where most members were either newly assigned or hired to work in the payment domain. This team was responsible for managing payment systems that include Virtual Account payments, E-wallet Payments, and Direct Debit Payments. To simplify the example, I will refer to the payment gateway as “GatePay.”
At Gatepay, we face huge customer issues that cause engineers to become only technical operations support. It’s a nightmare. There are many SOPs and Runbooks to learn to solve common problems, especially those related to the Virtual Account payment method. Frustrated with this situation, we decided to overhaul the VA system, scale it, and grow the team.
During development, we learn many things, but one of the key takeaways is how to solve the VA number generation, which I will explain in this article.
The Virtual Account Concept
A virtual account is essentially like a bank account, but it is not stored persistently, unlike a real bank account. It doesn't have transaction records, it only receives payments. It may look like: 8123991200002342, then the user can send money to that account through any means, such as mobile banking, internet banking, ATM, etc.
As a payment gateway, GatePay has a direct connection to the bank. In this case, they act as the “reseller” of the VA to their customer. Usually, the bank provides a number and slots that GatePay can use in its payment gateway.
Regarding the VA itself, from the bank's perspective, they typically assign a few number prefixes as identifiers for their merchants (in this case, GatePay).

When a customer/merchant (any business that needs VA for its transaction) integrates with a bank, it receives an identifier, such as a 4-digit number or more extracted from a 16-digit sequence. The rule is that GatePay must always include this identifier at the beginning of the VA number they wish to create. Therefore, GatePay will have a 12-digit space that they can “resell” to their customers.
Since the bank only offers a limited number of slots for the Virtual Account, such as a 12-digit space within a 16-digit number, for B2C companies that directly interact with end users, the easiest way sometimes is to map a real-world identifier of the user, e.g., phone number, national ID card number, student ID, etc. For example, if using a phone number, a customer with the phone number 0822-7444-7444 might have the virtual account as 8123082274447444 (where 8123 is the GatePay identifier for the bank, and the phone number belongs to GatePay’s customer).
But, how about for B2B companies, e.g., a Payment Gateway, that sell the Virtual Account payment service? There are many payment gateways in Indonesia offering Virtual Accounts. Have you ever wondered how they ensure each of their customers can allocate a VA without conflicts between end-users?
Back to the GatePay story, this GatePay won’t have unlimited identifiers that can be used with the bank. Probably, the Bank will only give 1-5 slots that GatePay can resell, eg
8123xxxxxxxxxxxx
8124xxxxxxxxxxxx
8125xxxxxxxxxxxx
What does it mean? GatePay only has 3 pools that it can resell to its B2B customers. And you can imagine, each business may have thousands or even millions of end users. So GatePay must be able to support the VA generation for their business with these 3 pools.
For the sake of simplicity, I’m going to ignore anything related to the VA feature, eg, “Open/Close VA”. We will just focus on how GatePay can distribute the VA pool to its customers.
What we did at GatePay back then was also create yet another pool of identifiers that could be shared among all the B2B customers. It’s like this below.
This enables GatePay to have more B2B companies along with their respective slot numbers, similar to what the Bank did for GatePay.
When the slots are exhausted, we will always be able to request more slots from the Bank. For how long, maybe until even the bank is exhausted too.
VA Number Generation Methods!
There are many ways we can generate the numbers here. But in this blog post, I will mention three that I have applied to at least in a couple of payment companies in the past.
However, before explaining all the methods, I will share two use cases that GatePay faced previously, to simplify and make it more challenging.
GatePay allows its customers (B2B clients) to create a “custom VA”, where the customer (B2B client) provides the VA number during the VA creation process through the API. For example, an e-commerce, GreenShop, sends its own VA number for each payment for its customer, e.g., 8123991276582992, during the VA creation API call.
GatePay will handle the generation by itself.
Option #1: Naive Random Generation
A naive and straightforward solution is to use a generic math.random function, like in the diagram below.
When a request comes from a customer for a new VA number, the system will check if it contains a “custom” VA number generated by the customer or if no custom VA is sent. If a custom VA is provided, it will be stored directly in the database. If no custom VA is provided, it will generate a random number from (0…99999999) based on the pattern of the XXXXXXXX that we have previously:
After that, we will get a number, format it, and save it to the database. For example, let’s say from 0..99999999, we get 00002342. Then, we will format it in this format, as shown in our example above. The bank code is 8123, and the sub-code is 9912. <bank-code><sub_code><randomized number> → become: 8123991200002342
This approach seems simple and works for a while, but here’s the catch: the probability of a generated number colliding increases as more VA numbers are reserved. The more VA numbers we reserve, the longer it takes to create a new unique number, which will affect the SLO and SLA for the customers.
This affects larger, older customers or enterprise clients that have a high volume of transaction requests.
Option #2: Hierarchical Directory
Another solution that I learned during my career is to utilize the file-system lookup. The concept is that first, it will create a root directory for each bank identifier, e.g., 8123. Then, inside that identifier directory, there will be more sub-codes, with each sub-code assigned to each merchant. Inside each sub-code, there will be additional directories, e.g., 0000 (to store 0000 → 00000000 … 00009999), etc.
See the example below of how the numbers are generated.
8123/ # Bank identifier
└── 9912/ # Merchant/Sub Code
├── 0000/ # First 4 digits of 8-digit account number
│ ├── 0000/ # Last 4 digits (account 8123991200000000)
│ ├── 0001/ # Account 8123991200000001
│ ├── 2342/ # Account 8123991200002342
│ └── .../ # Other accounts in the 0000 range
├── 1234/ # First 4 digits "1234"
│ ├── 5678/ # Account 8123991212345678
│ ├── 0009/ # Account 8123991212340009
│ └── .../ # Other accounts in the 1234 range
├── 9000/ # First 4 digits "9000"
│ ├── 1001/ # Account 8123991290001001
│ └── .../ # Other accounts in the 9000 range
└── .../ # etcMy initial impression of this service is excellent. The performance is outstanding! It always ensures that the generated VA is unique and performs well. However, upon further reflection, I don’t think this will be scalable. It requires a persistent disk, not a database, but a directory or disk. Additionally, it’s running on a Virtual Machine (VM). This solution is acceptable if the company, or in this case GatePay, is willing to maintain dedicated VM instances.
Option #3: FPE Algorithm
Another approach I learned also happened when I was working at a payment company. Instead of randomizing the number, we can use an encryption method that preserves the format. This algorithm is commonly used in credit card numbers to encrypt 16-digit numbers, for example.
I quoted this from Wikipedia, describing what FPE is:
In cryptography, format-preserving encryption (FPE), refers to encrypting in such a way that the output (the ciphertext) is in the same format as the input (the plaintext). The meaning of "format" varies. Typically only finite sets of characters are used; numeric, alphabetic or alphanumeric. For example:
Encrypting a 16-digit credit card number so that the ciphertext is another 16-digit number.
Encrypting an English word so that the ciphertext is another English word.
Encrypting an n-bit number so that the ciphertext is another n-bit number (this is the definition of an n-bit block cipher).
For such finite domains, and for the purposes of the discussion below, the cipher is equivalent to a permutation of N integers {0, ... , N−1} where N is the size of the domain.
Since FPE is simply an encryption logic, we use it with an encrypted auto-increment number. For example, we might have an auto-increment range from 1 to 99999999. For each number, we format it into a string like `0000001`, `00000002`, and so on, then encrypt it with FPE. This process always produces a fixed, reversible, yet seemingly random number.
The secret is to keep the format the same in an 8-digit number but as a string. Then, encrypt it. After that, we will get an encrypted number and combine it with the Bank Identifier and Sub Code, e.g., 8123991291237727.
The performance is decent compared to the hierarchical directory solution, but it’s still superior to using a random number. This solution prevents collisions because it can be decrypted. For example, if we don’t know or have lost the last sequence, we can always decrypt it and retrieve the most recent sequence.
And, of course, with the new algorithm, we can scale our application more aggressively without fear of failure due to a corrupted disk or collision probability.
An additional step in this solution is handling a custom request for a VA number from the customer because we need to reserve it. We can solve this with some simple logic that stores a sequence pointer in the database.
First, the incoming VA number from the customer will be decrypted to obtain the sequence number. Let's say a VA number from the customer is: 8123991281338626. As per the format defined above, we will extract the number excluding the bank identifier and sub-code, resulting in 81338626, and then call the FPE decrypt function decrypt(81338626). For example, we get 00000012. From this, we can parse it into a number and determine that 12, which means sequence number 12 is already reserved.
Using the combination of sequencePointer and reserved sequence number, we can easily implement auto-increment. If the reserved number matches, we move to the following sequence. It always increments with each new VA number generated and stored, but it won’t increase if any VA reservation request is made. To illustrate the logic, consider the step below.
But of course, the core problem still exists if not handled carefully. Let’s say we have the sequence pointer at 10, and somehow, from 11 to 9999, it has already been reserved by the customer. That means we still need to increment up to the 10,000th sequence. This could be problematic if it involves a database query, etc.
At this point, designing a distributed system is necessary to ensure that slot numbers are fully occupied at 100%. Of course, with a predicted incremental number, we can accomplish many things, such as using a cron job that pre-calculates the VA that can be used for the next VA creation.
Conclusion
There are many ways to solve a problem; some solutions are easy, while others are too advanced and add complexity. But regardless, it's always fun to learn about new problems we can solve. I worked in the payment industry for five years, and it has already taught me a great deal.
Based on my experience, I think the best solution in terms of performance is option #2, but it has a tradeoff that is a bit hard to accept, especially for me, since it must run in a VM.
But following the same logic, I think we could potentially move the logic from a persistent hard disk to a database that can be managed with a standard database approach. If I have time, I might spend more time exploring that area. The second-best solution is the FPE algorithm. I believe this could be safer since it’s similar to the algorithm used in generating credit card numbers.
However, if the timeline is tight and we need to launch an MVP within a week, then a simple, generic randomize function should suffice. I’m not an algorithm expert; I’m a builder with a customer-first mindset. Sometimes, it doesn’t have to be the best, just something that can cover the costs until we have more customers to pay us to improve it.
These notes were written in collaboration between Iman Tumorang (Staff Engineer at Veriff.com) and Yong Cheng (Principal Engineer at Govtech.sg) to explain how the Virtual account payment technology works behind the scenes. Also, we’re open to fractional / consultant roles across payments/fintech, platform engineering, or general technology strategy. Contact us at softwarearchitect.id for inquiries or to find architects/experts who match your problem/business requirements.










