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

问题描述 投票:0回答:1

我使用 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: "[email protected]",
                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 firebase google-cloud-platform paypal google-cloud-functions
1个回答
1
投票

问题来自于这样一个事实:在您的 Android 应用程序中,您调用了 HTTPS 可调用函数(通过

mFunctions.getHttpsCallable("payout")
),但您的云函数不是 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)并使用

onWrite
onCreate
触发器触发云功能。这种方式的好处是直接将支付信息保存在数据库中。

© www.soinside.com 2019 - 2024. All rights reserved.