Monday, December 30, 2013

BPAY payments / Customer Reference Number (CRN) generation - Luhn algorithm, mod 10 version 5 (MOD10V05)

1. BPAY payments

BPAY has become quite a comfortable and an easy online payment solution for Australian customers, more and more applications provide this option to be used. In order to start accepting BPAY payments in your application or website - you just need to make a few steps, like:
  • Register as the BPAY Biller at http://www.bpay.com.au/ -> you'll get your BPAY Biller code then;
  • When your customer is willing to make a BPAY transfer - your application should generate a Customer Reference Number (CRN) programmatically (see below), or you can even generate valid CRNs manually using CRN Generator Tool (win/mac desktop app) - anyway, each CRN generated for a User can be used by him further on, although some applications/services do prefer to generate a new CRN for each new Order a User makes - both approaches are quite fine;
  • Then you should provide this pair of values to such a Customer:
    • your BPAY Biller code;
    • his CRN generated/assigned to him;
  • Then he makes a BPAY payment using these credentials - using various online banking interfaces or even mobile apps that support BPAY option;
  • You accept it on your end, then you just need to check the CRN value mentioned inside the payment details -> identify your Customer -> process the corresponding Order.

2. Customer Reference Number (CRN) generation

CRN is an identifier of a Customer within the BPAY Biller's customer base. This is a number of certain length (that is set up in advance by Biller when he gets registered), consisting of the following two parts:

  • CRN "seed" - certain numeric value that you choose, representing your Customer or an Order document inside your database. It can be your Customer or Order ID, but we recommend to avoid exposing your internal autoincremented IDs, instead - some randomized public IDs (of certain length, e.g. 12 digits) would better be used. So, for example, such a 12digits-length number is taken for this part.
  • and a Check digit has to be calculated and appended to the value above. For BPAY CRNs specifically this Check digit gets calculated based on the value above - using the Luhn algorithm, mod 10 version 5 (MOD10V05):


$seed_length = strlen($seed);
$total = 0;

// Calculate the sum of the products of "seed"'s digits with their 1-based array position numbers, accordingly
for($i = 0; $i < $seed_length; $i++) $total += $seed{$i} * ($i + 1);

// The check digit to be found - would be the the sum above mod 10
$check_digit = fmod($total, 10);

// Resulting CRN: concatenation of the "seed" value with the Check digit calculated above
$result = $seed. $check_digit;