Firebase:如何在实时数据库onCreate函数中触发HTTPs onRequest函数? [英] Firebase: How to trigger HTTPs onRequest function within a Realtime Database onCreate function?

查看:64
本文介绍了Firebase:如何在实时数据库onCreate函数中触发HTTPs onRequest函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VueJS电子商务应用程序中单击结帐"按钮后,将在我的Firebase订单"子节点中创建一个包含订单参数的新订单"字段.我创建了一个实时数据库onCreate'newBuyerOrder'函数,以便在创建新的订单"字段后向用户发送一封电子邮件,通知该新订单.现在,我还要调用一个使用HTTPs onRequest函数构建的REST API'/checkout',以通过Paypal REST SDK处理订单.我该怎么办?

Upon clicking the 'checkout' button in my VueJS ecommerce app, a new 'order' field containing order params will be created in my firebase 'orders' child node. I've created a Realtime Database onCreate 'newBuyerOrder' function to send the user an email notifying him of this new order once a new 'order' field is created. Now I'd also like to call a REST API '/checkout' that I've built with the HTTPs onRequest function to process the order via Paypal REST SDK. How can I do that?

我尝试过的一种解决方法是构建HTTPs onCall函数,客户端浏览器可以调用该函数以通过Paypal使用订单参数来处理订单,并使用"newBuyerOrder" onCreate函数分别发送电子邮件.但不幸的是,HTTPs onCall不允许客户端重定向.而且,瞧,在进行Paypal REST调用时需要客户端重定向,因此HTTPs onCall不适用于我的目的.

A workaround that I've tried is to build HTTPs onCall functions that the client browser can call to process the order via Paypal with order params and send the email separately with the 'newBuyerOrder' onCreate function. But unfortunately, HTTPs onCall does not allow client redirect. And, lo and behold, client redirect is required while making Paypal REST calls so HTTPs onCall does not work for my purpose.

在functions/package.json

in functions/package.json

"dependencies": {
    "@sendgrid/mail": "^6.3.1",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0",
    "paypal-rest-sdk": "^1.8.1"
}

在functions/src/index.ts

in functions/src/index.ts

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as sendgrid from  '@sendgrid/mail'
import * as paypal from  'paypal-rest-sdk'

// init firebase admin
admin.initializeApp()

// set sendgrid api in function config
const SENDGRID_API_KEY = ...

// set paypal api in function config
paypal.configure({
  ...
});

// setup paypal payment object and redirect user to paypal payment 
page
export const checkout = functions.https.onRequest((req, res) => {
// 1.Set up a payment information object, Build PayPal payment 
request
  const payReq = JSON.stringify({
    ...
  })
// 2.Initialize the payment and redirect the user.
  paypal.payment.create(payReq, (error, payment) => {
    if (error) {
      // handle error
    } else {
      // redirect to paypal approval link
      for (let i = 0; i < payment.links.length; i++) {
        if (payment.links[i].rel === 'approval_url') {
          res.redirect(302, payment.links[i].href)
        }
      }
    }
  })
})


// send email and trigger paypal checkout api given new buyer order
export const newBuyerOrder = functions.database
.ref('users/{userId}/orders/{orderId}')
.onCreate((snapshot, context) => 
    // expected solution to call 'checkout' REST API from above
    // send email via sendgrid
    const msg = {...}
    return sendgrid.send(msg)
})

我希望一旦在实时数据库中创建了新的订单字段,并且客户将被重定向到Paypal批准页面,就会调用'/checkout'API.

I expect the '/checkout' API to be called once a new order field is created in the Realtime Database and that the client gets redirected to the Paypal approval page.

推荐答案

从另一个Cloud Functions调用一个Cloud Function几乎从来不是一个好主意.一方面,您将为两次调用付费,而一次调用只能完成一次工作.有更好的方法来重用代码.

Calling one Cloud Function from another Cloud Functions is almost never a good idea. For one thing, you'll be paying for two invocations, where the work could be done in just one. There are better ways to re-use the code.

例如,您可以将付款代码包含在Realtime Database触发的Cloud Function中?.

For example, you could include the code for the payment in the Realtime Database triggered Cloud Function?.

或者您可以提取该Cloud Function的业务功能,并将其放入常规的JavaScript函数中,以便从Realtime Database触发的函数中进行调用.

Or you could extract the business functionality of that Cloud Function, and put that in a regular JavaScript function, so that you an call if from the Realtime Database triggered function.

数据库触发器来自实时数据库到云功能.不涉及客户端应用程序代码,因此也无法重定向.

A database trigger comes from the Realtime Database to Cloud Functions. No client-side application code is involved, so it also can't be redirected.

如果要在操作完成后将响应发送回客户端,则通常将客户端的响应写回到客户端监视的位置的数据库中.例如,您可以将响应写到:/users/{userId}/order_results/{orderId} ,然后客户端可以

If you want to send a response back to the client when the operation has completed, you typically write the response for the client back into the database in a place that the client watches. For example, you could write the response to: /users/{userId}/order_results/{orderId} and the client can then wait for a value at this location.

这篇关于Firebase:如何在实时数据库onCreate函数中触发HTTPs onRequest函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