The MoonPay module offers a streamlined solution for integrating cryptocurrency purchasing capabilities into Solana-based applications. This comprehensive guide covers everything from basic setup to advanced implementation patterns.
Why Choose MoonPay for Your Solana App?
MoonPay enables users to:
- Purchase SOL and other supported cryptocurrencies directly within your app
- Use familiar payment methods (credit/debit cards, Apple Pay)
- Enjoy a trusted, compliant onboarding experience
- Access global payment processing with competitive rates
Core Features Overview
- Instant purchases with fiat-to-crypto conversion
- Multi-currency support including SOL and popular tokens
- Embeddable widget that maintains your app's branding
- Multi-platform compatibility (web and mobile implementations)
- Sandbox environment for thorough testing
Implementation Guide
Installation Process
npm install @solana-appkit/moonpay
# or
yarn add @solana-appkit/moonpayBasic Configuration
Configure your MoonPay service with these essential parameters:
import { createMoonPayService } from '@solana-appkit/moonpay';
const moonPay = createMoonPayService({
apiKey: 'your_sandbox_key',
environment: 'sandbox', // Switch to 'production' for live
defaultCurrency: 'SOL',
theme: 'dark' // Optional theming
});๐ Get your MoonPay API keys here
Module Structure
The package organizes functionality into clear components:
src/
โโโ components/ # Reusable UI elements
โโโ hooks/ # Custom React hooks
โโโ services/ # Core business logic
โโโ types/ # Type definitions
โโโ utils/ # Helper functionsKey Implementation Patterns
Standard Purchase Flow
function StandardPurchase() {
const { showWidget } = useMoonPay();
const handlePurchase = (amount: number) => {
showWidget({
walletAddress: userWallet.address,
currencyCode: 'SOL',
amount
});
};
return <Button onClick={() => handlePurchase(50)}>Buy $50 SOL</Button>;
}Advanced Customization
For developers needing more control:
function CustomPurchaseFlow() {
const { getWidgetUrl } = useMoonPay();
const [customAmount, setCustomAmount] = useState('');
const url = getWidgetUrl({
walletAddress: 'user_wallet_address',
currencyCode: 'SOL',
amount: customAmount,
colorCode: '#6200EE' // Custom accent color
});
return <WebView source={{ uri: url }} />;
}Best Practices
Transaction Monitoring
Implement robust status tracking:
async function checkTransactionStatus(transactionId: string) {
const status = await moonPayService.getTransactionStatus(transactionId);
// Handle different status cases
switch(status) {
case 'completed':
updateUserBalance();
break;
case 'failed':
showErrorNotification();
break;
// Additional cases...
}
}Error Handling
Build resilient error management:
try {
await moonPayService.initiatePurchase(params);
} catch (error) {
if (error.isNetworkError) {
showOfflineMessage();
} else if (error.isPaymentError) {
showPaymentFailed();
} else {
logErrorToAnalytics(error);
showGenericError();
}
}Performance Optimization
- Lazy load the MoonPay widget only when needed
- Cache configuration to reduce startup time
- Pre-warm connections when purchase likelihood is high
๐ Learn advanced optimization techniques
Security Considerations
- Always use HTTPS connections
- Never expose API keys in client-side code
- Implement proper CORS policies
- Regularly rotate API credentials
Frequently Asked Questions
What payment methods does MoonPay support?
MoonPay accepts:
- Visa/Mastercard credit/debit cards
- Apple Pay/Google Pay
- Bank transfers (selected regions)
- SEPA transfers (EU)
How long do transactions typically take?
Transaction times vary:
- Card payments: 5-30 minutes
- Bank transfers: 1-3 business days
- Apple Pay: Typically under 10 minutes
What are the fee structures?
MoonPay's fees include:
- Processing fee: 1-4.5%
- Network fee: Varies by cryptocurrency
- May include additional bank charges
Can I test purchases without real money?
Yes! The sandbox environment allows complete testing with:
- Test credit card numbers
- Simulated transactions
- Full functionality without real funds
How do I handle refunds?
MoonPay manages the entire refund process:
- Refunds go back to original payment method
- Processing time depends on payment type
- Refund amounts may vary due to exchange rates
Conclusion
The MoonPay module provides Solana applications with a powerful, compliant solution for cryptocurrency onboarding. By following this guide's implementation patterns and best practices, developers can create seamless fiat-to-crypto experiences that drive user adoption and engagement.
For additional resources: