I am cross-platforming my app using Android and iOS and Firebase. In the iOS app, I have a option for the driver to use bids for the requested ride, whereas in the android app, it is just a calculated price.
If the Android rider requests a ride with an iOS driver, the iOS driver can offer a discounted bid rather than the calculated ridePrice.
In Firebase, in the RideRequests node, it adds ridePrice
as the price to be paid but if its a bid price, it also adds a bidPrice. I am trying to set it up that if 'rideBidPrice' exists, then rideAmt = rideBidPrice
but if not, then rideAmt = ridePrice
.
Unfortunately, what I am doing isn't getting to the PayPalPayment
method.
How can I do this?
firebase db:
"RideRequests" : {
"buIpWNEFgmZJWN3jIB9IwS8r74d2" : { // rider Id
"archiveTimestamp" : "2019-03-17 22:00",
"currentAddress" : "2 Peel Plaza",
"destAddress" : "Courtenay Bay Causeway",
"destLat" : 47.277879500000004,
"destLong" : -63.04432369999999,
"driver" : "Dean Winchester",
"driverID" : "nD1v8oxHv3ObdQAKeKjuTt6f5TL2",
"rideBidPrice" : 5.74,
"ridePrice" : 8.38,
"riderPaid" : "false",
"status" : "driverBid",
"userId" : "buIpWNEFgmZJWN3jIB9IwS8r74d2",
"username" : "riderANDROID"
}
Code:
private void checkForBids() {
Log.e(TAG, "checkForBids");
DatabaseReference bids = FirebaseDatabase.getInstance().getReference(Common.request_tbl)
.child(riderId).child("rideBidPrice");
bids.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
bidPrice = dataSnapshot.getValue(Double.class);
if (bidPrice != null) {
rideAmt = bidPrice;
} else {
rideAmt = ridePrice;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void payPalPayment() {
Log.e(TAG, "payPalPayment");
PayPalPayment payment = new PayPalPayment(new BigDecimal(rideAmt),
"CAD", "Ryyde Payment ", PayPalPayment.PAYMENT_INTENT_SALE);
// PaymentActivity is created by PayPal API
Intent intent = new Intent(this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
I also tried putting the payPalPayment method inside the block but won't work because of the intent.
EDIT #1
This works:
As per @AlexMamo post to below post:
How to return DataSnapshot value as a result of a method?
I have added an interface:
public interface MyCallback {
void onCallback(Double value);
}
The method that is actually getting the data from the database:
public void readData(final MyCallback myCallback) {
Log.e(TAG, "readData");
DatabaseReference bids = FirebaseDatabase.getInstance().getReference(Common.request_tbl)
.child(riderId).child("rideBidPrice");
bids.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double value = dataSnapshot.getValue(Double.class);
myCallback.onCallback(value);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Then I call the method inside the PayPalPayment method like this:
private void payPalPayment() {
readData(new MyCallback() {
@Override
public void onCallback(Double value) {
PayPalPayment payment = new PayPalPayment(new BigDecimal(value),
"CAD", "Ryyde Payment ", PayPalPayment.PAYMENT_INTENT_SALE);
// PaymentActivity is created by PayPal API
Intent intent = new Intent(RiderHome.this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
});
}