mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-30 01:27:31 +08:00
feat: Convert QrData into Qr data image source url (#1674)
This commit is contained in:
committed by
GitHub
parent
ccd74613db
commit
55ff761e9e
@ -5,6 +5,7 @@ pub mod ext_traits;
|
||||
#[cfg(feature = "kv_store")]
|
||||
pub mod storage_partitioning;
|
||||
|
||||
use base64::Engine;
|
||||
pub use common_utils::{
|
||||
crypto,
|
||||
ext_traits::{ByteSliceExt, BytesExt, Encode, StringExt, ValueExt},
|
||||
@ -12,7 +13,9 @@ pub use common_utils::{
|
||||
validation::validate_email,
|
||||
};
|
||||
use error_stack::{IntoReport, ResultExt};
|
||||
use image::Luma;
|
||||
use nanoid::nanoid;
|
||||
use qrcode;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
pub use self::ext_traits::{OptionExt, ValidateCall};
|
||||
@ -155,3 +158,46 @@ pub fn to_currency_base_unit_asf64(
|
||||
pub fn get_payment_attempt_id(payment_id: impl std::fmt::Display, attempt_count: i16) -> String {
|
||||
format!("{payment_id}_{attempt_count}")
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct QrImage {
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
impl QrImage {
|
||||
pub fn new_from_data(
|
||||
data: String,
|
||||
) -> Result<Self, error_stack::Report<common_utils::errors::QrCodeError>> {
|
||||
let qr_code = qrcode::QrCode::new(data.as_bytes())
|
||||
.into_report()
|
||||
.change_context(common_utils::errors::QrCodeError::FailedToCreateQrCode)?;
|
||||
|
||||
// Renders the QR code into an image.
|
||||
let qrcode_image_buffer = qr_code.render::<Luma<u8>>().build();
|
||||
let qrcode_dynamic_image = image::DynamicImage::ImageLuma8(qrcode_image_buffer);
|
||||
|
||||
let mut image_bytes = Vec::new();
|
||||
|
||||
// Encodes qrcode_dynamic_image and write it to image_bytes
|
||||
let _ = qrcode_dynamic_image.write_to(&mut image_bytes, image::ImageOutputFormat::Png);
|
||||
|
||||
let image_data_source = format!(
|
||||
"{},{}",
|
||||
consts::QR_IMAGE_DATA_SOURCE_STRING,
|
||||
consts::BASE64_ENGINE.encode(image_bytes)
|
||||
);
|
||||
Ok(Self {
|
||||
data: image_data_source,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils;
|
||||
#[test]
|
||||
fn test_image_data_source_url() {
|
||||
let qr_image_data_source_url = utils::QrImage::new_from_data("Hyperswitch".to_string());
|
||||
assert!(qr_image_data_source_url.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user