Created
April 27, 2020 16:30
-
-
Save Gazer/ef9cd5c73a88e523672020251d420267 to your computer and use it in GitHub Desktop.
Create a MercadoPago Preference when a document is created in Firestore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
let FieldValue = require('firebase-admin').firestore.FieldValue; | |
admin.initializeApp(); | |
const db = admin.firestore(); | |
const express = require('express'); | |
const app = express(); | |
const mercadopago = require('mercadopago'); | |
// WARNING: You need to use the Pay As You Go plan to use this function | |
mercadopago.configure({ | |
sandbox: true, | |
access_token: '<YOUT ACCESS TOKEN>' | |
}); | |
exports.createPreferenceForOrder = functions.firestore | |
.document('orders/{orderId}') | |
.onCreate(async (snap, context) => { | |
const data = snap.data(); | |
let preference = { | |
items: [ | |
{ | |
title: data.name, | |
quantity: 1, | |
currency_id: 'ARS', | |
unit_price: data.price | |
} | |
], | |
payer: { | |
email: "[email protected]" | |
} | |
}; | |
let response = await mercadopago.preferences.create(preference); | |
let preferenceId = response.body.id; | |
snap.ref.set({"preference_id": preferenceId}, {merge: true}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment