基于Firebase后端实现Adyen加密支付:curl转Firebase Functions请求问询
Convert Adyen Curl Request to Firebase Cloud Function
Here's how to translate your Adyen payment request into a Firebase Cloud Function that handles the backend call to Adyen's API:
Step 1: Set Up Dependencies
First, install axios in your Firebase Functions directory (we’ll use it to send HTTP requests to Adyen):
npm install axios
Step 2: Create the Firebase Function
This HTTP-triggered function will accept encrypted card data from your frontend, construct the Adyen request, and return the response:
const functions = require('firebase-functions'); const axios = require('axios'); // Use Firebase Environment Variables to store sensitive credentials (never hardcode them!) const ADYEN_CREDENTIALS = { username: functions.config().adyen.username, password: functions.config().adyen.password, merchantAccount: functions.config().adyen.merchant_account, apiUrl: functions.config().adyen.api_url || 'https://checkout-test.adyen.com/v68/payments' }; exports.processAdyenPayment = functions.https.onRequest(async (req, res) => { try { // Extract data from the client's POST request body const { encryptedCardData, reference, amountValue, currency } = req.body; // Build the payload matching your original curl request const adyenPayload = { additionalData: { "card.encrypted.json": encryptedCardData }, amount: { value: amountValue || 10000, // Default to 100 EUR (value is in minor units) currency: currency || "EUR" }, reference: reference || "Your Reference Here", merchantAccount: ADYEN_CREDENTIALS.merchantAccount }; // Send POST request to Adyen with Basic Authentication const adyenResponse = await axios.post( ADYEN_CREDENTIALS.apiUrl, adyenPayload, { auth: { username: ADYEN_CREDENTIALS.username, password: ADYEN_CREDENTIALS.password }, headers: { 'Content-Type': 'application/json' } } ); // Return Adyen's response directly to the frontend res.status(200).json(adyenResponse.data); } catch (error) { // Log errors for debugging and return a user-friendly response functions.logger.error('Adyen payment request failed:', error); res.status(error.response?.status || 500).json({ error: 'Payment processing failed', details: error.response?.data || error.message }); } });
Step 3: Configure Environment Variables
Store your Adyen credentials securely using Firebase’s environment variables (replace placeholders with your actual values):
firebase functions:config:set adyen.username="ws@Company.SomeCompany" adyen.password="SomePassword" adyen.merchant_account="TestMerchant" adyen.api_url="https://checkout-test.adyen.com/v68/payments"
Key Notes
- Encrypted Card Data: Your frontend should send the
encryptedCardData(generated by Adyen’s Web Components/Drop-in) to this function via a POST request. - API Version: Double-check Adyen’s latest API version (the example uses v68, but this may update over time).
- Security: For added safety, verify Firebase Auth tokens in the function to ensure only authenticated users can process payments.
- Error Handling: Extend the error logic to handle specific Adyen error codes (e.g., invalid card details) if needed.
内容的提问来源于stack exchange,提问作者HarryTgerman
相关产品推荐
相关产品推荐

