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

refactor(connector): [Paypal] Add support for both BodyKey and SignatureKey #2633

Merged
merged 23 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5a10f50
feat: add support for both auth_type bodykey and signaturekey
swangi-kumari Oct 4, 2023
c7d2586
Merge branch 'main' into paypal-auth
swangi-kumari Oct 18, 2023
b224f21
fix: resolve conflicts
swangi-kumari Oct 18, 2023
c5937b0
Merge branch 'main' into paypal-auth
swangi-kumari Oct 31, 2023
1ef5961
refactor: add more fields in orders request
ThisIsMani Nov 2, 2023
578c81d
chore: run formatter
github-actions[bot] Nov 2, 2023
c1d9b9c
Merge branch 'main' into paypal-auth
ThisIsMani Nov 7, 2023
c804f5b
Merge branch 'main' of https://github.com/juspay/hyperswitch into pay…
ThisIsMani Nov 20, 2023
87c802f
feat: Add support for temporary auth
ThisIsMani Nov 14, 2023
dd6938f
chore: run formatter
github-actions[bot] Nov 20, 2023
781ddb0
refactor: remove unnecessary code and improve build_headers
ThisIsMani Nov 20, 2023
5621a08
fix: typos
ThisIsMani Nov 21, 2023
8a757bc
fix: clippy lints
ThisIsMani Nov 21, 2023
8dccd63
Merge branch 'main' of https://github.com/juspay/hyperswitch into pay…
ThisIsMani Nov 21, 2023
2507503
Merge branch 'main' of https://github.com/juspay/hyperswitch into pay…
ThisIsMani Nov 21, 2023
8475298
refactor: resolve comments
ThisIsMani Nov 21, 2023
eca262e
fix: use old amount struct in disputes
ThisIsMani Nov 21, 2023
7e7fce5
fix: typos
ThisIsMani Nov 21, 2023
0840fdf
refactor: impl from for ItemDetails
ThisIsMani Nov 21, 2023
8df40dd
fix: clippy lints
ThisIsMani Nov 21, 2023
e5d6321
Merge branch 'main' of https://github.com/juspay/hyperswitch into pay…
ThisIsMani Nov 21, 2023
96aba6f
Merge branch 'main' into paypal-auth
ThisIsMani Nov 21, 2023
727cc62
Merge branch 'main' into paypal-auth
ThisIsMani Nov 21, 2023
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
100 changes: 71 additions & 29 deletions crates/router/src/connector/paypal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use base64::Engine;
use common_utils::ext_traits::ByteSliceExt;
use diesel_models::enums;
use error_stack::{IntoReport, ResultExt};
use masking::PeekInterface;
use masking::{ExposeInterface, PeekInterface, Secret};
use transformers as paypal;

use self::transformers::{PaypalAuthResponse, PaypalMeta, PaypalWebhookEventType};
use self::transformers::{auth_headers, PaypalAuthResponse, PaypalMeta, PaypalWebhookEventType};
use super::utils::PaymentsCompleteAuthorizeRequestData;
use crate::{
configs::settings,
Expand All @@ -31,7 +31,7 @@ use crate::{
self,
api::{self, CompleteAuthorize, ConnectorCommon, ConnectorCommonExt, VerifyWebhookSource},
transformers::ForeignFrom,
ErrorResponse, Response,
ConnectorAuthType, ErrorResponse, Response,
},
utils::{self, BytesExt},
};
Expand Down Expand Up @@ -110,8 +110,8 @@ where
.clone()
.ok_or(errors::ConnectorError::FailedToObtainAuthType)?;
let key = &req.attempt_id;

