我正在尝试创建云函数,该函数将从Cloud Firestore获取数据,并使用从Cloud Firestore获取的值向支付网关API发送支付请求。
我可以使用以下功能从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);
});
});
但是,此功能仅打印文档值;我需要在另一个函数中使用值,如下所示:
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查询像以前一样打印在控制台上时,支付处理器返回了一个错误。
然后我试着用静态值调用支付处理器,如下所示:
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获取文档并在云函数(如price: doc.price
(中使用其值呢?
我认为您需要将请求逻辑移动到.then(...)
块中,因为现在看起来您正试图在实际返回的块之外使用文档结果,这可能就是为什么您会得到一个"无效卡号";错误
Firestore API将Promises用于大多数/所有获取操作,因此您总是需要等待结果才能使用它
// ...
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 = { ... }