Kount
Ruby interface for the Kount fraud detection service
Installation
Add this line to your application's Gemfile:
gem 'kount'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kount
Usage
Setup
Set up your credentials that you configured within the Kount administrative interface. If you have a login for Kount, the folks at Kount should also have supplied you with a guide for generating your API and Risk Inquiry service certificates. The ultimate result is a certificate and key file in .pem format. There should be one of these each for the API and Risk Inquiry services, and for the test and production environments for a total of 8 files.
Suggested set up for a Rails app (place in an initializer):
# Set custom variables within gem
if Rails.env == 'production'
# API certificate
Kount.api_cert = 'config/keys/awc.kount.net.cert.pem'
Kount.api_cert_key = 'config/keys/awc.kount.net.key.pem'
# Risk Inquiry System Certificate
Kount.ris_cert = 'config/keys/ris.kount.net.cert.pem'
Kount.ris_cert_key = 'config/keys/ris.kount.net.key.pem'
Kount.mode = :production
else
# API certificate
Kount.api_cert = 'config/keys/awc.test.kount.net.cert.pem'
Kount.api_cert_key = 'config/keys/awc.test.kount.net.key.pem'
# Risk Inquiry System Certificate
Kount.ris_cert = 'config/keys/ris.test.kount.net.cert.pem'
Kount.ris_cert_key = 'config/keys/ris.test.kount.net.key.pem'
Kount.mode = :test
end
Also set up your merchant ID (this should also have been supplied to you by Kount):
Kount.merchant_id = '123456'
Making calls
In the following example, I am updating the status of an order to Declined through an API call to the Orders status endpoint. The basic usage is to first create a payload hash:
payload = {
['status[',kount_transaction_id,']'].join => 'D',
['note[',kount_transaction_id,']'].join => text
}
Then instantiate a Kount::Request object and call "transmit" on it. Arguments:
1: The symbol :api to indicate it is an API call, as opposed to the Risk Inquiry service which would be :risk 2: The endpoint to call 3: 'get' or 'post' 4: The payload hash constructed in the previous step
ou = Kount::Request.new
ou.transmit(:api,'v1/orders/status.json','post',payload)
Risk Inquiry Service
Since the Risk Inquiry service has a very specific set of inputs, the Kount::RiskInquiry class is a layer of abstraction which accepts all of the input fields described in the Kount docs as accessor methods.
Instantiate the object:
ri = Kount::RiskInquiry.new
Set all the necessary fields to make a successful call:
ri.mode = 'Q' # Standard mode for initial post
ri.mack = 'Y'
ri.ipad = created_by_ip_address
ri.uniq = buyer.id
ri.name = buyer.name
# et cetera
Array fields accept arrays. You can set the array directly or iterate over another array you have prepared:
ri.prod_type = prods.map(&:name)
# or
ri.prod_type = []
prods.each { |prod|
ri.prod_type << prod.name
}
Custom fields that you have configured within Kount can be passed as a hash:
ri.custom_fields = {
:customfield1 => 'value1',
:customfield2 => 'value2'
}
Transmit the inquiry, which will return a Kount::Response object:
ri.request
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request