Как сохранить данные, используя облачные функции, в firestore? - PullRequest
0 голосов
/ 18 мая 2018

Я видел некоторые другие ответы и пытался некоторые из них безуспешно.

В моем случае я создал приложение для iOS и интегрировал метод оплаты Stripe, чтобы он достиг моей функции javascript в облачных функциях.На самом деле я могу видеть платежи, которые я осуществляю, в моем аккаунте Stripe, но я не смог сохранить их в нашей базе данных FireStore.

это мои настройки на стороне Google:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();

const stripe = require('stripe')(functions.config().stripe.token); 
//I've preset this data to firebase functions middleware

    function charge(req, res) {
        const body = (req.body);

        const userId = body.userId;
        const token = body.token;
        const amount = body.amount;
        const currency = body.currency;

        // Charge card
        stripe.charges.create({
            amount,
            currency,
            description: 'Firebase Example',
            source: token,
        }).then(charge => {
            send(res, 200, {
    // I WANNA RECORD DATA INTO MY DATABASE HERE!!
                message: 'Success',
                charge,
            });
        }).catch(err => {
            console.log(err);
            send(res, 500, {
                error: err.message,
            });
        });
    }

    function send(res, code, body) {
        res.send({
            statusCode: code,
            body: JSON.stringify(body),
        });
    }

    app.use(cors);
    app.post('/', (req, res) => {

        // Catch any unexpected errors to prevent crashing
        try {
            charge(req, res);
        } catch(e) {
            console.log(e);
            send(res, 500, {
                error: `The server received an unexpected error. Please 
try again and contact the site admin if the error persists.`,
            });
        }
    });

exports.charge = functions.https.onRequest(app);

И это наша настройка базы данных, в которой я хочу сохранить вот так: - в платежи> userId ... Я буду сохранять каждую транзакцию, которую этот userId делает с полями: «token», «currency» и «amount».

Примечание: у меня уже есть все эти значения в моей функции, а также есть userId.

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...