Python: generate a dummy PayPal transaction ID for testing

In this article we will see how to generate fake PayPal transaction IDs with Python for testing purposes.

Let's imagine we have a test e-commerce database where each order is paid with PayPal and must have a transaction_id value set. Obviously we can't perform an API procedure using the PayPal sandbox for all the orders already in the dataset, so we have to generate dummy values to populate that field.

We can create the following function:

import random
import string

def create_txn_id():
    # Sandbox real example: '16827359LA7216114'
    first_part_len = 8
    middle_part_len = 2
    last_part_len = 7
    letters = string.ascii_uppercase
    digits = string.digits

    first = ''.join([random.choice(digits) for _ in range(first_part_len)])
    second = ''.join([random.choice(letters) for _ in range(middle_part_len)])
    third = ''.join([random.choice(digits) for _ in range(last_part_len)])
    return first + second + third

The result obtained will be very close to the real example for the overall length of the string and the distribution of the characters it contains.

Back to top