Skip to content

Instantly share code, notes, and snippets.

View nwaughachukwuma's full-sized avatar
🧶
Making stuff

Chukwuma Nwaugha nwaughachukwuma

🧶
Making stuff
View GitHub Profile
@nwaughachukwuma
nwaughachukwuma / getCardToken.ts
Last active December 23, 2019 21:57
Client side code for tokenising credit card details
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)
@nwaughachukwuma
nwaughachukwuma / createCharge.ts
Created December 23, 2019 15:32
Create a charge from a customer transaction
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 {
@nwaughachukwuma
nwaughachukwuma / onCompleteAccountCreation.ts
Last active December 23, 2019 02:38
Save connected account after onboarding on Stripe
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()) {
/**
* 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`)
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',
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 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
@nwaughachukwuma
nwaughachukwuma / assignClaim.ts
Last active October 28, 2019 15:01
Assign claims to a verified admin user
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;
@nwaughachukwuma
nwaughachukwuma / userDocumentTrigger.ts
Last active October 27, 2019 12:20
Listen for changes on a user document
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
@nwaughachukwuma
nwaughachukwuma / lodash.get.js
Created October 15, 2019 06:50 — forked from harish2704/lodash.get.js
Simple lodash.get function in javascript
/* 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;
}