从Firebase Function返回数据到Android [英] Return data to Android from Firebase Function
问题描述
我要做什么:只需从Firebase Cloud Function返回数据即可.
What I am trying to do: Simply return data from Firebase Cloud Function.
此功能用于在付款网关的服务器中创建付款订单.
The function is used to create a payment order in the payment gateway's server.
我需要的有关订单详细信息的数据显示在 function(err,data)
中(请参见下文),但是我需要将此数据发送回我的Android应用.
My required data about the order's details are present in the function(err,data)
(see below), but I need this data sent back to my Android app.
我遇到的问题:我可以在Firebase控制台的日志中看到打印的数据,但不会返回到我的Android应用.
Problem I faced: I could see the data printed in the Firebase console's log but it doesn't return to my Android app.
我的Firebase云功能:
const functions = require("firebase-functions");
exports.order = functions.https.onCall((amnt, response) => {
const Ippopay = require('node-ippopay');
const ippopay_instance = new Ippopay({
public_key: 'YOUR_PUBLIC_KEY',
secret_key: 'YOUR_SECRET_KEY',
});
ippopay_instance.createOrder({
amount: amnt,
currency: 'DOLLAR',
payment_modes: "cc,dc,nb,cheque",
customer: {
name: "Test",
email: "test@gmail.com",
phone: {
country_code: "42",
national_number: "4376543210"
}
}
}, function (err, data) {
return data.order.order_id;
});
});
我的Android客户端代码:
public class Payment extends AppCompatActivity implements IppoPayListener {
Button pay;
EditText amount;
private FirebaseFunctions mFunctions;
TextView order_data;
String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
pay=findViewById(R.id.pay_button);
amount=findViewById(R.id.user_amount);
order_data=findViewById(R.id.data_text);
pay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("PAY Button clicked", "yes");
mFunctions = FirebaseFunctions.getInstance("us-central1");
mFunctions.getHttpsCallable("order").call(5).continueWith(new Continuation<HttpsCallableResult, Object>() {
@Override
public Object then(@NonNull Task<HttpsCallableResult> task) throws Exception {
HttpsCallableResult result=task.getResult();
if(result !=null)
{
data=result.getData().toString();
return result.getData().toString();
}
return null;
}
});
order_data.setText(data);
onPaymentClick();
}
});
}
/* ... */
}
我是一个初学者,因此很可能会出现一些致命的愚蠢错误.:)
I'm a Beginner so there's a high possibility of some dead silly mistakes. :)
推荐答案
根据当前代码的样子,您可以混合使用 HTTP请求云功能.
Based on what your code looks like at the moment, you have a mix of code from a Callable Cloud Function and the older HTTP Request Cloud Function.
要从可调用的Cloud Function返回数据,您应该返回 Promise
,一种运行异步代码并返回值的方法.较早的JavaScript和许多其他语言改用回调,这就是您在这里使用的.
To return data from a callable Cloud Function, you should return a Promise
, a method of running asynchronous code that returns a value. Older JavaScript and many other languages use callbacks instead, which is what you have here.
最简单的一种基于回调的方法:
In it's simplest form, this callback-based method:
someModule.doSomething(input, function (err, result) {
// check for errors and handle result
});
将通过以下方式转换为使用Promises:
would be converted to use Promises using:
new Promise((resolve, reject) => {
someModule.doSomething(
input,
(err, result) => err ? reject(err) : resolve(result) // short form of "if error, reject with an error, otherwise resolve (succeed) with result"
)
});
要让客户端正确处理错误,您需要包裹所有错误 functions.https.HttpsError
中的a>a>.
For errors to be handled correctly by clients, you need to wrap any errors in a functions.https.HttpsError
.
将其组合在一起可以得到:
Combining this together gives:
const functions = require("firebase-functions");
exports.order = functions.https.onCall((amnt, context) => {
const Ippopay = require('node-ippopay');
return new Promise((resolve, reject) => {
const ippopay_instance = new Ippopay({
public_key: 'YOUR_PUBLIC_KEY',
secret_key: 'YOUR_SECRET_KEY',
});
ippopay_instance.createOrder({
amount: amnt,
currency: 'DOLLAR',
payment_modes: "cc,dc,nb,cheque",
customer: {
name: "Test",
email: "test@gmail.com",
phone: {
country_code: "42",
national_number: "4376543210"
}
}
}, function (err, data) {
if (err) {
// something went wrong, send error back to caller
reject(new functions.https.HttpsError('unknown', 'Ippopay threw an unexpected error', err));
return;
}
// successful, send data back to caller
resolve(data.order.order_id);
});
});
});
您还应该确保使用 context.auth
来限制对此功能的访问.您不想向错误的客户收费.
You should also make sure you make use of context.auth
to restrict access to this function. You wouldn't want to bill the wrong customer.
这篇关于从Firebase Function返回数据到Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!