# Step 5 – Sample code
source: https://developer.mastercard.com/account-catalog-services/tutorial/build-end-to-end-app/step5/index.md

## Step 5 -- Sample code {#step-5--sample-code}

By the end of this tutorial, your `Main.java` file should look something like this:

```JAVA
package org.example; // change accordingly

import com.mastercard.acs.generated.apis.AccountCatalogServicesApi;
import com.mastercard.acs.generated.invokers.ApiClient;
import com.mastercard.acs.generated.models.AccountIndicator;
import com.mastercard.acs.generated.models.AccountLevelManagementServiceProperties;
import com.mastercard.acs.generated.models.AccountRegistration;
import com.mastercard.acs.generated.models.AccountRegistrationResult;
import com.mastercard.developer.encryption.JweConfig;
import com.mastercard.developer.encryption.JweConfigBuilder;
import com.mastercard.developer.interceptors.OkHttpEncryptionInterceptor;
import com.mastercard.developer.interceptors.OkHttpOAuth1Interceptor;
import com.mastercard.developer.utils.AuthenticationUtils;
import com.mastercard.developer.utils.EncryptionUtils;

import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.util.UUID;

public class Main {

    public static void main(String[] args) throws Exception {
        // change these values accordingly
        String consumerKey =
                "NfjqhLorGQC7oMZR2uA8UeW3B6j4MGFAYCh9DJOL79fe66b5!e7db68fce6a8402cbbdd1cd2beb497650000000000000000";
        String signingKeyFilePath =
                "src/main/resources/test-crypto-issue-sandbox-signing.p12";
        String signingKeyAlias = "mastercard123";
        String signingKeyPass = "mastercard123";
        String clientEncryptionCertPath =
                "src/main/resources/account-catalog-services-clientenc1731232241397-client-encryption-key.pem";
        String mastercardEncryptionKeyFilePath =
                "src/main/resources/account-catalog-services-mastercard123-mastercard-encryption-key.p12";
        String mastercardEncryptionAlias = "mastercard123";
        String mastercardEncryptionPass = "mastercard123";

        // Create generic apiClient with required configurations
        ApiClient apiClient =
                getApiClient(
                        consumerKey,
                        signingKeyFilePath,
                        signingKeyAlias,
                        signingKeyPass,
                        clientEncryptionCertPath,
                        mastercardEncryptionKeyFilePath,
                        mastercardEncryptionAlias,
                        mastercardEncryptionPass);

        // Example 1 - Register account details
        AccountRegistrationResult response = registerAccount(apiClient);

        if (response != null) {
            System.out.println(response.toJson());
        }

        System.exit(0);
    }

    public static ApiClient getApiClient(
            String consumerKey,
            String signingKeyFilePath,
            String signingKeyAlias,
            String signingKeyPass,
            String clientEncryptionCertPath,
            String mastercardEncryptionKeyFilePath,
            String mastercardEncryptionAlias,
            String mastercardEncryptionPass)
            throws Exception {

        // Step 1.1 - Load the signing key - Provided by the OAuth1 Signer lib
        PrivateKey signingKey =
                AuthenticationUtils.loadSigningKey(signingKeyFilePath, signingKeyAlias, signingKeyPass);

        // Step 1.2 -- Inject OAuth signing key and consumer key to interceptor
        OkHttpOAuth1Interceptor authInterceptor = new OkHttpOAuth1Interceptor(consumerKey, signingKey);

        // Step 2.1 - This will be the certificate used to encrypt the payload before sending
        Certificate encryptionCertificate =
                EncryptionUtils.loadEncryptionCertificate(clientEncryptionCertPath);
        // Step 2.2 - The response received from the call will need to be decrypted using this key

        PrivateKey decryptionKey =
                EncryptionUtils.loadDecryptionKey(
                        mastercardEncryptionKeyFilePath, mastercardEncryptionAlias, mastercardEncryptionPass);

        // Step 2.3 -- Prepare JWEConfig
        JweConfig jweConfig = JweConfigBuilder.aJweEncryptionConfig()
                .withEncryptionCertificate(encryptionCertificate)
                .withDecryptionKey(decryptionKey)
                .withDecryptionPath("$.encryptedValue", "$")
                .withEncryptionPath("$", "$")
                .withEncryptedValueFieldName("encryptedValue")
                .build();

        // Step 2.4 -- Inject JweConfig to interceptor
        OkHttpEncryptionInterceptor encryptionInterceptor = OkHttpEncryptionInterceptor.from(jweConfig);

        // Step 3 -- Create ApiClient
        ApiClient client = new ApiClient();

        // Step 3.1 -- Configure the Mastercard service URL
        client.setBasePath("https://sandbox.api.mastercard.com/asc/acs-api");

        // Step 3.2 -- Add the interceptors responsible for signing, encrypting, and decrypting HTTP
        // requests/responses
        client.setHttpClient(
                client
                        .getHttpClient()
                        .newBuilder()
                        .addInterceptor(encryptionInterceptor)
                        .addInterceptor(authInterceptor)
                        .build());

        // Step 3.3 - Enable or disable OK Http debugging - disable on UAT/PROD env.
        client.setDebugging(true);


        return client;
    }

    private static AccountRegistrationResult registerAccount(ApiClient apiClient) {
        // Example : register an account

        // The resulting AccountRegistrationResult object will contain the response information already decrypted and ready to use
        AccountRegistrationResult response = null;
        try {
            AccountCatalogServicesApi clientApi = new AccountCatalogServicesApi(apiClient);
            String correlationId = UUID.randomUUID().toString();
            AccountRegistration accountRegistration = new AccountRegistration();
            accountRegistration.setAccountIdentifier("2229580001400003"); // add correct Pan value
            accountRegistration.setAccountIndicator(AccountIndicator.PRIMARY_ACCOUNT_NUMBER);
            AccountLevelManagementServiceProperties almProperties = new AccountLevelManagementServiceProperties();
            
            
            almProperties.setRewardsProgramId("MCC001");
            almProperties.setAccountIdentifierStatus("A");
            accountRegistration.setAccountLevelManagementServiceProperties(almProperties);
            System.out.println("request payload in clear text->" + accountRegistration.toJson());
            // Make an API Call
            response = clientApi.registerAccount(correlationId, accountRegistration);

        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return response;
    }

}
```

Once the code is written, it can be executed using the `mvn exec:java -Dexec.mainClass="package.of.Main"` command.
Tip: To ensure you always execute the latest code and correct dependencies, use the following one-liner `mvn clean compile exec:java -Dexec.mainClass="package.of.Main"`. Note: For more information about test cases, refer to the [testing](https://developer.mastercard.com/account-catalog-services/documentation/testing/index.md).