Ok(vec![
let auth = paypal::PaypalAuthType::try_from(&req.connector_auth_type)?;
let mut headers = vec![
(
headers::CONTENT_TYPE.to_string(),
self.get_content_type().to_string().into(),
Expand All @@ -121,17 +121,59 @@ where
format!("Bearer {}", access_token.token.peek()).into_masked(),
),
(
"Prefer".to_string(),
auth_headers::PREFER.to_string(),
"return=representation".to_string().into(),
),
(
"PayPal-Request-Id".to_string(),
auth_headers::PAYPAL_REQUEST_ID.to_string(),
key.to_string().into_masked(),
),
])
];
if let Ok(paypal::PaypalConnectorCredentials {
payer_id: Some(payer_id),
client_id,
..
}) = auth.get_credentials()
{
let auth_assertion_header = construct_auth_assertion_header(payer_id, client_id);
headers.extend(vec![
(
auth_headers::PAYPAL_AUTH_ASSERTION.to_string(),
auth_assertion_header.to_string().into_masked(),
),
(
auth_headers::PAYPAL_PARTNER_ATTRIBUTION_ID.to_string(),
"HyperSwitchPPCP_SP".to_string().into(),
),
])
} else {
headers.extend(vec![(
auth_headers::PAYPAL_PARTNER_ATTRIBUTION_ID.to_string(),
"HyperSwitchlegacy_Ecom".to_string().into(),
)])
}
Ok(headers)
}
}

fn construct_auth_assertion_header(
payer_id: &Secret<String>,
client_id: &Secret<String>,
) -> String {
let algorithm = consts::BASE64_ENGINE
.encode("{\"alg\":\"none\"}")
.to_string();
let merchant_credentials = format!(
"{{\"iss\":\"{}\",\"payer_id\":\"{}\"}}",
client_id.clone().expose(),
payer_id.clone().expose()
);
let encoded_credentials = consts::BASE64_ENGINE
.encode(merchant_credentials)
.to_string();
format!("{}.{}.", algorithm, encoded_credentials)
ThisIsMani marked this conversation as resolved.
Show resolved Hide resolved
}

impl ConnectorCommon for Paypal {
fn id(&self) -> &'static str {
"paypal"
Expand All @@ -151,14 +193,14 @@ impl ConnectorCommon for Paypal {

fn get_auth_header(
&self,
auth_type: &types::ConnectorAuthType,
auth_type: &ConnectorAuthType,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let auth: paypal::PaypalAuthType = auth_type
.try_into()
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;
let auth = paypal::PaypalAuthType::try_from(auth_type)?;
let credentials = auth.get_credentials()?;

Ok(vec![(
headers::AUTHORIZATION.to_string(),
auth.api_key.into_masked(),
credentials.client_secret.clone().into_masked(),
)])
}

Expand Down Expand Up @@ -260,14 +302,14 @@ impl ConnectorIntegration<api::AccessTokenAuth, types::AccessTokenRequestData, t
req: &types::RefreshTokenRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let auth: paypal::PaypalAuthType = (&req.connector_auth_type)
.try_into()
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;

let auth_id = auth
.key1
.zip(auth.api_key)
.map(|(key1, api_key)| format!("{}:{}", key1, api_key));
let auth = paypal::PaypalAuthType::try_from(&req.connector_auth_type)?;
let credentials = auth.get_credentials()?;

let auth_id = credentials
.client_id
.clone()
.zip(credentials.client_secret.clone())
.map(|(client_id, client_secret)| format!("{}:{}", client_id, client_secret));
let auth_val = format!("Basic {}", consts::BASE64_ENGINE.encode(auth_id.peek()));

Ok(vec![
Expand Down Expand Up @@ -998,14 +1040,14 @@ impl
>,
_connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let auth: paypal::PaypalAuthType = (&req.connector_auth_type)
.try_into()
.change_context(errors::ConnectorError::FailedToObtainAuthType)?;

let auth_id = auth
.key1
.zip(auth.api_key)
.map(|(key1, api_key)| format!("{}:{}", key1, api_key));
let auth = paypal::PaypalAuthType::try_from(&req.connector_auth_type)?;
let credentials = auth.get_credentials()?;

let auth_id = credentials
.client_id
.clone()
.zip(credentials.client_secret.clone())
.map(|(client_id, client_secret)| format!("{}:{}", client_id, client_secret));
let auth_val = format!("Basic {}", consts::BASE64_ENGINE.encode(auth_id.peek()));

Ok(vec![
Expand Down
Loading
Loading