Skip to content

Commit

Permalink
feat(connector): [Payme] Add Sync, RSync & webhook flow support (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjunKarthik authored Aug 4, 2023
1 parent 3250204 commit 8057980
Show file tree
Hide file tree
Showing 2 changed files with 371 additions and 71 deletions.
235 changes: 185 additions & 50 deletions crates/router/src/connector/payme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod transformers;

use std::fmt::Debug;

use common_utils::{crypto, ext_traits::ByteSliceExt};
use common_utils::crypto;
use error_stack::{IntoReport, ResultExt};
use masking::ExposeInterface;
use transformers as payme;
Expand Down Expand Up @@ -336,7 +336,55 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>
for Payme
{
// default implementation of build_request method will be executed
fn get_url(
&self,
_req: &types::RouterData<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
Ok(format!("{}api/get-sales", self.base_url(connectors)))
}

fn get_content_type(&self) -> &'static str {
self.common_get_content_type()
}

fn get_headers(
&self,
req: &types::RouterData<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
self.build_headers(req, connectors)
}

fn get_request_body(
&self,
req: &types::RouterData<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::PaymeQuerySaleRequest::try_from(req)?;
let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj,
utils::Encode::<payme::PayRequest>::encode_to_string_of_json,
)
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some(payme_req))
}

fn build_request(
&self,
req: &types::RouterData<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
Ok(Some(
services::RequestBuilder::new()
.method(services::Method::Post)
.url(&types::PaymentsSyncType::get_url(self, req, connectors)?)
.attach_default_headers()
.headers(types::PaymentsSyncType::get_headers(self, req, connectors)?)
.body(types::PaymentsSyncType::get_request_body(self, req)?)
.build(),
))
}

fn handle_response(
&self,
data: &types::RouterData<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>,
Expand All @@ -350,9 +398,9 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe
types::PaymentsSyncData: Clone,
types::PaymentsResponseData: Clone,
{
let response: payme::PaymePaySaleResponse = res
let response: payme::PaymePaymentsResponse = res
.response
.parse_struct("Payme PaymentsResponse")
.parse_struct("PaymePaymentsResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from(types::ResponseRouterData {
response,
Expand Down Expand Up @@ -512,18 +560,15 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
data: &types::RefundsRouterData<api::Execute>,
res: Response,
) -> CustomResult<types::RefundsRouterData<api::Execute>, errors::ConnectorError> {
let response: payme::RefundResponse = res
let response: payme::PaymeRefundResponse = res
.response
.parse_struct("payme RefundResponse")
.parse_struct("PaymeRefundResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from((
&data.request,
types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
},
))
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}

fn get_error_response(
Expand All @@ -535,7 +580,84 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
}

impl ConnectorIntegration<api::RSync, types::RefundsData, types::RefundsResponseData> for Payme {
// default implementation of build_request method will be executed
fn get_url(
&self,
_req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
Ok(format!("{}api/get-transactions", self.base_url(connectors)))
}

fn get_headers(
&self,
req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
self.build_headers(req, connectors)
}

fn get_content_type(&self) -> &'static str {
self.common_get_content_type()
}

fn get_request_body(
&self,
req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::PaymeQueryTransactionRequest::try_from(req)?;
let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj,
utils::Encode::<payme::PayRequest>::encode_to_string_of_json,
)
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some(payme_req))
}

fn build_request(
&self,
req: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
let request = services::RequestBuilder::new()
.method(services::Method::Post)
.url(&types::RefundSyncType::get_url(self, req, connectors)?)
.attach_default_headers()
.headers(types::RefundSyncType::get_headers(self, req, connectors)?)
.body(types::RefundSyncType::get_request_body(self, req)?)
.build();
Ok(Some(request))
}

fn handle_response(
&self,
data: &types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
res: Response,
) -> CustomResult<
types::RouterData<api::RSync, types::RefundsData, types::RefundsResponseData>,
errors::ConnectorError,
>
where
api::RSync: Clone,
types::RefundsData: Clone,
types::RefundsResponseData: Clone,
{
let response: payme::PaymeQueryTransactionResponse = res
.response
.parse_struct("GetSalesResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
}

fn get_error_response(
&self,
res: Response,
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
self.build_error_response(res)
}
}

