From 094e6456f2c17be254bcbe54071961efaddf6296 Mon Sep 17 00:00:00 2001 From: Sabitha Varghese Date: Thu, 27 Oct 2022 15:48:51 +0530 Subject: [PATCH 1/2] Updated PSP Merchant Management API --- .gitignore | 1 + lib/SiftClient.php | 155 ++++++++++++++++++++++++++++++++++++++++ lib/SiftRequest.php | 5 +- test/SiftClientTest.php | 93 ++++++++++++++++++++++++ 4 files changed, 252 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ecdf2d7..9ffa846 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor .idea +nbproject diff --git a/lib/SiftClient.php b/lib/SiftClient.php index bed1b3f..7712e55 100644 --- a/lib/SiftClient.php +++ b/lib/SiftClient.php @@ -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. * @@ -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'; } diff --git a/lib/SiftRequest.php b/lib/SiftRequest.php index fdae7de..cbfe849 100644 --- a/lib/SiftRequest.php +++ b/lib/SiftRequest.php @@ -3,6 +3,7 @@ class SiftRequest { const GET = 'GET'; const POST = 'POST'; + const PUT = 'PUT'; const DELETE = 'DELETE'; private static $mock = null; @@ -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 { @@ -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) diff --git a/test/SiftClientTest.php b/test/SiftClientTest.php index aa5afdf..cbc80f1 100644 --- a/test/SiftClientTest.php +++ b/test/SiftClientTest.php @@ -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', @@ -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 { @@ -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()); + } + } From c96098cb86326559c2e0dfbc57e3a007f35f6dc9 Mon Sep 17 00:00:00 2001 From: sbogolii-sift Date: Mon, 7 Nov 2022 17:04:39 +0200 Subject: [PATCH 2/2] API-6868: Updated version to 4.2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9068ca1..bc57e68 100644 --- a/composer.json +++ b/composer.json @@ -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"