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
import {map} from 'lodash' | |
const stripeUrl = 'https://api.stripe.com/v1/' | |
const publicStripeKey = 'your stripe public key' | |
export const createPaymentMethod = async () => { | |
// get form data: react native or react | |
const { cardNumber, expiryMonth, expiryYear, cvc } = this.state // or event.target.elements; | |
getCardToken(cardNumber, expiryMonth, expiryYear, cvc) |
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
import { stripe } from './stripe-util' | |
export async function preAuthorizeCharge(customer: string, amount: number, accountId: string, metadata: any = {}) { | |
const percentageOfTransaction = 0.05 // percentage charge on a business for using the platform | |
const application_fee_amount = Math.round((amount * percentageOfTransaction) * 100) | |
const cost = Math.round(amount * 100); | |
const descriptor = 'brief description of transaction'; | |
const description = 'a more detailed description' | |
try { |
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
import { Request, Response } from "express"; | |
import { check, validationResult } from 'express-validator/check'; | |
import * as Request from 'request-promise-native' | |
import { StripeConfig } from './config' | |
import { admin } from './admin' // already initialized | |
const onCompleteAccountCreation = async (req: Request, res: Response) => { | |
const errors = validationResult(req) | |
if (!errors.isEmpty()) { |
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
/** | |
* Firestore now supports a where-in query on the admin SDK | |
*/ | |
import admin from "../admin" // pre-initialised | |
// Instead of this | |
async function hasUserTraveledTo(listOfStates, userId) { | |
for (const state in listOfStates) { | |
const userHasTraveledTo = await admin.firestore().collection(`users/${userId}/travelDestinations`) |
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
import { admin } from './admin' | |
import * as APIRequest from 'request-promise-native' | |
import { get } from 'lodash' | |
async function verifyUser(token: string) { | |
try { | |
const options = { | |
uri:'https://www.googleapis.com/oauth2/v3/tokeninfo', | |
method: 'POST', |
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
import * as functions from 'firebase-functions'; | |
import { validationResult } from 'express-validator/check'; | |
import { admin, getDocumentRef } from './admin' | |
import { verifyUser } from './verifyUser' | |
const acceptableStatus = [...]; // some validations. e.g. approve|decline|pending | |
export const updateCustomerEntry = functions.https.onRequest(async (request, response) => { | |
const errors = validationResult(request) | |
if (!errors.isEmpty()) { |
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
/** | |
* This function is called from an onEdit trigger which passes a parameter to it | |
* The aim is to get the row where a change is made and update the cloud functions | |
* endpoint with it | |
*/ | |
function updateCustomer(param) { | |
// cloud functions HTTP on request URL | |
var url = 'https://project_location-project_id.cloudfunctions.net/updateCustomer'; | |
var oauthToken = ScriptApp.getOAuthToken(); // get the user's OAuth token |
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
import { DocumentSnapshot } from "firebase-functions/lib/providers/firestore"; | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
const assignCustomClaim = async (newSnap: DocumentSnapshot, prevSnap: DocumentSnapshot) => { | |
const emailVerified = newSnap.get('verified'); | |
// we are only listening after email verification | |
if (prevSnap.get('verified') === emailVerified) return; |
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'); | |
import { assignCustomClaim } from './assignClaim' | |
// onWrite to user document | |
exports const userDocumentChangedTrigger = functions.firestore | |
.document('users/{userId}') | |
.onWrite(async (change, context) => { | |
const prevSnap = change.before | |
const newSnap = change.after |
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
/* Implementation of lodash.get function */ | |
function getProp( object, keys, defaultVal ){ | |
keys = Array.isArray( keys )? keys : keys.split('.'); | |
object = object[keys[0]]; | |
if( object && keys.length>1 ){ | |
return getProp( object, keys.slice(1) ); | |
} | |
return object === undefined? defaultVal : object; | |
} |