MantlePay Documentation

Learn how to integrate MantlePay into your application to start accepting crypto payments.

Getting Started

MantlePay provides a simple API and SDK to help you accept crypto payments in your application. Follow the steps below to get started.

1. Create an account

Sign up for a MantlePay account to get your API keys and access the developer console.

2. Install the SDK

Terminal
npm install @mantlepay/sdk

3. Initialize the SDK

App.jsx
import { MantlePayProvider } from '@mantlepay/sdk';

function App() {
  return (
    <MantlePayProvider apiKey="YOUR_API_KEY">
      <YourApp />
    </MantlePayProvider>
  );
}

4. Create a payment

CheckoutButton.jsx
import { useMantlePay } from '@mantlepay/sdk';

function CheckoutButton() {
  const { createPayment } = useMantlePay();

  const handleClick = async () => {
    try {
      const payment = await createPayment({
        amount: '10.00',
        currency: 'USD',
        token: 'ETH',
        metadata: {
          orderId: '12345',
          customerEmail: 'user@example.com'
        }
      });
      
      // Redirect to the hosted checkout page
      window.location.href = payment.checkoutUrl;
    } catch (error) {
      console.error('Error creating payment:', error);
    }
  };

  return (
    <button onClick={handleClick}>
      Pay with Crypto
    </button>
  );
}

Handling Webhooks

MantlePay sends webhook notifications when payment status changes. Set up webhook handlers to receive these notifications.

webhook-handler.js
import express from 'express';
import { MantlePay } from '@mantlepay/sdk';

const app = express();
const mantlePay = new MantlePay('YOUR_API_KEY');

app.post('/webhooks/mantlepay', express.raw({ type: 'application/json' }), async (req, res) => {
  const signature = req.headers['mantlepay-signature'];
  
  try {
    const event = mantlePay.webhooks.constructEvent(
      req.body,
      signature,
      'YOUR_WEBHOOK_SECRET'
    );
    
    // Handle the event
    switch (event.type) {
      case 'payment.succeeded':
        const payment = event.data;
        console.log('Payment succeeded:', payment.id);
        // Update your database, fulfill the order, etc.
        break;
      case 'payment.failed':
        console.log('Payment failed:', event.data.id);
        // Handle the failure
        break;
      default:
        console.log('Unhandled event type:', event.type);
    }
    
    res.status(200).send();
  } catch (err) {
    console.error('Webhook error:', err.message);
    res.status(400).send(`Webhook Error: ${err.message}`);
  }
});

app.listen(3000, () => {
  console.log('Webhook handler running on port 3000');
});