Android SDK

Installation


GoPay Tokenization SDK is available via Gradle on Android.


// Android installation via gradle
allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
implementation "com.midtrans:gopay-checkout:1.0.5"



Initialization


To start using the SDK, you need to initialize the installed libraries. You can provide the following mandatory parameters:


Parameter NameExplanation
merchant_idThis is your merchant ID. You can find this in your Midtrans dashboard
callback_urlThis is the callback URL that will redirect back after webview is invoked
merchant_server_urlThis is the address of the merchant server that you register to Midtrans

You'll also need to pass the Android context to initialize the client. Use the following script to initialize on Android.

For the callback_url, this parameter is needed to let the SDK go back to close the webview automatically. For example, your app URL scheme should be something like myapp://app, all lowercase.


//Android Initialization

GoPayCheckoutClient goPayCheckoutClient = new GoPayCheckoutClient(
    CONTEXT,
    MERCHANT_ID,
    CALLBACK_URL,
    MERCHANT_SERVER_URL,
    false // to enable logging
);

The following script can be used as an example for account linking on Android :


// use the goPayCheckoutClient object from initialization

// Account linking on Android
goPayCheckoutClient.linkAccount(
    ACTIVITY,
    new GoPayPartnerDetails(
        PHONE,
        COUNTRY_CODE
    ),
    new GoPayCheckoutCallback<AccountResponse>() {
        @Override
        public void onResponse(AccountResponse response) {
            // Handle response of account linking
            // For actual status merchant need to use get account status API
        }

        @Override
        public void onFailure(GoPayCheckoutError error, AccountResponse errorResponse) {
            // Handle error
        }
    },
    METADATA // Map<String, String> optional,
    "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="// authorization parameter, optional
);



Account Linking On Android


For account linking call GoPayCheckoutClient.linkAccount with parameter explained below


NameTypeExplanationRequired
activityObjectCaller activityRequired
goPayPartnerDetailsObjectContains partner informationRequired
goPayCheckoutCallbackObjectCallback delivering response or errorRequired
metadataObjectMap of String to String, put custom info for your usageOptional
authorizationStringString value for authorization header. Recommended to use at least Basic Auth.Optional

The GoPayPartnerDetails object has the following properties that you need to fill up


NameTypeExplanationRequired
phoneStringPhone number account to be linkedRequired
countryCodeStringCountry code related to accountRequired

The GoPayCheckoutCallback interface has methods that you need to implement


NameReturn TypeExplanationRequired
onResponseVoidCalled when linking receiving responseRequired
onFailureVoidCalled when linking process errorRequired

The AccountResponse object has the following properties that you will require in other APIs


NameTypeExplanation
accountIdStringAccount ID to be used enquire account status, create a transaction and disable the account
accountStatusStringCurrent account linking status. Possible values are ENABLED, PENDING, and DISABLED

The errorResponse object maps the error response from the Partner API. You can parse the message to identify the problem.


Account Status Enquiry On Android


The following script can be used as an example for account status enquiry on Android


//Account Status Enquiry On Android

goPayCheckoutClient.enquireAccount(
    ACCOUNT_ID,
    new GoPayCheckoutCallback<AccountResponse>() {
        @Override
        public void onResponse(AccountResponse response) {
            // Handle response of account enquiry
        }

        @Override
        public void onFailure(GoPayCheckoutError error, AccountResponse errorResponse) {
            // Handle error
        }
    },
    "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="// authorization parameter, optional
);

For account enquiring call GoPayCheckoutClient.enquireAccount with parameter explained below.


NameTypeExplanationRequired
accountIdStringGoPay account idRequired
goPayCheckoutCallbackObjectCallback delivering response or errorRequired
authorizationStringString value for authorization header. Recommended to use at least Basic Auth.Optional

The GoPayCheckoutCallback interface has methods that you need to implement


NameReturn TypeExplanationRequired
onResponseVoidCalled when enquiring account receiving responseRequired
onFailureVoidCalled when enquiring account process errorRequired

The AccountResponse object has the following properties that you will require in other APIs


NameTypeExplanation
accountIdStringAccount ID to be used enquire account status, create a transaction and disable an account
accountStatusStringCurrent account linking status. Possible values are ENABLED, PENDING and DISABLED
MetadataAccountMetadataList of available PaymentOption objects

The PaymentOption object has the following properties that you will require in other APIs


NameTypeExplanation
nameStringName of payment option
activeBooleanPayment option status. True indicates active
tokenStringPayment token to create transactions
balanceAmountAn object consisting of amount and currency

