Fraud Detection Implementation
The fraud detection model offers a common interface for integration of arbitrary fraud detection services, as well as a standard integration included with SAP Commerce.
SAP Commerce includes a standard provider that checks for a number of fraud symptoms without the need for an external system. However, this standard service should be used in combination with a third-party service provider as a first-level check in order to reduce the number of paid requests to the third party provider. You can extend this service by adding custom symptoms.
Implementation
The following diagram shows the fraud service and all related items, result objects and included provider implementations.
The central service used for business logic is FraudService, which provides access to all available FraudServiceProvider services.
Perform Fraud Checking
This section illustrates how to perform fraud checking, how the obtained results should be handled, and how fraud detection status can be persisted. It also explains how the standard SAP Commerce Cloud fraud service provider works and how to extend it.
The fraud detection model functions by testing for several symptoms. These symptoms are usually weighted differently according to their importance. Therefore the result of a fraud check produces a list of tested, respectively positive symptoms, each of which has a numerical score showing its importance. The overall score is the sum of all these symptom values.
FraudService fraudService = ...
Order placedOrder = ...
// using standard provider named 'Hybris'
FraudServiceResponse result = fraudService.recognizeOrderFraudSymptoms("Hybris",placedOrder);
// this is the overall score
double score = result.getScore();
// we're also able to check all result symptoms
for( FraudSymptom symptom : result.getSymptoms() )
{
double singleScore = symptom.getScore();
String symptomCode = symptom.getSymptom();
String explanationText = symptom.getExplanation();
}
The FraudServiceResponse is a plain Java object. The service caller is responsible to store its information elsewhere.
Storing Fraud Reports
Once a fraud check result was obtained and the caller decided whether or not the order is fraudulent, this information should be persisted for retrieval in further order processing.
The item type FraudReport is designed to store fraud check results for a particular order.
It enables to store the overall score result and evaluation statuses of particular symptoms configured in the fraud service.
The following snippet shows how to store the fraud evaluation response data in the default model:
ModelService modelService = ...
Order placedOrder = ...
FraudServiceResponse result = ...
final FraudReportModel fraudReport = modelService.create(FraudReportModel.class);
fraudReport.setOrder(placedOrder);
fraudReport.setStatus(status);
fraudReport.setProvider(providerName);
fraudReport.setTimestamp(new Date());
fraudReport.setCode(" .. "); //your own report code generation
List<FraudSymptomScoringModel> symptoms = new ArrayList<FraudSymptomScoringModel>();
// store info for each symptom
for (final FraudSymptom symptom : result.getSymptoms())
{
final FraudSymptomScoringModel symptomScoring = modelService.create(FraudSymptomScoringModel.class);
symptomScoring.setFraudReport(fraudReport);
symptomScoring.setName(symptom.getSymptom());
symptomScoring.setExplanation(symptom.getExplanation());
symptomScoring.setScore(symptom.getScore());
symptoms.add(symptomScoring);
}
fraudReport.setFraudSymptomScorings(symptoms);
modelService.save(fraudReport)
Standard Fraud Detection Provider
The fraud functionality contains a standard fraud detection service that does not rely on third-party service providers. This service should therefore be seen as a first-level check, which should be supplemented with an integrated third-party service.
Each supported symptom is implemented as OrderFraudSymptomDetection and can be added or removed via the spring configuration. This approach also enables you to add new symptom check implementations when required.
Supported Symptoms
Currently the following sample implementations are available:
|
Class Name de.hybris.platform.fraud.symptom.impl |
Description |
|---|---|
| BlackListSymptom |
Sample - based on a static email/userId list. |
| WhiteListSymptom |
Sample - based on a static email/userId list. |
| FirstTimeOrderSymptom |
Checks if user is a first timer. |
| DifferentAddressesSymptom |
Sample - checks if address rules apply. |
| OrderThresholdSymptom |
Sample - checks if an order value threshold was reached. |
| OrderEntriesSymptom |
Sample - checks if product order limit applies (in the mockup: maxNumberPerOrder only). |
The actual limit per product is defined in ProductOrderLimit and is responsible to specify limit settings for one or more products.
Specify the ProductOrderLimit attributes to define a product's ordering limitations:
|
Attribute Name |
Type |
Description |
|---|---|---|
| code |
String |
Human recognizable identifier of the limit. |
| maxNumberPerOrder |
Integer |
Maximum allowed amount of a given product in the cart. Used by the OrderEntriesSymptom detection. |
| intervalValue |
Integer |
Quantifier of interval definition. |
| intervalResolution |
Enumeration of time units (DAY, WEEK, MONTH,..etc.) |
unit of interval definition. Together with intervalValue fully describes the time interval, for example (2,DAY), (1, WEEK). |
| intervalMaxOrdersNumber |
Integer |
Maximum allowed amount of given orders in the defined time interval. |
How To Extend
To extend, you can implement a new symptom check and add it to the standard service configuration.
public class MySymptom implements OrderFraudSymptomDetection
{
public FraudServiceResponse recognizeSymptom(FraudServiceResponse fraudResponse, AbstractOrderModel order)
{
if (... positive ... )
{
fraudResponse.addSymptom(
new FraudSymptom(
"...explanation...",
100d, // score
"MySymptom" // symptom code
)
);
}
else // optionally we may even add a symptom to result if test was negative
{
fraudResponse.addSymptom( new FraudSymptom( 0d,"MySymptom") );
}
return fraudResponse;
}
}
The Spring configuration should look like this:
<bean id="mySymptom" class="MySymptom">
<!-- replace definition of default hybris fraud provider -->
<bean id="defaultHybrisFraudServiceProvider" class="de.hybris.platform.fraud.impl.DefaultHybrisFraudServiceProvider">
<property name="providerName" value="Hybris"/>
<property name="symptomList">
<list>
<ref bean="blackListSymptom"/>
...
<ref bean="mySymptom"/>
</list>
</property>
</bean>
Defining the Importance of Fraud Symptoms
You may want to recognize some of the fraud symptoms as very important (for example a user from a black list) or less important. To do so, you can set the symptom fraud score increment in the spring configuration. This value, when the given symptom occurs, influences the order overall fraud evaluation.
All symptoms extend from the AbstractOrderFraudSymptomDetection class:
public abstract class AbstractOrderFraudSymptomDetection implements OrderFraudSymptomDetection
{
public static final double DEFAULT_INCREMENT = 50;
private double increment = DEFAULT_INCREMENT;
private String symptomName;
protected FraudSymptom createSymptom(final boolean positive)
{
return createSymptom(null, positive);
}
protected FraudSymptom createSymptom(final String explanation, final boolean positive)
{
return new FraudSymptom(explanation, positive ? getIncrement() : 0, getSymptomName());
}
public String getStrategyName()
{
return getSymptomName() + STRATEGY_SUFFIX;
}
public String getSymptomName()
{
return symptomName;
}
@Required
public void setSymptomName(final String name)
{
this.symptomName = name;
}
/**
* @return the value to be used as score in case the symptom has been tested positively.
*/
public double getIncrement()
{
return increment;
}
public void setIncrement(final double increment)
{
this.increment = increment;
}
}
If you do not specify any score increment for your particular symptom, the default increment (+50) is taken from the abstract class.
As an example, the WhiteListSympton redefines the increment to -500, in order to favor orders from trusted users:
<bean id="whiteListSymptom" class="de.hybris.platform.fraud.symptom.impl.WhiteListSymptom"> <property name="symptomName" value="White list" /> <property name="increment" value="-500" /> <property name="favoredEmails"> <list> <value>goldMember@gmail.com</value> <value>silverMember@gmail.com</value> </list> </property> <property name="favoredUserIDs"> <list> <value>goldMember</value> <value>silverMember</value> </list> </property> </bean>
Integrating an External Fraud Detection Provider
Integrating an external provider requires an implementation of FraudServiceProvider. Its responsibility is to produce FraudServiceResponse objects. There are no further constraints.
A response can be produced according to order criteria, such as suspicious addresses, cart content, or according to user activity criteria like frequently changed registration data. The former is offered by the standard provider behind the recognizeOrderFraudSymptoms method, but the latter is designed for future development purposes. In the existing implementation, it is not implemented, hence the recognizeUserActivitySymptoms method should not be called.
Response objects may contain symptom information or simply the overall score. Providing explanation texts is also optional.
public class MyDummyProvider implements FraudServiceProvider
{
public String getProviderName()
{
return "Dummy";
}
public FraudServiceResponse recognizeOrderFraudSymptoms(AbstractOrderModel order)
{
return new FraudServiceResponse(getProviderName())
{
public double getScore()
{
return 0;
}
}
}
public FraudServiceResponse recognizeUserActivitySymptoms(UserModel user)
{
throw new RuntimeException("no implemented");
}
}
Then replace the fraud service definition to add this provider.
<bean id="myDummyProvider" class="MyDummyProvider">
<bean id="defaultFraudService" class="de.hybris.platform.fraud.impl.DefaultFraudService">
<property name="providers">
<list>
<ref bean="internalFraudServiceProvider"/>
<ref bean="commercialFraudServiceProvider"/>
<ref bean="myDummyProvider"/>
</list>
</property>
</bean>
Fraud Detection as Part of Fulfillment Process
The fraud service can be used in the order fulfillment process, where the order evaluation can take place in the process node that is executed before the order is sent for fulfillment.
Check the related example process of the order management. Note that the fraudCheck node either redirects the process to manual order check by the customer service if there is fraud risk, and otherwise continues with normal fulfillment.
The fraudCheck node should be implemented in a node action that extends from the AbstractSimpleOrderDecisionAction as the output decision follows from fraud evaluation: FRAUD, or NO FRAUD (which refers to the natural decision-action transitions: OK, NOK).
Here is a sample fraud check node as a part of fulfillment process:
public class MyFraudCheckAction extends AbstractSimpleOrderDecisionAction
{
private FraudService fraudService;
private String providerName;
private int scoreLimit;
@Override
public Transition executeAction(final OrderProcessModel process)
{
final OrderModel order = process.getOrder();
final FraudServiceResponse response = fraudService.recognizeOrderSymptoms(providerName, order);
final double score = response.getScore();
if (score < scoreLimit)
{
final FraudReportModel fraudReport = createFraudReport(providerName, response, order, FraudStatus.OK);
order.setFraudulent(Boolean.FALSE);
order.setPotentiallyFraudulent(Boolean.FALSE);
order.setStatus(OrderStatus.FRAUD_CHECKED);
modelService.save(fraudReport);
modelService.save(order);
return Transition.OK;
}
else
{
final FraudReportModel fraudReport = createFraudReport(providerName, response, order, FraudStatus.FRAUD);
order.setFraudulent(Boolean.TRUE);
order.setPotentiallyFraudulent(Boolean.FALSE);
order.setStatus(OrderStatus.FRAUD_CHECKED);
modelService.save(fraudReport);
modelService.save(order);
return Transition.NOK;
}
}
private FraudReportModel createFraudReport(final String providerName, final FraudServiceResponse response, final OrderModel order,
final FraudStatus satus)
{
final FraudReportModel report;
report = ...
//save the fraud evaluation report in data base for later use.
return report;
}
@Required
public void setFraudService(final FraudService fraudService)
{
this.fraudService = fraudService;
}
public void setProviderName(final String providerName)
{
this.providerName = providerName;
}
public void setScoreLimit(final int scoreLimit)
{
this.scoreLimit = scoreLimit;
}
}
<bean id="fraudCheckOrderInternal" class="de.hybris.platform.fulfilment.actions.MyFraudCheckAction" parent="abstractAction"> <property name="fraudService" ref="fraudService"/> <property name="providerName" value="hybris"/> </bean>
Such a sample fraud checking node should make use of the fraudService to evaluate a processed order and use simple comparison with a defined threshold value in order to distinguish fraudulent from non-fraudulent orders. Besides setting proper flags on the order: order.FRAUDULENT, order.STATUS, the node should make proper transition to your next process nodes.
In the example above, the threshold value is injected via Spring. You may want to provide the threshold value differently, in a way that best suits your business scenarios.
The sample node does not show how to save the fraud check reports. Such reports may be used later to manually evaluate orders later. You can use the default, out-of-the-box data model to save fraud reports. Have in mind that in your business scenario, you can save the fraud evaluation results differently, for example in files.