-
Notifications
You must be signed in to change notification settings - Fork 37
feat: implement multi-asset payment gateway (#103) #128
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
Merged
MPSxDev
merged 2 commits into
PayStell:main
from
Chucks1093:feat/multi-asset-payment-gateway-103
Feb 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,343 @@ | ||
| import { Request, Response } from "express"; | ||
| import { AssetConfigurationService } from "../services/AssetConfigurationService"; | ||
| import { AssetPriceService } from "../services/AssetPriceService"; | ||
| import { MultiAssetPaymentService } from "../services/MultiAssetPaymentService"; | ||
| import { AppError } from "../utils/AppError"; | ||
| import logger from "../utils/logger"; | ||
|
|
||
| export class MultiAssetPaymentController { | ||
| private assetConfigService: AssetConfigurationService; | ||
| private assetPriceService: AssetPriceService; | ||
| private multiAssetPaymentService: MultiAssetPaymentService; | ||
|
|
||
| constructor() { | ||
| this.assetConfigService = new AssetConfigurationService(); | ||
| this.assetPriceService = new AssetPriceService(); | ||
| this.multiAssetPaymentService = new MultiAssetPaymentService(); | ||
| } | ||
|
|
||
| createAssetConfig = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { | ||
| merchantId, | ||
| asset, | ||
| isEnabled, | ||
| minAmount, | ||
| maxAmount, | ||
| priority, | ||
| autoConvert, | ||
| settlementAsset, | ||
| } = req.body; | ||
|
|
||
| const config = await this.assetConfigService.createAssetConfig({ | ||
| merchantId, | ||
| assetCode: asset.code, | ||
| assetIssuer: asset.issuer || null, | ||
| isEnabled, | ||
| minAmount, | ||
| maxAmount, | ||
| priority, | ||
| autoConvert, | ||
| settlementAssetCode: settlementAsset?.code || null, | ||
| settlementAssetIssuer: settlementAsset?.issuer || null, | ||
| }); | ||
|
|
||
| res.status(201).json({ | ||
| success: true, | ||
| message: "Asset configuration created successfully", | ||
| data: config, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error creating asset config:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| updateAssetConfig = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { id } = req.params; | ||
| const { merchantId } = req.body; | ||
| const { | ||
| isEnabled, | ||
| minAmount, | ||
| maxAmount, | ||
| priority, | ||
| autoConvert, | ||
| settlementAsset, | ||
| } = req.body; | ||
|
|
||
| const config = await this.assetConfigService.updateAssetConfig( | ||
| id, | ||
| merchantId, | ||
| { | ||
| isEnabled, | ||
| minAmount, | ||
| maxAmount, | ||
| priority, | ||
| autoConvert, | ||
| settlementAssetCode: settlementAsset?.code || null, | ||
| settlementAssetIssuer: settlementAsset?.issuer || null, | ||
| }, | ||
| ); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| message: "Asset configuration updated successfully", | ||
| data: config, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error updating asset config:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| deleteAssetConfig = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { id } = req.params; | ||
| const { merchantId } = req.body; | ||
|
|
||
| await this.assetConfigService.deleteAssetConfig(id, merchantId); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| message: "Asset configuration deleted successfully", | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error deleting asset config:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| getMerchantAssetConfigs = async ( | ||
| req: Request, | ||
| res: Response, | ||
| ): Promise<void> => { | ||
| try { | ||
| const { merchantId } = req.params; | ||
|
|
||
| const configs = | ||
| await this.assetConfigService.getMerchantAssetConfigs(merchantId); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: configs, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error fetching merchant configs:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| getExchangeRate = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { sourceAsset, destinationAsset, amount } = req.body; | ||
|
|
||
| const quote = await this.assetPriceService.getExchangeRate( | ||
| sourceAsset.code, | ||
| sourceAsset.issuer, | ||
| destinationAsset.code, | ||
| destinationAsset.issuer, | ||
| amount, | ||
| ); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: quote, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error getting exchange rate:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| executePathPayment = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { | ||
| merchantId, | ||
| sourceAddress, | ||
| destinationAddress, | ||
| sourceAsset, | ||
| destinationAsset, | ||
| amount, | ||
| sourceSecret, | ||
| memo, | ||
| } = req.body; | ||
|
|
||
| const result = await this.multiAssetPaymentService.executePathPayment({ | ||
| merchantId, | ||
| sourceAddress, | ||
| destinationAddress, | ||
| sourceAssetCode: sourceAsset.code, | ||
| sourceAssetIssuer: sourceAsset.issuer, | ||
| destAssetCode: destinationAsset?.code || sourceAsset.code, | ||
| destAssetIssuer: destinationAsset?.issuer || sourceAsset.issuer, | ||
| amount, | ||
| sourceSecret, | ||
| memo, | ||
| }); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| message: "Path payment executed successfully", | ||
| data: result, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error executing path payment:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| estimatePayment = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { merchantId, sourceAsset, destinationAsset, amount } = req.body; | ||
|
|
||
| const estimate = await this.multiAssetPaymentService.estimatePayment({ | ||
| merchantId, | ||
| sourceAssetCode: sourceAsset.code, | ||
| sourceAssetIssuer: sourceAsset.issuer, | ||
| destAssetCode: destinationAsset.code, | ||
| destAssetIssuer: destinationAsset.issuer, | ||
| amount, | ||
| }); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: estimate, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error estimating payment:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| getSupportedAssetPairs = async ( | ||
| req: Request, | ||
| res: Response, | ||
| ): Promise<void> => { | ||
| try { | ||
| const { merchantId } = req.params; | ||
|
|
||
| const pairs = | ||
| await this.multiAssetPaymentService.getSupportedAssetPairs(merchantId); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: pairs, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error getting supported pairs:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| getOrderbook = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { sourceAsset, destinationAsset } = req.body; | ||
|
|
||
| const orderbook = await this.assetPriceService.getOrderbook( | ||
| sourceAsset.code, | ||
| sourceAsset.issuer, | ||
| destinationAsset.code, | ||
| destinationAsset.issuer, | ||
| ); | ||
|
|
||
| res.status(200).json({ | ||
| success: true, | ||
| data: orderbook, | ||
| }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| res.status(error.statusCode).json({ | ||
| success: false, | ||
| message: error.message, | ||
| }); | ||
| } else { | ||
| logger.error("Error getting orderbook:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Internal server error", | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use authenticated merchant ID instead of request body.
The
updateAssetConfigmethod takesmerchantIdfromreq.body, but the route usesauthenticateMerchantmiddleware which setsreq.merchant. Using the body value could allow a merchant to attempt modifying another merchant's configuration (though the service layer may prevent this). For defense in depth, use the authenticated merchant's ID.♻️ Suggested fix
updateAssetConfig = async (req: Request, res: Response): Promise<void> => { try { const { id } = req.params; - const { merchantId } = req.body; + const merchantId = (req as any).merchant?.id; const { isEnabled,Apply similar changes to
deleteAssetConfigandcreateAssetConfig.📝 Committable suggestion
🤖 Prompt for AI Agents