The errorResponse object maps the error response from the Partner API. You can parse the message to identify the problem.


Create Transaction On Android


The following script can be used as an example for creating transactions on Android


//### Create Transaction On Android

List<ItemDetail> items = new ArrayList<>();
items.add(
    new ItemDetail(
        UUID.randomUUID().toString(),
        "Beli barang bagus",
        12000L,
        1,
        null
    )
);

goPayCheckoutClient
    .createTransaction(
        Activity.this,
        new GoPayDetails(
            ACCOUNT_ID,
            PAYMENT_OPTION_TOKEN
        ),
        new TransactionDetails(
            ORDER_ID,
            GROSS_AMOUNT,
            "IDR" //CURRENCY
        ),
        PHONE_NUMBER,
        items,
        new GoPayCheckoutCallback<TransactionResponse>() {
            @Override
            public void onResponse(TransactionResponse response) {
               // Handle transaction finished
               // Result is available in the HTTP Notification not from SDK
            }
            @Override
            public void onFailure(GoPayCheckoutError error, TransactionResponse errorResponse) {
               // Handle error
            }
        },
    METADATA // Map<String, String> optional,
    "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="// authorization parameter, optional
);

For creating transaction call GoPayCheckoutClient.createTransaction with parameter explained below.


NameTypeExplanationRequired
goPayDetailsObjectContains account_id, payment_option_tokenRequired
transactionDetailsObjectContains order_id, gross_amount, and currencyRequired
phoneNumberStringFilled with phone numberRequired
itemDetailsObjectList of Items each contains id, name, price, quatity, categoryOptional
goPayCheckoutCallbackObjectCallback delivering response or errorRequired
metadataObjectMap of String to String, put custom info for your usageOptional
authorizationStringString value for authorization header. Recommended to use at least Basic Auth.Optional

The GoPayDetails object has the following properties


NameTypeExplanationRequired
accountIdStringGoPay accound idRequired
paymentOptionTokenStringCall enquire account to obtainRequired

The TransactionDetails object has the following properties


NameTypeExplanationRequired
orderIdStringOrder id of transactionRequired
grossAmountLongGross amount of transactionRequired
currencyStringCurrency of transactionRequired

The GoPayCheckoutCallback interface has methods that you need to implement


NameReturn TypeExplanationRequired
onResponseVoidCalled when creating transaction receiving responseRequired
onFailureVoidCalled when creating transaction process errorRequired

The TransactionResponse object has the following properties


NameTypeExplanation
transactionIdStringThe transaction ID recorded in the system.
transactionStatusStringThe transaction status. This will always return pending as the status is sent via HTTP Notification.
orderIDStringThe order ID set in the TransactionDetails passed into the create transaction API.
grossAmountStringThe transaction amount.
currencyStringThe currency of the transaction.
paymentTypeStringThe payment option type used in the transaction.
transactionTimeStringThe time of transaction.
fraudStatusStringThis is the fraud checking Status.

The errorResponse object maps the error response from the Partner API. You can parse the message to identify
the problem.


Disable Account On Android


The following script can be used as an example for disabling account on Android.


//Disable Account On Android
goPayCheckoutClient.disableAccount(
    ACCOUNT_ID,
    new GoPayCheckoutCallback<AccountResponse>() {
        @Override
        public void onResponse(AccountResponse response) {
            // Handle response of disable the account
        }

        @Override
        public void onFailure(GoPayCheckoutError error, AccountResponse errorResponse) {
            // Handle error
        }
    },
    METADATA // Map<String, String> optional,
    "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="// a new authorization parameter
);

For disabling account SDK will send following payload properties


NameTypeExplanationRequired
accountIdStringAccount id to be disabledRequired
goPayCheckoutCallbackObjectCallback delivering response or errorRequired
metadataObjectMap of String to String, put custom info for your usageOptional
authorizationStringString value for authorization header. Recommended to use at least Basic Auth.Optional

The GoPayCheckoutCallback interface has methods that you need to implement


NameReturn TypeExplanationRequired
onResponseVoidCalled when disabling account receiving responseRequired
onFailureVoidCalled when disabling account process errorRequired

The AccountResponse object has the following properties.


NameTypeExplanation
accountIdStringAccount ID similar to what you use to disable an account
accountStatusStringCurrent account linking status. The result will always return DISABLED

The errorResponse object maps the error response from the Partner API. You can parse the message to identify the problem.