vsafe-ruby 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +241 -0
  3. data/Rakefile +6 -0
  4. data/lib/vsafe.rb +14 -0
  5. data/lib/vsafe/auth_result.rb +11 -0
  6. data/lib/vsafe/avs_result.rb +24 -0
  7. data/lib/vsafe/card_type.rb +29 -0
  8. data/lib/vsafe/charge_source.rb +12 -0
  9. data/lib/vsafe/client.rb +101 -0
  10. data/lib/vsafe/client_error.rb +4 -0
  11. data/lib/vsafe/config.rb +22 -0
  12. data/lib/vsafe/cvn_result.rb +17 -0
  13. data/lib/vsafe/payment_status.rb +15 -0
  14. data/lib/vsafe/request_error.rb +11 -0
  15. data/lib/vsafe/response.rb +47 -0
  16. data/lib/vsafe/response_error.rb +36 -0
  17. data/lib/vsafe/responses/charge_account_to_temporary_token.rb +12 -0
  18. data/lib/vsafe/responses/charge_authorize.rb +19 -0
  19. data/lib/vsafe/responses/charge_confirm.rb +10 -0
  20. data/lib/vsafe/responses/charge_sale.rb +20 -0
  21. data/lib/vsafe/responses/get_session_tags.rb +10 -0
  22. data/lib/vsafe/responses/reverse_payment.rb +12 -0
  23. data/lib/vsafe/responses/validate_charge_account.rb +19 -0
  24. data/lib/vsafe/version.rb +3 -0
  25. data/spec/spec_helper.rb +17 -0
  26. data/spec/vsafe/auth_result_spec.rb +12 -0
  27. data/spec/vsafe/avs_result_spec.rb +12 -0
  28. data/spec/vsafe/card_type_spec.rb +44 -0
  29. data/spec/vsafe/charge_source_spec.rb +12 -0
  30. data/spec/vsafe/client_spec.rb +186 -0
  31. data/spec/vsafe/config_spec.rb +14 -0
  32. data/spec/vsafe/cvn_result_spec.rb +12 -0
  33. data/spec/vsafe/payment_status_spec.rb +12 -0
  34. data/spec/vsafe/request_error_spec.rb +12 -0
  35. data/spec/vsafe/response_error_spec.rb +37 -0
  36. data/spec/vsafe/response_spec.rb +161 -0
  37. data/spec/vsafe/responses/charge_account_to_temporary_token_spec.rb +27 -0
  38. data/spec/vsafe/responses/charge_authorize_spec.rb +61 -0
  39. data/spec/vsafe/responses/charge_confirm_spec.rb +18 -0
  40. data/spec/vsafe/responses/charge_sale_spec.rb +68 -0
  41. data/spec/vsafe/responses/get_session_tags_spec.rb +27 -0
  42. data/spec/vsafe/responses/reverse_payment_spec.rb +37 -0
  43. data/spec/vsafe/responses/validate_charge_account.rb +19 -0
  44. data/spec/vsafe/vsafe_spec.rb +12 -0
  45. metadata +174 -0
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+ require "vsafe/config"
3
+
4
+ RSpec.describe VSafe::Config do
5
+ describe ".new" do
6
+ it "sets default configs" do
7
+ config = VSafe::Config.new
8
+
9
+ expect(config.sandbox).to eq(true)
10
+ expect(config.request_timeout).to be_a(Fixnum)
11
+ expect(config.url).not_to be_empty
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+ require "vsafe/cvn_result"
3
+
4
+ RSpec.describe VSafe::CvnResult do
5
+ describe ".new" do
6
+ let(:code) { 1 }
7
+
8
+ it "sets the code" do
9
+ expect(VSafe::CvnResult.new(code.to_s).code).to eq(code)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+ require "vsafe/payment_status"
3
+
4
+ RSpec.describe VSafe::PaymentStatus do
5
+ describe ".new" do
6
+ let(:code) { 1 }
7
+
8
+ it "sets the code" do
9
+ expect(VSafe::PaymentStatus.new(code.to_s).code).to eq(code)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+ require "vsafe/request_error"
3
+
4
+ RSpec.describe VSafe::RequestError do
5
+ describe ".new" do
6
+ let(:response) { double }
7
+
8
+ it "sets response" do
9
+ expect(VSafe::RequestError.new(response).response).to eq(response)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+ require "vsafe/response_error"
3
+
4
+ RSpec.describe VSafe::ResponseError do
5
+ let(:response_body) {
6
+ {
7
+ "ResponseCode" => "1",
8
+ "ResponseText" => "text"
9
+ }
10
+ }
11
+ describe ".new" do
12
+ it "initializes fine" do
13
+ expect { VSafe::ResponseError.new(response_body) }.to_not raise_error
14
+ end
15
+ end
16
+
17
+ describe "#code" do
18
+ it "returns code" do
19
+ expect(VSafe::ResponseError.new(response_body).code).to eq(1)
20
+ end
21
+ end
22
+
23
+ describe "#text" do
24
+ it "returns text" do
25
+ expect(VSafe::ResponseError.new(response_body).text).to eq("text")
26
+ end
27
+ end
28
+
29
+ describe "#suggested_message" do
30
+ it "returns message" do
31
+ VSafe::ResponseError::SUGGESTED_MESSAGES.keys.each do |code|
32
+ response_body["ResponseCode"] = code
33
+ expect(VSafe::ResponseError.new(response_body).suggested_message).to eq(VSafe::ResponseError::SUGGESTED_MESSAGES[code])
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,161 @@
1
+ require "spec_helper"
2
+ require "vsafe/response"
3
+ require "vsafe/response_error"
4
+ require "vsafe/request_error"
5
+
6
+ RSpec.describe VSafe::Response do
7
+ let(:error_code) { 1001 }
8
+ let(:success_body) {
9
+ {
10
+ "ResponseCode" => VSafe::Response::SUCCESS_RESPONSE_CODE,
11
+ "ResponseText" => "Success"
12
+ }
13
+ }
14
+ let(:fail_body) {
15
+ {
16
+ "ResponseCode" => error_code.to_s,
17
+ "ResponseText" => "Login Failed"
18
+ }
19
+ }
20
+
21
+ context ".new" do
22
+ context "when request successful" do
23
+ let(:httparty_response) { double(:response, success?: true, parsed_response: success_body) }
24
+
25
+ context "when result is successful" do
26
+ context "when response code is string" do
27
+ it "initializes successfully" do
28
+ response = VSafe::Response.new(httparty_response)
29
+
30
+ expect(response.response).to eq(httparty_response)
31
+ expect(response.to_hash).to eq(success_body)
32
+ expect(response.error).to eq(nil)
33
+ expect(response.success?).to eq(true)
34
+ end
35
+ end
36
+
37
+ context "when response code is integer" do
38
+ let(:success_body) {
39
+ {
40
+ "ResponseCode" => VSafe::Response::SUCCESS_RESPONSE_CODE.to_i,
41
+ "ResponseText" => "Success"
42
+ }
43
+ }
44
+
45
+ it "initializes successfully" do
46
+ response = VSafe::Response.new(httparty_response)
47
+
48
+ expect(response.response).to eq(httparty_response)
49
+ expect(response.to_hash).to eq(success_body)
50
+ expect(response.error).to eq(nil)
51
+ expect(response.success?).to eq(true)
52
+ end
53
+ end
54
+ end
55
+
56
+ context "when result errored" do
57
+ let(:httparty_response) { double(:response, success?: true, parsed_response: fail_body) }
58
+
59
+ it "initializes successful" do
60
+ response = VSafe::Response.new(httparty_response)
61
+ error = response.error
62
+ expect(response.response).to eq(httparty_response)
63
+ expect(response.to_hash).to eq(fail_body)
64
+ expect(error.code).to eq(error_code)
65
+ expect(error.text).to eq("Login Failed")
66
+ expect(response.success?).to eq(false)
67
+ end
68
+ end
69
+ end
70
+
71
+ context "when request failed" do
72
+ let(:httparty_response) { double(:response, success?: false, parsed_response: fail_body) }
73
+
74
+ it "raises error" do
75
+ expect {
76
+ VSafe::Response.new(httparty_response)
77
+ }.to raise_error(VSafe::RequestError)
78
+ end
79
+ end
80
+ end
81
+
82
+ context "#success?" do
83
+ context "when success" do
84
+ let(:httparty_response) { double(:response, success?: true, parsed_response: success_body) }
85
+
86
+ it "returns true" do
87
+ response = VSafe::Response.new(httparty_response)
88
+ expect(response.success?).to eq(true)
89
+ end
90
+ end
91
+
92
+ context "when fail" do
93
+ let(:httparty_response) { double(:response, success?: true, parsed_response: fail_body) }
94
+
95
+ it "returns false" do
96
+ response = VSafe::Response.new(httparty_response)
97
+
98
+ expect(response.success?).to eq(false)
99
+ end
100
+ end
101
+ end
102
+
103
+ context ".define_attribute_mapping" do
104
+ class ::DummyWrapperClass
105
+ attr_accessor :value
106
+
107
+ def initialize(value)
108
+ @value = value
109
+ end
110
+ end
111
+
112
+ class DummyClassNoWrapper < VSafe::Response
113
+ VSafe::Response.define_attribute_mapping("test1", "Test")
114
+ end
115
+
116
+ class DummyClassWithWrapper < VSafe::Response
117
+ VSafe::Response.define_attribute_mapping("test2", "Test", DummyWrapperClass)
118
+ end
119
+
120
+ let(:attribute_name) { "test" }
121
+ let(:response_attribute_name) { "Test" }
122
+ let(:attribute_value) { "abc" }
123
+ let(:success_body) {
124
+ {
125
+ "ResponseCode" => "0",
126
+ "ResponseText" => "Success",
127
+ response_attribute_name => attribute_value
128
+ }
129
+ }
130
+ let(:httparty_response) { double(:response, success?: true, parsed_response: success_body) }
131
+
132
+ context "when there is no wrapper" do
133
+ it "returns attribute value" do
134
+ response = DummyClassNoWrapper.new(httparty_response)
135
+
136
+ expect(response.test1).to eq(attribute_value)
137
+ end
138
+ end
139
+
140
+ context "when there is wrapper" do
141
+ it "returns attribute value" do
142
+ response = DummyClassWithWrapper.new(httparty_response)
143
+
144
+ test_result = response.test2
145
+
146
+ expect(test_result.is_a?(DummyWrapperClass)).to eq(true)
147
+ expect(test_result.value).to eq(attribute_value)
148
+ end
149
+ end
150
+ end
151
+
152
+ context "#to_hash" do
153
+ let(:httparty_response) { double(:response, success?: true, parsed_response: success_body) }
154
+
155
+ it "returns parsed_response" do
156
+ response = VSafe::Response.new(httparty_response)
157
+
158
+ expect(response.to_hash).to eq(success_body)
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ require "vsafe/responses/charge_account_to_temporary_token"
3
+ require "vsafe/card_type"
4
+
5
+ RSpec.describe VSafe::Responses::ChargeAccountToTemporaryToken do
6
+ let(:body) {
7
+ {
8
+ "ResponseCode" => VSafe::Responses::ChargeAccountToTemporaryToken::SUCCESS_RESPONSE_CODE,
9
+ "ResponseText" => "Success",
10
+ "ChargeAccountNumberToken" => "abc",
11
+ "PaymentDeviceLast4" => "1234",
12
+ "PaymentDeviceTypeCD" => VSafe::CardType::AMERICAN_EXPRESS
13
+ }
14
+ }
15
+ let(:httparty_response) { double(:response, success?: true, parsed_response: body) }
16
+
17
+ context "attribute mapping" do
18
+ it "returns value" do
19
+ response = VSafe::Responses::ChargeAccountToTemporaryToken.new(httparty_response)
20
+
21
+ expect(response.token).to eq("abc")
22
+ expect(response.last4).to eq("1234")
23
+ expect(response.card_type).to be_kind_of(VSafe::CardType)
24
+ expect(response.card_type.code).to eq(VSafe::CardType::AMERICAN_EXPRESS)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+ require "vsafe/responses/charge_authorize"
3
+
4
+ RSpec.describe VSafe::Responses::ChargeAuthorize do
5
+ let(:avs_result_code) { "1" }
6
+ let(:auth_result_code) { "2" }
7
+ let(:cvn_result_code) { "3" }
8
+ let(:charge_permanent_token) { "4" }
9
+ let(:payment_acquirer_name) { "payment_acquirer_name" }
10
+ let(:payment_id) { "payment_id" }
11
+ let(:payment_status) { "5" }
12
+ let(:success_body) {
13
+ {
14
+ "AVSResultCode" => avs_result_code,
15
+ "AuthResultCode" => auth_result_code,
16
+ "CVNResultCode" => cvn_result_code,
17
+ "ChargePermanentToken" => charge_permanent_token,
18
+ "PaymentAcquirerName" => payment_acquirer_name,
19
+ "PaymentID" => payment_id,
20
+ "PaymentStatus" => payment_status
21
+ }
22
+ }
23
+ let(:http_response) { double(:response, success?: true, parsed_response: success_body) }
24
+ let(:charge_authorize_response) { VSafe::Responses::ChargeAuthorize.new(http_response) }
25
+
26
+ context "#avs_result" do
27
+ it "returns value" do
28
+ expect(charge_authorize_response.avs_result.code).to eq(avs_result_code.to_i)
29
+ end
30
+ end
31
+
32
+ context "#auth_result" do
33
+ it "returns value" do
34
+ expect(charge_authorize_response.auth_result.code).to eq(auth_result_code.to_i)
35
+ end
36
+ end
37
+
38
+ context "#charge_permanent_token" do
39
+ it "returns value" do
40
+ expect(charge_authorize_response.charge_permanent_token).to eq(charge_permanent_token)
41
+ end
42
+ end
43
+
44
+ context "#payment_acquirer_name" do
45
+ it "returns value" do
46
+ expect(charge_authorize_response.payment_acquirer_name).to eq(payment_acquirer_name)
47
+ end
48
+ end
49
+
50
+ context "#payment_id" do
51
+ it "returns value" do
52
+ expect(charge_authorize_response.payment_id).to eq(payment_id)
53
+ end
54
+ end
55
+
56
+ context "#payment_status" do
57
+ it "returns value" do
58
+ expect(charge_authorize_response.payment_status.code).to eq(payment_status.to_i)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+ require "vsafe/responses/charge_confirm"
3
+
4
+ RSpec.describe VSafe::Responses::ChargeConfirm do
5
+ let(:payment_status) { "1" }
6
+ let(:success_body) {
7
+ {
8
+ "PaymentStatus" => payment_status
9
+ }
10
+ }
11
+ let(:http_response) { double(:response, success?: true, parsed_response: success_body) }
12
+
13
+ context "#payment_status" do
14
+ it "returns value" do
15
+ expect(VSafe::Responses::ChargeConfirm.new(http_response).payment_status.code).to eq(payment_status.to_i)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+ require "vsafe/responses/charge_sale"
3
+
4
+ RSpec.describe VSafe::Responses::ChargeSale do
5
+ let(:avs_result_code) { "1" }
6
+ let(:auth_result_code) { "2" }
7
+ let(:cvn_result_code) { "3" }
8
+ let(:charge_permanent_token) { "4" }
9
+ let(:payment_acquirer_name) { "payment_acquirer_name" }
10
+ let(:payment_id) { "payment_id" }
11
+ let(:payment_id_pk) { "payment_id_pk" }
12
+ let(:payment_status) { "5" }
13
+ let(:success_body) {
14
+ {
15
+ "AVSResultCode" => avs_result_code,
16
+ "AuthResultCode" => auth_result_code,
17
+ "CVNResultCode" => cvn_result_code,
18
+ "ChargePermanentToken" => charge_permanent_token,
19
+ "PaymentAcquirerName" => payment_acquirer_name,
20
+ "PaymentID" => payment_id,
21
+ "PaymentID_pk" => payment_id_pk,
22
+ "PaymentStatus" => payment_status
23
+ }
24
+ }
25
+ let(:http_response) { double(:response, success?: true, parsed_response: success_body) }
26
+ let(:response) { VSafe::Responses::ChargeSale.new(http_response) }
27
+ context "#avs_result" do
28
+ it "returns value" do
29
+ expect(response.avs_result.code).to eq(avs_result_code.to_i)
30
+ end
31
+ end
32
+
33
+ context "#auth_result" do
34
+ it "returns value" do
35
+ expect(response.auth_result.code).to eq(auth_result_code.to_i)
36
+ end
37
+ end
38
+
39
+ context "#charge_permanent_token" do
40
+ it "returns value" do
41
+ expect(response.charge_permanent_token).to eq(charge_permanent_token)
42
+ end
43
+ end
44
+
45
+ context "#payment_acquirer_name" do
46
+ it "returns value" do
47
+ expect(response.payment_acquirer_name).to eq(payment_acquirer_name)
48
+ end
49
+ end
50
+
51
+ context "#payment_id" do
52
+ it "returns value" do
53
+ expect(response.payment_id).to eq(payment_id)
54
+ end
55
+ end
56
+
57
+ context "#payment_id_pk" do
58
+ it "returns value" do
59
+ expect(response.payment_id_pk).to eq(payment_id_pk)
60
+ end
61
+ end
62
+
63
+ context "#payment_status" do
64
+ it "returns value" do
65
+ expect(response.payment_status.code).to eq(payment_status.to_i)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ require "vsafe/responses/get_session_tags"
3
+
4
+ RSpec.describe VSafe::Responses::GetSessionTags do
5
+ let(:org_id) { "test_org_id" }
6
+ let(:web_session_id) { "test_web_session_id" }
7
+ let(:success_body) {
8
+ {
9
+ "OrgID" => org_id,
10
+ "WebSessionID" => "test_web_session_id"
11
+ }
12
+ }
13
+ let(:http_response) { double(:response, success?: true, parsed_response: success_body) }
14
+ let(:response) { VSafe::Responses::GetSessionTags.new(http_response) }
15
+
16
+ context "#org_id" do
17
+ it "returns value" do
18
+ expect(response.org_id).to eq(org_id)
19
+ end
20
+ end
21
+
22
+ context "#web_session_id" do
23
+ it "returns value" do
24
+ expect(response.web_session_id).to eq(web_session_id)
25
+ end
26
+ end
27
+ end