2017 © Pedro Peláez
 

library sift-php

Sift Science PHP library

image

siftscience/sift-php

Sift Science PHP library

  • Friday, July 20, 2018
  • by lopatin
  • Repository
  • 94 Watchers
  • 13 Stars
  • 338,911 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 24 Forks
  • 6 Open issues
  • 24 Versions
  • 9 % Grown

The README.md

Sift Bindings CircleCI

Installation

With Composer

  1. Add siftscience/sift-php as a composer dependency

```shell script composer require siftscience/sift-php, (*1)


2. Now `SiftClient` will be autoloaded into your project. ```php require 'vendor/autoload.php'; $sift = new SiftClient([ 'api_key' => 'my_api_key', 'account_id' => 'my_account_id' ]); // or Sift::setApiKey('my_api_key'); Sift::setAccountId('my_account_id'); $sift = new SiftClient();

Manually

  1. Download the latest release., (*2)

  2. Extract into a folder in your project root named "sift-php"., (*3)

  3. Include SiftClient in your project like this:, (*4)

    require 'sift-php/lib/SiftRequest.php';
    require 'sift-php/lib/SiftResponse.php';
    require 'sift-php/lib/SiftClient.php';
    require 'sift-php/lib/Sift.php';
    
    
    $sift = new SiftClient([
        'api_key' => 'my_api_key',
        'account_id' => 'my_account_id'
    ]);
    

Usage

Track an event

To learn more about the Events API visit our developer docs., (*5)

Optional Params - return_score: :true or :false - return_action: :true or :false - return_workflow_status: :true or :false - return_route_info: :true or :false - force_workflow_run: :true or :false - include_score_percentiles: :true or :false - warnings: :true or :false - abuse_types: ["payment_abuse", "content_abuse", "content_abuse", "account_abuse", "legacy", "account_takeover"], (*6)

Here's an example that sends a $transaction event to Sift., (*7)

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->track('$transaction', [
    '$user_id' => '23056',
    '$user_email' => 'buyer@gmail.com',
    '$seller_user_id' => '2371',
    '$transaction_id' => '573050',
    '$currency_code' => 'USD',
    '$amount' => 15230000,
    '$time' => 1327604222,
    'seller_user_email' => 'seller@gmail.com',
    'trip_time' => 930,
    'distance_traveled' => 5.26,
]);

Label a user as good/bad

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->label('23056', [
    '$is_bad' => true,
    '$abuse_type' => 'promotion_abuse'
]);

Unlabel a user

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->unlabel('23056', ['abuse_type' => 'content_abuse']);

Get a user's score

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->score('23056');
$response->body['scores']['payment_abuse']['score']; // => 0.030301357270181357

Get the status of a workflow run

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getWorkflowStatus('my_run_id');
$response->body['state']; // => "running"

Get the latest decisions for a user

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getUserDecisions('example_user');
$response->body['decisions']['account_abuse']['decision']['id']; // => "ban_user"

Get the latest decisions for an order

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getOrderDecisions('example_order');
$response->body['decisions']['payment_abuse']['decision']['id']; // => "ship_order"

Get the latest decisions for a session

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->getSessionDecisions('example_user', 'example_session');
$response->body['decisions']['account_takeover']['decision']['id']; // => "session_decision"

List of configured Decisions

Optional Params - entity_type: user or order or session - abuse_types: ["payment_abuse", "content_abuse", "content_abuse", "account_abuse", "legacy", "account_takeover"], (*8)

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $this->client->getDecisions(['entity_type' => 'example_entity_type','abuse_types' => 'example_abuse_types']);
$response->isOk()

