Skip to content

Commit

Permalink
backend/feat/split_settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
0utkarsh committed Oct 18, 2024
1 parent 9b9d354 commit 33a7e43
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 10 deletions.
38 changes: 35 additions & 3 deletions lib/mobility-core/src/Kernel/External/Payment/Interface/Juspay.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,22 @@ mkCreateOrderReq returnUrl clientId CreateOrderReq {..} =
mandate_end_date = mandateEndDate,
options_get_upi_deep_links = optionsGetUpiDeepLinks,
metadata_expiry_in_mins = metadataExpiryInMins,
metadata_gateway_reference_id = metadataGatewayReferenceId
metadata_gateway_reference_id = metadataGatewayReferenceId,
split_settlement_details = mkSplitSettlementDetails <$> splitSettlementDetails
}

mkSplitSettlementDetails :: SplitSettlementDetails -> Juspay.SplitSettlementDetails
mkSplitSettlementDetails splitDetails =
Juspay.SplitSettlementDetails
{ marketplace = mkMarketplace splitDetails.marketplace,
mdr_borne_by = show splitDetails.mdrBorneBy,
vendor = mkVendor splitDetails.vendor
}
where
mkMarketplace Marketplace {..} = Juspay.Marketplace {..}
mkVendor vendor = Juspay.Vendor {split = mkSplit <$> vendor.split}
mkSplit split = Juspay.Split {amount = split.amount, merchant_commission = split.merchantCommission, sub_mid = split.subMid}

orderStatus ::
( HasCallStack,
Metrics.CoreMetrics m,
Expand Down Expand Up @@ -265,6 +278,7 @@ mkOrderStatusResp Juspay.OrderData {..} =
amountRefunded = realToFrac <$> amount_refunded,
payerVpa = payer_vpa,
upi = castUpi <$> upi,
splitSettlementResponse = mkSplitSettlementResponse <$> split_settlement_response,
..
}

Expand All @@ -286,7 +300,7 @@ mkExecutionReq MandateExecutionReq {..} merchantId =
{ merchantId,
mandateId = mandateId,
mandate = Juspay.MandateInfo {notificationId = notificationId, executionDate = show $ utcTimeToPOSIXSeconds executionDate},
order = Juspay.MandateOrder {orderId = orderId, orderAmount = show amount, orderCustomerId = customerId},
order = Juspay.MandateOrder {orderId = orderId, orderAmount = show amount, orderCustomerId = customerId, splitSettlementDetails = mkSplitSettlementDetails <$> splitSettlementDetails},
format = "json"
}

Expand Down Expand Up @@ -384,7 +398,7 @@ mkWebhookOrderStatusResp now (eventName, Juspay.OrderAndNotificationStatusConten
payerVpa = justOrder.payer_vpa,
upi = castUpi <$> justOrder.upi,
refunds = maybe [] mkRefundsData justOrder.refunds,
amountRefunded = realToFrac <$> justOrder.amount_refunded
amountRefunded = realToFrac <$> justOrder.amount_refunded -- not adding split
}
Nothing -> do
let (isRetriedOrder, retargetPaymentLink, retargetPaymentLinkExpiry, isRetargetedOrder) = parseRetargetAndRetryData justOrder.metadata justOrder.links justOrder.additional_info
Expand All @@ -409,6 +423,7 @@ mkWebhookOrderStatusResp now (eventName, Juspay.OrderAndNotificationStatusConten
amountRefunded = realToFrac <$> justOrder.amount_refunded,
payerVpa = justOrder.payer_vpa,
upi = castUpi <$> justOrder.upi,
splitSettlementResponse = mkSplitSettlementResponse <$> justOrder.split_settlement_response,
..
}
(Nothing, Just justMandate, _, _) ->
Expand Down Expand Up @@ -458,10 +473,27 @@ mkWebhookOrderStatusResp now (eventName, Juspay.OrderAndNotificationStatusConten
amountRefunded = Nothing,
payerVpa = justTransaction.payer_vpa,
upi = castUpi <$> justTransaction.upi,
splitSettlementResponse = Nothing,
..
}
(_, _, Nothing, _) -> BadStatusResp

