Observing States and Submitting Data

Get all states

Return all states of views at the current moment

public List<FieldState> getAllStates()

Returns
List Returns list with all fields states.
private fun getData() {
    val states = vgsForm.getAllStates()
}
private void getData() {
    List<FieldState> states = vgsForm.getAllStates();
}

Fields state tracking

When an object of this type is attached to an VSG secure fields, its methods will be called when the text or focus are changed.

public static interface OnFieldStateChangeListener

Public methods
abstract void onStateChange(FieldState state)

Public methods

public abstract void onStateChange(FieldState state)

Parameter
FieldState Represents secure field current state.
vgsForm.addOnFieldStateChangeListener(object : OnFieldStateChangeListener {
    override fun onStateChange(state: FieldState) {
        when(state) {
            is FieldState.CardNumberState -> { }
            is FieldState.CVCState -> { }
            is FieldState.CardExpirationDateState -> { }
            is FieldState.CardHolderNameState -> { }
        }
    }
})
vgsForm.addOnFieldStateChangeListener(new OnFieldStateChangeListener() {
    @Override
    public void onStateChange(@NotNull FieldState state) {
        if(state instanceof FieldState.CardNumberState) {

        } else if(state instanceof FieldState.CVCState) {

        } else if(state instanceof FieldState.CardExpirationDateState) {

        } else if(state instanceof FieldState.CardHolderNameState) {

        }
    }
});

Custom data

Custom headers. There is an option to send your custom headers:

 private fun submitData() {
    val headers = HashMap<String, String>()
    headers["customHeader"] = "value"
    vgsForm.setCustomHeaders(headers)

    vgsForm.asyncSubmit(this@MainActivity, "/post", HTTPMethod.POST)
}
private void submitData() {
    HashMap headers = new HashMap<String, String>();
    headers.put("customHeader", "value");
    vgsForm.setCustomHeaders(headers);

    vgsForm.asyncSubmit(this, "/post", HTTPMethod.POST);
}

Custom fields. More to the point SDK allows send custom fields in the same request that the SDK CTA sends:

private fun submitData() {
    val data = HashMap<String, String>()
    data["nonSDKValue"] = "value"
    vgsForm.setCustomData(data)

    vgsForm.asyncSubmit(this@MainActivity, "/post", HTTPMethod.POST)
}
private void submitData() {
    HashMap data = new HashMap<String, String>();
    data.put("nonSDKValue", "value");
    vgsForm.setCustomData(data);

    vgsForm.asyncSubmit(this, "/post", HTTPMethod.POST);
}

Reset custom data. To clear all custom headers use resetCustomHeaders or resetCustomData to clear custom fields added before.

private fun submitData() {
    vgsForm.resetCustomHeaders()
    val headers = HashMap<String, String>()
    headers["customHeader"] = "value"
    vgsForm.setCustomHeaders(headers)

    vgsForm.resetCustomData()
    val data = HashMap<String, String>()
    data["nonSDKValue"] = "value"
    vgsForm.setCustomData(data)

    vgsForm.asyncSubmit(this@MainActivity, "/post", HTTPMethod.POST)
}
private void submitData() {
    vgsForm.resetCustomHeaders()
    HashMap headers = new HashMap<String, String>();
    headers.put("customHeader", "value");
    vgsForm.setCustomHeaders(headers);

    vgsForm.resetCustomData()
    HashMap data = new HashMap<String, String>();
    data.put("nonSDKValue", "value");
    vgsForm.setCustomData(data);

    vgsForm.asyncSubmit(this, "/post", HTTPMethod.POST);
}

Getting a Result from an external Activity

You can also start another activity and receive a result back. To receive a result, call startActivityForResult().

For example, your app can start a camera app and receive the credit card number as a result.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    vgsForm.onActivityResult(requestCode, resultCode, data)
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    vgsForm.onActivityResult(requestCode, resultCode, data);
}

Submit information

Call asyncSubmit to execute and send data on VGS Server.

public void asyncSubmit(Activity activity, String path, HTTPMethod method)

Parameter
activity Activity: Your user Activity.
path String: The resource identifies the host that the web client wants to access
method Enum: Http method which is used to request on Server.
private fun submitData() {
    vgsForm.asyncSubmit(this, "/post", HTTPMethod.POST)
}
private void submitData() {
    vgsForm.asyncSubmit(this, "/post", HTTPMethod.POST);
}

Call submit to execute and send data on VGS Server if you want to handle multithreading by yourself.

Warning

Do not use submit on the UI thread as this will crash.

public void submit(Activity activity, String path, HTTPMethod method)

Parameter
activity Activity: Your user Activity.
path String: The resource identifies the host that the web client wants to access
method Enum: Http method which is used to request on Server.
private fun submitData() {
    vgsForm.submit(this, "/post", HTTPMethod.POST)
}
private void submitData() {
    vgsForm.submit(this, "/post", HTTPMethod.POST);
}

Handling responses

Interface definition for a callback to be invoked when VGSCollect receive response from Server.

public static interface VgsCollectResponseListener

Public methods
abstract void onResponse( VGSResponse response )

Public methods

public abstract void onResponse(VGSResponse response)

Parameter
VGSResponse Represents response from Server.
vgsForm.addOnResponseListeners(object : VgsCollectResponseListener {
    override fun onResponse(response: VGSResponse?) {
        when(response) {
            is VGSResponse.SuccessResponse -> {}
            is VGSResponse.ErrorResponse -> {}
        }
    }
})
vgsForm.addOnResponseListeners(new VgsCollectResponseListener() {
    @Override
    public void onResponse(VGSResponse response) {
        if(response instanceof VGSResponse.SuccessResponse) {

        } else {

        }
    }
});

End session

Clear all information collected before by VGSCollect. Call onDestroy inside Android onDestroy callback.

override fun onDestroy() {
    vgsForm.onDestroy();
    super.onDestroy();
}
@Override
protected void onDestroy() {
    vgsForm.onDestroy()
    super.onDestroy()
}