Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions generator/cybersource-php-template/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use CyberSource\Authentication\Core\Authentication as Authentication;
use CyberSource\Authentication\Util\GlobalParameter as GlobalParameter;
use CyberSource\Authentication\PayloadDigest\PayloadDigest as PayloadDigest;
use \{{invokerPackage}}\Logging\LogFactory as LogFactory;
use \CyberSource\Authentication\Util\MLEUtility;

$stream_headers = array();

Expand Down Expand Up @@ -206,7 +207,7 @@ class ApiClient
* @throws \{{invokerPackage}}\ApiException on a non 2xx response
* @return mixed
*/
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null, $isResponseMLEForAPI=false)
{
self::$logger->info("CALLING API \"$resourcePath\" STARTED");
$headers = [];
Expand Down Expand Up @@ -243,7 +244,7 @@ class ApiClient

if($this->merchantConfig->getAuthenticationType() != GlobalParameter::MUTUAL_AUTH)
{
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath);
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath, $isResponseMLEForAPI);
}

$requestHeaders=[];
Expand Down Expand Up @@ -480,12 +481,34 @@ class ApiClient
self::$logger->close();
return [$http_body, $response_info['http_code'], $http_header];
}

// Decrypt BEFORE json_decode
if (MLEUtility::checkIsMleEncryptedResponse($http_body)) {

try {
$http_body = MLEUtility::decryptMleResponsePayload($this->merchantConfig, $http_body);
} catch (\Exception $e) {
$error_message = "Response MLE decryption failed: " . $e->getMessage();
self::$logger->error("ApiException : " . $error_message);
self::$logger->close();
throw new ApiException($error_message);
}
}

