Last active
June 26, 2018 18:07
-
-
Save ethernet8023/ce97963b014e9dfa5ed0e346eecd22de to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SquareConnect = require("square-connect"); | |
| const admin = require("firebase-admin"); | |
| const functions = require("firebase-functions"); | |
| exports.catalog = functions.https.onRequest((request, response) => { | |
| admin.auth().verifyIdToken(request.body.idToken) | |
| .then(decodedToken => | |
| admin.database() | |
| .ref(`/squareAccessToken/${decodedToken.uid}`) | |
| .once("value") | |
| ).then(userToken => { | |
| SquareConnect.ApiClient.instance.authentications["oauth2"] | |
| .accessToken = userToken.val(); | |
| const catalogApi = new SquareConnect.CatalogApi(); | |
| return catalogApi.listCatalog(); | |
| }).then(catalog => { | |
| let formattedResponse = catalog.objects.map(elem => | |
| axios.post( | |
| 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=' + | |
| functions.config().web.key, { | |
| longDynamicLink: | |
| `https://[YOUR_UNIQUE_LINK].page.link/?` + | |
| `link=https://[YOUR_PROJECT_NAME].firebaseapp.com/checkout/` + | |
| `${uid.split(":")[1]}/${elem.item_data.variations[0].id}`, | |
| suffix: { option: "SHORT" } | |
| }).then(resp => ({ | |
| name: elem.item_data.name, | |
| catalogId: elem.id, | |
| varId: elem.item_data.variations[0].id, | |
| price: elem.item_data.variations[0].item_variation_data.price_money, | |
| checkoutUrl: resp.data.shortLink | |
| })) | |
| ); | |
| return Promise.all(formattedResponse); | |
| }) | |
| .then(values => { response.json(values); }) | |
| .catch(error => { | |
| console.log(error); | |
| response.send(error); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment