@test-glide/payment-react-native 0.1.4 → 0.1.6
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.
- package/lib/module/core/BivoSecureStore.js +22 -12
- package/lib/module/core/BivoSecureStore.js.map +1 -1
- package/lib/typescript/src/core/BivoSecureStore.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/BivoSecureStore.ts +25 -13
- package/lib/module/utils/fetch.js +0 -47
- package/lib/module/utils/fetch.js.map +0 -1
- package/lib/typescript/src/utils/fetch.d.ts +0 -13
- package/lib/typescript/src/utils/fetch.d.ts.map +0 -1
- package/src/utils/fetch.ts +0 -58
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { fetchData } from "../utils/fetch.js";
|
|
4
3
|
import { formatCardYear, formatString } from "../utils/utils.js";
|
|
4
|
+
async function fetchData(url, options = {}) {
|
|
5
|
+
const response = await fetch(url, {
|
|
6
|
+
method: options.method || 'GET',
|
|
7
|
+
headers: {
|
|
8
|
+
'Content-Type': 'application/json',
|
|
9
|
+
...(options.headers || {})
|
|
10
|
+
},
|
|
11
|
+
body: options.body ? JSON.stringify(options.body) : undefined
|
|
12
|
+
});
|
|
13
|
+
return response.json();
|
|
14
|
+
}
|
|
5
15
|
export class BivoSecureStore {
|
|
6
16
|
form = {};
|
|
7
17
|
errors = {};
|
|
@@ -104,36 +114,36 @@ export class BivoSecureStore {
|
|
|
104
114
|
}
|
|
105
115
|
async submit(endpoint, token) {
|
|
106
116
|
// console.log("Submitting form:", this.form, this.errors);
|
|
107
|
-
const expiry = this.form.
|
|
117
|
+
const expiry = this.form.exp?.split('/') || [];
|
|
108
118
|
|
|
109
119
|
// Replace fetchData with your API call
|
|
110
120
|
const response = await fetchData(endpoint, {
|
|
111
121
|
method: 'POST',
|
|
112
122
|
body: {
|
|
113
123
|
token,
|
|
114
|
-
pan: formatString(this.form.
|
|
124
|
+
pan: formatString(this.form.card || ''),
|
|
115
125
|
expiry_month: Number(expiry[0] || 0) || null,
|
|
116
126
|
expiry_year: formatCardYear(expiry[1] || '') || null,
|
|
117
|
-
cvv: this.form.
|
|
127
|
+
cvv: this.form.cvv ? formatString(this.form.cvv) : null,
|
|
118
128
|
address: {
|
|
119
|
-
postal_code: this.form.
|
|
120
|
-
}
|
|
129
|
+
postal_code: this.form.zip ? formatString(this.form.zip) : null
|
|
130
|
+
},
|
|
131
|
+
other: {
|
|
132
|
+
...this.form
|
|
133
|
+
} // include any additional fields
|
|
121
134
|
}
|
|
122
135
|
});
|
|
123
|
-
if (response.
|
|
136
|
+
if (response.status === "ERROR") {
|
|
124
137
|
return {
|
|
125
138
|
success: false,
|
|
126
|
-
error: response
|
|
127
|
-
data: response
|
|
139
|
+
error: response
|
|
128
140
|
};
|
|
129
141
|
} else {
|
|
130
142
|
return {
|
|
131
143
|
success: true,
|
|
132
|
-
data: response
|
|
133
|
-
info: response
|
|
144
|
+
data: response
|
|
134
145
|
};
|
|
135
146
|
}
|
|
136
|
-
// return response.json();
|
|
137
147
|
}
|
|
138
148
|
}
|
|
139
149
|
//# sourceMappingURL=BivoSecureStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["fetchData","
|
|
1
|
+
{"version":3,"names":["formatCardYear","formatString","fetchData","url","options","response","fetch","method","headers","body","JSON","stringify","undefined","json","BivoSecureStore","form","errors","fieldConfig","constructor","vaultId","environment","setField","fieldName","value","onStateChange","required","regex","errorMsg","parsedRegex","RegExp","e","console","error","validateField","trimmedValue","trim","test","digits","replace","length","monthStr","yearStr","split","month","parseInt","year","fullYear","now","Date","currentMonth","getMonth","currentYear","getFullYear","isSubmitDisabled","fieldNames","some","submit","endpoint","token","expiry","exp","pan","card","expiry_month","Number","expiry_year","cvv","address","postal_code","zip","other","status","success","data"],"sourceRoot":"../../../src","sources":["core/BivoSecureStore.ts"],"mappings":";;AAAA,SAASA,cAAc,EAAEC,YAAY,QAAQ,mBA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MAAM;MACdE,IAAI,EAAE;QACJiD,KAAK;QACLG,GAAG,EAAE5D,YAAY,CAAC,IAAI,CAACc,IAAI,CAAC+C,IAAI,IAAI,EAAE,CAAC;QACvCC,YAAY,EAAEC,MAAM,CAACL,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI;QAC5CM,WAAW,EAAEjE,cAAc,CAAC2D,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,IAAI;QACpDO,GAAG,EAAE,IAAI,CAACnD,IAAI,CAACmD,GAAG,GAAGjE,YAAY,CAAC,IAAI,CAACc,IAAI,CAACmD,GAAG,CAAC,GAAG,IAAI;QACvDC,OAAO,EAAE;UACPC,WAAW,EAAE,IAAI,CAACrD,IAAI,CAACsD,GAAG,GACtBpE,YAAY,CAAC,IAAI,CAACc,IAAI,CAACsD,GAAG,CAAC,GAC3B;QACN,CAAC;QACDC,KAAK,EAAC;UAAC,GAAG,IAAI,CAACvD;QAAI,CAAC,CAAE;MACxB;IACF,CAAC,CAAC;IAEF,IAAIV,QAAQ,CAACkE,MAAM,KAAK,OAAO,EAAE;MAC7B,OAAO;QAAEC,OAAO,EAAE,KAAK;QAAExC,KAAK,EAAE3B;MAAS,CAAC;IAC5C,CAAC,MAAM;MACL,OAAO;QAAEmE,OAAO,EAAE,IAAI;QAAEC,IAAI,EAAEpE;MAAS,CAAC;IAC1C;EACJ;AACF","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BivoSecureStore.d.ts","sourceRoot":"","sources":["../../../../src/core/BivoSecureStore.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"BivoSecureStore.d.ts","sourceRoot":"","sources":["../../../../src/core/BivoSecureStore.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAepD,qBAAa,eAAe;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,SAAS,GAAG,MAAM,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IAClC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IACpC,WAAW,EAAE,MAAM,CACjB,MAAM,EACN;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAC1D,CAAM;gBAEK,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,GAAG,MAAM;IAK5D,QAAQ,CACN,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,EAC3C,QAAQ,GAAE,OAAe,EACzB,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM;IA2BnB,aAAa,CACX,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,OAAO,EAClB,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM;IAqEnB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE;IAQ/B,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CA4B5D"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import { fetchData } from '../utils/fetch';
|
|
2
1
|
import { formatCardYear, formatString } from '../utils/utils';
|
|
3
2
|
|
|
4
3
|
type FieldState = { value: string; error?: string };
|
|
5
4
|
|
|
5
|
+
async function fetchData(url: string, options: any = {}): Promise<any> {
|
|
6
|
+
const response = await fetch(url, {
|
|
7
|
+
method: options.method || 'GET',
|
|
8
|
+
headers: {
|
|
9
|
+
'Content-Type': 'application/json',
|
|
10
|
+
...(options.headers || {}),
|
|
11
|
+
},
|
|
12
|
+
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return response.json();
|
|
16
|
+
}
|
|
17
|
+
|
|
6
18
|
export class BivoSecureStore {
|
|
7
19
|
vaultId: string;
|
|
8
20
|
environment: 'sandbox' | 'live';
|
|
@@ -133,32 +145,32 @@ export class BivoSecureStore {
|
|
|
133
145
|
});
|
|
134
146
|
}
|
|
135
147
|
|
|
136
|
-
async submit(endpoint: string, token: string):Promise<any> {
|
|
148
|
+
async submit(endpoint: string, token: string): Promise<any> {
|
|
137
149
|
// console.log("Submitting form:", this.form, this.errors);
|
|
138
|
-
const expiry = this.form.
|
|
150
|
+
const expiry = this.form.exp?.split('/') || [];
|
|
139
151
|
|
|
140
152
|
// Replace fetchData with your API call
|
|
141
153
|
const response = await fetchData(endpoint, {
|
|
142
154
|
method: 'POST',
|
|
143
155
|
body: {
|
|
144
156
|
token,
|
|
145
|
-
pan: formatString(this.form.
|
|
157
|
+
pan: formatString(this.form.card || ''),
|
|
146
158
|
expiry_month: Number(expiry[0] || 0) || null,
|
|
147
159
|
expiry_year: formatCardYear(expiry[1] || '') || null,
|
|
148
|
-
cvv: this.form.
|
|
160
|
+
cvv: this.form.cvv ? formatString(this.form.cvv) : null,
|
|
149
161
|
address: {
|
|
150
|
-
postal_code: this.form.
|
|
151
|
-
? formatString(this.form.
|
|
162
|
+
postal_code: this.form.zip
|
|
163
|
+
? formatString(this.form.zip)
|
|
152
164
|
: null,
|
|
153
165
|
},
|
|
166
|
+
other:{...this.form}, // include any additional fields
|
|
154
167
|
},
|
|
155
168
|
});
|
|
156
169
|
|
|
157
|
-
if (response.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
// return response.json();
|
|
170
|
+
if (response.status === "ERROR") {
|
|
171
|
+
return { success: false, error: response };
|
|
172
|
+
} else {
|
|
173
|
+
return { success: true, data: response };
|
|
174
|
+
}
|
|
163
175
|
}
|
|
164
176
|
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
export const fetchData = async (url, payload = {}) => {
|
|
4
|
-
const {
|
|
5
|
-
method = "GET",
|
|
6
|
-
headers = {},
|
|
7
|
-
body = null,
|
|
8
|
-
...options
|
|
9
|
-
} = payload;
|
|
10
|
-
try {
|
|
11
|
-
const config = {
|
|
12
|
-
method,
|
|
13
|
-
headers: {
|
|
14
|
-
Accept: "*/*",
|
|
15
|
-
"Content-Type": "application/json",
|
|
16
|
-
...headers
|
|
17
|
-
},
|
|
18
|
-
...options
|
|
19
|
-
};
|
|
20
|
-
if (body && ["POST", "PUT", "PATCH", "DELETE"].includes(method.toUpperCase())) {
|
|
21
|
-
config.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
22
|
-
}
|
|
23
|
-
const response = await fetch(url, config);
|
|
24
|
-
const contentType = response.headers.get("content-type");
|
|
25
|
-
let responseData;
|
|
26
|
-
if (contentType?.includes("application/json")) {
|
|
27
|
-
responseData = await response.json();
|
|
28
|
-
} else {
|
|
29
|
-
responseData = await response.text();
|
|
30
|
-
}
|
|
31
|
-
if (!response.ok) {
|
|
32
|
-
const errData = responseData;
|
|
33
|
-
throw new Error(errData?.message || `HTTP error! status: ${response.status}`);
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
data: responseData,
|
|
37
|
-
error: null
|
|
38
|
-
};
|
|
39
|
-
} catch (err) {
|
|
40
|
-
const errorMessage = err instanceof Error ? err.message : "An error occurred";
|
|
41
|
-
return {
|
|
42
|
-
data: null,
|
|
43
|
-
error: errorMessage
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=fetch.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["fetchData","url","payload","method","headers","body","options","config","Accept","includes","toUpperCase","JSON","stringify","response","fetch","contentType","get","responseData","json","text","ok","errData","Error","message","status","data","error","err","errorMessage"],"sourceRoot":"../../../src","sources":["utils/fetch.ts"],"mappings":";;AAaA,OAAO,MAAMA,SAAS,GAAG,MAAAA,CACvBC,GAAW,EACXC,OAAqB,GAAG,CAAC,CAAC,KACE;EAC5B,MAAM;IAAEC,MAAM,GAAG,KAAK;IAAEC,OAAO,GAAG,CAAC,CAAC;IAAEC,IAAI,GAAG,IAAI;IAAE,GAAGC;EAAQ,CAAC,GAAGJ,OAAO;EAEzE,IAAI;IACF,MAAMK,MAAmB,GAAG;MAC1BJ,MAAM;MACNC,OAAO,EAAE;QACPI,MAAM,EAAE,KAAK;QACb,cAAc,EAAE,kBAAkB;QAClC,GAAGJ;MACL,CAAC;MACD,GAAGE;IACL,CAAC;IAED,IAAID,IAAI,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAACI,QAAQ,CAACN,MAAM,CAACO,WAAW,CAAC,CAAC,CAAC,EAAE;MAC7EH,MAAM,CAACF,IAAI,GAAG,OAAOA,IAAI,KAAK,QAAQ,GAAGA,IAAI,GAAGM,IAAI,CAACC,SAAS,CAACP,IAAI,CAAC;IACtE;IAEA,MAAMQ,QAAQ,GAAG,MAAMC,KAAK,CAACb,GAAG,EAAEM,MAAM,CAAC;IAEzC,MAAMQ,WAAW,GAAGF,QAAQ,CAACT,OAAO,CAACY,GAAG,CAAC,cAAc,CAAC;IACxD,IAAIC,YAAe;IAEnB,IAAIF,WAAW,EAAEN,QAAQ,CAAC,kBAAkB,CAAC,EAAE;MAC7CQ,YAAY,GAAG,MAAMJ,QAAQ,CAACK,IAAI,CAAC,CAAM;IAC3C,CAAC,MAAM;MACLD,YAAY,GAAG,MAAMJ,QAAQ,CAACM,IAAI,CAAC,CAAM;IAC3C;IAEA,IAAI,CAACN,QAAQ,CAACO,EAAE,EAAE;MAChB,MAAMC,OAAO,GAAGJ,YAAuC;MACvD,MAAM,IAAIK,KAAK,CACZD,OAAO,EAAEE,OAAO,IAAe,uBAAuBV,QAAQ,CAACW,MAAM,EACxE,CAAC;IACH;IAEA,OAAO;MAAEC,IAAI,EAAER,YAAY;MAAES,KAAK,EAAE;IAAK,CAAC;EAC5C,CAAC,CAAC,OAAOC,GAAG,EAAE;IACZ,MAAMC,YAAY,GAAGD,GAAG,YAAYL,KAAK,GAAGK,GAAG,CAACJ,OAAO,GAAG,mBAAmB;IAC7E,OAAO;MAAEE,IAAI,EAAE,IAAI;MAAEC,KAAK,EAAEE;IAAa,CAAC;EAC5C;AACF,CAAC","ignoreList":[]}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
2
|
-
interface FetchPayload extends Omit<RequestInit, "method" | "headers" | "body"> {
|
|
3
|
-
method?: HttpMethod;
|
|
4
|
-
headers?: Record<string, string>;
|
|
5
|
-
body?: Record<string, unknown> | string | null;
|
|
6
|
-
}
|
|
7
|
-
interface FetchResult<T> {
|
|
8
|
-
data: T | null;
|
|
9
|
-
error: string | null;
|
|
10
|
-
}
|
|
11
|
-
export declare const fetchData: <T = unknown>(url: string, payload?: FetchPayload) => Promise<FetchResult<T>>;
|
|
12
|
-
export {};
|
|
13
|
-
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE9D,UAAU,YAAa,SAAQ,IAAI,CAAC,WAAW,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;IAC7E,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC;CAChD;AAED,UAAU,WAAW,CAAC,CAAC;IACrB,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,eAAO,MAAM,SAAS,GAAU,CAAC,GAAG,OAAO,EACzC,KAAK,MAAM,EACX,UAAS,YAAiB,KACzB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAyCxB,CAAC"}
|
package/src/utils/fetch.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
2
|
-
|
|
3
|
-
interface FetchPayload extends Omit<RequestInit, "method" | "headers" | "body"> {
|
|
4
|
-
method?: HttpMethod;
|
|
5
|
-
headers?: Record<string, string>;
|
|
6
|
-
body?: Record<string, unknown> | string | null;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
interface FetchResult<T> {
|
|
10
|
-
data: T | null;
|
|
11
|
-
error: string | null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const fetchData = async <T = unknown>(
|
|
15
|
-
url: string,
|
|
16
|
-
payload: FetchPayload = {}
|
|
17
|
-
): Promise<FetchResult<T>> => {
|
|
18
|
-
const { method = "GET", headers = {}, body = null, ...options } = payload;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
const config: RequestInit = {
|
|
22
|
-
method,
|
|
23
|
-
headers: {
|
|
24
|
-
Accept: "*/*",
|
|
25
|
-
"Content-Type": "application/json",
|
|
26
|
-
...headers,
|
|
27
|
-
},
|
|
28
|
-
...options,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
if (body && ["POST", "PUT", "PATCH", "DELETE"].includes(method.toUpperCase())) {
|
|
32
|
-
config.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const response = await fetch(url, config);
|
|
36
|
-
|
|
37
|
-
const contentType = response.headers.get("content-type");
|
|
38
|
-
let responseData: T;
|
|
39
|
-
|
|
40
|
-
if (contentType?.includes("application/json")) {
|
|
41
|
-
responseData = await response.json() as T;
|
|
42
|
-
} else {
|
|
43
|
-
responseData = await response.text() as T;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (!response.ok) {
|
|
47
|
-
const errData = responseData as Record<string, unknown>;
|
|
48
|
-
throw new Error(
|
|
49
|
-
(errData?.message as string) || `HTTP error! status: ${response.status}`
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return { data: responseData, error: null };
|
|
54
|
-
} catch (err) {
|
|
55
|
-
const errorMessage = err instanceof Error ? err.message : "An error occurred";
|
|
56
|
-
return { data: null, error: errorMessage };
|
|
57
|
-
}
|
|
58
|
-
};
|