You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何在Firebase Cloud Functions中配置Mollie Node.js API

Setting Up Mollie Node.js API in Firebase Cloud Functions

Hey there! I know getting started with Cloud Functions and third-party payment APIs can feel overwhelming, so let's break this down step by step for you. Since you already have a Blaze plan (great call—this lets you make external requests), we'll focus on integrating Mollie properly.

Step 1: Install the Mollie SDK

First, navigate to your Firebase Cloud Functions directory in your terminal, then install the official Mollie Node.js client:

npm install @mollie/api-client

Step 2: Securely Store Your Mollie API Key

Never hardcode your API keys! Instead, use Firebase's environment config to store it. Run this command in your terminal:

firebase functions:config:set mollie.api_key="your_mollie_test_api_key"

You can find your test API key in your Mollie dashboard under Developers > API keys.

Step 3: Create a Cloud Function to Generate a Mollie Payment

Here's a complete example of an HTTP-triggered function that creates a Mollie payment (we'll use your hardcoded test properties as a starting point):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { createMollieClient } = require('@mollie/api-client');

admin.initializeApp();

// Initialize Mollie client with your API key from environment config
const mollieClient = createMollieClient({
  apiKey: functions.config().mollie.api_key,
});

// HTTP function to create a payment
exports.createMolliePayment = functions.https.onRequest(async (req, res) => {
  try {
    // Your test payment properties (you can replace these with dynamic data later)
    const payment = await mollieClient.payments.create({
      amount: {
        value: '10.00',
        currency: 'EUR',
      },
      description: 'Test Payment from Firebase Cloud Functions',
      redirectUrl: 'https://your-website.com/payment-success', // Replace with your success page
      webhookUrl: `https://${req.headers.host}/mollie-webhook`, // Points to our webhook function below
    });

    // Send the payment URL back to the client so they can redirect the user
    res.status(200).json({ paymentUrl: payment._links.checkout.href });
  } catch (error) {
    console.error('Mollie payment creation error:', error);
    res.status(500).json({ error: 'Failed to create payment' });
  }
});

Step 4: Set Up a Webhook to Handle Payment Updates

Mollie will send a request to your server when the payment status changes (e.g., paid, canceled). We need a function to receive and validate this:

// HTTP function to handle Mollie webhooks
exports.mollieWebhook = functions.https.onRequest(async (req, res) => {
  try {
    // Validate the webhook signature to ensure it's from Mollie
    const paymentId = req.body.id;
    const signature = req.headers['x-mollie-signature'];
    
    // Verify the signature using Mollie's client
    await mollieClient.webhooks.verifySignature(req.rawBody, signature, functions.config().mollie.api_key);

    // Fetch the updated payment details
    const payment = await mollieClient.payments.get(paymentId);

    // Update your Firebase database with the payment status (example)
    await admin.firestore().collection('payments').doc(paymentId).set({
      status: payment.status,
      amount: payment.amount,
      updatedAt: admin.firestore.FieldValue.serverTimestamp(),
    });

    // Send a 200 OK to Mollie to confirm receipt
    res.status(200).send('OK');
  } catch (error) {
    console.error('Mollie webhook error:', error);
    // Send a 403 Forbidden if signature validation fails
    res.status(403).json({ error: 'Invalid webhook signature' });
  }
});

Step 5: Deploy and Configure Mollie

  1. Deploy your functions:
firebase deploy --only functions
  1. After deployment, copy the URL for your mollie-webhook function (it will look like https://your-project-region-your-project-id.cloudfunctions.net/mollie-webhook).
  2. Go to your Mollie dashboard, navigate to Settings > Webhooks, and add this URL as a new webhook. Make sure to enable the payment status events you want to receive.

Key Notes for Beginners

  • Test Mode First: Use Mollie's test API keys and test payment methods (like the test credit card 4111 1111 1111 1111) to avoid real charges.
  • Error Handling: Always wrap async operations in try/catch blocks to catch API errors or network issues.
  • Dynamic Data: Later, you can replace the hardcoded payment details with data from the request (e.g., items from a cart sent by your frontend).

If you ran into specific errors with your initial code, feel free to share more details, but this setup should get you up and running with Mollie in Cloud Functions!

内容的提问来源于stack exchange,提问作者Sam van beastlo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:10:42