跳转到主要内容

MaxMind minFraud Score, Insights, Factors and Report Transactions API

项目描述

描述

此软件包提供了对MaxMind minFraud Score、Insights和Factors Web服务以及报告交易Web服务的API。

安装

要安装minfraud模块,请输入

$ pip install minfraud

如果您无法使用pip,您也可以从源目录使用easy_install

$ easy_install .

文档

完整的API文档可在Read the Docs上找到。

使用方法

要使用此API,创建一个新的minfraud.Client对象以进行同步请求或minfraud.AsyncClient对象以进行异步请求。构造函数接受您的MaxMind账户ID和许可证密钥

>>> client = Client(42, 'licensekey')
>>> async_client = AsyncClient(42, 'licensekey')

要使用Sandbox Web服务而不是生产Web服务,您可以提供主机参数

>>> client = Client(42, 'licensekey', 'sandbox.maxmind.com')
>>> async_client = AsyncClient(42, 'licensekey', 'sandbox.maxmind.com')

评分、洞察和因素使用

使用 factors() 方法调用因素服务

>>> client.factors({'device': {'ip_address': '152.216.7.110'}})
>>> await async_client.factors({'device': {'ip_address': '152.216.7.110'}})

使用 insights() 方法调用洞察服务

>>> client.insights({'device': {'ip_address': '152.216.7.110'}})
>>> await async_client.insights({'device': {'ip_address': '152.216.7.110'}})

使用 score() 方法调用评分网络服务

>>> client.score({'device': {'ip_address': '152.216.7.110'}})
>>> await async_client.score({'device': {'ip_address': '152.216.7.110'}})

这些方法中的每一个都接收一个字典,表示要发送到网络服务的交易。此字典的结构应符合REST API 文档中指定的格式。所有字段都是可选的。

报告交易使用

MaxMind 鼓励使用此 API,因为通过此渠道接收的数据用于不断改进我们的欺诈检测算法。使用 report() 方法调用报告交易网络服务

>>> client.report({'ip_address': '152.216.7.110', 'tag': 'chargeback'})
>>> await async_client.report({'ip_address': '152.216.7.110', 'tag': 'chargeback'})

此方法接收一个字典,表示要发送到网络服务的报告。此字典的结构应符合REST API 文档中指定的格式。必需的字段包括 tag 以及以下一个或多个:ip_addressmaxmind_idminfraud_idtransaction_id

请求验证(适用于所有请求方法)

假设未禁用验证,在将交易发送到网络服务之前,将验证交易字典的结构和内容。如果验证失败,将引发 minfraud.InvalidRequestError

如果字典有效,将向网络服务发出请求。如果请求成功,将返回服务响应的模型对象。如果请求失败,将引发以下错误之一。

错误

可能的错误包括

  • minfraud.AuthenticationError - 当服务器无法验证请求时将引发此错误,例如,如果许可证密钥或帐户 ID 无效。

  • minfraud.InvalidRequestError - 如果服务器以其他原因拒绝请求(例如,保留 IP 地址)或请求在发送到服务器之前验证失败,将引发此错误。

  • minfraud.HttpError - 当发生意外的 HTTP 错误(例如,防火墙干扰对服务器的请求)时将引发此错误。

  • minfraud.MinFraudError - 当发生其他错误(例如,来自服务器的意外内容)时将引发此错误。此错误也作为上述错误的基类。

此外,scoreinsightsfactors 还可能引发

  • minfraud.InsufficientFundsError - 如果您的帐户余额不足,将引发此错误。

示例

评分、洞察和因素示例

