@controleonline/react-native-cielo-payment 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ plugins {
2
+ id 'com.android.library'
3
+ }
4
+
5
+ android {
6
+ namespace 'com.controleonline.pos.cielo.payment'
7
+ }
8
+
9
+ dependencies {
10
+ implementation 'com.facebook.react:react-native:+'
11
+ }
@@ -0,0 +1,2 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.controleonline.pos.cielo.payment"/>
@@ -0,0 +1,194 @@
1
+ package com.controleonline.pos.cielo.payment;
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext;
4
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
5
+ import com.facebook.react.bridge.ReactMethod;
6
+
7
+ import com.facebook.react.bridge.Promise;
8
+ import com.facebook.react.bridge.Arguments;
9
+ import com.facebook.react.bridge.WritableMap;
10
+ import com.facebook.react.bridge.ActivityEventListener;
11
+ import com.facebook.react.bridge.BaseActivityEventListener;
12
+
13
+ import android.app.Activity;
14
+ import android.content.Context;
15
+ import android.content.Intent;
16
+ import android.net.Uri;
17
+ import android.util.Base64;
18
+
19
+ import androidx.annotation.NonNull;
20
+ import androidx.annotation.Nullable;
21
+
22
+ import java.nio.charset.StandardCharsets;
23
+ import org.json.JSONObject;
24
+
25
+ public class Cielo extends ReactContextBaseJavaModule {
26
+ private static final String CALLBACK = "ControleOnline://POS";
27
+ private static final String SCHEME = "lio";
28
+ private static final String PAYMENT = "payment";
29
+ private static final String PRINT = "print";
30
+ private static final String CANCEL = "payment-reversal";
31
+
32
+ private Promise mPromise;
33
+ private ReactApplicationContext mContext;
34
+
35
+ public Cielo(ReactApplicationContext reactContext) {
36
+ super(reactContext);
37
+ mContext = reactContext;
38
+ mContext.addActivityEventListener(mActivityEventListener);
39
+ }
40
+
41
+ @Override
42
+ public String getName() {
43
+ return "Cielo";
44
+ }
45
+
46
+ @ReactMethod
47
+ public void payment(@NonNull String json, Promise promise) {
48
+ try {
49
+ mPromise = promise;
50
+ String base64 = Base64.encodeToString(json.getBytes(), Base64.NO_WRAP);
51
+
52
+ Uri.Builder uriBuilder = new Uri.Builder();
53
+ uriBuilder.scheme(SCHEME)
54
+ .authority(PAYMENT)
55
+ .appendQueryParameter("request", base64)
56
+ .appendQueryParameter("urlCallback", CALLBACK);
57
+
58
+ final Activity activity = getCurrentActivity();
59
+
60
+ if (activity == null) {
61
+ throw new Exception("Not found activity");
62
+ }
63
+
64
+ Intent intent = new Intent(Intent.ACTION_VIEW);
65
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
66
+ intent.setData(uriBuilder.build());
67
+ activity.startActivity(intent);
68
+ } catch (Exception e) {
69
+ WritableMap params = Arguments.createMap();
70
+ params.putInt("code", 5000);
71
+ params.putString("result", e.getMessage());
72
+ params.putBoolean("success", false);
73
+
74
+ mPromise.resolve(params);
75
+ mPromise = null;
76
+ }
77
+ }
78
+
79
+ @ReactMethod
80
+ public void cancel(@NonNull String json, Promise promise) {
81
+ try {
82
+ mPromise = promise;
83
+ String base64 = Base64.encodeToString(json.getBytes(), Base64.NO_WRAP);
84
+
85
+ Uri.Builder uriBuilder = new Uri.Builder();
86
+ uriBuilder.authority(CANCEL);
87
+ uriBuilder.scheme(SCHEME);
88
+ uriBuilder.appendQueryParameter("request", base64);
89
+ uriBuilder.appendQueryParameter("urlCallback", CALLBACK);
90
+
91
+ final Activity activity = getCurrentActivity();
92
+
93
+ if (activity == null) {
94
+ throw new Exception("Not found activity");
95
+ }
96
+
97
+ Intent intent = new Intent(Intent.ACTION_VIEW);
98
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
99
+ intent.setData(uriBuilder.build());
100
+
101
+ activity.startActivity(intent);
102
+ } catch (Exception e) {
103
+ WritableMap params = Arguments.createMap();
104
+ params.putInt("code", 5000);
105
+ params.putString("result", e.getMessage());
106
+ params.putBoolean("success", false);
107
+
108
+ mPromise.resolve(params);
109
+ mPromise = null;
110
+ }
111
+ }
112
+
113
+ private WritableMap createParams(Intent intent) {
114
+ WritableMap params = Arguments.createMap();
115
+
116
+ try {
117
+ if (intent != null) {
118
+ Uri data = intent.getData();
119
+ if (data != null) {
120
+ String response = data.getQueryParameter("response");
121
+ if (response != null) {
122
+ try {
123
+ byte[] decoded = Base64.decode(response, Base64.DEFAULT);
124
+ String json = new String(decoded, StandardCharsets.UTF_8);
125
+ boolean error = false;
126
+ org.json.JSONObject jsonObject = new org.json.JSONObject(json);
127
+ int code = jsonObject.optInt("code", -1);
128
+
129
+ if (code == 1 || code == 2)
130
+ error = true;
131
+
132
+ params.putString("result", json);
133
+ params.putInt("code", code);
134
+ params.putBoolean("success", !error);
135
+ } catch (Exception ex) {
136
+ params = Arguments.createMap();
137
+ params.putString("result", ex.toString());
138
+ params.putInt("code", 5010);
139
+ params.putBoolean("success", false);
140
+ }
141
+ }
142
+ }
143
+ }
144
+ } catch (Exception ex) {
145
+ params.putString("code", "5005");
146
+ params.putString("result", ex.toString());
147
+ params.putBoolean("success", false);
148
+ }
149
+
150
+ return params;
151
+ }
152
+
153
+ @ReactMethod
154
+ public void print(String json, Promise promise) {
155
+ try {
156
+ mPromise = promise;
157
+ String base64 = Base64.encodeToString(json.getBytes(), Base64.NO_WRAP);
158
+ Uri.Builder uriBuilder = new Uri.Builder();
159
+ uriBuilder.scheme(SCHEME)
160
+ .authority(PRINT)
161
+ .appendQueryParameter("request", base64)
162
+ .appendQueryParameter("urlCallback", CALLBACK);
163
+ final Activity activity = getCurrentActivity();
164
+ if (activity == null) {
165
+ throw new Exception("Not found activity");
166
+ }
167
+ Intent intent = new Intent(Intent.ACTION_VIEW);
168
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
169
+ intent.setData(uriBuilder.build());
170
+ activity.startActivity(intent);
171
+ } catch (Exception e) {
172
+ WritableMap params = Arguments.createMap();
173
+ params.putInt("code", 5000);
174
+ params.putString("result", e.getMessage());
175
+ params.putBoolean("success", false);
176
+ mPromise.resolve(params);
177
+ mPromise = null;
178
+ }
179
+ }
180
+
181
+ private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
182
+ @Override
183
+ public void onNewIntent(Intent intent) {
184
+ super.onNewIntent(intent);
185
+
186
+ WritableMap params = createParams(intent);
187
+
188
+ if (mPromise != null && params != null) {
189
+ mPromise.resolve(params);
190
+ mPromise = null;
191
+ }
192
+ }
193
+ };
194
+ }
@@ -0,0 +1,25 @@
1
+ package com.controleonline.pos.cielo.payment;
2
+
3
+ import java.util.Collections;
4
+ import java.util.List;
5
+ import java.util.ArrayList;
6
+
7
+ import com.facebook.react.ReactPackage;
8
+ import com.facebook.react.uimanager.ViewManager;
9
+ import com.facebook.react.bridge.ReactApplicationContext;
10
+ import com.facebook.react.bridge.NativeModule;
11
+
12
+ public class CieloPackage implements ReactPackage {
13
+
14
+ @Override
15
+ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
16
+ return Collections.emptyList();
17
+ }
18
+
19
+ @Override
20
+ public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
21
+ List<NativeModule> modules = new ArrayList<NativeModule>();
22
+ modules.add(new Cielo(reactContext));
23
+ return modules;
24
+ }
25
+ }
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { NativeModules } from 'react-native';
2
+
3
+ export default NativeModules.Cielo;
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@controleonline/react-native-cielo-payment",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "files": [
6
+ "android",
7
+ "index.js"
8
+ ],
9
+ "peerDependencies": {
10
+ "react-native": ">=0.70.0"
11
+ }
12
+ }