All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kh.gov.nbc.bakong_khqr.utils.BakongKHQRUtils Maven / Gradle / Ivy

Go to download

The standardization of KHQR code specifications will help promote wider use of mobile retail payments in Cambodia and provide consistent user experience for merchants and consumers. It can enable interoperability in the payment industry. A common QR code would facilitate payments among different schemes, e-wallets and banks and would encourage small merchants to adopt KHQR code as payment method. KHQR is created for retail or remittance in Cambodia and Cross-Border. It only requires a single QR for receiving transactions from any payment provider through Bakong including Bakong App.

The newest version!
package kh.gov.nbc.bakong_khqr.utils;

import kh.gov.nbc.bakong_khqr.core.CRC;

import java.nio.charset.StandardCharsets;

public class BakongKHQRUtils {
    public static String getChecksumResult(String dataPayload) {
        final byte[] buffer = dataPayload.getBytes();

        /* Note the change here */
        int crc = 0xFFFF;
        for (byte b : buffer) {
            crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
            crc ^= (b & 0xff);//byte to int, trunc sign
            crc ^= ((crc & 0xff) >> 4);
            crc ^= (crc << 12) & 0xffff;
            crc ^= ((crc & 0xFF) << 5) & 0xffff;
        }
        crc &= 0xffff;
        return String.format("%4s", Integer.toHexString(crc).toUpperCase()).replace(' ', '0');
    }

    public static String ofNullable(Object object) {
        if (object == null) {
            return StringUtils.EMPTY;
        } else return object.toString();
    }

    public static String getCRC(String data) {
        //crc tag and length(6304)
        final int crc16 = CRC.crc16((data + "6304").getBytes(StandardCharsets.UTF_8));

        return String.format("%04X", crc16);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy