# Java Core SDK
source: https://developer.mastercard.com/mastercard-merchant-presented-qr/documentation/device-sdks/java-core-sdk/index.md

The Java Core SDK can be used by:

* Originating Institutions and Wallet Providers to parse and verify QR code content for web applications and Android applications
* Receiving Institutions to generate QR codes for web applications and Android applications

## Download Libraries {#download-libraries}

* [pushpayment-core-sdk-2.1.1.jar](https://static.developer.mastercard.com/content/mastercard-merchant-presented-qr/sdk-files/pushpayment-core-sdk-2.1.1.jar)
* [pushpayment-core-sdk-2.1.1-javadoc.jar](https://static.developer.mastercard.com/content/mastercard-merchant-presented-qr/sdk-files/pushpayment-core-sdk-2.1.1-javadoc.jar)

## Adding Dependencies with Gradle {#adding-dependencies-with-gradle}

1. Copy the **pushpayment-core-sdk-2.1.1.jar** into your project libs folder.
2. Configure your build.gradle file to include the following:

* Gradle

```Gradle
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.zxing:core:3.3.0' //This is a java library for generating Bitmap Image
}
```

## Usage {#usage}

### Parse QR Code Content {#parse-qr-code-content}

This function can be used to parse QR code content. This function throws an exception when a QR code is not compliant with the EMVCo Merchant Presented QR specification.
* Java

```Java
private void parseQRCode(String code) {
    PushPaymentData qrcode;
    try {
        qrcode = Parser.parse(code);
        System.out.println(qrcode.dumpData());
    } catch (ConflictiveTagException e) {
        System.out.println("ConflictiveTagException : " + e);
    } catch (InvalidTagValueException e) {
        System.out.println("InvalidTagValueException : " + e);
    } catch (MissingTagException e) {
        System.out.println("MissingTagException : " + e);
    } catch (UnknownTagException e) {
        System.out.println("UnknownTagException : " + e);
    } catch (FormatException e) {
        System.out.println("FormatException : " + e);
    }
}
```

### Parse QR Code Content with Validation warnings {#parse-qr-code-content-with-validation-warnings}

Unlike the *parseQRCode* function above, this function will not throw an exception for InvalidTagValue, RFU Tag and Missing Tag. Instead it returns a PushPaymentData object with validation error when the scanned QR code is not compliant with the EMVCo Merchant Presented QR specification.
* Java

```Java
private void parseQRCodeWithWarnings(String code){
        try {
            PushPaymentData data = Parser.parseWithValidationWarnings(code);
            for (MPQRError error : data.validationErrors) {
                System.out.println("validation error : " + error.errorMessage);
                System.out.println("validation error tag : " + error.getErrorTag().getTag());
                System.out.println("validation error root tag : " + error.getErrorRootTag());
                System.out.println("validation error value : " + error.getErrorValue());
            }
        } catch (ConflictiveTagException e) {
            System.out.println("ConflictiveTagException : " + e);
        } catch (DuplicateTagException e) {
            System.out.println("InvalidTagValueException : " + e);
        } catch (UnknownTagException e) {
            System.out.println("UnknownTagException : " + e);
        } catch (FormatException e) {
            System.out.println("FormatException : " + e);
        }
    }
```

### Generate QR Code {#generate-qr-code}

This function can be used to generate a QR code.
* Java

```Java
public void generateQRCode(){
        //construct the PushPaymentData object
        PushPaymentData pushPaymentData = new PushPaymentData();

        try {
            // Payload format indicator
            pushPaymentData.setPayloadFormatIndicator("01");

            // Point of initiation method
            pushPaymentData.setValue("01", "12");

            //Merchant identifier (Amex) "12"
            pushPaymentData.setMerchantIdentifierAmex12("3400678934521469");

            //Merchant Identifier data 26-51
            String rootTag = "26";
            MAIData maiData = new MAIData(rootTag);
            maiData.setAID("AID0349509H");
            maiData.setValue("01", "PNS93484jf");
            maiData.setValue("02", "PNSDyn8494738");
            String str01 = (String) maiData.getValue("01");
            pushPaymentData.setDynamicMAIDTag(maiData);

            // Merchant category code
            pushPaymentData.setMerchantCategoryCode("1434");

            // Transaction currency code
            pushPaymentData.setTransactionCurrencyCode("156");

            // Transaction amount
            pushPaymentData.setValue("54","100.8");

            // Country code
            pushPaymentData.setCountryCode("CN");

            // Merchant name
            pushPaymentData.setMerchantName("BEST TRANSPORT");

            // Merchant city
            pushPaymentData.setMerchantCity("BEIJING");

            // Postal code
            pushPaymentData.setPostalCode("56748");

            // Additional data
            AdditionalData addData = new AdditionalData();
            addData.setStoreId("A6008");
            addData.setAdditionalDataRequest("ME");
            addData.setValue(AdditionalDataTag.TAG_00.getTag(), "73523647");
            addData.setMerchantTaxId("934538746AIUW");
            addData.setMerchantChannel("967");

            String rootSubTag = "50";
            UnrestrictedData additionalUnrestrictedData = new UnrestrictedData(rootSubTag);
            additionalUnrestrictedData.setAID("GUI123");
            additionalUnrestrictedData.setValue("01", "CONT");
            additionalUnrestrictedData.setValue("02", "DYN6");
            addData.setUnrestrictedDataForSubTag(rootSubTag, additionalUnrestrictedData);

            pushPaymentData.setAdditionalData(addData);

            // Language Data
            LanguageData langData = new LanguageData();
            langData.setLanguagePreference("ZH");
            langData.setAlternateMerchantCity("北京");
            langData.setAlternateMerchantName("最佳运输");
            pushPaymentData.setLanguageData(langData);

            // MasterCardData
            MasterCardData masterCardData = new MasterCardData();
            masterCardData.setAlias("testAlias123");
            masterCardData.setMAID("9384676784");
            masterCardData.setPFID("test@testing.com");
            masterCardData.setMarketSpecificAlias("AC92836723TT");
            pushPaymentData.setMasterCardData(masterCardData);

        } catch (Exception e) {
            System.out.println("Error occurred during creation of pushpaymentdata: " + e.getMessage());
        }

        try {
            String qrContent = pushPaymentData.generatePushPaymentString();
            Bitmap map = encodeToQrCode(qrContent);
            System.out.println("QR content : " + qrContent);
        } catch (Exception e) {
            System.out.println("Error occurred during generation of qr string: " + e.getMessage());
        }
    }

private Bitmap encodeToQrCode(String text) {
    QRCodeWriter writer = new QRCodeWriter();
    BitMatrix matrix;
    int width = 200;  //get desired QR code width
    int height = 200; //get desired QR code height

    try {
        Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");     //for allowing Chinese characters
        matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hintMap);

    } catch (WriterException ex) {
        ex.printStackTrace();
        return null;
    }

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
        }
    }

    return bmp;
}
```

### Get AdditionalData value {#get-additionaldata-value}

This function can be used to retrive the additional data tag value (TAG_62) from a QR code string.
* Java

```Java
private void getAdditionalDataValueFromQRString(String qrCode){
        try {
            String additionalData =  Parser.getAdditionalDataValueFromQRString(qrCode);
            System.out.println(additionalData);
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
```

