Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add register_payment method #17

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,84 @@ assessment = api.register_login(
# => #<OpenStruct id="...", device_id="...", risk_assessment="..", evidence=...>
```

### Registering Payment
lucaswilliamgomes marked this conversation as resolved.
Show resolved Hide resolved
lucaswilliamgomes marked this conversation as resolved.
Show resolved Hide resolved

This method registers a new payment for the given installation and account, returning a `hash`,
containing the risk assessment and supporting evidence.

```ruby
assessment = api.register_payment(
installation_id: 'installation-id',
account_id: 'account-id'
)

# => #<OpenStruct id="...", device_id="...", risk_assessment="..", evidence=...>
```

It also supports optional parameters, for example:

```ruby
addresses = [
{
'type': 'shipping',
'structured_address': {
'locale': 'pt-BR',
'country_name': 'Brasil',
'country_code': 'BR',
'state': 'SP',
'city': 'São Paulo',
'borough': '',
'neighborhood': 'Bela Vista',
'street': 'Av. Paulista',
'number': '1578',
'complements': 'Andar 2',
'postal_code': '01310-200'
},
'address_coordinates': {
'lat': -23.561414,
'lng': -46.6558819
}
}
]

payment_value = {
'amount': 5.0,
'currency': 'BRL'
}

payment_methods = [
{
'type': 'credit_card',
'credit_card_info': {
'bin': '123456',
'last_four_digits': '1234',
'expiry_year': '2027',
'expiry_month': '10'
}
},
{
'type': 'debit_card',
'debit_card_info': {
'bin': '123456',
'last_four_digits': '1234',
'expiry_year': '2027',
'expiry_month': '10'
}
}
]

assessment = api.register_payment(
installation_id: 'installation-id',
account_id: 'account-id',
external_id: 'external-id',
addresses: addresses,
payment_value: payment_value,
payment_methods: payment_methods
)

# => #<OpenStruct id="...", device_id="...", risk_assessment="..", evidence=...>
```

### Registering a Feedback

This method registers a feedback event for the given identifiers (optional arguments), returning true when success.
Expand Down
1 change: 1 addition & 0 deletions lib/incognia_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative "incognia_api/resources/api_resource"
require_relative "incognia_api/resources/signup_assessment"
require_relative "incognia_api/resources/login_assessment"
require_relative "incognia_api/resources/payment_assessment"
require_relative "incognia_api/resources/credentials"

require_relative "incognia_api/constants/feedback_event"
Expand Down
13 changes: 13 additions & 0 deletions lib/incognia_api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,18 @@ def register_feedback(event: , timestamp: nil, **ids)

response.success?
end

def register_payment(installation_id:, account_id:, **opts)
params = { installation_id: installation_id, account_id: account_id, type: :payment }
params.merge!(opts)

response = connection.request(
:post,
'v2/authentication/transactions',
params
)

PaymentAssessment.from_hash(response.body) if response.success?
end
end
end
5 changes: 5 additions & 0 deletions lib/incognia_api/resources/payment_assessment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require_relative "api_resource"

module Incognia
class PaymentAssessment < APIResource; end
end
36 changes: 36 additions & 0 deletions spec/fixtures/payment-unknown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"id": "f3bf8d46-1abcd-2abcd-3abcd-51f95b05f6fb",
"risk_assessment": "unknown",
"reasons": [],
"evidence": {
"device_model": "iPhone9,3",
"known_account": false,
"location_services": {
"location_permission_enabled": true,
"location_sensors_enabled": true
},
"device_integrity": {
"probable_root": false,
"emulator": false,
"gps_spoofing": false,
"from_official_store": true,
"app_tampering": false,
"installation_source": "not_available"
},
"device_fraud_reputation": "unknown",
"device_behavior_reputation": "unknown",
"accessed_accounts": 2,
"app_reinstallations": 0,
"active_installations": 2,
"first_device_login": true,
"app_tampering": {
"result": "not_available",
"app_debugging": "not_available",
"code_injection": "not_available",
"signature_mismatch": "not_available",
"package_mismatch": "not_available"
}
},
"installation_id": "installation_id",
"device_id": "device_id"
}
13 changes: 13 additions & 0 deletions spec/helpers/api_spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def self.included(rspec)
rspec.let(:unknown_login_fixture) do
File.new("spec/fixtures/login-unknown.json").read
end
rspec.let(:unknown_payment_fixture) do
File.new("spec/fixtures/payment-unknown.json").read
end
lucaswilliamgomes marked this conversation as resolved.
Show resolved Hide resolved
rspec.let(:missing_required_params_fixture) do
File.new("spec/fixtures/missing-required-params.json").read
end
Expand Down Expand Up @@ -84,6 +87,16 @@ def stub_login_request
headers: { 'Content-Type' => 'application/json' })
end

def stub_payment_request
stub_request(
:post, "https://api.incognia.com/api/v2/authentication/transactions"
).with(body: hash_including(type: 'payment')).
to_return(
status: 200,
body: unknown_payment_fixture,
headers: { 'Content-Type' => 'application/json' })
end
lucaswilliamgomes marked this conversation as resolved.
Show resolved Hide resolved

def stub_register_feedback_request
stub_request(:post, "https://api.incognia.com/api/v2/feedbacks").
to_return(
Expand Down
80 changes: 79 additions & 1 deletion spec/incognia_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,85 @@ module Incognia

end

describe "#register_payment" do
let(:installation_id) { SecureRandom.uuid }
let(:account_id) { SecureRandom.uuid }

it "when successful returns the resource" do
stub_token_request
stub_payment_request

payment = api.register_payment(
installation_id: installation_id,
account_id: account_id
)

expected = JSON.parse(unknown_payment_fixture, symbolize_names: true)
expect(payment.id).to eql expected[:id]
expect(payment.risk_assessment).to eql expected[:risk_assessment]
expect_evidences_to_match(payment, expected)
end

context "HTTP request" do
it "hits the endpoint with installation_id and account_id" do
stub_token_request

stub = stub_payment_request.with(
body: {
type: 'payment',
installation_id: installation_id, account_id: account_id
},
headers: {
'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/
}
)

payment = api.register_payment(
installation_id: installation_id,
account_id: account_id
)

expect(stub).to have_been_made.once
end

context 'when receiving any other optional arguments' do
shared_examples_for 'receiving optional args' do |optional_arguments|
it "hits the endpoint also with #{optional_arguments}" do
stub_token_request

stub = stub_payment_request.with(
body: {
type: 'payment',
installation_id: installation_id,
account_id: account_id
}.merge(opts),
headers: {
'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/
}
)

payment = api.register_payment(
installation_id: installation_id,
account_id: account_id,
**opts
)

expect(stub).to have_been_made.once
end
end

it_behaves_like 'receiving optional args', 'external_id', 'payment request', 'aaa' do
let(:opts) { { external_id: 'external-id' } }
end
it_behaves_like 'receiving optional args', 'payment_value' do
let(:opts) { { payment_value: { 'amount': 5.0, 'currency': 'BRL' } } }
end
it_behaves_like 'receiving optional args', 'external_id and payment_value' do
let(:opts) { { external_id: 'external-id', payment_value: 12.5 } }
end
end
end
end

describe "#register_feedback" do
let(:event) { Incognia::Constants::FeedbackEvent.constants.sample.to_s }
Expand Down Expand Up @@ -389,5 +468,4 @@ def expect_evidences_to_match(model, expected)
to eql expected[:evidence][:location_services][:location_sensors_enabled]
end
end

end