Skip to content

Commit

Permalink
Merge pull request #86 from sabitha-varghese/MSUE-123
Browse files Browse the repository at this point in the history
Updated PSP Merchant Management API
  • Loading branch information
sbogolii-sift authored Nov 7, 2022
2 parents 45bd6ff + c96098c commit abcb4ad
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
.idea
nbproject
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["sift", "sift science", "php", "fraud"],
"homepage": "https://github.com/SiftScience/sift-php",
"license": "MIT",
"version": "4.1.0",
"version": "4.2.0",
"authors": [
{
"name": "Alex Lopatin"
Expand Down
155 changes: 155 additions & 0 deletions lib/SiftClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,140 @@ public function resend($parameters, $opts = array()) {
}
}


/**
* Get a list of PSP Merchant profiles.
* @param array $parameters An array of name-value pairs .
*
* @param array $opts Array of optional parameters for this request:
* - string 'account_id': by default, this client's account ID is used.
* - int 'timeout': By default, this client's timeout is used.
*
* @return null|SiftResponse
*/
public function merchants($parameters, $opts = array()) {
$this->mergeArguments($opts, array(
'account_id' => $this->account_id,
'timeout' => $this->timeout,
));
if (isset($parameters['batch_token']))
$this->validateArgument($parameters['batch_token'], 'batch_token', 'string');
if (isset($parameters['batch_size']))
$this->validateArgument($parameters['batch_size'], 'batch_size', 'integer');
$this->validateArgument($this->account_id, 'accountId', 'string');

$url = ($this->api_endpoint .
'/v3/accounts/' . rawurlencode($opts['account_id']) .
'/psp_management/merchants');

try {
$request = new SiftRequest($url, SiftRequest::GET, $opts['timeout'], self::API3_VERSION, array('auth' => $this->api_key . ':', 'body' => $parameters));
return $request->send();
} catch (Exception $e) {
$this->logError($e->getMessage());
return null;
}
}

/**
* Create a new PSP Merchant profile.
* @param array $parameters An array of merchant profile data .
*
* @param array $opts Array of optional parameters for this request:
* - string 'account_id': by default, this client's account ID is used.
* - int 'timeout': By default, this client's timeout is used.
*
* @return null|SiftResponse
*/
public function postMerchant($parameters, $opts = array()) {
$this->mergeArguments($opts, array(
'timeout' => $this->timeout,
'account_id' => $this->account_id,
));

$this->validateArgument($parameters, 'properties', 'array');
$this->validateMerchantArgument($parameters);
$url = ($this->api_endpoint .
'/v3/accounts/' . rawurlencode($opts['account_id']) .
'/psp_management/merchants');
try {
$request = new SiftRequest($url, SiftRequest::POST, $opts['timeout'], self::API3_VERSION, array('auth' => $this->api_key . ':',
'body' => $parameters,
));

return $request->send();
} catch (Exception $e) {
$this->logError($e->getMessage());
return null;
}
}

/**
* Get a PSP Merchant profile..
* @param array $merchant_id :Merchant ID .
*
* @param array $opts Array of optional parameters for this request:
* - string 'account_id': by default, this client's account ID is used.
* - int 'timeout': By default, this client's timeout is used.
*
* @return null|SiftResponse
*/
public function getMerchant($merchant_id, $opts = array()) {
$this->mergeArguments($opts, array(
'account_id' => $this->account_id,
'timeout' => $this->timeout,
));

$this->validateArgument($this->account_id, 'accountId', 'string');
$this->validateArgument($merchant_id, 'merchantId', 'string');

$url = ($this->api_endpoint .
'/v3/accounts/' . rawurlencode($opts['account_id']) .
'/psp_management/merchants/' . rawurlencode($merchant_id));

try {
$request = new SiftRequest($url, SiftRequest::GET, $opts['timeout'], self::API3_VERSION, array('auth' => $this->api_key . ':'));
return $request->send();
} catch (Exception $e) {
$this->logError($e->getMessage());
return null;
}
}

/**
* Update a PSP Merchant profile..
* @param array $parameters An array of merchant profile data .
*
* @param array $opts Array of optional parameters for this request:
* - string 'account_id': by default, this client's account ID is used.
* - int 'timeout': By default, this client's timeout is used.
*
* @return null|SiftResponse
*/
public function putMerchant($merchant_id, $parameters, $opts = array()) {
$this->mergeArguments($opts, array(
'timeout' => $this->timeout,
'account_id' => $this->account_id,
));

$this->validateArgument($parameters, 'properties', 'array');
$this->validateArgument($merchant_id, 'merchantId', 'string');
$this->validateMerchantArgument($parameters);

$url = ($this->api_endpoint .
'/v3/accounts/' . rawurlencode($opts['account_id']) .
'/psp_management/merchants/' . rawurlencode($merchant_id));
try {
$request = new SiftRequest($url, SiftRequest::PUT, $opts['timeout'], self::API3_VERSION, array('auth' => $this->api_key . ':',
'body' => $parameters,
));
return $request->send();
} catch (Exception $e) {
$this->logError($e->getMessage());
return null;
}
}

/**
* Merges a function's default parameter values into an array of arguments.
*
Expand Down Expand Up @@ -913,6 +1047,27 @@ private function validateArgument($arg, $name, $type) {
throw new InvalidArgumentException("${name} cannot be empty.");
}

private function validateMerchantArgument($parameters) {
$this->validateArgument($parameters['id'], 'ID', 'string');
$this->validateArgument($parameters['name'], 'name', 'string');
$this->validateArgument($parameters['address']['address_1'], 'address_1', 'string');
$this->validateArgument($parameters['address']['address_2'], 'address_2', 'string');
$this->validateArgument($parameters['address']['city'], 'city', 'string');
$this->validateArgument($parameters['address']['region'], 'region', 'string');
$this->validateArgument($parameters['address']['country'], 'country', 'string');
$this->validateArgument($parameters['address']['zipcode'], 'zipcode', 'string');
$this->validateArgument($parameters['address']['phone'], 'phone', 'string');
$this->validateArgument($parameters['category'], 'category', 'string');
$this->validateArgument($parameters['service_level'], 'service_level', 'string');
$this->validateArgument($parameters['status'], 'status', 'string');
$this->validateArgument($parameters['risk_profile']['level'], 'level', 'string');
$this->validateArgument($parameters['risk_profile']['score'], 'Score', 'integer');
if (($parameters['risk_profile']['level'] != 'low') && ($parameters['risk_profile']['level'] != 'medium') && ($parameters['risk_profile']['level'] != 'high'))
throw new InvalidArgumentException("Invalid Level");
if ($parameters['status'] != 'churned' && $parameters['status'] != 'active' && $parameters['status'] != 'inactive' && $parameters['status'] != 'paused')
throw new InvalidArgumentException("Invalid Status");
}

private function restApiUrl($version) {
return self::urlPrefix($version) . '/events';
}
Expand Down
5 changes: 3 additions & 2 deletions lib/SiftRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class SiftRequest {
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';

private static $mock = null;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function send() {
}

// HTTP-method-specific configuration.
if ($this->method == self::POST) {
if ($this->method == self::POST || $this->method == self::PUT) {
if (function_exists('json_encode')) {
$jsonString = json_encode($this->body);
} else {
Expand All @@ -89,7 +90,7 @@ public function send() {
$jsonString = $json->encodeUnsafe($this->body);
}

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
array_push($headers, 'Content-Type: application/json',
'Content-Length: ' . strlen($jsonString)
Expand Down
93 changes: 93 additions & 0 deletions test/SiftClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ protected function setUp(): void {
'api_key' => SiftClientTest::$API_KEY,
'account_id' => SiftClientTest::$ACCOUNT_ID
));

$this->merchant = new SiftClient(array(
'api_key' => '09f7f361575d11ff',
'account_id' => '5f053f004025ca08a187fad6'
));

$this->transaction_properties = array(
'$buyer_user_id' => '123456',
'$seller_user_id' => '56789',
Expand Down Expand Up @@ -96,6 +102,56 @@ protected function setUp(): void {
'$verified_event' => '$login',
'$verified_entity_id' => '09f7f361575d11ff',
);

$this->merchant_properties = array(
'batch_size' => 2,
);

$this->post_merchant_properties = array(
'id' => 'api-key-1000',
'name' => 'Wonderful Payments In',
'description' => 'Wonderful Payments payment provider',
'address' => array(
'name' => 'Alany',
'address_1' => 'Big Payment blvd, 22',
'address_2' => 'apt, 8',
'city' => 'New Orleans',
'region' => 'NA',
'country' => 'US',
'zipcode' => '76830',
'phone' => '0394888320'
),
'category' => '1002',
'service_level' => 'Platinum',
'status' => 'active',
'risk_profile' => array(
'level' => 'low',
'score' => 10,
)
);

$this->put_merchant_properties = array(
'id' => 'api-key-1',
'name' => 'Wonderful Payments Inc',
'description' => 'Wonderful Payments payment provider',
'address' => array(
'name' => 'Alany',
'address_1' => 'Big Payment blvd, 22',
'address_2' => 'apt, 8',
'city' => 'New Orleans',
'region' => 'NA',
'country' => 'US',
'zipcode' => '76830',
'phone' => '0394888320'
),
'category' => '1002',
'service_level' => 'Platinum',
'status' => 'active',
'risk_profile' => array(
'level' => 'low',
'score' => 10,
)
);
}

protected function tearDown(): void {
Expand Down Expand Up @@ -696,4 +752,41 @@ public function testCheck() {
$this->assertEquals($response->apiErrorMessage, 'OK');
}

public function testMerchants() {
$mockUrl = 'https://api.sift.com/v3/accounts/5f053f004025ca08a187fad6/psp_management/merchants';
$mockResponse = new SiftResponse('{"status": 0, "error_message": "OK"}', 200, null);
SiftRequest::setMockResponse($mockUrl, SiftRequest::GET, $mockResponse);

$response = $this->merchant->merchants($this->merchant_properties);
$this->assertTrue($response->isOk());
}

public function testPostMerchant() {
$mockUrl = 'https://api.sift.com/v3/accounts/5f053f004025ca08a187fad6/psp_management/merchants';
$mockResponse = new SiftResponse('{"status": 0, "error_message": "OK"}', 200, null);
SiftRequest::setMockResponse($mockUrl, SiftRequest::POST, $mockResponse);

$response = $this->merchant->postMerchant($this->post_merchant_properties);
$this->assertTrue($response->isOk());
$this->assertEquals($response->apiErrorMessage, 'OK');
}

public function testGetMerchant() {
$mockUrl = 'https://api.sift.com/v3/accounts/5f053f004025ca08a187fad6/psp_management/merchants/api-key-1';
$mockResponse = new SiftResponse('{"status": 0, "error_message": "OK"}', 200, null);
SiftRequest::setMockResponse($mockUrl, SiftRequest::GET, $mockResponse);

$response = $this->merchant->getMerchant('api-key-1');
$this->assertTrue($response->isOk());
}

public function testPutMerchant() {
$mockUrl = 'https://api.sift.com/v3/accounts/5f053f004025ca08a187fad6/psp_management/merchants/api-key-1';
$mockResponse = new SiftResponse('{"status": 0, "error_message": "OK"}', 200, null);
SiftRequest::setMockResponse($mockUrl, SiftRequest::PUT, $mockResponse);

$response = $this->merchant->putMerchant('api-key-1', $this->put_merchant_properties);
$this->assertTrue($response->isOk());
}

}

0 comments on commit abcb4ad

Please sign in to comment.