@crmcom/self-service-sdk 2.1.2

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/jcccards.js ADDED
@@ -0,0 +1,112 @@
1
+ import { httpJCCUtil } from "./httpJCCUtil";
2
+ import { httpUtil } from "./httpUtil";
3
+ import { createResult, ErrorCodes } from "./resultUtil";
4
+ import { sha256 } from 'js-sha256';
5
+ import { logger } from './logger';
6
+
7
+ export const jcccards = {
8
+ addCard,
9
+ }
10
+
11
+ async function addCard(number) {
12
+ try {
13
+ let id = httpUtil.getSession().sub;
14
+ let response = await httpUtil.post({
15
+ resourcePath: '/v1/contacts/' + id + '/intents',
16
+ body: {
17
+ payment_gateway: 'JCC_MERCHANT'
18
+ },
19
+ withAccessToken: true
20
+ });
21
+ logger.debug("Intents response received");
22
+ if (response.code == "OK") {
23
+ if (response.data && response.data.parameters && response.data.parameters.length > 0) {
24
+ var parameters = response.data.parameters;
25
+ var appKey = findValueByKey("client_app_key", parameters);
26
+ var appID = findValueByKey("app_id", parameters);
27
+ var scheme = findValueByKey("scheme", parameters);
28
+ var urlEndPoint = findValueByKey("url_endpoint", parameters);
29
+ logger.debug("JCC endpoint:", urlEndPoint)
30
+ if (appKey && appID && scheme && urlEndPoint) {
31
+ var hash = sha256(appID + number + scheme + appKey);
32
+ logger.debug("JCC hash generated");
33
+ hash = hash.toUpperCase();
34
+ var payloadAddJCC = {
35
+ appId: appID,
36
+ cardNo: number,
37
+ scheme: scheme,
38
+ signature: hash
39
+ }
40
+ var hashRes = await getCardHash(payloadAddJCC, urlEndPoint);
41
+ logger.debug("JCC hash response received");
42
+ return hashRes;
43
+ } else {
44
+ return createResult(ErrorCodes.UNKNOWN, response);
45
+ }
46
+ } else {
47
+ return createResult(ErrorCodes.UNKNOWN, response);
48
+ }
49
+ } else {
50
+ return createResult(ErrorCodes.UNKNOWN, response);
51
+ }
52
+ } catch (e) {
53
+ logger.error('Exception addCard:', e);
54
+ return createResult(ErrorCodes.UNKNOWN, e);
55
+ }
56
+ }
57
+
58
+ async function getCardHash(payload, urlEndPoint) {
59
+ logger.debug('getCardHash start');
60
+ let xmlBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jcc="http://JCCLoyaltyService">'
61
+ + '<soapenv:Header/><soapenv:Body><jcc:getCardHashRequest><AppId>' + payload.appId
62
+ + '</AppId><CardNo>' + payload.cardNo + '</CardNo><Scheme>' + payload.scheme + '</Scheme><Signature>' + payload.signature + '</Signature>'
63
+ + '</jcc:getCardHashRequest></soapenv:Body></soapenv:Envelope>';
64
+ logger.debug("JCC SOAP request prepared");
65
+ const result = await httpJCCUtil.post('', xmlBody, urlEndPoint);
66
+ if (result.code == 'OK') {
67
+ var bodyData = result.data;
68
+ if (bodyData) {
69
+ if (bodyData.Envelope.Body && bodyData.Envelope.Body.length > 0) {
70
+ var bodyContent = bodyData.Envelope.Body[0];
71
+ if (bodyContent.Fault) {
72
+ return { code: 'FAILED_TO_REMOVE_CARD', data: bodyContent };
73
+ } else {
74
+ var cardHashResponse = bodyContent.getCardHashResponse[0].Response[0];
75
+ if (cardHashResponse.ErrorCode[0] == "0") {
76
+ return { code: 'OK', data: createCardResult(bodyContent.getCardHashResponse[0]) };
77
+ } else {
78
+ if (cardHashResponse.ErrorDesc[0].includes('already EXIST')) {
79
+ return { code: 'CARD_ALREADY_EXIT', data: cardHashResponse };
80
+ } else
81
+ return { code: 'FAILED_TO_ADD_CARD', data: cardHashResponse };
82
+ }
83
+ }
84
+ } else {
85
+ return createResult(ErrorCodes.UNKNOWN, bodyData);
86
+ }
87
+ } else {
88
+ return createResult(ErrorCodes.UNKNOWN, bodyData);
89
+ }
90
+ } else {
91
+ return createResult(ErrorCodes.UNKNOWN, result.data);
92
+ }
93
+ }
94
+
95
+ function createCardResult(cardResponse) {
96
+ return {
97
+ card_no_hashed: cardResponse.CardNoHashed && cardResponse.CardNoHashed.length > 0 ? cardResponse.CardNoHashed[0] : null,
98
+ card_no_masked: cardResponse.CardNoMasked && cardResponse.CardNoMasked.length > 0 ? cardResponse.CardNoMasked[0] : null,
99
+ country_of_issue: cardResponse.isLocal ? 'CYP' : null
100
+ }
101
+ }
102
+
103
+ function findValueByKey(key, data) {
104
+ var object = data.filter(e => {
105
+ return e.key == key && e.value;
106
+ })
107
+ if (object && object.length > 0) {
108
+ return object[0].value
109
+ } else {
110
+ return null;
111
+ }
112
+ }
package/logger.js ADDED
@@ -0,0 +1,40 @@
1
+ let _logLevel = 'none';
2
+
3
+ const LOG_LEVELS = {
4
+ none: 0,
5
+ error: 1,
6
+ warn: 2,
7
+ info: 3,
8
+ debug: 4,
9
+ };
10
+
11
+ export const logger = {
12
+ setLevel(level) {
13
+ if (LOG_LEVELS[level] !== undefined) {
14
+ _logLevel = level;
15
+ }
16
+ },
17
+ getLevel() {
18
+ return _logLevel;
19
+ },
20
+ debug(...args) {
21
+ if (LOG_LEVELS[_logLevel] >= LOG_LEVELS.debug) {
22
+ console.log('[CRM.COM SDK DEBUG]', ...args);
23
+ }
24
+ },
25
+ info(...args) {
26
+ if (LOG_LEVELS[_logLevel] >= LOG_LEVELS.info) {
27
+ console.log('[CRM.COM SDK INFO]', ...args);
28
+ }
29
+ },
30
+ warn(...args) {
31
+ if (LOG_LEVELS[_logLevel] >= LOG_LEVELS.warn) {
32
+ console.warn('[CRM.COM SDK WARN]', ...args);
33
+ }
34
+ },
35
+ error(...args) {
36
+ if (LOG_LEVELS[_logLevel] >= LOG_LEVELS.error) {
37
+ console.error('[CRM.COM SDK ERROR]', ...args);
38
+ }
39
+ },
40
+ };
package/mobilepass.js ADDED
@@ -0,0 +1,64 @@
1
+ import { httpUtil } from './httpUtil'
2
+ import { ErrorCodes, createResult, createCommonResult } from './resultUtil'
3
+ import { logger } from './logger';
4
+
5
+ export const mobilepass = {
6
+ getGooglePass,
7
+ getApplePass,
8
+ getAndroidWalletPass
9
+ }
10
+
11
+ async function getGooglePass({
12
+ contact_id,
13
+ organisation_id
14
+ }, { }) {
15
+ try {
16
+ let id = contact_id ? contact_id : httpUtil.getSession().sub;
17
+ let orgId = organisation_id ? organisation_id : httpUtil.getSession().current_organisation_id;
18
+ let response = await httpUtil.get({
19
+ resourcePath: '/v2/google_pass/' + orgId + '/contacts/' + id,
20
+ withAccessToken: true
21
+ });
22
+ return createCommonResult(response);
23
+ } catch (e) {
24
+ logger.error('Exception getGooglePass:', e);
25
+ return createResult(ErrorCodes.UNKNOWN, e);
26
+ }
27
+ }
28
+
29
+ async function getApplePass({
30
+ contact_id,
31
+ organisation_id
32
+ }, { }) {
33
+ try {
34
+ let id = contact_id ? contact_id : httpUtil.getSession().sub;
35
+ let orgId = organisation_id ? organisation_id : httpUtil.getSession().current_organisation_id;
36
+ let response = await httpUtil.get({
37
+ resourcePath: '/v2/apple_pass/' + orgId + '/contacts/' + id,
38
+ withAccessToken: true
39
+ });
40
+ return createCommonResult(response);
41
+ } catch (e) {
42
+ logger.error('Exception getApplePass:', e);
43
+ return createResult(ErrorCodes.UNKNOWN, e);
44
+ }
45
+ }
46
+
47
+ async function getAndroidWalletPass({
48
+ contact_id,
49
+ organisation_id
50
+ }, { }) {
51
+ try {
52
+ let id = contact_id ? contact_id : httpUtil.getSession().sub;
53
+ let orgId = organisation_id ? organisation_id : httpUtil.getSession().current_organisation_id;
54
+ let response = await httpUtil.get({
55
+ resourcePath: '/v2/android_wallet_pass/' + orgId + '/contacts/' + id,
56
+ withAccessToken: true
57
+ });
58
+ return createCommonResult(response);
59
+ } catch (e) {
60
+ logger.error('Exception getAndroidWalletPass:', e);
61
+ return createResult(ErrorCodes.UNKNOWN, e);
62
+ }
63
+ }
64
+