Skip to content

Commit

Permalink
feat(connector): [Paypal] use connector request reference id as refer…
Browse files Browse the repository at this point in the history
…ence for paypal (#2577)
  • Loading branch information
prasunna09 committed Oct 13, 2023
1 parent b5feab6 commit 500405d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/router/src/connector/paypal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ impl api::IncomingWebhook for Paypal {
resource
.purchase_units
.first()
.map(|unit| unit.reference_id.clone())
.and_then(|unit| unit.invoice_id.clone().or(unit.reference_id.clone()))
.ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
),
))
Expand Down
85 changes: 65 additions & 20 deletions crates/router/src/connector/paypal/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ pub struct OrderAmount {

#[derive(Default, Debug, Serialize, Eq, PartialEq)]
pub struct PurchaseUnitRequest {
reference_id: String,
reference_id: Option<String>, //reference for an item in purchase_units
invoice_id: Option<String>, //The API caller-provided external invoice number for this order. Appears in both the payer's transaction history and the emails that the payer receives.
custom_id: Option<String>, //Used to reconcile client transactions with PayPal transactions.
amount: OrderAmount,
}

Expand Down Expand Up @@ -260,10 +262,13 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
currency_code: item.router_data.request.currency,
value: item.amount.to_owned(),
};
let reference_id = item.router_data.attempt_id.clone();
let connector_request_reference_id =
item.router_data.connector_request_reference_id.clone();

let purchase_units = vec![PurchaseUnitRequest {
reference_id,
reference_id: Some(connector_request_reference_id.clone()),
custom_id: Some(connector_request_reference_id.clone()),
invoice_id: Some(connector_request_reference_id),
amount,
}];
let card = item.router_data.request.get_card()?;
Expand Down Expand Up @@ -304,9 +309,14 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
currency_code: item.router_data.request.currency,
value: item.amount.to_owned(),
};
let reference_id = item.router_data.attempt_id.clone();

let connector_req_reference_id =
item.router_data.connector_request_reference_id.clone();

let purchase_units = vec![PurchaseUnitRequest {
reference_id,
reference_id: Some(connector_req_reference_id.clone()),
custom_id: Some(connector_req_reference_id.clone()),
invoice_id: Some(connector_req_reference_id),
amount,
}];
let payment_source =
Expand Down Expand Up @@ -367,9 +377,13 @@ impl TryFrom<&PaypalRouterData<&types::PaymentsAuthorizeRouterData>> for PaypalP
currency_code: item.router_data.request.currency,
value: item.amount.to_owned(),
};
let reference_id = item.router_data.attempt_id.clone();
let connector_req_reference_id =
item.router_data.connector_request_reference_id.clone();

let purchase_units = vec![PurchaseUnitRequest {
reference_id,
reference_id: Some(connector_req_reference_id.clone()),
custom_id: Some(connector_req_reference_id.clone()),
invoice_id: Some(connector_req_reference_id),
amount,
}];
let payment_source =
Expand Down Expand Up @@ -655,7 +669,8 @@ pub struct PaymentsCollection {

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct PurchaseUnitItem {
pub reference_id: String,
pub reference_id: Option<String>,
pub invoice_id: Option<String>,
pub payments: PaymentsCollection,
}

Expand All @@ -680,11 +695,17 @@ pub struct PaypalLinks {
rel: String,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct RedirectPurchaseUnitItem {
pub invoice_id: String,
}

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

Expand Down Expand Up @@ -712,6 +733,7 @@ pub struct PaypalPaymentsSyncResponse {
id: String,
status: PaypalPaymentStatus,
amount: OrderAmount,
invoice_id: Option<String>,
supplementary_data: PaypalSupplementaryData,
}

Expand Down Expand Up @@ -792,7 +814,7 @@ impl<F, T>
capture_id: Some(id),
psync_flow: item.response.intent.clone()
}),
types::ResponseId::ConnectorTransactionId(item.response.id),
types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
),

