Skip to content

Commit 319c288

Browse files
authored
Merge pull request #152 from clicksign/feature/CLKF-86
[CLKF-86] Implementa suporte à customização de cores dos botões no package verify
2 parents 0bab54f + 7250494 commit 319c288

3 files changed

Lines changed: 89 additions & 6 deletions

File tree

packages/verify/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import AuthSession from "./src/embedded";
1+
import ClicksignVerify from './src/embedded';
22

3-
globalThis.AuthSession = AuthSession;
3+
globalThis.ClicksignVerify = ClicksignVerify;
44

5-
export { AuthSession }
5+
export default ClicksignVerify;

packages/verify/src/embedded.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class ClicksignVerify {
77
this.key = key;
88
this.listen = {};
99
this.locale = '';
10+
this.custom = null;
1011
this.endpoint = 'https://app.clicksign.com';
1112
this.origin = `${window.location.protocol}://${window.location.host}`;
1213
}
@@ -56,8 +57,30 @@ export default class ClicksignVerify {
5657
return `${this.endpoint}${this.path}${this.params}`;
5758
}
5859

60+
get data() {
61+
if (!this.custom) return '';
62+
63+
return ClicksignVerify.base64EncodeUrl(JSON.stringify({ custom: this.custom }));
64+
}
65+
66+
static base64EncodeUrl(value) {
67+
let base64;
68+
69+
if (typeof btoa === 'function') base64 = btoa(value);
70+
else if (typeof Buffer !== 'undefined') base64 = Buffer.from(value, 'utf-8').toString('base64');
71+
else throw new Error('No base64 encoder available');
72+
73+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
74+
}
75+
5976
get params() {
60-
return `?origin=${this.origin}`;
77+
const query = new URLSearchParams({ origin: this.origin });
78+
79+
if (this.data) query.set('data', this.data);
80+
81+
const queryToString = query.toString();
82+
83+
return queryToString ? `?${queryToString}` : '';
6184
}
6285

6386
get path() {

packages/verify/src/embedded.spec.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ const sessionKey = 'foobar123';
55
const endpoint = 'https://example.com';
66
const originUrl = `${window.location.protocol}://${window.location.host}`;
77

8-
function getSourceUrl(locale = '') {
8+
function getDataParam(custom = null) {
9+
if (!custom) return '';
10+
11+
return ClicksignVerify.base64EncodeUrl(JSON.stringify({ custom }));
12+
}
13+
14+
function getSourceUrl(locale = '', custom = null) {
915
const prefix = `${endpoint}/app/verify`;
1016
const verifyPath = locale ? `${prefix}/${locale}` : prefix;
17+
const query = new URLSearchParams({ origin: originUrl });
18+
const data = getDataParam(custom);
19+
20+
if (data) query.set('data', data);
1121

12-
return `${verifyPath}/transactions/${sessionKey}?origin=${originUrl}`;
22+
return `${verifyPath}/transactions/${sessionKey}?${query.toString()}`;
1323
}
1424

1525
function createContainerElement() {
@@ -27,6 +37,8 @@ describe('ClicksignVerify', () => {
2737

2838
instance.endpoint = endpoint;
2939
instance.origin = originUrl;
40+
instance.locale = '';
41+
instance.custom = null;
3042
instance.listen = {};
3143
});
3244

@@ -39,6 +51,7 @@ describe('ClicksignVerify', () => {
3951
expect(instance.origin).toBe(originUrl);
4052
expect(instance.endpoint).toBe(endpoint);
4153
expect(instance.locale).toBe('');
54+
expect(instance.custom).toBeNull();
4255
expect(instance.source).toBe(getSourceUrl());
4356
});
4457

@@ -102,6 +115,53 @@ describe('ClicksignVerify', () => {
102115
});
103116
});
104117

118+
describe('Customization', () => {
119+
it('should initialize custom as null by default', () => {
120+
expect(instance.custom).toBeNull();
121+
});
122+
123+
it('should include custom colors in data query param in the iframe source URL when custom colors is set', () => {
124+
const custom = {
125+
colors: {
126+
buttonTextColor: '#ffffff',
127+
buttonBackgroundColor: '#000000',
128+
},
129+
};
130+
131+
instance.custom = custom;
132+
instance.start(containerElementId);
133+
134+
const iframeElement = document.getElementById(containerElementId).children[0];
135+
const iframeSrcUrl = new URL(iframeElement.src);
136+
137+
expect(instance.params).toContain('data=');
138+
expect(instance.source).toBe(getSourceUrl('', custom));
139+
expect(iframeSrcUrl.searchParams.get('data')).toBe(getDataParam(custom));
140+
});
141+
});
142+
143+
describe('Query Params', () => {
144+
it('should include origin query param in the iframe source URL', () => {
145+
instance.start(containerElementId);
146+
147+
const iframeElement = document.getElementById(containerElementId).children[0];
148+
const iframeSrcUrl = new URL(iframeElement.src);
149+
150+
expect(instance.params).toBe(`?${new URLSearchParams({ origin: originUrl }).toString()}`);
151+
expect(iframeSrcUrl.searchParams.get('origin')).toBe(originUrl);
152+
});
153+
154+
it('should not include data query param when there is no payload', () => {
155+
instance.start(containerElementId);
156+
157+
const iframeElement = document.getElementById(containerElementId).children[0];
158+
const iframeSrcUrl = new URL(iframeElement.src);
159+
160+
expect(instance.params).not.toContain('data=');
161+
expect(iframeSrcUrl.searchParams.get('data')).toBeNull();
162+
});
163+
});
164+
105165
describe('Events', () => {
106166
const eventMock = vi.fn();
107167
const events = ['loaded', 'completed', 'error'];

0 commit comments

Comments
 (0)