I am trying to integrate stripe into my android application with the server code set up in Backendless as a Custom API Service. After charging the card, with a successful call back method, I am not able to see that in my payments list on my stripe dashboard. Not sure where the problem is. Any suggestions will be appreciated.Below is what I have so far: ChargeItem://Charge Class
package com.mbaas.service;
public class ChargeItem {
public String token;
public int price;
public String description;
}
ChargeService //Backendless Service
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import java.util.HashMap;
import java.util.Map;
public class ChargeService implements IBackendlessService
{
public boolean makeCharge(ChargeItem charges){
Stripe.apiKey = "my stripe secret key";
// Get the credit card details submitted by the form
String token = charges.token;
double price = charges.price;
String desc = charges.description;
String userId = charges.userId;
String orderId = charges.orderId;
// Create a charge: this will charge the user's card
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("orderId",orderId);
chargeParams.put("userId",userId);
chargeParams.put("amount", price); // Amount in cents
chargeParams.put("currency", "usd");
chargeParams.put("source", token);
chargeParams.put("description", desc);
@SuppressWarnings("unused")
Charge charge = Charge.create(chargeParams);
}
catch (StripeException e) {
// The card has been declined
return false;
}
return true;
}
}
//My stripe token call back method
private void convertCardToToken(Card card, final Orders order){
Stripe stripe = new Stripe(getApplicationContext(), CustomApplication.PUBLISHABLE_KEY);
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
ChargeItem chargeItem = new ChargeItem();
chargeItem.setToken(token.getId());
chargeItem.setOrderId(order.getObjectId());
chargeItem.setPrice(order.getOrder_price());
chargeItem.setUserName(order.getOwnerId());
chargeItem.setDescription("Delivery Fee");
ChargeService.initApplication();
ChargeService chargeService = ChargeService.getInstance();
chargeService.makeChargeAsync(chargeItem, new AsyncCallback<Boolean>() {
@Override
public void handleResponse(Boolean response) {
Toast.makeText(getApplicationContext(),
"Payment Successful",
Toast.LENGTH_LONG
).show();
}
@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(getApplicationContext(), fault.getMessage(),
Toast.LENGTH_LONG
).show();
}
});
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(CheckoutActivity.this,
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
I'm not familiar with Backendless so I cannot provide much help here, but there a few issues with your code:
price
is a double
. To avoid rounding errors, all amounts in Stripe's API are in cents (or more generally, in the smallest unit for the currency you're using), so price
should be an int
.
userId
and orderId
are not valid parameters when creating a charge. You likely want to pass these variables as metadata values.
To help you debug further, you should also check your logs in your Stripe account's dashboard at https://dashboard.stripe.com/test/logs?method=not_get. You should see requests to POST /v1/tokens
(sent by your Android app) and requests to POST /v1/charges
(sent by Backendless).