/
SmartyPayButton.ts
99 lines (78 loc) · 2.02 KB
/
SmartyPayButton.ts
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
/**
* SMARTy Pay Client SDK
* @author Evgeny Dolganov <evgenij.dolganov@gmail.com>
*/
import {postForm} from './util';
import {Lang, parseLang} from './model/lang';
import {Theme} from './model/theme';
import {labelPay, makeErrorParam, tokenLabel} from './i18n';
import {CustomFontSupport} from './model/font';
import {initFontByClass} from './util/font';
import {initButton} from './common/button';
export interface SmartyPayButtonProps extends CustomFontSupport {
target: string | undefined,
apiKey: string | undefined,
token: string | undefined,
amount: string | undefined,
lang?: string,
theme?: Theme,
}
interface CallProps {
apiKey: string,
token: string,
amount: string,
lang: Lang,
}
export class SmartyPayButton {
private button: HTMLButtonElement|undefined;
private callProps: CallProps|undefined;
constructor(props: SmartyPayButtonProps) {
initFontByClass(props, () => this.init(props));
}
private init(props: SmartyPayButtonProps){
const lang = parseLang(props.lang);
const {
apiKey,
token,
amount,
} = props;
let errorElem: HTMLElement|undefined;
if( ! apiKey){
errorElem = makeErrorParam('apiKey', lang);
}
else if( ! token){
errorElem = makeErrorParam('token', lang);
}
else if( ! amount){
errorElem = makeErrorParam('amount', lang);
}
const initResp = initButton({
...props,
owner: 'SmartyPayButton',
buttonText: `${labelPay(lang)} ${amount && token? `${amount} ${tokenLabel(token)}` : ''}`,
errorElem,
onClick: ()=> this.click()
});
if( ! initResp)
return;
this.button = initResp.button;
this.callProps = {
apiKey: apiKey!,
amount: amount!,
token: token!,
lang,
};
}
click(){
if( ! this.button || ! this.callProps){
return;
}
const {apiKey, token, amount, lang} = this.callProps;
postForm('https://api.smartypay.io/checkout', {
'api-key': apiKey,
token,
amount,
lang,
});
}
}