In the first part of this article, we used the open source Neo4j graph library to compute Multi-Level Marketing (MLM) commissions for distributors across a sales network. We left off with a list of e-mails and associated payouts, which are now handed off to PayPal’s MassPay API in this second and final installment.
MassPay can pay up to 250 people at once. In order to use it, e-mails and payouts are formatted as name-value pairs (NVP) and sent over HTTPS. MassPay is perhaps PayPal’s most simple NVP API; therefore, it’s a good candidate for demonstrating the creation of a class that’s reusable across all the NVP APIs. The class we work on uses the builder design pattern to achieve what’s called a fluent interface. The best way to explain is to show the code we’re shooting for:
NvpApiBuilder api = new NvpApiBuilder(SERVER); api.authenticateWith(USERNAME, PWD, SIGNATURE) .toCall("MassPay", "56.0") .withParam("L_EMAIL0", "firstname.lastname@example.org") .withParam("L_AMT0", "15.0") .withParam("L_EMAIL1", "email@example.com") .withParam("L_AMT1", "10.0") .go();
With a fluent interface, the elements and rules of an API are expressed in a mini-language made by chaining method calls together. The order and quantity of each call is usually predefined and important, and the goal is to make the whole thing human-readable and user friendly. This way, developers can learn an API through a simple block of code like the one shown ...