Design decisions

The library is divided into several packages:

  • core sends request and sets response
  • profile represents PayPal user.
  • request represents a single PayPal request.
  • fields represnts fields which are/can be used in a request.
  • util helper class (validation, formatting).

To add new request, implement Request interface. To create a new field, implement RequestFields interface. The same is with the rest - code to interface.

To use the library, we need to create user/profile first. We can use BaseProfile class, or if you need more complexity, you can implement Profile interface and create new class.
BaseProfile class uses Joshua's Bloch Builder pattern from his book Effective Java. This way it is thread safe and immutable.

Profile user = new BaseProfile.Builder("name", "password").signature("AfcWX...").build();

Then, instanciate PayPal class with the new profile/user

PayPal pp = new PayPal(user, Environment.SANDBOX);

Now, it is ready for sending requests and setting responses.

/* create new request */
GetBalance balance = new Balance();

/* send request and set response */

PayPal class does NOT have send method, but it HAS setResponse method, which sets response on supplied Request object. This way, the request and response are together in the appropriate object, in this case inside balance object.
Every Request instance has:
getNVPRequest method, which returns Map of Strings - name value pair request to be sent to PayPal;
setNVPResponse method, which is used by PayPal instance to set response from PayPal; and
getNVPResponse method, which returns empty Map if request has not been sent or no reponse has been returned. This way every request knows its request parameters and response paramenters.

/* response */
Map<String, String> response = balance.getNVPResponse();