forked from izatop/cloudpayments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiptApi.ts
More file actions
54 lines (46 loc) · 1.89 KB
/
ReceiptApi.ts
File metadata and controls
54 lines (46 loc) · 1.89 KB
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
import {ok} from "assert";
import * as objectHash from "object-hash";
import {ClientRequestAbstract, ClientResponse} from "./Client";
import {
BaseResponse,
CustomerReceipt,
ReceiptApiRequest,
ReceiptRequest,
validateTaxationSystem,
validateVAT,
} from "./Api";
export class ReceiptApi extends ClientRequestAbstract {
public getEndpoint() {
return this.options.endpoint.replace(/\/$/, "").concat("/kkt");
}
/**
* Create receipt
*
* @param {Receipt} request Common request fields
* @param {Receipt} receipt Receipt fields
* @param {string} requestId Idempotent request id (calculated automatically if not provided)
* @returns {Promise<Response>}
*/
async createReceipt(request: ReceiptRequest, receipt: CustomerReceipt, requestId?: string) {
const {..._request} = request;
const {..._receipt} = receipt;
if (this.options.org) {
if (!_request.Inn && this.options.org.inn) {
_request.Inn = this.options.org.inn;
}
if (!_receipt.taxationSystem && validateTaxationSystem(this.options.org.taxationSystem)) {
_receipt.taxationSystem = this.options.org.taxationSystem;
}
}
ok(_request.Type, "Type is required");
ok(_request.Inn, "Inn is required");
ok(validateTaxationSystem(_receipt.taxationSystem), "A receipt field taxationSystem should be valid");
ok(_receipt.Items && _receipt.Items.length > 0, "A receipt field Items should be filled");
ok(_receipt.Items.filter(x => !validateVAT(x.vat)).length === 0, "You should fill VAT with valid values");
const data: ReceiptApiRequest = {
..._request,
CustomerReceipt: _receipt,
};
return new ClientResponse(await this.call<BaseResponse>("receipt", data, requestId || objectHash(receipt)));
}
}