PaypalPaymentIntent::Authorize => (
Expand All @@ -801,7 +823,7 @@ impl<F, T>
capture_id: None,
psync_flow: item.response.intent.clone()
}),
types::ResponseId::ConnectorTransactionId(item.response.id),
types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
),

PaypalPaymentIntent::Authenticate => {
Expand Down Expand Up @@ -836,7 +858,10 @@ impl<F, T>
mandate_reference: None,
connector_metadata: Some(connector_meta),
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: purchase_units
.invoice_id
.clone()
.or(Some(item.response.id)),
}),
..item.data
})
Expand Down Expand Up @@ -925,19 +950,22 @@ impl<F, T>
capture_id: None,
psync_flow: item.response.intent
});
let purchase_units = item.response.purchase_units.first();

Ok(Self {
status,
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
redirection_data: Some(services::RedirectForm::from((
link.ok_or(errors::ConnectorError::ResponseDeserializationFailed)?,
services::Method::Get,
))),
mandate_reference: None,
connector_metadata: Some(connector_meta),
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: Some(
purchase_units.map_or(item.response.id, |item| item.invoice_id.clone()),
),
}),
..item.data
})
Expand Down Expand Up @@ -1075,13 +1103,21 @@ impl<F, T>
status: storage_enums::AttemptStatus::from(item.response.status),
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(
item.response.supplementary_data.related_ids.order_id,
item.response
.supplementary_data
.related_ids
.order_id
.clone(),
),
redirection_data: None,
mandate_reference: None,
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: item
.response
.invoice_id
.clone()
.or(Some(item.response.supplementary_data.related_ids.order_id)),
}),
..item.data
})
Expand Down Expand Up @@ -1133,6 +1169,7 @@ pub struct PaypalCaptureResponse {
id: String,
status: PaypalPaymentStatus,
amount: Option<OrderAmount>,
invoice_id: Option<String>,
final_capture: bool,
}

Expand Down Expand Up @@ -1174,11 +1211,14 @@ impl TryFrom<types::PaymentsCaptureResponseRouterData<PaypalCaptureResponse>>
mandate_reference: None,
connector_metadata: Some(serde_json::json!(PaypalMeta {
authorize_id: connector_payment_id.authorize_id,
capture_id: Some(item.response.id),
capture_id: Some(item.response.id.clone()),
psync_flow: PaypalPaymentIntent::Capture
})),
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: item
.response
.invoice_id
.or(Some(item.response.id)),
}),
amount_captured: Some(amount_captured),
..item.data
Expand All @@ -1193,11 +1233,11 @@ pub enum PaypalCancelStatus {
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PaypalPaymentsCancelResponse {
id: String,
status: PaypalCancelStatus,
amount: Option<OrderAmount>,
invoice_id: Option<String>,
}

impl<F, T>
Expand All @@ -1220,12 +1260,15 @@ impl<F, T>
Ok(Self {
status,
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
redirection_data: None,
mandate_reference: None,
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: item
.response
.invoice_id
.or(Some(item.response.id)),
}),
..item.data
})
Expand Down Expand Up @@ -1408,6 +1451,7 @@ pub struct PaypalSellerPayableBreakdown {
pub struct PaypalCardWebhooks {
pub supplementary_data: PaypalSupplementaryData,
pub amount: OrderAmount,
pub invoice_id: Option<String>,
}

#[derive(Deserialize, Debug, Serialize)]
Expand Down Expand Up @@ -1528,6 +1572,7 @@ impl TryFrom<(PaypalCardWebhooks, PaypalWebhookEventType)> for PaypalPaymentsSyn
status: PaypalPaymentStatus::try_from(webhook_event)?,
amount: webhook_body.amount,
supplementary_data: webhook_body.supplementary_data,
invoice_id: webhook_body.invoice_id,
})
}
}
Expand Down

0 comments on commit 500405d

Please sign in to comment.