Language

2Checkout Documentation

Java

This library provides developers with a simple set of bindings to the 2Checkout Payment API, Checkout, Instant Notification Service and Admin API.

Github URL - https://github.com/2Checkout/2checkout-java


Installation

Add the included 2checkout-java.jar and it’s dependencies listed below.

Example build.gradle for your project.

dependencies {
    compile 'org.apache.httpcomponents:httpclient:4.2.5'
    compile 'com.google.code.gson:gson:2.2.3'
    compile files('./2checkout-java.jar')
}

You can also build the JAR yourself using Gradle to have the dependencies added automatically.

Sandbox Setting: Twocheckout.mode = “sandbox”;


Example Payment Admin API Usage

Example Usage:

Twocheckout.privatekey = "8CE03B2D-FE41-4C53-9156-52A8ED5A0FA3";

try {
    HashMap<String, String> billing = new HashMap<String, String>();
    billing.put("name", "Testing Tester");
    billing.put("addrLine1", "xvxcvxcvxcvcx");
    billing.put("city", "Columbus");
    billing.put("state", "Ohio");
    billing.put("country", "USA");
    billing.put("zipCode", "43230");
    billing.put("email", "tester@2co.com");
    billing.put("phone", "555-555-5555");

    HashMap<String, Object> request = new HashMap<String, Object>();
    request.put("sellerId", "1817037");
    request.put("merchantOrderId", "test123");
    request.put("token", "MGI4OTU0OTQtMDIxNi00YThlLTliOTctZjg1YmJiMzg0MjA3");
    request.put("currency", "USD");
    request.put("total", "1.00");
    request.put("billingAddr", billing);

    Authorization response = TwocheckoutCharge.authorize(request);
    String message = response.getResponseMsg();
} catch (Exception e) {
    String message = e.toString();
}

Example Response: (toJson)

{
    "type": "AuthResponse",
    "responseCode": "APPROVED",
    "currencyCode": "USD",
    "orderNumber": "205181062452",
    "transactionId": "205181062461",
    "responseMsg": "Successfully authorized the provided credit card",
    "merchantOrderId": "test123",
    "total": 1,
    "lineItems": [
        {
            "type": "product",
            "name": "test123",
            "description": "",
            "price": 1,
            "quantity": 1,
            "tangible": "N",
            "productId": "",
            "options": []
        }
    ]
}

Example Admin API Usage

Example Usage:

Twocheckout.apiusername = "APIuser1817037";
Twocheckout.apipassword = "APIpass1817037";

HashMap params = new HashMap();
params.put("comment", "test");
params.put("category", "1");

Sale sale = TwocheckoutSale.retrieve("4774380224");
TwocheckoutResponse result = sale.refund(params);
String message = result.getResponseMessage();

Example Response: (toJson)

{
   "response_code" : "OK",
   "response_message" : "refund added to invoice"
}

Example Checkout Usage:

Example Usage: (Using Spark)

import spark.*;
import static spark.Spark.*;

  get(new Route("/") {
     @Override
     public Object handle(Request request, Response response) {

         HashMap params = new HashMap();
         params.put("sid", "1817037");
         params.put("mode", "2CO");
         params.put("li_0_type", "product");
         params.put("li_0_name", "Example Product");
         params.put("li_0_price", "1.00");

         String form = TwocheckoutCharge.submit(params);

         form = "" + form + "";

         return form;
    }
  });

Example Return Usage:

Example Usage: (Using Spark)

  post(new Route("/") {
     @Override
     public Object handle(Request request, Response response) {
         HashMap params = new HashMap();
         params.put("sid", request.queryParams("sid"));
         params.put("total", request.queryParams("total"));
         params.put("order_number", request.queryParams("order_number"));
         params.put("key", request.queryParams("key"));
         Boolean result = TwocheckoutReturn.check(params, "tango");
         if (result == true) {
             return "Success";
         } else {
             return "Fail";
         }
     }
  });

Example INS Usage:

Example Usage: (Using Spark)

  post(new Route("/") {
     @Override
     public Object handle(Request request, Response response) {
         HashMap params = new HashMap();
         params.put("vendor_id", request.queryParams("vendor_id"));
         params.put("invoice_id", request.queryParams("invoice_id"));
         params.put("sale_id", request.queryParams("sale_id"));
         params.put("md5_hash", request.queryParams("md5_hash"));
         Boolean result = TwocheckoutNotification.check(params, "tango");
         if (result == true) {
             return "Success";
         } else {
             return "Fail";
         }
     }
  });

Exceptions:

TwocheckoutException exceptions are thrown by if an error has returned. It is best to catch these exceptions so that they can be gracefully handled in your application.

Example Usage

HashMap params = new HashMap();
params.put("comment", "test");
params.put("category", "1");
try {
    Sale sale = TwocheckoutInvoice.retrieve("4832573658");
    TwocheckoutResponse result = sale.refund(params);
} catch (TwocheckoutException e) {
    String message = e.toString();
}