-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
449 additions
and
288 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { SmartyPayAPI } from './index'; | ||
|
||
import type { Currency } from 'smartypay-client-model'; | ||
|
@@ -82,7 +83,7 @@ function printHelp() { | |
} | ||
|
||
function processPaymentReq(input?: string) { | ||
if (input) { | ||
if (input || input === '') { | ||
const curKey = findEmptyPaymentKey(); | ||
if (curKey) { | ||
setPaymentKey(curKey, input); | ||
|
@@ -124,9 +125,11 @@ async function createPayment() { | |
const api = new SmartyPayAPI(cred); | ||
|
||
const result = await api.payments.createPayment({ | ||
expiresAt: new Date(paymentReq.expiresAt!), | ||
amount: paymentReq.amount!, | ||
token: paymentReq.token! as Currency, | ||
expiresAt: paymentReq.expiresAt ? new Date(paymentReq.expiresAt) : undefined, | ||
amount: { | ||
value: paymentReq.amount!, | ||
currency: paymentReq.token! as Currency, | ||
}, | ||
metadata: paymentReq.metadata, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { removeEnd } from '../util'; | ||
|
||
import type { ApiOpt } from './type'; | ||
|
||
export function apiHost({ host, isStaging }: ApiOpt) { | ||
// custom host | ||
if (host) return removeEnd(host, '/'); | ||
|
||
// staging api | ||
if (isStaging) return 'https://ncps-api.staging.mnxsc.tech'; | ||
|
||
// default prod api | ||
return 'https://api.smartypay.io'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { CryptoUtil } from '../util/CryptoUtil'; | ||
|
||
export function getMessageSignature(message: string, secretKey: string): string { | ||
return CryptoUtil.hmacSha256Hex(secretKey, message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { CryptoUtil } from '../util/CryptoUtil'; | ||
import { get } from '../util/NetUtil'; | ||
|
||
import { apiHost } from './apiHost'; | ||
|
||
import type { ApiOpt } from './type'; | ||
|
||
export async function getSignReq<T>(apiPath: string, apiOpt: ApiOpt): Promise<T> { | ||
const { secretKey, publicKey, timeout } = apiOpt; | ||
|
||
const now = Date.now(); | ||
const ts = Math.round(now / 1000).toString(); | ||
const messageToSign = `${ts}GET${apiPath}`; | ||
const sig = CryptoUtil.hmacSha256Hex(secretKey, messageToSign); | ||
const targetHost = apiHost(apiOpt); | ||
|
||
const resp = await get(`${targetHost}${apiPath}`, undefined, { | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
'x-api-key': publicKey, | ||
'x-api-sig': sig, | ||
'x-api-ts': ts, | ||
}, | ||
timeout, | ||
}); | ||
|
||
return JSON.parse(resp) as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { getMessageSignature } from './getMessageSignature'; | ||
|
||
/** | ||
* Check Sha256 signature. | ||
* [Docs](https://docs.smartypay.io/api/webhooks) | ||
*/ | ||
export function isValidSignature(message: string, signature: string, secretKey: string): boolean { | ||
return getMessageSignature(message, secretKey) === signature; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import { CryptoUtil } from '../util/CryptoUtil'; | ||
import { post } from '../util/NetUtil'; | ||
|
||
import { apiHost } from './apiHost'; | ||
|
||
import type { ApiOpt } from './type'; | ||
|
||
export async function postSignReq<T>(apiPath: string, bodyData: any, signReq: ApiOpt): Promise<T> { | ||
const { secretKey, publicKey, timeout } = signReq; | ||
|
||
const now = Date.now(); | ||
const ts = Math.round(now / 1000).toString(); | ||
const body = JSON.stringify(bodyData); | ||
const messageToSign = `${ts}POST${apiPath}${body}`; | ||
const sig = CryptoUtil.hmacSha256Hex(secretKey, messageToSign); | ||
const targetHost = apiHost(signReq); | ||
|
||
const resp = await post(`${targetHost}${apiPath}`, body, { | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
'x-api-key': publicKey, | ||
'x-api-sig': sig, | ||
'x-api-ts': ts, | ||
}, | ||
timeout, | ||
}); | ||
|
||
return JSON.parse(resp) as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
SMARTy Pay Node SDK | ||
@author Evgeny Dolganov <[email protected]> | ||
*/ | ||
|
||
import type { Currency } from 'smartypay-client-model'; | ||
|
||
export interface ApiOpt { | ||
publicKey: string; | ||
secretKey: string; | ||
timeout?: number; | ||
host?: string; | ||
isStaging?: boolean; | ||
} | ||
|
||
export interface Amount { | ||
value: string; | ||
currency: Currency; | ||
} | ||
|
||
export const OneDayDelta = 1000 * 60 * 60 * 24; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.