Skip to content

Commit

Permalink
POINT-229: Android SDK - Fill in device data automatically (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-koreniuk-cko authored Apr 25, 2022
1 parent 3224e0a commit d1e5d43
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import androidx.annotation.NonNull;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -24,7 +27,9 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
Expand All @@ -33,7 +38,7 @@

public class ProcessOut {

public static final String SDK_VERSION = "v2.16.0";
public static final String SDK_VERSION = "v2.17.0";

private String projectId;
private Context context;
Expand Down Expand Up @@ -69,10 +74,14 @@ public void tokenize(@NonNull Card card, JSONObject metadata, @NonNull final Tok

private void tokenizeBase(@NonNull Card card, JSONObject metadata, @NonNull final TokenCallback callback) {
JSONObject body = null;

try {
body = new JSONObject(gson.toJson(card));
if (metadata != null)
body.put("metadata", metadata);

body.put("device", new JSONObject(getDeviceInfo()));

Network.getInstance(this.context, this.projectId).CallProcessOut("/cards", Request.Method.POST, body, new Network.NetworkResult() {
@Override
public void onError(Exception error) {
Expand All @@ -94,6 +103,30 @@ public void onSuccess(JSONObject json) {
}
}

private Map<String, Object> getDeviceInfo() {
Map<String, Object> deviceInfo = new HashMap<>();

Calendar calendar = Calendar.getInstance();
int tzOffsetMin = -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))/(1000*60);

DisplayMetrics metrics = this.context.getResources().getDisplayMetrics();

Locale locale = this.context.getResources().getConfiguration().locale;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
deviceInfo.put("app_language", locale.toLanguageTag());
} else {
deviceInfo.put("app_language", locale.getLanguage());
}

deviceInfo.put("app_language", locale.getLanguage());
deviceInfo.put("app_screen_width", metrics.widthPixels);
deviceInfo.put("app_screen_height", metrics.heightPixels);
deviceInfo.put("app_timezone_offset", tzOffsetMin);

return deviceInfo;
}

/**
* Updates the CVC of a previously stored card
*
Expand All @@ -105,6 +138,9 @@ public void updateCvc(@NonNull Card card, @NonNull final CvcUpdateCallback callb

try {
body = new JSONObject(gson.toJson(card));

body.put("device", new JSONObject(getDeviceInfo()));

Network.getInstance(
this.context, this.projectId).CallProcessOut(
"/cards/" + card.getId(),
Expand Down Expand Up @@ -482,6 +518,7 @@ public void makeCardToken(@NonNull final String source, @NonNull final String cu
try {
TokenRequest request = new TokenRequest(source);
final JSONObject body = new JSONObject(gson.toJson(request));
body.put("device", new JSONObject(getDeviceInfo()));

Network.getInstance(this.context, this.projectId).CallProcessOut("/customers/" + customerId + "/tokens/" + tokenId, Request.Method.PUT, body, new Network.NetworkResult() {
@Override
Expand Down Expand Up @@ -666,7 +703,8 @@ private String generateAdditionalDataString(@NonNull Map<String, String> additio
* @param body the request body
* @param callback callback for handling customer action
*/
private void requestAuthorization(@NonNull final String invoiceId, @NonNull final JSONObject body, @NonNull final RequestAuthorizationCallback callback) {
private void requestAuthorization(@NonNull final String invoiceId, @NonNull final JSONObject body, @NonNull final RequestAuthorizationCallback callback) throws JSONException {
body.put("device", new JSONObject(getDeviceInfo()));
Network.getInstance(this.context /* Using the same context as other network calls */, this.projectId).CallProcessOut(
"/invoices/" + invoiceId + "/authorize", Request.Method.POST,
body, new Network.NetworkResult() {
Expand Down

0 comments on commit d1e5d43

Please sign in to comment.