Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connector): [Novalnet] add Recurring payment flow for cards #5921

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crates/hyperswitch_connectors/src/connectors/novalnet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod transformers;
use std::collections::HashSet;

use base64::Engine;
use common_enums::enums;
use common_utils::{
Expand All @@ -9,6 +11,7 @@ use common_utils::{
};
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::{
payment_method_data::PaymentMethodData,
router_data::{AccessToken, ConnectorAuthType, ErrorResponse, RouterData},
router_flow_types::{
access_token_auth::AccessTokenAuth,
Expand Down Expand Up @@ -41,7 +44,9 @@ use masking::{ExposeInterface, Mask};
use transformers as novalnet;

use crate::{
constants::headers, types::ResponseRouterData, utils, utils::PaymentsAuthorizeRequestData,
constants::headers,
types::ResponseRouterData,
utils::{self, PaymentMethodDataType, PaymentsAuthorizeRequestData},
};

#[derive(Clone)]
Expand Down Expand Up @@ -183,6 +188,15 @@ impl ConnectorValidation for Novalnet {

Err(errors::ConnectorError::MissingConnectorTransactionID.into())
}
fn validate_mandate_payment(
&self,
pm_type: Option<enums::PaymentMethodType>,
pm_data: PaymentMethodData,
) -> CustomResult<(), errors::ConnectorError> {
let mandate_supported_pmd: HashSet<PaymentMethodDataType> =
HashSet::from([PaymentMethodDataType::Card]);
utils::is_mandate_supported(pm_data, pm_type, mandate_supported_pmd, self.id())
}
}

impl ConnectorRedirectResponse for Novalnet {
Expand Down
180 changes: 127 additions & 53 deletions crates/hyperswitch_connectors/src/connectors/novalnet/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use hyperswitch_domain_models::{
router_data::{ConnectorAuthType, ErrorResponse, RouterData},
router_flow_types::refunds::{Execute, RSync},
router_request_types::{PaymentsCancelData, PaymentsCaptureData, PaymentsSyncData, ResponseId},
router_response_types::{PaymentsResponseData, RedirectForm, RefundsResponseData},
router_response_types::{
MandateReference, PaymentsResponseData, RedirectForm, RefundsResponseData,
},
types::{
PaymentsAuthorizeRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,
PaymentsSyncRouterData, RefundSyncRouterData, RefundsRouterData,
Expand Down Expand Up @@ -79,18 +81,24 @@ pub struct NovalnetPaymentsRequestCustomer {
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]

pub struct NovalNetCard {
pub struct NovalnetCard {
card_number: CardNumber,
card_expiry_month: Secret<String>,
card_expiry_year: Secret<String>,
card_cvc: Secret<String>,
card_holder: Secret<String>,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
pub struct NovalnetMandate {
token: Secret<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum NovalNetPaymentData {
PaymentCard(NovalNetCard),
PaymentCard(NovalnetCard),
MandatePayment(NovalnetMandate),
}

#[derive(Default, Debug, Serialize, Clone)]
Expand All @@ -110,6 +118,7 @@ pub struct NovalnetPaymentsRequestTransaction {
return_url: Option<String>,
error_return_url: Option<String>,
enforce_3d: Option<i8>, //NOTE: Needed for CREDITCARD, GOOGLEPAY
create_token: Option<i8>,
}

#[derive(Debug, Serialize, Clone)]
Expand All @@ -125,34 +134,69 @@ impl TryFrom<&NovalnetRouterData<&PaymentsAuthorizeRouterData>> for NovalnetPaym
fn try_from(
item: &NovalnetRouterData<&PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
match item.router_data.request.payment_method_data.clone() {
PaymentMethodData::Card(req_card) => {
let auth = NovalnetAuthType::try_from(&item.router_data.connector_auth_type)?;
let auth = NovalnetAuthType::try_from(&item.router_data.connector_auth_type)?;

let merchant = NovalnetPaymentsRequestMerchant {
signature: auth.product_activation_key,
tariff: auth.tariff_id,
};
let merchant = NovalnetPaymentsRequestMerchant {
signature: auth.product_activation_key,
tariff: auth.tariff_id,
};

let enforce_3d = match item.router_data.auth_type {
enums::AuthenticationType::ThreeDs => Some(1),
enums::AuthenticationType::NoThreeDs => None,
};
let test_mode = match item.router_data.test_mode {
Some(true) => 1,
Some(false) | None => 0,
};

let billing = NovalnetPaymentsRequestBilling {
house_no: item.router_data.get_billing_line1()?,
street: item.router_data.get_billing_line2()?,
city: item.router_data.get_billing_city()?,
zip: item.router_data.get_billing_zip()?,
country_code: item.router_data.get_billing_country()?,
};

let customer_ip = item
.router_data
.request
.get_browser_info()?
.get_ip_address()?;

let customer = NovalnetPaymentsRequestCustomer {
first_name: item.router_data.get_billing_first_name()?,
last_name: item.router_data.get_billing_last_name()?,
email: item.router_data.get_billing_email()?,
mobile: item.router_data.get_billing_phone_number().ok(),
billing,
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
customer_ip,
};

let lang = item
.router_data
.request
.get_optional_language_from_browser_info()
.unwrap_or("EN".to_string());
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
let custom = NovalnetCustom { lang };
let hook_url = item.router_data.request.get_webhook_url().ok();
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved

let novalnet_card = NovalNetPaymentData::PaymentCard(NovalNetCard {
match item.router_data.request.payment_method_data.clone() {
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
PaymentMethodData::Card(req_card) => {
let novalnet_card = NovalNetPaymentData::PaymentCard(NovalnetCard {
card_number: req_card.card_number,
card_expiry_month: req_card.card_exp_month,
card_expiry_year: req_card.card_exp_year,
card_cvc: req_card.card_cvc,
card_holder: item.router_data.get_billing_full_name()?,
});

let enforce_3d = match item.router_data.auth_type {
enums::AuthenticationType::ThreeDs => Some(1),
enums::AuthenticationType::NoThreeDs => None,
let create_token = if item.router_data.request.is_mandate_payment() {
Some(1)
} else {
None
};
let test_mode = match item.router_data.test_mode {
Some(true) => 1,
Some(false) | None => 0,
};

let return_url = item.router_data.request.get_return_url().ok();
let hook_url = item.router_data.request.get_webhook_url().ok();

cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
let transaction = NovalnetPaymentsRequestTransaction {
test_mode,
payment_type: NovalNetPaymentTypes::CREDITCARD,
Expand All @@ -164,38 +208,40 @@ impl TryFrom<&NovalnetRouterData<&PaymentsAuthorizeRouterData>> for NovalnetPaym
error_return_url: return_url.clone(),
payment_data: novalnet_card,
enforce_3d,
create_token,
};

let billing = NovalnetPaymentsRequestBilling {
house_no: item.router_data.get_billing_line1()?,
street: item.router_data.get_billing_line2()?,
city: item.router_data.get_billing_city()?,
zip: item.router_data.get_billing_zip()?,
country_code: item.router_data.get_billing_country()?,
};

let customer_ip = item
.router_data
.request
.get_browser_info()?
.get_ip_address()?;

let customer = NovalnetPaymentsRequestCustomer {
first_name: item.router_data.get_billing_first_name()?,
last_name: item.router_data.get_billing_last_name()?,
email: item.router_data.get_billing_email()?,
mobile: item.router_data.get_billing_phone_number().ok(),
billing,
customer_ip,
};
Ok(Self {
merchant,
transaction,
customer,
custom,
})
}
PaymentMethodData::MandatePayment => {
let connector_mandate_id = item.router_data.request.connector_mandate_id().ok_or(
errors::ConnectorError::MissingRequiredField {
field_name: "connector_mandate_id",
},
)?;

let lang = item
.router_data
.request
.get_optional_language_from_browser_info()
.unwrap_or("EN".to_string());
let novalnet_mandate_data = NovalNetPaymentData::MandatePayment(NovalnetMandate {
token: Secret::new(connector_mandate_id),
});

let custom = NovalnetCustom { lang };
let transaction = NovalnetPaymentsRequestTransaction {
test_mode,
payment_type: NovalNetPaymentTypes::CREDITCARD,
amount: item.amount.clone(),
currency: item.router_data.request.currency.to_string(),
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
order_no: item.router_data.connector_request_reference_id.clone(),
hook_url,
return_url: None,
error_return_url: None,
payment_data: novalnet_mandate_data,
enforce_3d,
create_token: None,
};

Ok(Self {
merchant,
Expand Down Expand Up @@ -354,7 +400,13 @@ impl<F, T> TryFrom<ResponseRouterData<F, NovalnetPaymentsResponse, T, PaymentsRe
.clone()
.map(ResponseId::ConnectorTransactionId)
.unwrap_or(ResponseId::NoResponseId),
redirection_data,
redirection_data: if transaction_status
== NovalnetTransactionStatus::Confirmed
{
None //NOTE: for mandate successful flow, Novalnet always sends transaction.status of CONFIRMED
} else {
redirection_data
},
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
mandate_reference: None,
connector_metadata: None,
network_txn_id: None,
Expand Down Expand Up @@ -384,7 +436,7 @@ pub struct NovalnetResponseCustomer {
pub first_name: Secret<String>,
pub gender: Secret<String>,
pub last_name: Secret<String>,
pub mobile: Secret<String>,
pub mobile: Option<Secret<String>>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -430,7 +482,7 @@ pub struct NovalnetResponseCard {
pub card_expiry_year: Secret<u16>,
pub card_holder: Secret<String>,
pub card_number: Secret<String>,
pub cc_3d: Secret<u8>,
pub cc_3d: Option<Secret<u8>>,
pub last_four: Secret<String>,
pub token: Option<Secret<String>>,
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -676,6 +728,20 @@ impl TryFrom<&PaymentsSyncRouterData> for NovalnetSyncRequest {
}
}

impl NovalnetResponseTransactionData {
pub fn get_token(transaction_data: Option<&Self>) -> Option<String> {
if let Some(data) = transaction_data {
match &data.payment_data {
NovalnetResponsePaymentData::PaymentCard(card_data) => {
card_data.token.as_ref().map(|t| t.clone().expose().clone())
cookieg13 marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
None
}
}
}

impl<F>
TryFrom<ResponseRouterData<F, NovalnetPSyncResponse, PaymentsSyncData, PaymentsResponseData>>
for RouterData<F, PaymentsSyncData, PaymentsResponseData>
Expand All @@ -694,8 +760,11 @@ impl<F>
let transaction_status = item
.response
.transaction
.clone()
.map(|transaction_data| transaction_data.status)
.unwrap_or(NovalnetTransactionStatus::Pending);
let mandate_reference_id =
NovalnetResponseTransactionData::get_token(item.response.transaction.as_ref());

Ok(Self {
status: common_enums::AttemptStatus::from(transaction_status),
Expand All @@ -705,7 +774,12 @@ impl<F>
.map(ResponseId::ConnectorTransactionId)
.unwrap_or(ResponseId::NoResponseId),
redirection_data: None,
mandate_reference: None,
mandate_reference: mandate_reference_id.as_ref().map(|id| {
MandateReference {
connector_mandate_id: Some(id.clone()),
payment_method_id: None,
}
}),
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: transaction_id.clone(),
Expand Down
Loading
Loading