feat:H1083-L1-L3-L4-L5 [Payment Authorise and Sync] + [Refunds and Sync] + [Redirection Flow (BNPL)] + [3DS Payment and Sync] for MultiSafePay (#658)

Co-authored-by: Zaid <syed.zaidali@juspay.in>
Co-authored-by: Rajak Rupakkumar Asishkumar <rajak.rupakkumar@juspay.in>
Co-authored-by: Jagan Elavarasan <jaganelavarasan@gmail.com>
This commit is contained in:
rupakrajak
2023-02-28 05:04:26 +05:30
committed by GitHub
parent 0de5d44195
commit 79aa8f3d3d
19 changed files with 1791 additions and 127 deletions

View File

@ -1,5 +1,9 @@
use std::collections::HashMap;
use error_stack::{report, IntoReport, ResultExt};
use masking::Secret;
use once_cell::sync::Lazy;
use regex::Regex;
use crate::{
core::errors::{self, CustomResult},
@ -38,17 +42,91 @@ pub trait RouterData {
fn get_billing(&self) -> Result<&api::Address, Error>;
fn get_billing_country(&self) -> Result<String, Error>;
fn get_billing_phone(&self) -> Result<&api::PhoneDetails, Error>;
fn get_description(&self) -> Result<String, Error>;
fn get_billing_address(&self) -> Result<&api::AddressDetails, Error>;
fn get_connector_meta(&self) -> Result<serde_json::Value, Error>;
fn get_session_token(&self) -> Result<String, Error>;
fn get_billing_address(&self) -> Result<&api::AddressDetails, Error>;
fn to_connector_meta<T>(&self) -> Result<T, Error>
where
T: serde::de::DeserializeOwned;
fn get_return_url(&self) -> Result<String, Error>;
}
impl<Flow, Request, Response> RouterData for types::RouterData<Flow, Request, Response> {
fn get_billing(&self) -> Result<&api::Address, Error> {
self.address
.billing
.as_ref()
.ok_or_else(missing_field_err("billing"))
}
fn get_billing_country(&self) -> Result<String, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.address.as_ref())
.and_then(|ad| ad.country.clone())
.ok_or_else(missing_field_err("billing.address.country"))
}
fn get_billing_phone(&self) -> Result<&api::PhoneDetails, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.phone.as_ref())
.ok_or_else(missing_field_err("billing.phone"))
}
fn get_description(&self) -> Result<String, Error> {
self.description
.clone()
.ok_or_else(missing_field_err("description"))
}
fn get_billing_address(&self) -> Result<&api::AddressDetails, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.address.as_ref())
.ok_or_else(missing_field_err("billing.address"))
}
fn get_connector_meta(&self) -> Result<serde_json::Value, Error> {
self.connector_meta_data
.clone()
.ok_or_else(missing_field_err("connector_meta_data"))
}
fn get_session_token(&self) -> Result<String, Error> {
self.session_token
.clone()
.ok_or_else(missing_field_err("session_token"))
}
fn to_connector_meta<T>(&self) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
serde_json::from_value::<T>(self.get_connector_meta()?)
.into_report()
.change_context(errors::ConnectorError::NoConnectorMetaData)
}
fn get_return_url(&self) -> Result<String, Error> {
self.router_return_url
.clone()
.ok_or_else(missing_field_err("return_url"))
}
}
pub trait PaymentsRequestData {
fn get_card(&self) -> Result<api::Card, Error>;
fn get_return_url(&self) -> Result<String, Error>;
}
impl PaymentsRequestData for types::PaymentsAuthorizeRouterData {
fn get_card(&self) -> Result<api::Card, Error> {
match self.request.payment_method_data.clone() {
api::PaymentMethodData::Card(card) => Ok(card),
_ => Err(missing_field_err("card")()),
}
}
}
pub trait PaymentsAuthorizeRequestData {
@ -98,75 +176,33 @@ impl RefundsRequestData for types::RefundsData {
}
}
impl<Flow, Request, Response> RouterData for types::RouterData<Flow, Request, Response> {
fn get_billing_country(&self) -> Result<String, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.address.as_ref())
.and_then(|ad| ad.country.clone())
.ok_or_else(missing_field_err("billing.address.country"))
}
static CARD_REGEX: Lazy<HashMap<CardIssuer, Result<Regex, regex::Error>>> = Lazy::new(|| {
let mut map = HashMap::new();
// Reference: https://gist.github.com/michaelkeevildown/9096cd3aac9029c4e6e05588448a8841
// [#379]: Determine card issuer from card BIN number
map.insert(CardIssuer::Master, Regex::new(r"^5[1-5][0-9]{14}$"));
map.insert(CardIssuer::AmericanExpress, Regex::new(r"^3[47][0-9]{13}$"));
map.insert(CardIssuer::Visa, Regex::new(r"^4[0-9]{12}(?:[0-9]{3})?$"));
map.insert(CardIssuer::Discover, Regex::new(r"^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$"));
map.insert(
CardIssuer::Maestro,
Regex::new(r"^(5018|5020|5038|5893|6304|6759|6761|6762|6763)[0-9]{8,15}$"),
);
map
});
fn get_billing_phone(&self) -> Result<&api::PhoneDetails, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.phone.as_ref())
.ok_or_else(missing_field_err("billing.phone"))
}
fn get_billing_address(&self) -> Result<&api::AddressDetails, Error> {
self.address
.billing
.as_ref()
.and_then(|a| a.address.as_ref())
.ok_or_else(missing_field_err("billing.address"))
}
fn get_billing(&self) -> Result<&api::Address, Error> {
self.address
.billing
.as_ref()
.ok_or_else(missing_field_err("billing"))
}
fn get_connector_meta(&self) -> Result<serde_json::Value, Error> {
self.connector_meta_data
.clone()
.ok_or_else(missing_field_err("connector_meta_data"))
}
fn get_session_token(&self) -> Result<String, Error> {
self.session_token
.clone()
.ok_or_else(missing_field_err("session_token"))
}
fn to_connector_meta<T>(&self) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
serde_json::from_value::<T>(self.get_connector_meta()?)
.into_report()
.change_context(errors::ConnectorError::NoConnectorMetaData)
}
}
impl PaymentsRequestData for types::PaymentsAuthorizeRouterData {
fn get_return_url(&self) -> Result<String, Error> {
self.router_return_url
.clone()
.ok_or_else(missing_field_err("router_return_url"))
}
fn get_card(&self) -> Result<api::Card, Error> {
match self.request.payment_method_data.clone() {
api::PaymentMethodData::Card(card) => Ok(card),
_ => Err(missing_field_err("card")()),
}
}
#[derive(Debug, Copy, Clone, strum::Display, Eq, Hash, PartialEq)]
pub enum CardIssuer {
AmericanExpress,
Master,
Maestro,
Visa,
Discover,
}
pub trait CardData {
fn get_card_expiry_year_2_digit(&self) -> Secret<String>;
fn get_card_issuer(&self) -> Result<CardIssuer, Error>;
}
impl CardData for api::Card {
@ -175,6 +211,24 @@ impl CardData for api::Card {
let year = binding.peek();
Secret::new(year[year.len() - 2..].to_string())
}
fn get_card_issuer(&self) -> Result<CardIssuer, Error> {
get_card_issuer(self.card_number.peek().clone().as_str())
}
}
fn get_card_issuer(card_number: &str) -> Result<CardIssuer, Error> {
for (k, v) in CARD_REGEX.iter() {
let regex: Regex = v
.clone()
.into_report()
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
if regex.is_match(card_number) {
return Ok(*k);
}
}
Err(error_stack::Report::new(
errors::ConnectorError::NotImplemented("Card Type".into()),
))
}
pub trait PhoneDetailsData {
fn get_number(&self) -> Result<Secret<String>, Error>;