#[async_trait::async_trait]
Expand All @@ -551,10 +673,10 @@ impl api::IncomingWebhook for Payme {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let resource: payme::WebhookEventDataResourceSignature = request
.body
.parse_struct("WebhookEvent")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let resource =
serde_urlencoded::from_bytes::<payme::WebhookEventDataResourceSignature>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
Ok(resource.payme_signature.expose().into_bytes())
}

Expand All @@ -564,10 +686,9 @@ impl api::IncomingWebhook for Payme {
_merchant_id: &str,
secret: &[u8],
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let resource: payme::WebhookEventDataResource =
request
.body
.parse_struct("WebhookEvent")
let resource =
serde_urlencoded::from_bytes::<payme::WebhookEventDataResource>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
Ok(format!(
"{}{}{}",
Expand All @@ -583,57 +704,71 @@ impl api::IncomingWebhook for Payme {
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::webhooks::ObjectReferenceId, errors::ConnectorError> {
let resource: payme::WebhookEventDataResource =
request
.body
.parse_struct("WebhookEvent")
let resource =
serde_urlencoded::from_bytes::<payme::WebhookEventDataResource>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let id = match resource.notify_type {
transformers::NotifyType::SaleComplete
| transformers::NotifyType::SaleAuthorized
| transformers::NotifyType::SaleFailure => api::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(resource.payme_sale_id),
),
transformers::NotifyType::Refund => api::webhooks::ObjectReferenceId::RefundId(
api_models::webhooks::RefundIdType::ConnectorRefundId(resource.payme_sale_id),
),
transformers::NotifyType::SaleChargeback
| transformers::NotifyType::SaleChargebackRefund => {
api::webhooks::ObjectReferenceId::PaymentId(
| transformers::NotifyType::SaleFailure => {
Ok(api::webhooks::ObjectReferenceId::PaymentId(
api_models::payments::PaymentIdType::ConnectorTransactionId(
resource.payme_sale_id,
),
)
))
}
};
transformers::NotifyType::Refund => Ok(api::webhooks::ObjectReferenceId::RefundId(
api_models::webhooks::RefundIdType::ConnectorRefundId(
resource.payme_transaction_id,
),
)),
transformers::NotifyType::SaleChargeback
| transformers::NotifyType::SaleChargebackRefund => {
Err(errors::ConnectorError::WebhookEventTypeNotFound)
}
}?;
Ok(id)
}

fn get_webhook_event_type(
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
let resource: payme::WebhookEventDataResourceEvent = request
.body
.parse_struct("WebhookEvent")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let resource =
serde_urlencoded::from_bytes::<payme::WebhookEventDataResourceEvent>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
Ok(api::IncomingWebhookEvent::from(resource.notify_type))
}

fn get_webhook_resource_object(
&self,
request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
let resource: payme::WebhookEventDataResource =
request
.body
.parse_struct("WebhookEvent")
let resource =
serde_urlencoded::from_bytes::<payme::WebhookEventDataResource>(request.body)
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
let sale_response = payme::PaymePaySaleResponse::try_from(resource)?;

let res_json = serde_json::to_value(sale_response)
.into_report()
.change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?;
let res_json = match resource.notify_type {
transformers::NotifyType::SaleComplete
| transformers::NotifyType::SaleAuthorized
| transformers::NotifyType::SaleFailure => {
serde_json::to_value(payme::PaymePaySaleResponse::from(resource))
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)
}
transformers::NotifyType::Refund => {
serde_json::to_value(payme::PaymeQueryTransactionResponse::from(resource))
.into_report()
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)
}
transformers::NotifyType::SaleChargeback
| transformers::NotifyType::SaleChargebackRefund => {
Err(errors::ConnectorError::WebhookEventTypeNotFound).into_report()
}
}?;

Ok(res_json)
}
Expand Down
Loading

0 comments on commit 8057980

Please sign in to comment.