Added cors

This commit is contained in:
Zakary Timson 2018-07-22 18:04:41 -04:00
parent 336b02e4c4
commit e9ddb92bf8

View File

@ -2,78 +2,75 @@ import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import * as paypal from 'paypal-rest-sdk';
const cors = require('cors')({origin: true});
paypal.configure({
mode: 'sandbox',
client_id: 'AaU8tQfmz1_MFDTKuf84yYERXvdDt2ZFJVrxhNW_49DazF4A_F0VBuKyV5_nntyEdZqUa5Oq9ZBj65GV',
client_secret: 'EAZ8aFDU4lHHLy1bQqULYWqznf3dBknXZW3AH__zFC0bUs8AGUyR6RNbm-jHvqtikX7PsSqMO5vxuvKm'
});
export const checkout = functions.https.onRequest(async (request, response) => {
// Create base request
let req = {
intent: 'sale',
payer: {
payment_method: 'paypal'
},
redirect_urls: {
return_url: 'https://fhsons.zakscode.com/success',
cancel_url: 'https://fhsons.zakscode.com/cart'
},
transactions: [
{
item_list: {
items: []
},
amount: {
total: 0,
currency: 'CAD'
},
description: 'Purchase of equipment and suplies from FH & Sons'
}
]
};
export const checkout = functions.https.onRequest((request, response) => {
cors(request, response, async () => {
// Create base request
let req = {
intent: 'sale',
payer: {payment_method: 'paypal'},
redirect_urls: {
return_url: 'https://fhsons.zakscode.com/success',
cancel_url: 'https://fhsons.zakscode.com/cart'
},
transactions: [
{
item_list: {items: []},
amount: {total: 0, currency: 'CAD'},
description: 'Purchase of equipment and suplies from FH & Sons'
}
]
};
// Fill in information from DB
let promises = [];
console.log(request.body);
let cart = request.body.cart.filter(row => row.quantity > 0);
cart.forEach(async row =>
promises.push(
admin
.firestore()
.doc(`product/${row.id}`)
.get()
)
);
// Fill in information from DB
let promises = [];
console.log(request.body);
let cart = request.body.cart.filter(row => row.quantity > 0);
cart.forEach(async row =>
promises.push(
admin
.firestore()
.doc(`product/${row.id}`)
.get()
)
);
let products = await Promise.all(promises);
console.log(products);
req.transactions[0].item_list.items = products.map((row, i) => {
return {name: row.name, sku: row.name, price: row.price, currency: 'CAD', quantity: cart[i].quantity};
});
req.transactions[0].amount.total = products.reduce((acc, row, i) => acc + row.price * cart[i].quantity, 0);
// Send request to PayPal
let create = new Promise((res, rej) => {
paypal.payment.create(req, (error, payment) => {
if (error) rej(error);
let link = payment.links.filter(row => row.rel == 'approval_url').map(row => row.href)[0];
if (link) {
res(link);
} else {
rej('no redirect URI present');
}
let products = await Promise.all(promises);
console.log(products);
req.transactions[0].item_list.items = products.map((row, i) => {
return {name: row.name, sku: row.name, price: row.price, currency: 'CAD', quantity: cart[i].quantity};
});
});
req.transactions[0].amount.total = products.reduce((acc, row, i) => acc + row.price * cart[i].quantity, 0);
try {
response.send(await create);
} catch (err) {
console.error(err);
response.status(500);
}
// Send request to PayPal
let create = new Promise((res, rej) => {
paypal.payment.create(req, (error, payment) => {
if (error) rej(error);
let link = payment.links.filter(row => row.rel == 'approval_url').map(row => row.href)[0];
if (link) {
res(link);
} else {
rej('no redirect URI present');
}
});
});
try {
response.send(await create);
} catch (err) {
console.error(err);
response.status(500);
}
});
});
exports.process = functions.https.onRequest((req, res) => {