This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
VGSShowModule.java
156 lines (135 loc) · 5.66 KB
/
VGSShowModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.verygoodsecurity.reactnative.show;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import android.app.Activity;
import android.content.Intent;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import com.verygoodsecurity.vgsshow.core.logs.VGSShowLogger;
import com.verygoodsecurity.vgsshow.VGSShow;
import com.verygoodsecurity.vgsshow.core.VGSEnvironment;
import com.verygoodsecurity.vgsshow.core.listener.VGSOnResponseListener;
import com.verygoodsecurity.vgsshow.core.network.model.VGSResponse;
import com.verygoodsecurity.vgsshow.core.network.client.VGSHttpMethod;
import com.verygoodsecurity.vgsshow.widget.core.VGSView;
import android.util.Log;
public class VGSShowModule extends ReactContextBaseJavaModule {
private static final String RESPONSE_EVENT_NAME = "VGSShowOnVGSResponse";
private static final String VAULT_ID = "tntpszqgikn";
private static final String MODULE_NAME = "VGSShow";
private static final String PATH = "/post";
private static ReactApplicationContext reactContext;
private VGSShow show;
VGSShowModule(ReactApplicationContext c) {
super(c);
reactContext = c;
}
@Override
public void initialize() {
super.initialize();
VGSShowLogger.INSTANCE.setLevel(VGSShowLogger.Level.DEBUG);
Activity activity = reactContext.getCurrentActivity();
show = new VGSShow.Builder(activity, VAULT_ID)
.setEnvironment(new VGSEnvironment.Sandbox())
.build();
initListeners();
}
public void subscribe(VGSView view) {
Log.e("test", "bindView "+view.getContentPath());
show.subscribe(view);
}
private void initListeners() {
show.addOnResponseListener(new VGSOnResponseListener() {
@Override
public void onResponse( VGSResponse response) {
// sendResponse(response);
Log.e("test", "submitAsync"+response.toString());
}
});
}
@Override
public String getName() {
return MODULE_NAME;
}
@ReactMethod
public void submitAsync(ReadableMap rnMap) {
String key11 = rnMap.hasKey("payment_card_number") ? rnMap.getString("payment_card_number") : "empty";
String key12 = rnMap.hasKey("payment_card_expiration_date") ? rnMap.getString("payment_card_expiration_date") : "empty";
Log.e("test", "key1: "+key11);
Log.e("test", "key2: "+key12);
Map<String, Object> map = recursivelyDeconstructReadableMap(rnMap);
convertWithIteration(map);
show.requestAsync(PATH, VGSHttpMethod.POST, map);
}
public void convertWithIteration(Map<String, ?> map) {
StringBuilder mapAsString = new StringBuilder();
for (String key : map.keySet()) {
mapAsString.append(key + "=" + map.get(key) + ", ");
}
Log.e("test", "map: "+mapAsString.toString());
}
private Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) {
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
Map<String, Object> deconstructedMap = new HashMap<>();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = readableMap.getType(key);
switch (type) {
case Null:
break;
case Boolean:
deconstructedMap.put(key, readableMap.getBoolean(key));
break;
case Number:
deconstructedMap.put(key, readableMap.getDouble(key));
break;
case String:
deconstructedMap.put(key, readableMap.getString(key));
break;
}
}
return deconstructedMap;
}
// private void sendResponse(VGSResponse response) {
// String responseStr = " ";
//
// if(response instanceof VGSResponse.SuccessResponse ) {
// parseNumberAlias(response);
// parseDateAlias(response);
//
// responseStr = "Code: " + ((VGSResponse.SuccessResponse) response).getSuccessCode();
// } else if(response instanceof VGSResponse.ErrorResponse ) {
// responseStr = new StringBuilder("Code: ")
// .append(response.getCode())
// .append(" \n ")
// .append(((VGSResponse.ErrorResponse) response).getLocalizeMessage())
// .toString();
// }
//
// this.getReactApplicationContext()
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit(RESPONSE_EVENT_NAME, responseStr);
//
// }
//
// private void parseNumberAlias(VGSResponse response) {
// String token = JSONObjectUtil.getValue(response.getBody(), "cardNumber");
// this.getReactApplicationContext()
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit("cardNumberToken", token);
// }
//
// private void parseDateAlias(VGSResponse response) {
// String token = JSONObjectUtil.getValue(response.getBody(), "expDate");
// this.getReactApplicationContext()
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit("expirationDateToken", token);
// }
}