0

I just want to send back an attribute that was not defined in POJO class. My POJO class is this

public class PostData {

    @SerializedName("Token")
    @Expose
    private String token;
    @SerializedName("Amount")
    @Expose
    private Integer amount;
    @SerializedName("AuthID")
    @Expose
    private String authID;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amt) {
        this.amount = amt;
    }

    public String getAuthID(){return authID;}

    public void setAuthID(String authID){
        this.authID = authID;
    }

}

Amount,Token,AuthID are sent from the client side to API.Here I used HTTP Cloud function as an HTTP POST.I need to send back the TransID to client side which is not there in POJO class.In client side I need to read the response and store it. Here is my cloud function code:

exports.Add = functions.https.onRequest((req,res)=>{
//var testPublicKey = [xxxxxxxxxxxxxxxxx]
var stripe = require("stripe")("stripe_key");

console.log("Token:"+req.body.Token)
console.log("Amount:"+req.body.Amount)

var tokenToCharge = req.body.Token;
const amountToCharge = req.body.Amount;
var authID = req.body.AuthID;
const databaseRef = admin.firestore();
const payer = databaseRef.collection('deyaPayUsers').doc(authID).collection('Wallet').doc(authID);
 const balance = payer.Usd
 stripe.charges.create({
amount : amountToCharge,
currency : "usd",
description : "charge created",
source : tokenToCharge,
}, function(err, charge) {
if(charge.status === "succeeded"){
var trans = databaseRef.runTransaction(t=>{
 return t.get(payer)
    .then(doc=>{
     var updatedBalance = doc.data().Usd + parseInt(charge.amount);
     t.update(payer,{Usd : updatedBalance});
    });//return close
 }).then(result=>{
 console.log('Transaction success!');
 }).catch(err=>{
 console.log('Transaction Failure:',err);
 });
 }
});
});//end of stripe create charge
oakinlaja
  • 826
  • 6
  • 10
Divya Galla
  • 513
  • 1
  • 9
  • 31
  • check it -https://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Adil Feb 26 '18 at 04:06

3 Answers3

0

you add one field in pojo class using transient key is ignore a value to send api call and get client side value.

    transient
    private String TransID;

and make getter and setter method.

0

is there any reason why you do not want to create a variable for the TransID in your POJO class? I understand that your POJO class interacts with your Client Side of your application. Hence, having a variable for the TransID(with getters and Setters) will give you more flexibility to read the response and store it as you want.

oakinlaja
  • 826
  • 6
  • 10
0

To read response using Retrofit have a separate class(which is here PostData) that represents the outputs of the JSON response which we get when a request was served by the backend.create an interface class and with @Method(GET, POST, DELETE etc;) annotation and pass parameters to the method.In client-side pass the parameters to the method and get the response by calling the getmethod of the parameter you are expecting.

Divya Galla
  • 513
  • 1
  • 9
  • 31