Added cors
This commit is contained in:
parent
336b02e4c4
commit
e9ddb92bf8
@ -2,78 +2,75 @@ import * as admin from 'firebase-admin';
|
|||||||
import * as functions from 'firebase-functions';
|
import * as functions from 'firebase-functions';
|
||||||
import * as paypal from 'paypal-rest-sdk';
|
import * as paypal from 'paypal-rest-sdk';
|
||||||
|
|
||||||
|
const cors = require('cors')({origin: true});
|
||||||
|
|
||||||
paypal.configure({
|
paypal.configure({
|
||||||
mode: 'sandbox',
|
mode: 'sandbox',
|
||||||
client_id: 'AaU8tQfmz1_MFDTKuf84yYERXvdDt2ZFJVrxhNW_49DazF4A_F0VBuKyV5_nntyEdZqUa5Oq9ZBj65GV',
|
client_id: 'AaU8tQfmz1_MFDTKuf84yYERXvdDt2ZFJVrxhNW_49DazF4A_F0VBuKyV5_nntyEdZqUa5Oq9ZBj65GV',
|
||||||
client_secret: 'EAZ8aFDU4lHHLy1bQqULYWqznf3dBknXZW3AH__zFC0bUs8AGUyR6RNbm-jHvqtikX7PsSqMO5vxuvKm'
|
client_secret: 'EAZ8aFDU4lHHLy1bQqULYWqznf3dBknXZW3AH__zFC0bUs8AGUyR6RNbm-jHvqtikX7PsSqMO5vxuvKm'
|
||||||
});
|
});
|
||||||
|
|
||||||
export const checkout = functions.https.onRequest(async (request, response) => {
|
export const checkout = functions.https.onRequest((request, response) => {
|
||||||
// Create base request
|
cors(request, response, async () => {
|
||||||
let req = {
|
// Create base request
|
||||||
intent: 'sale',
|
let req = {
|
||||||
payer: {
|
intent: 'sale',
|
||||||
payment_method: 'paypal'
|
payer: {payment_method: 'paypal'},
|
||||||
},
|
redirect_urls: {
|
||||||
redirect_urls: {
|
return_url: 'https://fhsons.zakscode.com/success',
|
||||||
return_url: 'https://fhsons.zakscode.com/success',
|
cancel_url: 'https://fhsons.zakscode.com/cart'
|
||||||
cancel_url: 'https://fhsons.zakscode.com/cart'
|
},
|
||||||
},
|
transactions: [
|
||||||
transactions: [
|
{
|
||||||
{
|
item_list: {items: []},
|
||||||
item_list: {
|
amount: {total: 0, currency: 'CAD'},
|
||||||
items: []
|
description: 'Purchase of equipment and suplies from FH & Sons'
|
||||||
},
|
}
|
||||||
amount: {
|
]
|
||||||
total: 0,
|
};
|
||||||
currency: 'CAD'
|
|
||||||
},
|
|
||||||
description: 'Purchase of equipment and suplies from FH & Sons'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
// Fill in information from DB
|
// Fill in information from DB
|
||||||
let promises = [];
|
let promises = [];
|
||||||
console.log(request.body);
|
console.log(request.body);
|
||||||
let cart = request.body.cart.filter(row => row.quantity > 0);
|
let cart = request.body.cart.filter(row => row.quantity > 0);
|
||||||
cart.forEach(async row =>
|
cart.forEach(async row =>
|
||||||
promises.push(
|
promises.push(
|
||||||
admin
|
admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.doc(`product/${row.id}`)
|
.doc(`product/${row.id}`)
|
||||||
.get()
|
.get()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
let products = await Promise.all(promises);
|
let products = await Promise.all(promises);
|
||||||
console.log(products);
|
console.log(products);
|
||||||
req.transactions[0].item_list.items = products.map((row, i) => {
|
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};
|
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');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
req.transactions[0].amount.total = products.reduce((acc, row, i) => acc + row.price * cart[i].quantity, 0);
|
||||||
|
|
||||||
try {
|
// Send request to PayPal
|
||||||
response.send(await create);
|
let create = new Promise((res, rej) => {
|
||||||
} catch (err) {
|
paypal.payment.create(req, (error, payment) => {
|
||||||
console.error(err);
|
if (error) rej(error);
|
||||||
response.status(500);
|
|
||||||
}
|
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) => {
|
exports.process = functions.https.onRequest((req, res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user