Skip to content

Commit

Permalink
Merge pull request #17 from inloco/feat/add-register-payment-method
Browse files Browse the repository at this point in the history
Add register_payment method
  • Loading branch information
ottony authored Jul 14, 2023
2 parents 89fc542 + 147b52a commit 59a04ce
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.5.1] - 2023-07-13

- Allow registering payments

## [0.5.0] - 2023-04-20

- Specify dependencies version
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
incognia_api (0.5.0)
incognia_api (0.5.1)
faraday (~> 1.10)
faraday_middleware (~> 1.2)

Expand Down Expand Up @@ -81,4 +81,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.3.7
2.4.13
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

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
2 changes: 1 addition & 1 deletion lib/incognia_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Incognia
VERSION = "0.5.0"
VERSION = "0.5.1"
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
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

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

0 comments on commit 59a04ce

Please sign in to comment.