gpt4 book ai didi

android - 如何将对象从android应用程序传递到firebase云函数以完成Paypal支付功能?

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:02 24 4
gpt4 key购买 nike

我使用 firebase 云函数作为 Paypal 支付的服务器端。文档不是很容易理解。当我尝试将一个对象从 android 应用程序发送到 firebase 云函数时,什么也没有发生。我想我添加错了。那么如何将对象从 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));
});

最佳答案

问题是因为在您的 Android 应用程序中您调用了 HTTPS 可调用函数(通过 mFunctions.getHttpsCallable("payout")),但您的 Cloud Function 不是 HTTPS 可调用函数而是一个“简单的”HTTPS 功能。

HTTPS 可调用函数的写法如下:

exports.payout = functions.https.onCall((data, context) => {
// ...
});

虽然 HTTPS 函数是这样写的:

exports.payout = functions.https.onRequest((req,res)=> {
// ...
})

所以你应该根据文档调整你的云函数的代码:https://firebase.google.com/docs/functions/callable


请注意,另一种选择可能是写入数据库(实时数据库或 Firestore)并使用 onWriteonCreate 触发器触发 Cloud Function。这种方式的好处是您直接将支付的信息保存在数据库中。

关于android - 如何将对象从android应用程序传递到firebase云函数以完成Paypal支付功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com