$data = json_decode($http_body);
if (json_last_error() > 0) { // if response is a string
$data = $http_body;
}
} else {
// Error path: still attempt decryption so error payload can be read
if (MLEUtility::checkIsMleEncryptedResponse($http_body)) {
try {
$http_body = MLEUtility::decryptMleResponsePayload($this->merchantConfig, $http_body);
} catch (\Exception $e) {
// Ignore; proceeding with encrypted body for error case
}
}

$data = json_decode($http_body);
if (json_last_error() > 0) { // if response is a string
$data = $http_body;
Expand Down Expand Up @@ -533,11 +556,38 @@ class ApiClient
self::$logger->close();
throw $exception;
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
if (MLEUtility::checkIsMleEncryptedResponse($http_body)) {
self::$logger->debug("[MLE][ApiClient][FileDownload] Encrypted response detected for file download");

try {
$http_body = MLEUtility::decryptMleResponsePayload($this->merchantConfig, $http_body);
} catch (\Exception $e) {
$exceptionMessage = $e->getMessage() ?: 'Unknown error';
$exceptionClass = get_class($e);
$error_message = "File download MLE decryption failed [{$exceptionClass}]: {$exceptionMessage}";
self::$logger->error("ApiException : " . $error_message);
self::$logger->close();

$statusCode = isset($response_info['http_code']) ? $response_info['http_code'] : 0;
throw new ApiException($error_message, $statusCode, $stream_headers ?? [], null);
}
}

$stream_headers['http_code'] = $response_info['http_code'];

self::$logger->close();
return [$http_body, $stream_headers['http_code'], $stream_headers];
} else {
// Error path: attempt decryption for file downloads too
if (MLEUtility::checkIsMleEncryptedResponse($http_body)) {

try {
$http_body = MLEUtility::decryptMleResponsePayload($this->merchantConfig, $http_body);
} catch (\Exception $e) {
// Ignore; proceed with encrypted body for error case
}
}

self::$logger->error("ApiException : [".$response_info['http_code']."] Error connecting to the API ($url)");
self::$logger->close();
throw new ApiException(
Expand Down Expand Up @@ -636,11 +686,11 @@ class ApiClient
* Purpose : This function calling the Authentication and making an Auth Header
*
*/
public function callAuthenticationHeader($method, $postData, $resourcePath)
public function callAuthenticationHeader($method, $postData, $resourcePath, $isResponseMLEForAPI)
{
$merchantConfig = $this->merchantConfig;
$authentication = new Authentication($merchantConfig->getLogConfiguration());
$getToken = $authentication->generateToken($resourcePath, $postData, $method, $merchantConfig);
$getToken = $authentication->generateToken($resourcePath, $postData, $method, $merchantConfig, $isResponseMLEForAPI);
if($merchantConfig->getAuthenticationType() == GlobalParameter::HTTP_SIGNATURE){
$host = "Host:".$merchantConfig->getHost();
$vcMerchant = "v-c-merchant-id:".$merchantConfig->getMerchantID();
Expand Down
7 changes: 6 additions & 1 deletion generator/cybersource-php-template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ use \Exception;
{{^returnType}}
self::$logger->debug("Return Type : null");
{{/returnType}}

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "{{operationId}},{{operationId}}WithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -297,7 +301,8 @@ use \Exception;
{{^returnType}}
null,
{{/returnType}}
'{{{path}}}'
'{{{path}}}',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
28 changes: 24 additions & 4 deletions lib/Api/BatchesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public function getBatchReportWithHttpInfo($batchId)
}

self::$logger->debug("Return Type : \CyberSource\Model\InlineResponse20012");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getBatchReport,getBatchReportWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -197,7 +201,8 @@ public function getBatchReportWithHttpInfo($batchId)
$httpBody,
$headerParams,
'\CyberSource\Model\InlineResponse20012',
'/accountupdater/v1/batches/{batchId}/report'
'/accountupdater/v1/batches/{batchId}/report',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down Expand Up @@ -312,6 +317,10 @@ public function getBatchStatusWithHttpInfo($batchId)
}

self::$logger->debug("Return Type : \CyberSource\Model\InlineResponse20011");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getBatchStatus,getBatchStatusWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -321,7 +330,8 @@ public function getBatchStatusWithHttpInfo($batchId)
$httpBody,
$headerParams,
'\CyberSource\Model\InlineResponse20011',
'/accountupdater/v1/batches/{batchId}/status'
'/accountupdater/v1/batches/{batchId}/status',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down Expand Up @@ -449,6 +459,10 @@ public function getBatchesListWithHttpInfo($offset = '0', $limit = '20', $fromDa
}

self::$logger->debug("Return Type : \CyberSource\Model\InlineResponse20010");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getBatchesList,getBatchesListWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -458,7 +472,8 @@ public function getBatchesListWithHttpInfo($offset = '0', $limit = '20', $fromDa
$httpBody,
$headerParams,
'\CyberSource\Model\InlineResponse20010',
'/accountupdater/v1/batches'
'/accountupdater/v1/batches',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down Expand Up @@ -576,6 +591,10 @@ public function postBatchWithHttpInfo($body)
}

self::$logger->debug("Return Type : \CyberSource\Model\InlineResponse202");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "postBatch,postBatchWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -585,7 +604,8 @@ public function postBatchWithHttpInfo($body)
$httpBody,
$headerParams,
'\CyberSource\Model\InlineResponse202',
'/accountupdater/v1/batches'
'/accountupdater/v1/batches',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
21 changes: 18 additions & 3 deletions lib/Api/BillingAgreementsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ public function billingAgreementsDeRegistrationWithHttpInfo($modifyBillingAgreem
}

self::$logger->debug("Return Type : \CyberSource\Model\PtsV2ModifyBillingAgreementPost201Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "billingAgreementsDeRegistration,billingAgreementsDeRegistrationWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -211,7 +215,8 @@ public function billingAgreementsDeRegistrationWithHttpInfo($modifyBillingAgreem
$httpBody,
$headerParams,
'\CyberSource\Model\PtsV2ModifyBillingAgreementPost201Response',
'/pts/v2/billing-agreements/{id}'
'/pts/v2/billing-agreements/{id}',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down Expand Up @@ -344,6 +349,10 @@ public function billingAgreementsIntimationWithHttpInfo($intimateBillingAgreemen
}

self::$logger->debug("Return Type : \CyberSource\Model\PtsV2CreditsPost201Response1");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "billingAgreementsIntimation,billingAgreementsIntimationWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -353,7 +362,8 @@ public function billingAgreementsIntimationWithHttpInfo($intimateBillingAgreemen
$httpBody,
$headerParams,
'\CyberSource\Model\PtsV2CreditsPost201Response1',
'/pts/v2/billing-agreements/{id}/intimations'
'/pts/v2/billing-agreements/{id}/intimations',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down Expand Up @@ -471,6 +481,10 @@ public function billingAgreementsRegistrationWithHttpInfo($createBillingAgreemen
}

self::$logger->debug("Return Type : \CyberSource\Model\PtsV2CreateBillingAgreementPost201Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "billingAgreementsRegistration,billingAgreementsRegistrationWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -480,7 +494,8 @@ public function billingAgreementsRegistrationWithHttpInfo($createBillingAgreemen
$httpBody,
$headerParams,
'\CyberSource\Model\PtsV2CreateBillingAgreementPost201Response',
'/pts/v2/billing-agreements'
'/pts/v2/billing-agreements',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
7 changes: 6 additions & 1 deletion lib/Api/BinLookupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public function getAccountInfoWithHttpInfo($createBinLookupRequest)
}

self::$logger->debug("Return Type : \CyberSource\Model\InlineResponse2012");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getAccountInfo,getAccountInfoWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -198,7 +202,8 @@ public function getAccountInfoWithHttpInfo($createBinLookupRequest)
$httpBody,
$headerParams,
'\CyberSource\Model\InlineResponse2012',
'/bin/v1/binlookup'
'/bin/v1/binlookup',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
7 changes: 6 additions & 1 deletion lib/Api/CaptureApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ public function capturePaymentWithHttpInfo($capturePaymentRequest, $id)
}

self::$logger->debug("Return Type : \CyberSource\Model\PtsV2PaymentsCapturesPost201Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "capturePayment,capturePaymentWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -211,7 +215,8 @@ public function capturePaymentWithHttpInfo($capturePaymentRequest, $id)
$httpBody,
$headerParams,
'\CyberSource\Model\PtsV2PaymentsCapturesPost201Response',
'/pts/v2/payments/{id}/captures'
'/pts/v2/payments/{id}/captures',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
7 changes: 6 additions & 1 deletion lib/Api/ChargebackDetailsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public function getChargebackDetailsWithHttpInfo($startTime, $endTime, $organiza
}

self::$logger->debug("Return Type : \CyberSource\Model\ReportingV3ChargebackDetailsGet200Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getChargebackDetails,getChargebackDetailsWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -213,7 +217,8 @@ public function getChargebackDetailsWithHttpInfo($startTime, $endTime, $organiza
$httpBody,
$headerParams,
'\CyberSource\Model\ReportingV3ChargebackDetailsGet200Response',
'/reporting/v3/chargeback-details'
'/reporting/v3/chargeback-details',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
7 changes: 6 additions & 1 deletion lib/Api/ChargebackSummariesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public function getChargebackSummariesWithHttpInfo($startTime, $endTime, $organi
}

self::$logger->debug("Return Type : \CyberSource\Model\ReportingV3ChargebackSummariesGet200Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getChargebackSummaries,getChargebackSummariesWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -213,7 +217,8 @@ public function getChargebackSummariesWithHttpInfo($startTime, $endTime, $organi
$httpBody,
$headerParams,
'\CyberSource\Model\ReportingV3ChargebackSummariesGet200Response',
'/reporting/v3/chargeback-summaries'
'/reporting/v3/chargeback-summaries',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
7 changes: 6 additions & 1 deletion lib/Api/ConversionDetailsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public function getConversionDetailWithHttpInfo($startTime, $endTime, $organizat
}

self::$logger->debug("Return Type : \CyberSource\Model\ReportingV3ConversionDetailsGet200Response");

// Response MLE check
$isResponseMLEForAPI = MLEUtility::checkIsResponseMLEForAPI($this->apiClient->merchantConfig, "getConversionDetail,getConversionDetailWithHttpInfo");

// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
Expand All @@ -213,7 +217,8 @@ public function getConversionDetailWithHttpInfo($startTime, $endTime, $organizat
$httpBody,
$headerParams,
'\CyberSource\Model\ReportingV3ConversionDetailsGet200Response',
'/reporting/v3/conversion-details'
'/reporting/v3/conversion-details',
$isResponseMLEForAPI
);

self::$logger->debug("Response Headers :\n" . \CyberSource\Utilities\Helpers\ListHelper::toString($httpHeader));
Expand Down
Loading