Apply decision to a user

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToUser('example_user','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

Apply decision to an order

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToOrder('example_user','example_order','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

Apply decision to a session

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->applyDecisionToSession('example_user','example_session','example_decision','example_source',['analyst' => 'analyst@example.com']
$response->isOk()

Creates a new webhook with a specified URL.

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->postWebhooks(["payload_type" => "ORDER_V1_0",
    "status"=> "active",
    "url"=> "https://example1.com/",
    "enabled_events" => ['$create_order'],
    "name"=> "My webhook name",
    "description"=> "This is a webhook!"]);
$response->isOk()

Retrieves a webhook when given an ID.

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->retrieveWebhook('webhook_id');
$response->isOk()

List All Webhooks

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->listAllWebhooks();
$response->isOk()

Update a Webhook

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->updateWebhook('webhook_id', ["payload_type" => "ORDER_V1_0",
    "status"=> "active",
    "url"=> "https://example1.com/",
    "enabled_events" => ['$create_order'],
    "name"=> "My webhook name update",
    "description"=> "This is a webhook! update"]);
$response->isOk()

Deletes a webhook when given an ID.

$sift = new SiftClient(['api_key' => 'my_api_key', 'account_id' => 'my_account_id']);
$response = $sift->deleteWebhook('webhook_id');
$response->isOk()

Contributing

Run the tests from the project root with PHPUnit like this:, (*9)

composer update
composer exec phpunit -v -- --bootstrap vendor/autoload.php test

Updating Packagist

  1. Update composer.json to reflect the new version, as well as any new requirements then merge changes into master., (*10)

  2. Create a new release with the version number and use it as the description. Packagist will automatically deploy a new package via the configured webhook., (*11)

HTTP connection pooling

You can substantially improve the performance of SiftClient by using HTTP connection pooling. Because standard PHP/fastcgi deployments don't have a mechanism for persisting connections between requests, the easiest way to pool connections is by routing requests through a proxy like Apache httpd or nginx., (*12)

Apache httpd, (*13)

Listen 8081

...

LoadModule proxy_module .../mod_proxy.so
LoadModule proxy_http_module .../mod_proxy_http.so
LoadModule ssl_module .../mod_ssl.so

<VirtualHost localhost:8081>
    ServerName api.sift.com
    SSLProxyEngine on
    SSLProxyVerify require
    SSLProxyVerifyDepth 3
    SSLProxyCACertificateFile ...
    ProxyPass / https://api.sift.com/
</VirtualHost>

nginx, (*14)

# Read more about nginx keepalive: https://www.nginx.com/blog/tuning-nginx/#keepalive
upstream sift {
    server api.sift.com:443;
    keepalive 16;
}

server {
    listen localhost:8081;
    server_name api.sift.com;

    location / {
        proxy_pass https://sift;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_ssl_verify on;
        proxy_ssl_verify_depth 3;
        proxy_ssl_trusted_certificate ...;
        proxy_ssl_name api.sift.com;
        proxy_ssl_server_name on;
    }
}

For Debian-based distributions, the certificate file is /etc/ssl/certs/ca-certificates.crt, (*15)

Then, instantiate SiftClient to route requests through the proxy:, (*16)

$sift = new SiftClient([
    'api_key' => 'my_api_key',
    'account_id' => 'my_account_id',
    'api_endpoint' => 'http://api.sift.com',
    'curl_opts' => [CURLOPT_CONNECT_TO => ['api.sift.com:80:localhost:8081']],
]);

Integration testing app

For testing the app with real calls it is possible to run the integration testing app, it makes calls to almost all our public endpoints to make sure the library integrates well. At the moment, the app is run on every merge to master, (*17)

How to run it locally

  1. Add env variable ACCOUNT_ID with the valid account id
  2. Add env variable API_KEY with the valid Api Key associated from the account
  3. Run the following under the project root folder
# install the lib from the local source code
composer install -n
# run the app
php test_integration_app/main.php

License

MIT, (*18)

The Versions

20/07 2018

dev-master

9999999-dev https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

10/07 2018

dev-rlong-wf

dev-rlong-wf https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

04/06 2018

dev-mlegore_get_session_decisions

dev-mlegore_get_session_decisions https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

06/04 2018

3.0.1

3.0.1.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

06/04 2018

dev-kkarpierz_fix_docs

dev-kkarpierz_fix_docs https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

28/03 2018

3.0.0

3.0.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

28/03 2018

dev-kkarpierz_update_v205

dev-kkarpierz_update_v205 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

13/02 2018

2.2.0

2.2.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

13/02 2018

dev-mjouahri_add_session_decisions

dev-mjouahri_add_session_decisions https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee
by Mohammed Jouahri

php fraud sift science sift

14/09 2017

dev-gary_php_2.1.1

dev-gary_php_2.1.1 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee

php fraud sift science sift

14/09 2017

2.1.1

2.1.1.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim
by Gary Lee

php fraud sift science sift

31/03 2017

2.1.0

2.1.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim

php fraud sift science sift

29/09 2016

2.0.1

2.0.1.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim

php fraud sift science sift

19/07 2016

2.0.0

2.0.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin
by Jacob Burnim

php fraud sift science sift

14/04 2016

1.2.2

1.2.2.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

14/04 2016

dev-yoav_bump_version

dev-yoav_bump_version https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

17/03 2016

dev-nick_add_return_action

dev-nick_add_return_action https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

29/02 2016

1.2.1

1.2.1.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

04/02 2015

1.2.0

1.2.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

03/09 2014

1.1.1.0

1.1.1.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

by Yoav Schatzberg
by Alex Lopatin

php fraud sift science sift

02/06 2014

1.0.4

1.0.4.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0

 

The Development Requires

php fraud sift science sift

07/04 2014

v1.0.3

1.0.3.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0
  • pear-pear/services_json *

 

The Development Requires

php fraud sift science sift

29/01 2014

v1.0.2

1.0.2.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

MIT

The Requires

  • php >=5.0.0
  • pear-pear/services_json *

 

The Development Requires

php fraud sift science sift

29/01 2014

1.0.0

1.0.0.0 https://github.com/SiftScience/sift-php

Sift Science PHP library

  Sources   Download

The Requires

  • php >=5.0.0
  • pear-pear/services_json *

 

The Development Requires

php fraud sift science sift