A payment provider for Medusa.js 2.0 that accepts Solana cryptocurrency payments.
- Accept Solana (SOL) cryptocurrency payments in your Medusa store
- Convert cart totals from USD/EUR to SOL
- Display wallet address for payment
- Monitor the blockchain for payment confirmation
- Support for payment authorization, capture, and refund workflows
- Medusa.js 2.0 or higher
- Node.js 16 or higher
- A Solana wallet address
npm install medusa-payment-solanaAdd the following to your medusa-config.js or medusa-config.ts:
module.exports = defineConfig({
// ...
modules: [
// ...
{
resolve: "@medusajs/medusa/payment",
options: {
providers: [
{
resolve: "medusa-payment-solana",
id: "solana",
options: {
walletAddress: process.env.SOLANA_ADDRESS,
rpcUrl: "https://api.testnet.solana.com", // Use mainnet for production
// Optional: Set a custom polling interval (in ms) for checking payments
paymentPollingInterval: 30000,
}
}
]
}
}
]
});Make sure to set the SOLANA_ADDRESS environment variable in your .env file:
SOLANA_ADDRESS=your_solana_wallet_address
- Go to Settings > Regions
- Edit a region or create a new one
- In the Payment Providers section, enable "Solana"
- Save the region
When a customer reaches the checkout page, they will be able to select "Solana" as a payment method. The checkout will display:
- The total amount in SOL
- The wallet address to send the payment to
- Instructions for completing the payment
npm run buildnpm test-
Initiate Payment: When a customer selects Solana as their payment method, the provider converts the cart total to SOL and generates a unique payment ID.
-
Display Payment Details: The storefront displays the Solana wallet address and the amount in SOL to be paid.
-
Monitor for Payment: The provider periodically checks the blockchain for incoming transactions to the specified wallet address.
-
Authorize Payment: Once a matching payment is detected, the payment is authorized.
-
Capture Payment: The payment is captured, and the order is processed.
The payment provider uses a flexible currency conversion system that allows you to implement your own conversion logic. This design was chosen to:
- Avoid hard dependencies on specific APIs
- Allow customization based on your needs
- Enable easy integration with your preferred exchange rate provider
- Create a new file for your converter:
// src/converters/my-converter.js
class MyConverter {
async convertToSol(amount, currencyCode) {
// Example using CoinGecko API
const response = await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=${currencyCode}`
);
const data = await response.json();
const rate = data.solana[currencyCode.toLowerCase()];
return amount / rate;
}
}
module.exports = MyConverter;- Update your Medusa config:
{
resolve: "medusa-payment-solana",
options: {
walletAddress: process.env.SOLANA_ADDRESS,
converter: {
resolve: "./src/converters/my-converter.js",
apiKey: process.env.CONVERTER_API_KEY
}
}
}- Set any required API keys in your .env file:
CONVERTER_API_KEY=your_api_key
If no custom converter is provided, the provider uses a default converter with fixed rates:
- 1 USD = 0.0075 SOL
- 1 EUR = 0.008 SOL
- Cache exchange rates to reduce API calls
- Implement error handling and fallback rates
- Consider rate limits when choosing an API provider
The provider includes functionality to generate QR codes for payments, but this is not enabled by default. To enable QR codes in your storefront, you can use the generatePaymentQRCode function from the utils.ts file.
MIT