[英]How to pass an object from android app to a firebase cloud function to complete Paypal payment functions?
I am using firebase cloud functions as serverside for Paypal payment. 我将Firebase云功能用作Paypal付款的服务器端。 Documentations are not obvious to understand.
文档不是很容易理解。 when I am trying to send an object from android app to firebase cloud functions, nothing has happened.
当我尝试将对象从android应用发送到firebase云函数时,什么都没发生。 I think I added it wrong.
我想我加错了。 so how can I pass an object from android app to the function??
那么如何将对象从android应用传递给函数呢?
public void payout(String PayerID,String paymentId) {
// Create the arguments to the callable function.
JSONObject postData = new JSONObject();
try {
postData.put("PayerID", PayerID);
postData.put("paymentId",paymentId);
} catch (JSONException e) {
e.printStackTrace();
}
mFunctions
.getHttpsCallable("payout")
.call(postData)
.continueWith(new Continuation<HttpsCallableResult, Object>() {
@Override
public Object then(@NonNull Task<HttpsCallableResult> task)
throws Exception {
return null;
}
});
}
/////////////////////////////////////////// ///////////////////////////////////////////
exports.payout=functions.https.onRequest((req,res)=>{
const sender_batch_id = Math.random().toString(36).substring(9);
const payReq=JSON.stringify({
sender_batch_header: {
sender_batch_id: sender_batch_id,
email_subject: "You have a nice payment"
},
items: [
{
recipient_type: "EMAIL",
amount: {
value: 0.90,
currency: "USD"
},
receiver: "amrmahmoudM@app.com",
note: "Thank you very much.",
sender_item_id: "item_3"
}
]
});
paypal.payout.create(payReq,(error, payout)=>{
if (error) {
console.warn(error.res);
res.status('500').end();
throw error;
}else{
console.info("payout created");
console.info(payout);
res.status('200').end();
}
});
});
exports.process = functions.https.onRequest((req, res) => {
const paymentId = req.body.paymentId;
var payerId = {
payer_id: req.body.PayerID
};
return paypal.payout.execute(paymentId, payerId, (error, payout) => {
if (error) {
console.error(error);
} else {
if (payout.state === 'approved') {
console.info('payment completed successfully, description: ',
payout.transactions[0].description);
const ref=admin.firestore().collection("Users").doc(payerId);
ref.set({'paid': true});
} else {
console.warn('payment.state: not approved ?');
}
}
}).then(r =>
console.info('promise: ', r));
});
The problem comes from the fact that in your Android app you call an HTTPS Callable Function (via mFunctions.getHttpsCallable("payout")
) but your Cloud Function is not an HTTPS Callable Function but a "simple" HTTPS Function. 问题来自以下事实:您在Android应用中调用了HTTPS可调用函数(通过
mFunctions.getHttpsCallable("payout")
),但是您的Cloud Function不是HTTPS可调用函数,而是“简单” HTTPS函数。
HTTPS Callable Functions are written like: HTTPS可调用函数的编写方式如下:
exports.payout = functions.https.onCall((data, context) => {
// ...
});
while HTTPS Functions are written like: HTTPS函数的编写方式如下:
exports.payout = functions.https.onRequest((req,res)=> {
// ...
})
So you should adapt the code of your Cloud Function according to the documentation: https://firebase.google.com/docs/functions/callable 因此,您应该根据以下文档调整Cloud Function的代码: https : //firebase.google.com/docs/functions/callable
Note that another option could be to write to the database (Real Time database or Firestore) and trigger the Cloud Function with an onWrite
or onCreate
trigger. 请注意,另一个选项可能是写入数据库(实时数据库或Firestore)并使用
onWrite
或onCreate
触发器触发Cloud Function。 The advantage of this approach is that you directly save the information of the payment in the database. 这种方法的优点是您可以将付款信息直接保存在数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.