Skip to content

Core Flow

suryagsaputra edited this page Nov 15, 2018 · 31 revisions

Core Flow Implementation Guide

Installation

Add midtrans bintray repository in your build.gradle.

Sandbox Dependencies

compile 'com.midtrans.corekit:$VERSION-SANDBOX'

Production Dependencies

compile 'com.midtrans.corekit:$VERSION'

Initialise SDK

This can be implemented in application or your main activity class.

SdkCoreFlowBuilder.init(CONTEXT, CLIENT_KEY, BASE_URL)
    .enableLog(true)
    .buildSDK();

Note:

  • CONTEXT is application or activity context
  • CLIENT_KEY is Veritrans Client Key (from MAP)
  • BASE_URL is merchant server URL.
    • For merchant server implementation please see this page.

To get SDK instance you can use this:

MidtransSDK midtransSDK = MidtransSDK.getInstance();

Initialize Transaction Request

Before using the SDK to pay, you must set the transaction details into the SDK instance.

Transaction Details (Required)

TRANSACTION_ID and TOTAL_AMOUNT was required to create a transaction request.

TransactionRequest transactionRequest = new TransactionRequest(TRANSACTION_ID, TOTAL_AMOUNT);
Customer Details (Required)

Customer Details was optional when creating transaction request.

CustomerDetails customer = new CustomerDetails(FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER);

transactionRequest.setCustomerDetails(customer);

Note: This was assumed that you have created transactionRequest object using required parameters.

Item Details (Optional)

ItemDetails class holds information about item purchased by user. TransactionRequest takes an array list of item details.

ItemDetails itemDetails1 = new ItemDetails(ITEM_ID_1, ITEM_PRICE_1, ITEM_QUANTITY_1, ITEM_NAME_1);
ItemDetails itemDetails2 = new ItemDetails(ITEM_ID_2, ITEM_PRICE_2, ITEM_QUANTITY_2, ITEM_NAME_2);

// Create array list and add above item details in it and then set it to transaction request.
ArrayList<ItemDetails> itemDetailsList = new ArrayList<>();
itemDetailsList.add(itemDetails1);
itemDetailsList.add(itemdetails2);

// Set item details into the transaction request.
transactionRequest.setItemDetails(itemDetailsList);

Note:

  • This was assumed that you have created transactionRequest object using required parameters.
  • ITEM_NAME maximum character length is 50.
Billing Address (Optional)

BillingAddress class holds information about billing. TransactionRequest takes an array list of billing details.

BillingAddress billingAddress1 = new BillingAddress(FIRST_NAME_1, LAST_NAME_1, ADDRESS_1, CITY_1, POSTAL_CODE_1, PHONE_NUMBER_1, COUNTRY_CODE_1);

BillingAddress billingAddress2 =new BillingAddress(FIRST_NAME_2, LAST_NAME_2, ADDRESS_2, CITY_2, POSTAL_CODE_2, PHONE_NUMBER_2, COUNTRY_CODE_2);

// Create array list and add above billing details in it and then set it to transaction request.
ArrayList<BillingAddress> billingAddressList = new ArrayList<>();
billingAddressList.add(billingAddress1);
billingAddressList.add(billingAddress2);

// Set billing address list into transaction request
transactionRequest.setBillingAddressArrayList(billingAddressList);

Note: This was assumed that you have created transactionRequest object using required parameters.

Shipping Address (Optional)

ShippingAddress class holds information about shipping address. TransactionRequest takes an array list of shipping details.

ShippingAddress shippingAddress1 = new ShippingAddress(FIRST_NAME_1, LAST_NAME_1, ADDRESS_1, CITY_1, POSTAL_CODE_1, PHONE_NUMBER_1, COUNTRY_CODE_1);

ShippingAddress shippingAddress2 =new ShippingAddress(FIRST_NAME_2, LAST_NAME_2, ADDRESS_2, CITY_2, POSTAL_CODE_2, PHONE_NUMBER_2, COUNTRY_CODE_2);

// Create array list and add above shipping details in it and then set it to transaction request.
ArrayList<BillingAddress> shippingAddressList = new ArrayList<>();
shippingAddressList.add(shippingAddress1);
shippingAddressList.add(shippingAddress1);

// Set shipping address list into transaction request.
transactionRequest.setShippingAddressArrayList(shippingAddressList);
Bill Info (Optional)

BillInfoModel class holds information about billing information that will be shown at billing details.

BillInfoModel billInfoModel = new BillInfoModel(BILL_INFO_KEY, BILL_INFO_VALUE);
// Set the bill info on transaction details
transactionRequest.setBillInfoModel(billInfoModel);

Set Transaction Request into SDK Instance

After creating transaction request, you must set it into SDK instance.

MidtransSDK.getInstance().setTransactionRequest(transactionRequest);

Note: transactionRequest is TransactionRequest object created in previous step.

Checkout Transaction

This is starting point when doing a payment. Basically, you just need to call checkout from SDK instance and pass a CheckoutCallback parameter.

MidtransSDK.getInstance().checkout(new CheckoutCallback() {
    @Override
    public void onSuccess(Token token) {
        // Checkout token will be used to charge the transaction later
        String checkoutToken = token.getTokenId();
        // Action when succeded
    }

    @Override
    public void onFailure(Token token, String reason) {
        // Action when failed
    }

    @Override
    public void onError(Throwable error) {
        // Action when error
    }
});

If you're using two click or one click transaction, you need to provide USER_ID.

MidtransSDK.getInstance().checkout(USER_ID, new CheckoutCallback() {
    @Override
    public void onSuccess(Token token) {
        // Checkout token will be used to charge the transaction later
        String checkoutToken = token.getTokenId();
        // Action when succeded
    }

    @Override
    public void onFailure(Token token, String reason) {
        // Action when failed
    }

    @Override
    public void onError(Throwable error) {
        // Action when error
    }
});

Get Enabled Transaction

After done the checkout, you can get Transaction details including enabled transaction options by using getTransactionOptions from SDK instance.

This transaction options will be helpful if you're using custom UI to show all enabled payments from MAP.

MidtransSDK.getInstance().getTransactionOptions(CHECKOUT_TOKEN, new TransactionOptionsCallback() {
    @Override
    public void onSuccess(Transaction transaction) {
        // List of enabled payment method string
           List<EnabledPayment> enabledMethods = transaction.getEnabledPayments();
    }

    @Override
   public void onFailure(Transaction transaction, String reason) {
   }

    @Override
   public void onError(Throwable error) {
   }
});

Table of payment codes returned from getEnabledPayments

Payment Code Payment Name
credit_card Credit Card
bca_va Bank Transfer BCA
echannel Mandiri Bill
bni_va Bank Transfer BNI
permata_va Bank Transfer Permata
other_va Bank Transfer All Bank
gopay Gopay
bca_klikbca KlikBCA
bca_klikpay BCA Klikpay
mandiri_clickpay Mandiri Clickpay
cimb_clicks CIMB Clicks
danamon_online Bank Transfer Danamon
bri_epay BRI E-Pay
mandiri_ecash Mandiri E-Cash
telkomsel_cash Telkomsel Cash
indomaret Indomaret
akulaku Akulaku

Charge Transaction

To charge a transaction, you need a checkout token to identify which transaction will be paid and payment parameters that is unique to each payment methods.

Clone this wiki locally