mkSplitSettlementResponse :: Juspay.SplitSettlementResponse -> SplitSettlementResponse
mkSplitSettlementResponse Juspay.SplitSettlementResponse {..} =
SplitSettlementResponse
{ splitDetails = split_details >>= (Just . map mkSplitDetailsResponse),
splitApplied = split_applied
}
where
mkSplitDetailsResponse Juspay.SplitDetailsResponse {..} =
SplitDetailsResponse
{ subVendorId = sub_vendor_id,
merchantCommission = merchant_commission,
amount = amount,
gatewaySubAccountId = gateway_sub_account_id,
epgTxnId = epg_txn_id
}

castSourceInfo :: Juspay.SourceInfo -> SourceInfo
castSourceInfo source_info =
SourceInfo
Expand Down
52 changes: 49 additions & 3 deletions lib/mobility-core/src/Kernel/External/Payment/Interface/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,36 @@ data CreateOrderReq = CreateOrderReq
mandateEndDate :: Maybe Text,
metadataGatewayReferenceId :: Maybe Text,
optionsGetUpiDeepLinks :: Maybe Bool,
metadataExpiryInMins :: Maybe Int
metadataExpiryInMins :: Maybe Int,
splitSettlementDetails :: Maybe SplitSettlementDetails
}

data Split = Split
{ amount :: HighPrecMoney,
merchantCommission :: HighPrecMoney,
subMid :: Text
}
deriving stock (Show, Eq, Generic)

newtype Vendor = Vendor
{ split :: [Split]
}
deriving stock (Show, Eq, Generic)

data SplitSettlementDetails = SplitSettlementDetails
{ marketplace :: Marketplace,
mdrBorneBy :: MBY,
vendor :: Vendor
}
deriving stock (Show, Eq, Generic)

data MBY = MARKETPLACE | VENDOR | ALL deriving stock (Show, Eq, Generic)

newtype Marketplace = Marketplace
{ amount :: HighPrecMoney
}
deriving stock (Show, Eq, Generic)

newtype OrderStatusReq = OrderStatusReq
{ orderShortId :: Text
}
Expand Down Expand Up @@ -103,7 +130,8 @@ data OrderStatusResp
amountRefunded :: Maybe HighPrecMoney,
refunds :: [RefundsData],
payerVpa :: Maybe Text,
upi :: Maybe Upi
upi :: Maybe Upi,
splitSettlementResponse :: Maybe SplitSettlementResponse
}
| MandateOrderStatusResp
{ eventName :: Maybe PaymentStatus,
Expand Down Expand Up @@ -159,6 +187,23 @@ data OrderStatusResp
deriving stock (Show, Read, Eq, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)

data SplitSettlementResponse = SplitSettlementResponse
{ splitDetails :: Maybe [SplitDetailsResponse],
splitApplied :: Maybe Bool
}
deriving stock (Show, Read, Eq, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)

data SplitDetailsResponse = SplitDetailsResponse
{ subVendorId :: Maybe Text,
amount :: Maybe HighPrecMoney,
merchantCommission :: Maybe HighPrecMoney,
gatewaySubAccountId :: Maybe Text,
epgTxnId :: Maybe Text
}
deriving stock (Show, Read, Eq, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)

data Upi = Upi
{ payerApp :: Maybe Text,
payerVpa :: Maybe Text,
Expand Down Expand Up @@ -246,7 +291,8 @@ data MandateExecutionReq = MandateExecutionReq
amount :: HighPrecMoney,
customerId :: Text,
mandateId :: Text,
executionDate :: UTCTime
executionDate :: UTCTime,
splitSettlementDetails :: Maybe SplitSettlementDetails
}

data MandateExecutionRes = MandateExecutionRes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,25 @@ data OrderData = OrderData
additional_info :: Maybe AdditionalInfo,
links :: Maybe LinkData,
amount_refunded :: Maybe Double,
refunds :: Maybe [RefundsData]
refunds :: Maybe [RefundsData],
split_settlement_response :: Maybe SplitSettlementResponse
}
deriving stock (Show, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)

