Skip to content

Commit

Permalink
refactor(connector): [Adyen] change expiresAt time from string to uni…
Browse files Browse the repository at this point in the history
…xtimestamp (#3506)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
swangi-kumari and hyperswitch-bot[bot] authored Jan 31, 2024
1 parent 7251f64 commit b7c0f9a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ pub struct BankTransferNextStepsData {
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct VoucherNextStepData {
/// Voucher expiry date and time
pub expires_at: Option<String>,
pub expires_at: Option<i64>,
/// Reference number required for the transaction
pub reference: String,
/// Url to download the payment instruction
Expand Down Expand Up @@ -2087,8 +2087,8 @@ pub struct MultibancoTransferInstructions {

#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct DokuBankTransferInstructions {
#[schema(value_type = String, example = "2023-07-26T17:33:00-07-21")]
pub expires_at: Option<String>,
#[schema(value_type = String, example = "1707091200000")]
pub expires_at: Option<i64>,
#[schema(value_type = String, example = "122385736258")]
pub reference: Secret<String>,
#[schema(value_type = String)]
Expand Down
47 changes: 47 additions & 0 deletions crates/common_utils/src/custom_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,53 @@ pub mod iso8601 {
})
}
}
/// Use the well-known ISO 8601 format which is without timezone when serializing and deserializing an
/// [`Option<PrimitiveDateTime>`][PrimitiveDateTime].
///
/// [PrimitiveDateTime]: ::time::PrimitiveDateTime
pub mod option_without_timezone {
use serde::{de, Deserialize, Serialize};
use time::macros::format_description;

use super::*;

/// Serialize an [`Option<PrimitiveDateTime>`] using the well-known ISO 8601 format which is without timezone.
pub fn serialize<S>(
date_time: &Option<PrimitiveDateTime>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
date_time
.map(|date_time| {
let format =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
date_time.assume_utc().format(format)
})
.transpose()
.map_err(S::Error::custom)?
.serialize(serializer)
}

/// Deserialize an [`Option<PrimitiveDateTime>`] from its ISO 8601 representation.
pub fn deserialize<'a, D>(deserializer: D) -> Result<Option<PrimitiveDateTime>, D::Error>
where
D: Deserializer<'a>,
{
Option::deserialize(deserializer)?
.map(|time_string| {
let format =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
PrimitiveDateTime::parse(time_string, format).map_err(|_| {
de::Error::custom(format!(
"Failed to parse PrimitiveDateTime from {time_string}"
))
})
})
.transpose()
}
}
}

/// Use the UNIX timestamp when serializing and deserializing an
Expand Down
15 changes: 12 additions & 3 deletions crates/router/src/connector/adyen/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,12 @@ pub struct AdyenPtsAction {
reference: String,
download_url: Option<Url>,
payment_method_type: PaymentType,
expires_at: Option<String>,
#[serde(rename = "expiresAt")]
#[serde(
default,
with = "common_utils::custom_serde::iso8601::option_without_timezone"
)]
expires_at: Option<PrimitiveDateTime>,
initial_amount: Option<Amount>,
pass_creation_token: Option<String>,
total_amount: Option<Amount>,
Expand Down Expand Up @@ -3434,6 +3439,10 @@ pub fn get_present_to_shopper_metadata(
response: &PresentToShopperResponse,
) -> errors::CustomResult<Option<serde_json::Value>, errors::ConnectorError> {
let reference = response.action.reference.clone();
let expires_at = response
.action
.expires_at
.map(|time| utils::get_timestamp_in_milliseconds(&time));

match response.action.payment_method_type {
PaymentType::Alfamart
Expand All @@ -3446,7 +3455,7 @@ pub fn get_present_to_shopper_metadata(
| PaymentType::Seicomart
| PaymentType::PayEasy => {
let voucher_data = payments::VoucherNextStepData {
expires_at: response.action.expires_at.clone(),
expires_at,
reference,
download_url: response.action.download_url.clone(),
instructions_url: response.action.instructions_url.clone(),
Expand All @@ -3470,7 +3479,7 @@ pub fn get_present_to_shopper_metadata(
Box::new(payments::DokuBankTransferInstructions {
reference: Secret::new(response.action.reference.clone()),
instructions_url: response.action.instructions_url.clone(),
expires_at: response.action.expires_at.clone(),
expires_at,
}),
);

Expand Down
2 changes: 1 addition & 1 deletion openapi/openapi_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -7738,7 +7738,7 @@
"properties": {
"expires_at": {
"type": "string",
"example": "2023-07-26T17:33:00-07-21"
"example": "1707091200000"
},
"reference": {
"type": "string",
Expand Down

0 comments on commit b7c0f9a

Please sign in to comment.