Skip to content

Commit

Permalink
feat: make responses same for all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Oct 24, 2024
1 parent 31b5fd4 commit c678354
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function execute(serviceType, requestObject, checkoutAttemptID = '') {
if (service === null) {
throw new Error(`Could not create ${serviceType} service object`);
}

const clientKey = AdyenConfigs.getAdyenClientKey();
let serviceUrl = service.getURL();
serviceUrl += `/${checkoutAttemptID}?clientKey=${clientKey}`;
Expand All @@ -23,8 +24,6 @@ function execute(serviceType, requestObject, checkoutAttemptID = '') {
service.addHeader('Idempotency-Key', uuid);

const callResult = service.call(JSON.stringify(requestObject));
AdyenLogs.info_log(`is OK ${JSON.stringify(callResult.isOk())}`);

if (!callResult.isOk()) {
throw new Error(
`${serviceType} service call error code${callResult
Expand All @@ -33,12 +32,12 @@ function execute(serviceType, requestObject, checkoutAttemptID = '') {
);
}

return { success: true };
const parsedResponse = JSON.parse(callResult.object?.getText());
return parsedResponse || callResult.status;
}

function createCheckoutAttemptId() {
try {
const analyticsResponse = {};
const requestObject = {
applicationInfo: AdyenHelper.getApplicationInfo(),
channel: 'Web',
Expand All @@ -47,9 +46,7 @@ function createCheckoutAttemptId() {

const response = execute(constants.SERVICE.ADYEN_ANALYTICS, requestObject);

analyticsResponse.attemptId = response.checkoutAttemptId;

return analyticsResponse;
return { data: response.checkoutAttemptId };
} catch (error) {
AdyenLogs.error_log(
'createCheckoutAttemptId for /analytics call failed:',
Expand All @@ -63,13 +60,18 @@ function submitData(requestObject, attemptIdParam = null) {
try {
let attemptId = attemptIdParam;

// If attemptId is not provided as a parameter, generate it
if (!attemptId) {
const initialAnalyticsCall = createCheckoutAttemptId();
attemptId = initialAnalyticsCall.attemptId;
attemptId = initialAnalyticsCall.data;
}

return execute(constants.SERVICE.ADYEN_ANALYTICS, requestObject, attemptId);
const response = execute(
constants.SERVICE.ADYEN_ANALYTICS,
requestObject,
attemptId,
);

return { data: response };
} catch (error) {
AdyenLogs.error_log('submitData for /analytics call failed:', error);
return { error: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function processData() {
);

const payload = createRequestObjectForAllReferenceIds(groupedObjects);
const submission = AnalyticsService.submitData(payload); // {error: true}
if (submission.success) {
const submission = AnalyticsService.submitData(payload);
if (submission.data) {
customObjectsToDelete.forEach((customObject) => {
deleteCustomObject(customObject);
});
Expand Down

0 comments on commit c678354

Please sign in to comment.