[英]Using Cloud Firestore data in a Cloud Function
我正在嘗試創建從 Cloud Firestore 獲取數據的 Cloud Functions,並使用從 Cloud Firestore 獲取的值向支付網關 API 發送支付請求。
我可以使用這個 function 從 Firestore 獲取我需要的文檔:
exports.getData = functions.https.onRequest((req, res) => {
const docRef = db.collection('requests').doc('vsFyJ5lisBgYNwQEzOLa');
const getDoc = docRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
return res.send(doc.data());
})
.catch(err => {
console.log('Error getting document', err);
});
});
但是,此 function 僅打印文檔值; 我需要使用另一個 function 中的值,如下所示:
exports.pay = functions.https.onRequest((req, res) => {
var Iyzipay = require('iyzipay');
const doc = db.collection('requests').doc('vsFyJ5lisBgYNwQEzOLa');
const iyzipay = new Iyzipay({
apiKey: 'apiKey',
secretKey: 'secretKey',
uri: 'https://sandbox-api.iyzipay.com'
});
var request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: doc.price,
paidPrice: doc.paidPrice,
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.PRODUCT,
paymentCard: {
cardHolderName: doc.cardHolderName,
cardNumber: doc.cardNumber,
expireMonth: doc.expireMonth,
expireYear: doc.expireYear,
cvc: doc.cvc,
registerCard: '0'
},
};
iyzipay.payment.create(request, function (err, result) {
console.log(err, result);
});
});
我試圖結合如下功能:
exports.pay = functions.https.onRequest((req, res) => {
var Iyzipay = require('iyzipay');
const doc = db.collection('requests').doc('vsFyJ5lisBgYNwQEzOLa');
const iyzipay = new Iyzipay({
apiKey: 'sandbox-afXhZPW0MQlE4dCUUlHcEopnMBgXnAZI',
secretKey: 'sandbox-wbwpzKIiplZxI3hh5ALI4FJyAcZKL6kq',
uri: 'https://sandbox-api.iyzipay.com'
});
const docRef = db.collection('requests').doc('vsFyJ5lisBgYNwQEzOLa');
const getDoc = docRef.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
return res.send(doc.data());
})
.catch(err => {
console.log('Error getting document', err);
});
var request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: '1',
paidPrice: '1.2',
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.PRODUCT,
paymentCard: {
cardHolderName: doc.cardHolderName,
cardNumber: doc.cardNumber,
expireMonth: doc.expireMonth,
expireYear: doc.expireYear,
cvc: doc.cvc,
registerCard: '0'
}
};
iyzipay.payment.create(request, function (err, result) {
console.log(err, result);
});
});
當 Firestore 查詢像以前一樣打印在控制台上時,支付處理器返回錯誤。
然后我嘗試使用 static 值嘗試調用支付處理器,如下所示:
var request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: '1',
paidPrice: '1.2',
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.LISTING,
paymentCard: {
cardHolderName: 'John Doe',
cardNumber: '5528790000000008',
expireMonth: '12',
expireYear: '2030',
cvc: '123',
registerCard: '0'
}
}
並且支付處理器返回成功,所以我獲取或使用 Firestore 文檔值的方式一定有問題。
為了再次測試它是否確實使用了 Firestore 值,我只將一個字段切換到cardNumber: doc.cardNumber
。 控制台返回:
✔ functions[pay]: http function initialized (http://localhost:5000/highmidlow1/us-central1/pay).
i functions: Beginning execution of "pay"
⚠ External network resource requested!
- URL: "https://sandbox-api.iyzipay.com/payment/auth"
- Be careful, this may be a production service.
⚠ Google API requested!
- URL: "https://oauth2.googleapis.com/token"
- Be careful, this may be a production service.
> null {
> status: 'failure',
> errorCode: '12',
> errorMessage: 'Invalid card number',
> locale: 'tr',
> systemTime: 1608311421424,
> conversationId: '123456789'
> }
> {
> expireYear: '2030',
> paidPrice: 1.2,
> cardNumber: '5528790000000008',
> cvc: '123',
> price: 1,
> expireMonth: '12', }
那么如何從 Firestore 獲取文檔並在 Cloud Function 中使用其值,例如price: doc.price
?
我認為您需要將您的請求邏輯移動到.then(...)
塊中,因為現在看起來您正試圖在實際返回的塊之外使用文檔結果,這可能就是您的原因重新收到“無效卡號”錯誤。
Firestore API 將 Promises 用於大多數/所有 fetch 操作,因此您始終需要在使用它之前等待結果。
// ...
docRef.get().then(doc => {
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
res.send(doc.data());
const request = { ... }
})
如果您使用的是支持async/await
的節點版本,則可能更容易發現這些類型的錯誤。
const doc = await docRef.get()
if (!doc.exists) {
console.log('No such document!');
return res.send('Not Found')
}
console.log(doc.data());
res.send(doc.data());
const request = { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.