Skip to content

Commit

Permalink
fix(connector): [Authorizedotnet]fix error deserialization incase of …
Browse files Browse the repository at this point in the history
…authentication failure (#2600)

Signed-off-by: chikke srujan <[email protected]>
  • Loading branch information
srujanchikke authored Oct 17, 2023
1 parent df17617 commit 4859b7d
Show file tree
Hide file tree
Showing 23 changed files with 633 additions and 25 deletions.
37 changes: 19 additions & 18 deletions crates/router/src/connector/authorizedotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,30 +883,31 @@ fn get_error_response(
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;

match response.transaction_response {
Some(transaction_response) => Ok({
transaction_response
.errors
.and_then(|errors| {
errors.into_iter().next().map(|error| types::ErrorResponse {
code: error.error_code,
message: error.error_text,
reason: None,
status_code,
})
})
.unwrap_or_else(|| types::ErrorResponse {
code: consts::NO_ERROR_CODE.to_string(),
message: consts::NO_ERROR_MESSAGE.to_string(),
reason: None,
Some(authorizedotnet::TransactionResponse::AuthorizedotnetTransactionResponse(
payment_response,
)) => Ok(payment_response
.errors
.and_then(|errors| {
errors.into_iter().next().map(|error| types::ErrorResponse {
code: error.error_code,
message: error.error_text.to_owned(),
reason: Some(error.error_text),
status_code,
})
}),
None => {
})
.unwrap_or_else(|| types::ErrorResponse {
code: consts::NO_ERROR_CODE.to_string(), // authorizedotnet sends 200 in case of bad request so this are hard coded to NO_ERROR_CODE and NO_ERROR_MESSAGE
message: consts::NO_ERROR_MESSAGE.to_string(),
reason: None,
status_code,
})),
Some(authorizedotnet::TransactionResponse::AuthorizedotnetTransactionResponseError(_))
| None => {
let message = &response.messages.message[0].text;
Ok(types::ErrorResponse {
code: consts::NO_ERROR_CODE.to_string(),
message: message.to_string(),
reason: None,
reason: Some(message.to_string()),
status_code,
})
}
Expand Down
29 changes: 22 additions & 7 deletions crates/router/src/connector/authorizedotnet/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,22 @@ pub struct ErrorMessage {
pub error_text: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum TransactionResponse {
AuthorizedotnetTransactionResponse(Box<AuthorizedotnetTransactionResponse>),
AuthorizedotnetTransactionResponseError(Box<AuthorizedotnetTransactionResponseError>),
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct AuthorizedotnetTransactionResponseError {
_supplemental_data_qualification_indicator: i64,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionResponse {
pub struct AuthorizedotnetTransactionResponse {
response_code: AuthorizedotnetPaymentStatus,
#[serde(rename = "transId")]
transaction_id: String,
Expand Down Expand Up @@ -550,7 +563,7 @@ impl<F, T>
>,
) -> Result<Self, Self::Error> {
match &item.response.transaction_response {
Some(transaction_response) => {
Some(TransactionResponse::AuthorizedotnetTransactionResponse(transaction_response)) => {
let status = enums::AttemptStatus::from(transaction_response.response_code.clone());
let error = transaction_response.errors.as_ref().and_then(|errors| {
errors.iter().next().map(|error| types::ErrorResponse {
Expand Down Expand Up @@ -598,11 +611,13 @@ impl<F, T>
..item.data
})
}
None => Ok(Self {
status: enums::AttemptStatus::Failure,
response: Err(get_err_response(item.http_code, item.response.messages)),
..item.data
}),
Some(TransactionResponse::AuthorizedotnetTransactionResponseError(_)) | None => {
Ok(Self {
status: enums::AttemptStatus::Failure,
response: Err(get_err_response(item.http_code, item.response.messages)),
..item.data
})
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"childrenOrder": ["Payments - Create", "Payments - Retrieve"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eventOrder": ["event.test.js", "event.prerequest.js"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pm.environment.set("random_number", _.random(1000, 100000));
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Validate status 2xx
pm.test("[POST]::/payments - Status code is 2xx", function () {
pm.response.to.be.success;
});

// Validate if response header has matching content-type
pm.test("[POST]::/payments - Content-Type is application/json", function () {
pm.expect(pm.response.headers.get("Content-Type")).to.include(
"application/json",
);
});

// Validate if response has JSON Body
pm.test("[POST]::/payments - Response has JSON Body", function () {
pm.response.to.have.jsonBody();
});

// Set response object as internal variable
let jsonData = {};
try {
jsonData = pm.response.json();
} catch (e) {}

// pm.collectionVariables - Set payment_id as variable for jsonData.payment_id
if (jsonData?.payment_id) {
pm.collectionVariables.set("payment_id", jsonData.payment_id);
console.log(
"- use {{payment_id}} as collection variable for value",
jsonData.payment_id,
);
} else {
console.log(
"INFO - Unable to assign variable {{payment_id}}, as jsonData.payment_id is undefined.",
);
}

// pm.collectionVariables - Set mandate_id as variable for jsonData.mandate_id
if (jsonData?.mandate_id) {
pm.collectionVariables.set("mandate_id", jsonData.mandate_id);
console.log(
"- use {{mandate_id}} as collection variable for value",
jsonData.mandate_id,
);
} else {
console.log(
"INFO - Unable to assign variable {{mandate_id}}, as jsonData.mandate_id is undefined.",
);
}

// pm.collectionVariables - Set client_secret as variable for jsonData.client_secret
if (jsonData?.client_secret) {
pm.collectionVariables.set("client_secret", jsonData.client_secret);
console.log(
"- use {{client_secret}} as collection variable for value",
jsonData.client_secret,
);
} else {
console.log(
"INFO - Unable to assign variable {{client_secret}}, as jsonData.client_secret is undefined.",
);
}

// Response body should have value "processing" or "failed" for "error_code"
if (jsonData?.error_code) {
pm.test(
"[POST]::/payments - Content check if value for 'error_code' matches '3' or '11' ",
function () {
pm.expect(jsonData.error_code).to.be.oneOf(["3", "11"]);
},
);
}

// Response body should have value "processing" or "failed" for "status"
if (jsonData?.error_message) {
pm.test(
"[POST]::/payments - Content check if value for 'error_message' matches 'processing' or 'This transaction has been declined.' ",
function () {
pm.expect(jsonData.error_message).to.be.oneOf(["A duplicate transaction has been submitted.", "This transaction has been declined."]);
},
);
}

// Response body should have value "processing" or "failed" for "status"
if (jsonData?.status) {
pm.test(
"[POST]::/payments - Content check if value for 'status' matches 'processing' or 'failed' ",
function () {
pm.expect(jsonData.status).to.be.oneOf(["failed"]);
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw_json_formatted": {
"amount": 7003,
"currency": "USD",
"confirm": true,
"routing": {
"data": "authorizedotnet",
"type": "single"
},
"capture_method": "automatic",
"capture_on": "2022-09-10T10:11:12Z",
"customer_id": "StripeCustomer",
"email": "[email protected]",
"name": "John Doe",
"phone": "999999999",
"phone_country_code": "+65",
"description": "Its my first payment request",
"authentication_type": "no_three_ds",
"return_url": "https://duck.com",
"payment_method": "card",
"payment_method_data": {
"card": {
"card_number": "370000000000002",
"card_exp_month": "10",
"card_exp_year": "25",
"card_holder_name": "joseph Doe",
"card_cvc": "900"
}
},
"billing": {
"address": {
"line1": "1467",
"line2": "Harrison Street",
"line3": "Harrison Street",
"city": "San Fransico",
"state": "California",
"zip": "94122",
"country": "US",
"first_name": "PiX"
}
},
"shipping": {
"address": {
"line1": "1467",
"line2": "Harrison Street",
"line3": "Harrison Street",
"city": "San Fransico",
"state": "California",
"zip": "94122",
"country": "US",
"first_name": "PiX"
}
},
"statement_descriptor_name": "joseph",
"statement_descriptor_suffix": "JS",
"metadata": {
"udf1": "value1",
"new_customer": "true",
"login_date": "2019-09-10T10:11:12Z"
}
},
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/payments",
"host": ["{{baseUrl}}"],
"path": ["payments"]
},
"description": "To process a payment you will have to create a payment, attach a payment method and confirm. Depending on the user journey you wish to achieve, you may opt to all the steps in a single request or in a sequence of API request using following APIs: (i) Payments - Update, (ii) Payments - Confirm, and (iii) Payments - Capture"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eventOrder": ["event.test.js"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Validate status 2xx
pm.test("[POST]::/payments - Status code is 2xx", function () {
pm.response.to.be.success;
});

// Validate if response header has matching content-type
pm.test("[POST]::/payments - Content-Type is application/json", function () {
pm.expect(pm.response.headers.get("Content-Type")).to.include(
"application/json",
);
});

// Validate if response has JSON Body
pm.test("[POST]::/payments - Response has JSON Body", function () {
pm.response.to.have.jsonBody();
});

// Set response object as internal variable
let jsonData = {};
try {
jsonData = pm.response.json();
} catch (e) {}

// pm.collectionVariables - Set payment_id as variable for jsonData.payment_id
if (jsonData?.payment_id) {
pm.collectionVariables.set("payment_id", jsonData.payment_id);
console.log(
"- use {{payment_id}} as collection variable for value",
jsonData.payment_id,
);
} else {
console.log(
"INFO - Unable to assign variable {{payment_id}}, as jsonData.payment_id is undefined.",
);
}

// pm.collectionVariables - Set mandate_id as variable for jsonData.mandate_id
if (jsonData?.mandate_id) {
pm.collectionVariables.set("mandate_id", jsonData.mandate_id);
console.log(
"- use {{mandate_id}} as collection variable for value",
jsonData.mandate_id,
);
} else {
console.log(
"INFO - Unable to assign variable {{mandate_id}}, as jsonData.mandate_id is undefined.",
);
}

// pm.collectionVariables - Set client_secret as variable for jsonData.client_secret
if (jsonData?.client_secret) {
pm.collectionVariables.set("client_secret", jsonData.client_secret);
console.log(
"- use {{client_secret}} as collection variable for value",
jsonData.client_secret,
);
} else {
console.log(
"INFO - Unable to assign variable {{client_secret}}, as jsonData.client_secret is undefined.",
);
}

// Response body should have value "processing" or "failed" for "status"
if (jsonData?.status) {
pm.test(
"[POST]::/payments - Content check if value for 'status' matches 'processing' or 'failed' ",
function () {
pm.expect(jsonData.status).to.be.oneOf(["processing", "failed"]);
},
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json"
}
],
"url": {
"raw": "{{baseUrl}}/payments/:id?force_sync=true",
"host": ["{{baseUrl}}"],
"path": ["payments", ":id"],
"query": [
{
"key": "force_sync",
"value": "true"
}
],
"variable": [
{
"key": "id",
"value": "{{payment_id}}",
"description": "(Required) unique payment id"
}
]
},
"description": "To retrieve the properties of a Payment. This may be used to get the status of a previously initiated payment or next action for an ongoing payment"
}
Loading

0 comments on commit 4859b7d

Please sign in to comment.