Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(paypal): support billing agreement mandates (#13574)

CHALLA NISHANTH BABUb72fac7e8d18
diff --git a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
index af62ad9c66..d397ad149d 100644
--- a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
@@ -470,6 +470,7 @@ pub struct ChargebeeInvoicePayments {
#[derive(Serialize, Deserialize, Debug)]
pub struct ChargebeeTransactionData {
+ id: Option<String>,
id_at_gateway: Option<String>,
status: ChargebeeTranasactionStatus,
error_code: Option<String>,
@@ -842,6 +843,7 @@ impl TryFrom<ChargebeeWebhookBody> for revenue_recovery::RevenueRecoveryAttemptD
.content
.transaction
.id_at_gateway
+ .or(item.content.transaction.id)
.map(common_utils::types::ConnectorTransactionId::TxnId);
let error_code = item.content.transaction.error_code.clone();
let error_message = item.content.transaction.error_text.clone();
diff --git a/crates/hyperswitch_connectors/src/connectors/paypal/transformers.rs b/crates/hyperswitch_connectors/src/connectors/paypal/transformers.rs
index 4c56963d9f..595415a630 100644
--- a/crates/hyperswitch_connectors/src/connectors/paypal/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/paypal/transformers.rs
@@ -474,6 +474,26 @@ pub struct CustomerRequestData {
merchant_customer_id: Option<common_utils::id_type::CustomerId>,
}
+/// PayPal keeps legacy Billing Agreements in a different namespace from Vault v3 tokens, and
+/// accepts them on their own field rather than as a `vault_id`. Billing connectors such as
+/// Chargebee hand us the agreement id for stored PayPal payment methods, so a mandate payment
+/// has to be able to charge either kind of credential.
+#[derive(Debug, Serialize, Deserialize)]
+pub struct BillingAgreementStruct {
+ billing_agreement_id: Secret<String>,
+}
+
+impl BillingAgreementStruct {
+ /// PayPal billing agreement ids are consistently `B-` prefixed, which is the only signal
+ /// available here - the mandate id reaches the connector as an opaque string with no
+ /// accompanying metadata describing which namespace it belongs to.
+ const ID_PREFIX: &'static str = "B-";
+
+ fn is_billing_agreement_id(mandate_id: &str) -> bool {
+ mandate_id.starts_with(Self::ID_PREFIX)
+ }
+}
+
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum CardRequest {
@@ -537,6 +557,7 @@ pub enum ShippingPreference {
pub enum PaypalRedirectionRequest {
PaypalRedirectionStruct(PaypalRedirectionStruct),
PaypalVaultStruct(VaultStruct),
+ BillingAgreementStruct(BillingAgreementStruct),
}
#[derive(Debug, Serialize)]
@@ -1282,16 +1303,26 @@ impl TryFrom<&PaypalRouterData<&PaymentsAuthorizeRouterData>> for PaypalPayments
}),
))),
enums::PaymentMethodType::Paypal => Ok(Some(PaymentSourceItem::Paypal(
- PaypalRedirectionRequest::PaypalVaultStruct(VaultStruct {
- vault_id: connector_mandate_id.into(),
- attributes: item.router_data.get_optional_customer_id().as_ref().map(
- |customer_id| VaultRequestAttributes {
- customer: Some(CustomerRequestData {
- merchant_customer_id: Some(customer_id.clone()),
- }),
+ if BillingAgreementStruct::is_billing_agreement_id(&connector_mandate_id) {
+ PaypalRedirectionRequest::BillingAgreementStruct(
+ BillingAgreementStruct {
+ billing_agreement_id: connector_mandate_id.into(),
},
- ),
- }),
+ )
+ } else {
+ PaypalRedirectionRequest::PaypalVaultStruct(VaultStruct {
+ vault_id: connector_mandate_id.into(),
+ attributes: item
+ .router_data
+ .get_optional_customer_id()
+ .as_ref()
+ .map(|customer_id| VaultRequestAttributes {
+ customer: Some(CustomerRequestData {
+ merchant_customer_id: Some(customer_id.clone()),
+ }),
+ }),
+ })
+ },
))),
enums::PaymentMethodType::Ach
| enums::PaymentMethodType::Affirm
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 9a47bf738c..6fdf8dc8c2 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -431,6 +431,8 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
.and_then(|noon| noon.order_category.clone())
});
+ let is_off_session = get_off_session(payment_data.mandate_data.as_ref(), None);
+
// Account funded transaction details are merchant supplied and are always read
// from the payment intent, never from the confirm request.
let recipient_details = payment_data
@@ -446,7 +448,7 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
.get_required_value("payment_method_data")?,
setup_future_usage: Some(payment_data.payment_intent.setup_future_usage),
mandate_id: payment_data.mandate_data.clone(),
- off_session: None,
+ off_session: is_off_session,
setup_mandate_details: None,
confirm: true,
capture_method: Some(payment_data.payment_intent.capture_method),
@@ -570,7 +572,14 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
payment_method_status: None,
payment_method_token: None,
connector_customer: connector_customer_id,
- recurring_mandate_payment_data: None,
+ recurring_mandate_payment_data: is_off_session.unwrap_or(false).then(|| {
+ hyperswitch_domain_models::router_data::RecurringMandatePaymentData {
+ payment_method_type: payment_data.payment_attempt.payment_method_subtype,
+ original_payment_authorized_amount: None,
+ original_payment_authorized_currency: None,
+ mandate_metadata: None,
+ }
+ }),
// TODO: This has to be generated as the reference id based on the connector configuration
// Some connectros might not accept accept the global id. This has to be done when generating the reference id
connector_request_reference_id,