J2Pay – Getting Started
Getting started will guide you how to start using J2pay quickly in very simple steps.
Download
J2Pay is available on maven.
<dependency> <groupId>com.tranxactive</groupId> <artifactId>j2pay</artifactId> <version>2.4.0</version> </dependency>
You can also download the jar file here
Example
In this example we will execute Purchase and Rebill transactions. First we will get the desired gateway i.e Authorize
Gateway gateway = GatewayFactory.getGateway(AvailableGateways.AUTHORIZE);
Since we are working on test environment we will enable the test mode.
gateway.setTestMode(true);
Next we will ask for the library to show us what are the API paramters for this gateway
JSONObject apiSampleParameters = gateway.getApiSampleParameters(); System.out.println(apiSampleParameters) //output {"name":"also called api user name / api login id","transactionKey":"the transaction key"}
As we can see in output, library is telling us that Authorize gateway requires two API parameters name and transactionKey. Now we will populate these fields by our merchant values.
apiSampleParameters.put("name", "<your account's user name here>"); apiSampleParameters.put("transactionKey", "<your account's transaction key here>");
Next we will use Customer and CustomerCard classes to pass the information to purchase method
Customer customer = new Customer(); customer .setFirstName("test first name") .setLastName("test last name") .setCountry(Country.US) .setState("TX") .setCity("test city") .setAddress("test address") .setZip("12345") .setPhoneNumber("1234567890") .setEmail("email@domain.com") .setIp("127.0.0.1"); CustomerCard customerCard = new CustomerCard(); customerCard .setName("test card name") .setNumber("5424000000000015") //Authorize test card .setCvv(123) .setExpiryMonth("01") .setExpiryYear("2022");
Purchase
We are all set to call purchase method
HTTPResponse purchaseResponse = gateway.purchase(apiSampleParameters, customer, customerCard, Currency.USD, 2.5f);
Handling Purchase Response
Now we can check whether the transaction was success or fail.
if(purchaseResponse.isSuccessful()){ //some code }
To print out the full response see below snippet
System.out.println(purchaseResponse.getJSONResponse()); //output { "lr": { "success": true, "message": "SUCCESS", "transactionId": "3902990127", "amount": 45, "cardExpiryYear": "2017", "cardFirst6": "601160", "cardExpiryMonth": "12", "maskedCard": "601160******6611", "rebillParams": { "customerVaultId": "174302554" }, "voidParams": { "transactionId": "3902990127" }, "currencyCode": "USD", "cardLast4": "6611", "refundParams": { "transactionId": "3902990127" } }, "gr": { // long gateway response } }
Note
Response is defined in great detail in API Responses section. For this example the only thing you should to know is gateway response is divided into two keys.
- lr, library response
- gr, gateway response
Library response only contains the values that library thinks important for you and could be useful for further actions like refund/void/rebill. Keep in mind library response has already prepared the parameters required for further actions on this transaction. i.e. refund, rebill or void.
Rebill
Remember, we saved the purchase respons in purchaseResponse variable. Below is the code showing how to execute rebill transaction in just two lines.
JSONObject rebillParams = purchaseResponse.getJSONObject("lr").getJSONObject("rebillParams"); HTTPResponse rebillResponse = gateway.rebill(apiSampleParameters, rebillParams, 50);
Congratulations on complete getting started guide. Please feel free to write us on info@tranxactive.com
You can also see the detailed example here.
Published on Java Code Geeks with permission by Muhhamad Ilyas, partner at our JCG program. See the original article here: Getting Started Opinions expressed by Java Code Geeks contributors are their own. |