Skip to content

Commit

Permalink
feat: implement card 3ds for paypal
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakilmostak committed Oct 4, 2023
1 parent d177b4d commit 8b1b766
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 18 deletions.
31 changes: 17 additions & 14 deletions crates/router/src/connector/paypal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use error_stack::{IntoReport, ResultExt};
use masking::PeekInterface;
use transformers as paypal;

use self::transformers::PaypalMeta;
use self::transformers::{PaypalAuthResponse, PaypalMeta};
use crate::{
configs::settings,
connector::{
Expand Down Expand Up @@ -387,24 +387,27 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
data: &types::PaymentsAuthorizeRouterData,
res: Response,
) -> CustomResult<types::PaymentsAuthorizeRouterData, errors::ConnectorError> {
match data.payment_method {
diesel_models::enums::PaymentMethod::Wallet
| diesel_models::enums::PaymentMethod::BankRedirect => {
let response: paypal::PaypalRedirectResponse = res
.response
.parse_struct("paypal PaymentsRedirectResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let response: PaypalAuthResponse =
res.response
.parse_struct("paypal PaypalAuthResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;

match response {
PaypalAuthResponse::PaypalOrdersResponse(response) => {
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}
_ => {
let response: paypal::PaypalOrdersResponse = res
.response
.parse_struct("paypal PaymentsOrderResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
PaypalAuthResponse::PaypalRedirectResponse(response) => {
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}
PaypalAuthResponse::PaypalThreeDsResponse(response) => {
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
Expand Down Expand Up @@ -489,7 +492,7 @@ impl
) -> CustomResult<types::PaymentsCompleteAuthorizeRouterData, errors::ConnectorError> {
let response: paypal::PaypalOrdersResponse = res
.response
.parse_struct("paypal PaymentsOrderResponse")
.parse_struct("paypal PaypalOrdersResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from(types::ResponseRouterData {
response,
Expand Down
128 changes: 124 additions & 4 deletions crates/router/src/connector/paypal/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ pub struct CardRequest {
name: Secret<String>,
number: Option<cards::CardNumber>,
security_code: Option<Secret<String>>,
attributes: Option<ThreeDsSetting>,
}

#[derive(Debug, Serialize)]
pub struct ThreeDsSetting {
verification: ThreeDsMethod,
}

#[derive(Debug, Serialize)]
pub struct ThreeDsMethod {
method: ThreeDsType,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ThreeDsType {
ScaAlways,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -251,12 +268,22 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
let card = item.router_data.request.get_card()?;
let expiry = Some(card.get_expiry_date_as_yyyymm("-"));

let attributes = match item.router_data.auth_type {
api_models::enums::AuthenticationType::ThreeDs => Some(ThreeDsSetting {
verification: ThreeDsMethod {
method: ThreeDsType::ScaAlways,
},
}),
api_models::enums::AuthenticationType::NoThreeDs => None,
};

let payment_source = Some(PaymentSourceItem::Card(CardRequest {
billing_address: get_address_info(item.router_data.address.billing.as_ref())?,
expiry,
name: ccard.card_holder_name.clone(),
number: Some(ccard.card_number.clone()),
security_code: Some(ccard.card_cvc.clone()),
attributes,
}));

Ok(Self {
Expand Down Expand Up @@ -631,7 +658,14 @@ pub struct PurchaseUnitItem {
pub payments: PaymentsCollection,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaypalThreeDsResponse {
id: String,
status: PaypalOrderStatus,
links: Vec<PaypalLinks>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaypalOrdersResponse {
id: String,
intent: PaypalPaymentIntent,
Expand All @@ -653,6 +687,14 @@ pub struct PaypalRedirectResponse {
links: Vec<PaypalLinks>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum PaypalAuthResponse {
PaypalOrdersResponse(PaypalOrdersResponse),
PaypalRedirectResponse(PaypalRedirectResponse),
PaypalThreeDsResponse(PaypalThreeDsResponse),
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum PaypalSyncResponse {
Expand Down Expand Up @@ -775,10 +817,9 @@ impl<F, T>
}

fn get_redirect_url(
item: PaypalRedirectResponse,
link_vec: Vec<PaypalLinks>,
) -> CustomResult<Option<Url>, errors::ConnectorError> {
let mut link: Option<Url> = None;
let link_vec = item.links;
for item2 in link_vec.iter() {
if item2.rel == "payer-action" {
link = item2.href.clone();
Expand Down Expand Up @@ -832,7 +873,7 @@ impl<F, T>
item.response.clone().status,
item.response.intent.clone(),
));
let link = get_redirect_url(item.response.clone())?;
let link = get_redirect_url(item.response.links.clone())?;
let connector_meta = serde_json::json!(PaypalMeta {
authorize_id: None,
capture_id: None,
Expand All @@ -857,6 +898,85 @@ impl<F, T>
}
}

impl<F>
TryFrom<
types::ResponseRouterData<
F,
PaypalThreeDsResponse,
types::PaymentsAuthorizeData,
types::PaymentsResponseData,
>,
> for types::RouterData<F, types::PaymentsAuthorizeData, types::PaymentsResponseData>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
item: types::ResponseRouterData<
F,
PaypalThreeDsResponse,
types::PaymentsAuthorizeData,
types::PaymentsResponseData,
>,
) -> Result<Self, Self::Error> {
let intent = if item.data.request.is_auto_capture()? {
PaypalPaymentIntent::Capture
} else {
PaypalPaymentIntent::Authorize
};
let connector_meta = serde_json::json!(PaypalMeta {
authorize_id: None,
capture_id: None,
psync_flow: intent.clone()
});

let status =
storage_enums::AttemptStatus::foreign_from((item.response.clone().status, intent));
let link = get_redirect_url(item.response.links.clone())?;

Ok(Self {
status,
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),
redirection_data: Some(paypal_threeds_link((
link.ok_or(errors::ConnectorError::ResponseDeserializationFailed)?,
item.data.request.complete_authorize_url.clone().ok_or(
errors::ConnectorError::MissingRequiredField {
field_name: "router_return_url",
},
)?,
services::Method::Get,
))),
mandate_reference: None,
connector_metadata: Some(connector_meta),
network_txn_id: None,
connector_response_reference_id: None,
}),
..item.data
})
}
}

fn paypal_threeds_link(
(mut redirect_url, complete_auth_url, method): (Url, String, services::Method),
) -> services::RedirectForm {
let mut form_fields = std::collections::HashMap::from_iter(
redirect_url
.query_pairs()
.map(|(key, value)| (key.to_string(), value.to_string())),
);

// paypal requires return url to be passed as a field along with payer_action_url
form_fields.insert(String::from("redirect_uri"), complete_auth_url);

// Do not include query params in the endpoint
redirect_url.set_query(None);

services::RedirectForm::Form {
endpoint: redirect_url.to_string(),
method,
form_fields,
}
}

impl<F, T>
TryFrom<
types::ResponseRouterData<F, PaypalPaymentsSyncResponse, T, types::PaymentsResponseData>,
Expand Down

0 comments on commit 8b1b766

Please sign in to comment.