admin管理员组文章数量:1426117
I'm struggling a bit with the integration of an automated workflow when a user purchases a product and pays by stripe. I'm using Firestore and Cloud Functions.
Workflow
- User purchases Product via Stripe Checkout.js
Payment is stored in 'payments' collection
{ product:<uid of the product> '...', user:<uid of the user> '..', method: 'stripe', token:< Stripe token> {..} }
Triggers Cloud function (onWrite for payment collection)
- TODO: Get product DocRef from 'products' (retrieve the product's price)
TODO: Add Document to 'purchases' collection inside a document of the collection 'users'
Charge payment
I've implemented this workflow, except for step 4 and 5, because I have no idea how to retrieve and add DocRef from Firestore within Cloud Functions (there are a lot of examples provided by the Firebase Docs on how it works with RT Database)
functions/index.js
exports.stripeCharge = functions.firestore
.document('/payments/{payment}')
.onWrite(event => {
const paymentId = event.params.payment;
const payment = event.data.data();
if (!payment || payment.method !== 'stripe' || payment.charge) return;
// 4. Get Product's Price
firestore.collection('products').doc(payment.product).then(product => {
const idempotency_key = paymentId;
const charge = {
amount: product.price,
currency: 'chf',
source: payment.token.id
};
stripe.charges.create(charge, {idempotency_key}).then(charge => {
// 5. Update User Purchases
firestore.collection('users').doc(payment.user).collection('purchases').add({
product: payment.product,
payment: paymentId,
date: new Date()
});
// Updated Charge
event.data.ref.update({charge});
});
});
Admin SDK I suppose that I have to use Admin SDK to achieve this, but I have no idea on how this is supposed to work with Firestore
I'm struggling a bit with the integration of an automated workflow when a user purchases a product and pays by stripe. I'm using Firestore and Cloud Functions.
Workflow
- User purchases Product via Stripe Checkout.js
Payment is stored in 'payments' collection
{ product:<uid of the product> '...', user:<uid of the user> '..', method: 'stripe', token:< Stripe token> {..} }
Triggers Cloud function (onWrite for payment collection)
- TODO: Get product DocRef from 'products' (retrieve the product's price)
TODO: Add Document to 'purchases' collection inside a document of the collection 'users'
Charge payment
I've implemented this workflow, except for step 4 and 5, because I have no idea how to retrieve and add DocRef from Firestore within Cloud Functions (there are a lot of examples provided by the Firebase Docs on how it works with RT Database)
functions/index.js
exports.stripeCharge = functions.firestore
.document('/payments/{payment}')
.onWrite(event => {
const paymentId = event.params.payment;
const payment = event.data.data();
if (!payment || payment.method !== 'stripe' || payment.charge) return;
// 4. Get Product's Price
firestore.collection('products').doc(payment.product).then(product => {
const idempotency_key = paymentId;
const charge = {
amount: product.price,
currency: 'chf',
source: payment.token.id
};
stripe.charges.create(charge, {idempotency_key}).then(charge => {
// 5. Update User Purchases
firestore.collection('users').doc(payment.user).collection('purchases').add({
product: payment.product,
payment: paymentId,
date: new Date()
});
// Updated Charge
event.data.ref.update({charge});
});
});
Admin SDK I suppose that I have to use Admin SDK to achieve this, but I have no idea on how this is supposed to work with Firestore
Share Improve this question edited Nov 25, 2017 at 16:17 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Nov 25, 2017 at 13:46 Kode BryantKode Bryant 5119 silver badges24 bronze badges1 Answer
Reset to default 5Accessing Firestore from the Admin SDK is pretty similar to accessing any other Firebase product from the Admin SDK: e.g. admin.firestore()...
See https://firebase.google./docs/reference/admin/node/firebase-admin.firestore.md#firebase-adminfirestore_module.
You're missing a get()
call when you're trying to access the document:
firestore.collection('products').doc(payment.product).get().then(product => {
if (!product.exists) {
console.log('No such product!');
} else {
console.log('Document data:', product.data());
}
If you haven't used Firestore from JavaScript before, Cloud Functions is not the easiest way to get started with it. I remend reading the docs for JavaScript/web users, and taking the Firestore codelab for web developers.
本文标签: javascriptGetadd Document in Cloud functions using FirestoreStack Overflow
版权声明:本文标题:javascript - Getadd Document in Cloud functions using Firestore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745406434a2657271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论