>>> import asyncio
>>> from minfraud import AsyncClient, Client
>>>
>>> request = {
>>>     'device': {
>>>         'ip_address': '152.216.7.110',
>>>         'accept_language': 'en-US,en;q=0.8',
>>>         'session_age': 3600,
>>>         'session_id': 'a333a4e127f880d8820e56a66f40717c',
>>>         'user_agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36'
>>>     },
>>>     'event': {
>>>         'shop_id': 's2123',
>>>         'type': 'purchase',
>>>         'transaction_id': 'txn3134133',
>>>         'time': '2014-04-12T23:20:50.052+00:00'
>>>     },
>>>     'account': {
>>>         'user_id': '3132',
>>>         'username_md5': '570a90bfbf8c7eab5dc5d4e26832d5b1'
>>>     },
>>>     'email': {
>>>         'address': '977577b140bfb7c516e4746204fbdb01',
>>>         'domain': 'maxmind.com'
>>>     },
>>>     'billing': {
>>>         'first_name': 'Jane',
>>>         'last_name': 'Doe',
>>>         'company': 'Company',
>>>         'address': '101 Address Rd.',
>>>         'address_2': 'Unit 5',
>>>         'city': 'Hamden',
>>>         'region': 'CT',
>>>         'country': 'US',
>>>         'postal': '06510',
>>>         'phone_country_code': '1',
>>>         'phone_number': '123-456-7890',
>>>     },
>>>     'shipping': {
>>>         'first_name': 'John',
>>>         'last_name': 'Doe',
>>>         'company': 'ShipCo',
>>>         'address': '322 Ship Addr. Ln.',
>>>         'address_2': 'St. 43',
>>>         'city': 'New Haven',
>>>         'region': 'CT',
>>>         'country': 'US',
>>>         'postal': '06510',
>>>         'phone_country_code': '1',
>>>         'phone_number': '123-456-0000',
>>>         'delivery_speed': 'same_day',
>>>     },
>>>     'credit_card': {
>>>         'bank_phone_country_code': '1',
>>>         'avs_result': 'Y',
>>>         'bank_phone_number': '123-456-1234',
>>>         'last_digits': '7643',
>>>         'cvv_result': 'N',
>>>         'bank_name': 'Bank of No Hope',
>>>         'issuer_id_number': '411111',
>>>         'was_3d_secure_successful': True
>>>     },
>>>     'payment': {
>>>         'decline_code': 'invalid number',
>>>         'was_authorized': False,
>>>         'processor': 'stripe'
>>>     },
>>>     'shopping_cart': [{
>>>         'category': 'pets',
>>>         'quantity': 2,
>>>         'price': 20.43,
>>>         'item_id': 'lsh12'
>>>     }, {
>>>         'category': 'beauty',
>>>         'quantity': 1,
>>>         'price': 100.0,
>>>         'item_id': 'ms12'
>>>     }],
>>>     'order': {
>>>         'affiliate_id': 'af12',
>>>         'referrer_uri': 'http://www.amazon.com/',
>>>         'subaffiliate_id': 'saf42',
>>>         'discount_code': 'FIRST',
>>>         'currency': 'USD',
>>>         'amount': 323.21
>>>      },
>>>     'custom_inputs': {
>>>         'section': 'news',
>>>         'num_of_previous_purchases': 19,
>>>         'discount': 3.2,
>>>         'previous_user': True
>>>     }
>>> }
>>>
>>> # This example function uses a synchronous Client object. The object
>>> # can be used across multiple requests.
>>> def client(account_id, license_key):
>>>     with Client(account_id, license_key) as client:
>>>
>>>         print(client.score(request))
Score(...)
>>>
>>>         print(client.insights(request))
Insights(...)
>>>
>>>         print(client.factors(request))
Factors(...)
>>>
>>> # This example function uses an asynchronous AsyncClient object. The
>>> # object can be used across multiple requests.
>>> async def async_client(account_id, license_key):
>>>     async with AsyncClient(account_id, license_key) as client:
>>>
>>>         print(await client.score(request))
Score(...)
>>>
>>>         print(await client.insights(request))
Insights(...)
>>>
>>>         print(await client.factors(request))
Factors(...)
>>>
>>> client(42, 'license_key')
>>> asyncio.run(async_client(42, 'license_key'))

报告交易示例

对于同步报告

>>> from minfraud import Client
>>>
>>> with Client(42, 'licensekey') as client
>>>     transaction_report = {
>>>         'ip_address': '152.216.7.110',
>>>         'tag': 'chargeback',
>>>         'minfraud_id': '2c69df73-01c0-45a5-b218-ed85f40b17aa',
>>>      }
>>>      client.report(transaction_report)

对于异步报告

>>> import asyncio
>>> from minfraud import AsyncClient
>>>
>>> async def report():
>>>     async with AsyncClient(42, 'licensekey') as client
>>>         transaction_report = {
>>>             'ip_address': '152.216.7.110',
>>>             'tag': 'chargeback',
>>>             'minfraud_id': '2c69df73-01c0-45a5-b218-ed85f40b17aa',
>>>          }
>>>          await async_client.report(transaction_report)
>>>
>>> asyncio.run(report())

要求

需要 Python 3.8 或更高版本。不支持旧版本。

版本控制

minFraud Python API 使用 语义版本控制

支持

请使用GitHub 问题跟踪器报告与此代码相关的所有问题。

如果您遇到的问题不是针对客户端 API 的 MaxMind 服务特定问题,请联系MaxMind 支持寻求帮助。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

minfraud-2.11.0.tar.gz (51.4 kB 查看哈希值)

上传时间

构建分发

minfraud-2.11.0-py3-none-any.whl (28.1 kB 查看哈希值)

上传时间 Python 3

支持