data SplitSettlementResponse = SplitSettlementResponse
{ split_details :: Maybe [SplitDetailsResponse],
split_applied :: Maybe Bool
}
deriving stock (Show, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)

data SplitDetailsResponse = SplitDetailsResponse
{ sub_vendor_id :: Maybe Text,
amount :: Maybe HighPrecMoney,
merchant_commission :: Maybe HighPrecMoney,
gateway_sub_account_id :: Maybe Text,
epg_txn_id :: Maybe Text
}
deriving stock (Show, Generic)
deriving anyclass (FromJSON, ToJSON, ToSchema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
module Kernel.External.Payment.Juspay.Types.CreateOrder where

import Data.Aeson
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Text as T
import qualified Data.Text.Encoding as DT
import Kernel.External.Payment.Juspay.Types.Common
import Kernel.Prelude
import Kernel.Types.Price
import Servant.API (FromHttpApiData (..), ToHttpApiData (..))

data CreateOrderReq = CreateOrderReq
{ order_id :: Text,
Expand All @@ -41,10 +45,53 @@ data CreateOrderReq = CreateOrderReq
mandate_end_date :: Maybe Text,
metadata_gateway_reference_id :: Maybe Text,
options_get_upi_deep_links :: Maybe Bool,
metadata_expiry_in_mins :: Maybe Int
metadata_expiry_in_mins :: Maybe Int,
split_settlement_details :: Maybe SplitSettlementDetails
}
deriving stock (Show, Eq, Generic)

data Split = Split
{ amount :: HighPrecMoney,
merchant_commission :: HighPrecMoney,
sub_mid :: Text
}
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

newtype Vendor = Vendor
{ split :: [Split]
}
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

data SplitSettlementDetails = SplitSettlementDetails
{ marketplace :: Marketplace,
mdr_borne_by :: Text,
vendor :: Vendor
}
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

instance FromHttpApiData SplitSettlementDetails where
parseUrlPiece = parseHeader . DT.encodeUtf8
parseQueryParam = parseUrlPiece
parseHeader = left T.pack . eitherDecode . BSL.fromStrict

instance ToHttpApiData SplitSettlementDetails where
toUrlPiece = DT.decodeUtf8 . toHeader
toQueryParam = toUrlPiece
toHeader = BSL.toStrict . encode

data MBY = MARKETPLACE | VENDOR | ALL
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

newtype Marketplace = Marketplace
{ amount :: HighPrecMoney
}
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

jsonReqOptions :: Options
jsonReqOptions =
defaultOptions
Expand All @@ -58,6 +105,7 @@ jsonReqOptions =
"metadata_remarks" -> "metadata.AXIS_BIZ:remarks"
"metadata_gateway_reference_id" -> "metadata.JUSPAY:gateway_reference_id"
"metadata_expiry_in_mins" -> "metadata.expiryInMins"
"split_settlement_details" -> "metadata.split_settlement_details"
other -> other
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Kernel.External.Payment.Juspay.Types.Mandate where

import Kernel.External.Payment.Juspay.Types.Common (NotificationStatus, TransactionStatus)
import Kernel.External.Payment.Juspay.Types.CreateOrder (SplitSettlementDetails)
import Kernel.Prelude
import Servant (ToHttpApiData (..))
import Web.FormUrlEncoded
Expand Down Expand Up @@ -74,7 +75,8 @@ data ProviderResponse = ProviderResponse
data MandateOrder = MandateOrder
{ orderId :: Text,
orderAmount :: Text,
orderCustomerId :: Text
orderCustomerId :: Text,
splitSettlementDetails :: Maybe SplitSettlementDetails
}

data MandateInfo = MandateInfo
Expand All @@ -99,7 +101,8 @@ instance ToForm MandateExecutionReq where
("mandate.notification_id", toQueryParam (notificationId mandate)),
("mandate.execution_date", toQueryParam (executionDate mandate)),
("merchant_id", toQueryParam merchantId),
("format", toQueryParam format)
("format", toQueryParam format),
("order.metadata.split_settlement_details", toQueryParam (splitSettlementDetails order))
]

data MandateExecutionRes = MandateExecutionRes
Expand Down

0 comments on commit 33a7e43

Please sign in to comment.