Eliya Cohen
Eliya Cohen

Reputation: 11498

Handle IPN on Cloud Functions of Firebase

On my client, I have an order which has the items I want to buy. When I "checkout" the order, I send a request to my firebase cloud function and the function responds with a link to the online payment transaction. After the customer completes the transaction, a request from the company (who serves the online payment service) is being dispatched to one of my cloud functions who shall process the payment.

After I validate the payment, I want to store the new data of my payment to the order (e.g. status, completion date etc...).

How can I store my new data? I know that with firestore I can simply use the .set(..) method.

Upvotes: 1

Views: 273

Answers (1)

Eliya Cohen
Eliya Cohen

Reputation: 11498

I've got my answer from the comments (Thanks to Oleskii Miroshnyk). Since Oleskii didn't post it as an Answer, then I will.

First, we need to import firebase-admin:

import * as admin from 'firebase-admin';

or in Javascript:

const admin = require('firebase-admin');

After that, we need to initialize our application like so:

const serviceAccount = require('../service-account-key.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://{PROJECT-ID}.firebaseio.com'
});

The service-account-key.json can be found in your firebase settings.

Once I finish with the configuration, I have access to the firestore method like so:

const firestore = admin.firestore();

firestore.collection(...).doc(...).set(...)

That's it. Quite simple actually.

Upvotes: 1

Related Questions