-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMoadian.cs
109 lines (85 loc) · 3.34 KB
/
Moadian.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
using Moadian.API;
using Moadian.Dto;
using Moadian.Services;
using Newtonsoft.Json.Linq;
namespace Moadian
{
public class Moadian
{
private TokenModel token;
protected readonly string publicKey;
protected readonly string privateKey;
protected readonly string orgKeyId;
protected readonly string username;
protected readonly string baseURL;
protected readonly HttpClientService httpClient;
public Moadian(string publicKey, string privateKey, string orgKeyId, string username, string baseURL = "https://tp.tax.gov.ir")
{
this.PublicKey = publicKey;
this.PrivateKey = privateKey;
this.OrgKeyId = orgKeyId;
this.Username = username;
this.BaseURL = baseURL;
var signatureService = new SignatureService(PrivateKey);
var encryptionService = new EncryptionService(publicKey, orgKeyId);
this.httpClient = new HttpClientService(signatureService, encryptionService, baseURL);
}
public string PublicKey { get; }
public string PrivateKey { get; }
public string OrgKeyId { get; }
public string Username { get; }
public string BaseURL { get; }
public Moadian SetToken(TokenModel token)
{
this.token = token;
return this;
}
public async Task<object> SendInvoice(Packet packet)
{
if (this.token == null)
{
throw new ArgumentException("Set token before sending invoice!");
}
var headers = new Dictionary<string, string>
{
{ "Authorization", "Bearer " + this.token.Token },
{ "requestTraceId", Guid.NewGuid().ToString() },
{ "timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString() },
};
var path = "req/api/self-tsp/async/normal-enqueue";
var response = await httpClient.SendPackets(path, new List<Packet>() { packet }, headers, true, true);
return response;
}
public async Task<TokenModel> GetToken()
{
var api = new Api(this.Username, httpClient);
var token = await api.GetToken();
return token;
}
public string GenerateTaxId(DateTime invoiceCreatedAt, int internalInvoiceId)
{
var invoiceIdService = new InvoiceIdService(this.Username);
return invoiceIdService.GenerateInvoiceId(invoiceCreatedAt, internalInvoiceId);
}
public async Task<dynamic> InquiryByReferenceNumber(string referenceNumber)
{
var api = new Api(this.Username, httpClient);
api.SetToken(this.token);
var response = await api.InquiryByReferenceNumber(referenceNumber);
return response;
}
public async Task<dynamic> GetEconomicCodeInformation(string taxID)
{
var api = new Api(this.Username, httpClient);
api.SetToken(this.token);
var response = await api.GetEconomicCodeInformation(taxID);
return response;
}
public object GetFiscalInfo()
{
var api = new Api(this.username, httpClient);
api.SetToken(this.token);
return api.GetFiscalInfo();
}
}
}