|
15 | 15 |
|
16 | 16 | # AfipSDK is the easyest way to connect with AFIP
|
17 | 17 | module Afip
|
18 |
| - @config = Afip::AfipConfiguration.new |
19 |
| - |
20 |
| - @ElectronicBilling = Afip::WebServices::ElectronicBilling.new(self) |
21 |
| - @RegisterInscriptionProof = Afip::WebServices::RegisterInscriptionProof.new(self) |
22 |
| - @RegisterScopeTen = Afip::WebServices::RegisterScopeTen.new(self) |
23 |
| - @RegisterScopeThirteen = Afip::WebServices::RegisterScopeThirteen.new(self) |
24 |
| - |
25 |
| - class << self |
26 |
| - extend Forwardable |
27 |
| - |
28 |
| - attr_reader :ElectronicBilling, |
29 |
| - :RegisterInscriptionProof, |
30 |
| - :RegisterScopeTen, |
31 |
| - :RegisterScopeThirteen, |
32 |
| - :config |
33 |
| - |
34 |
| - # User configurable options |
35 |
| - def_delegators :@config, :CUIT, :CUIT= |
36 |
| - def_delegators :@config, :cert, :cert= |
37 |
| - def_delegators :@config, :key, :key= |
38 |
| - def_delegators :@config, :production, :production= |
39 |
| - def_delegators :@config, :access_token, :access_token= |
| 18 | + def self.new(options) |
| 19 | + Afip::Instance.new(options) |
40 | 20 | end
|
41 | 21 |
|
42 |
| - # Gets token authorization for an AFIP Web Service |
43 |
| - # |
44 |
| - # If force is true it forces to create a new TA |
45 |
| - def self.getServiceTA(service, force = false) |
46 |
| - url = URI("https://app.afipsdk.com/api/v1/afip/auth") |
47 |
| - |
48 |
| - https = Net::HTTP.new(url.host, url.port) |
49 |
| - https.use_ssl = true |
50 |
| - |
51 |
| - request = Net::HTTP::Post.new(url) |
52 |
| - request["Content-Type"] = "application/json" |
53 |
| - request["sdk-version-number"] = Afip::VERSION |
54 |
| - request["sdk-library"] = "ruby" |
55 |
| - request["sdk-environment"] = @config.production == true ? "prod" : "dev" |
56 |
| - request["Authorization"] = "Bearer #{@config.access_token}" if @config.access_token |
57 |
| - |
58 |
| - data = { |
59 |
| - "environment": @config.production == true ? "prod" : "dev", |
60 |
| - "tax_id": @config.CUIT, |
61 |
| - "wsid": service, |
62 |
| - "force_create": force |
63 |
| - } |
64 |
| - |
65 |
| - data["cert"] = @config.cert if @config.cert |
66 |
| - data["key"] = @config.key if @config.key |
67 |
| - |
68 |
| - request.body = JSON.dump(data) |
69 |
| - response = https.request(request) |
70 |
| - |
71 |
| - unless response.is_a? Net::HTTPSuccess |
72 |
| - begin |
73 |
| - raise JSON.parse(response.read_body) |
74 |
| - rescue |
75 |
| - raise response.read_body |
76 |
| - end |
| 22 | + class Instance |
| 23 | + attr_accessor :CUIT, |
| 24 | + :cert, |
| 25 | + :key, |
| 26 | + :production, |
| 27 | + :access_token, |
| 28 | + :ElectronicBilling, |
| 29 | + :RegisterInscriptionProof, |
| 30 | + :RegisterScopeTen, |
| 31 | + :RegisterScopeThirteen |
| 32 | + |
| 33 | + def initialize(options) |
| 34 | + raise "CUIT field is required in options" unless options.key?(:CUIT) |
| 35 | + |
| 36 | + self.CUIT = options[:CUIT] |
| 37 | + self.production = options.key?(:production) ? options[:production] : false |
| 38 | + self.cert = options[:cert] |
| 39 | + self.key = options[:key] |
| 40 | + |
| 41 | + self.ElectronicBilling = Afip::WebServices::ElectronicBilling.new(self) |
| 42 | + self.RegisterInscriptionProof = Afip::WebServices::RegisterInscriptionProof.new(self) |
| 43 | + self.RegisterScopeTen = Afip::WebServices::RegisterScopeTen.new(self) |
| 44 | + self.RegisterScopeThirteen = Afip::WebServices::RegisterScopeThirteen.new(self) |
77 | 45 | end
|
78 | 46 |
|
79 |
| - JSON.parse(response.read_body) |
80 |
| - end |
| 47 | + # Gets token authorization for an AFIP Web Service |
| 48 | + # |
| 49 | + # If force is true it forces to create a new TA |
| 50 | + def getServiceTA(service, force = false) |
| 51 | + url = URI("https://app.afipsdk.com/api/v1/afip/auth") |
| 52 | + |
| 53 | + https = Net::HTTP.new(url.host, url.port) |
| 54 | + https.use_ssl = true |
| 55 | + |
| 56 | + request = Net::HTTP::Post.new(url) |
| 57 | + request["Content-Type"] = "application/json" |
| 58 | + request["sdk-version-number"] = Afip::VERSION |
| 59 | + request["sdk-library"] = "ruby" |
| 60 | + request["sdk-environment"] = production == true ? "prod" : "dev" |
| 61 | + request["Authorization"] = "Bearer #{access_token}" if access_token |
| 62 | + |
| 63 | + data = { |
| 64 | + "environment": production == true ? "prod" : "dev", |
| 65 | + "tax_id": self.CUIT, |
| 66 | + "wsid": service, |
| 67 | + "force_create": force |
| 68 | + } |
| 69 | + |
| 70 | + data["cert"] = cert if cert |
| 71 | + data["key"] = key if key |
| 72 | + |
| 73 | + request.body = JSON.dump(data) |
| 74 | + response = https.request(request) |
| 75 | + |
| 76 | + unless response.is_a? Net::HTTPSuccess |
| 77 | + begin |
| 78 | + raise JSON.parse(response.read_body) |
| 79 | + rescue |
| 80 | + raise response.read_body |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + JSON.parse(response.read_body) |
| 85 | + end |
81 | 86 |
|
82 |
| - # Get last request and last response XML |
83 |
| - def self.getLastRequestXML |
84 |
| - url = URI("https://app.afipsdk.com/api/v1/afip/requests/last-xml") |
| 87 | + # Get last request and last response XML |
| 88 | + def getLastRequestXML |
| 89 | + url = URI("https://app.afipsdk.com/api/v1/afip/requests/last-xml") |
85 | 90 |
|
86 |
| - https = Net::HTTP.new(url.host, url.port) |
87 |
| - https.use_ssl = true |
| 91 | + https = Net::HTTP.new(url.host, url.port) |
| 92 | + https.use_ssl = true |
88 | 93 |
|
89 |
| - request = Net::HTTP::Get.new(url) |
90 |
| - request["sdk-version-number"] = Afip::VERSION |
91 |
| - request["sdk-library"] = "ruby" |
92 |
| - request["sdk-environment"] = @config.production == true ? "prod" : "dev" |
93 |
| - request["Authorization"] = "Bearer #{@config.access_token}" if @config.access_token |
| 94 | + request = Net::HTTP::Get.new(url) |
| 95 | + request["sdk-version-number"] = Afip::VERSION |
| 96 | + request["sdk-library"] = "ruby" |
| 97 | + request["sdk-environment"] = production == true ? "prod" : "dev" |
| 98 | + request["Authorization"] = "Bearer #{access_token}" if access_token |
94 | 99 |
|
95 |
| - data["cert"] = @config.cert if @config.cert |
96 |
| - data["key"] = @config.key if @config.key |
| 100 | + data["cert"] = cert if cert |
| 101 | + data["key"] = key if key |
97 | 102 |
|
98 |
| - response = https.request(request) |
| 103 | + response = https.request(request) |
99 | 104 |
|
100 |
| - unless response.is_a? Net::HTTPSuccess |
101 |
| - begin |
102 |
| - raise JSON.parse(response.read_body) |
103 |
| - rescue |
104 |
| - raise response.read_body |
| 105 | + unless response.is_a? Net::HTTPSuccess |
| 106 | + begin |
| 107 | + raise JSON.parse(response.read_body) |
| 108 | + rescue |
| 109 | + raise response.read_body |
| 110 | + end |
105 | 111 | end
|
106 |
| - end |
107 | 112 |
|
108 |
| - JSON.parse(response.read_body) |
109 |
| - end |
| 113 | + JSON.parse(response.read_body) |
| 114 | + end |
110 | 115 |
|
111 |
| - # Create generic Web Service |
112 |
| - def self.webService(service, options = {}) |
113 |
| - options[:service] = service |
114 |
| - options[:generic] = true |
| 116 | + # Create generic Web Service |
| 117 | + def webService(service, options = {}) |
| 118 | + options[:service] = service |
| 119 | + options[:generic] = true |
115 | 120 |
|
116 |
| - Afip::WebService.new(self, options) |
| 121 | + Afip::WebService.new(self, options) |
| 122 | + end |
117 | 123 | end
|
118 | 124 | end
|
0 commit comments