Connect to a Backend Service

  • Follow the steps in Build on Android.
  • If you don't have a payment processing backend available for testing, follow Deploy the Server to start a small backend service suitable for testing.
Link to section

1. Set up a connection to a payment processing backend

To validate and process the payment token returned by the SDK CardEntry object, you must send it to a backend service that uses Connect payment APIs to capture payment.

To set up the backend connection, build a Retrofit object and configure it with the base URL for your backend service.

Link to section

2. Define a REST interface for POST requests

public interface ChargeService { @POST("/chargePaymentCard") Call<Void> charge(@Body ChargeRequest request); class ChargeErrorResponse { String errorMessage; } class ChargeRequest { final String nonce; ChargeRequest(String nonce) { this.nonce = nonce; } } }
Link to section

3. Make calls with the REST interface

To use your REST interface, create a Call<ChargeResult> implementation that packages and sends requests to the payment processing backend.

Link to section

4. Handle the backend results

Define a ChargeResult object and act on the results.

private ChargeResult responseToResult(Response<Void> response) { if (response.isSuccessful()) { return ChargeResult.success(); } try { //noinspection ConstantConditions ResponseBody errorBody = response.errorBody(); ChargeService.ChargeErrorResponse errorResponse = factory.errorConverter.convert(errorBody); return ChargeResult.error(errorResponse.errorMessage); } catch (IOException exception) { return ChargeResult.networkError(); } }