Skip to content

globallypaid/globallypaid-sdk-java

Repository files navigation

GloballyPaid Java SDK

Maven Central Build Status JavaDoc

The official GloballyPaid Java library.

Table of Contents

Installation

Requirements

  • Java 1.8 or later

Gradle users

Add this dependency to your project's build file in the root:

...
dependencies {
    ...
    implementation "com.globallypaid:globallypaid-java:1.0.1"
}

repositories {
    mavenCentral()
}
...

Maven users

Add this dependency to your project's POM:

<dependency>
  <groupId>com.globallypaid</groupId>
  <artifactId>globallypaid-java</artifactId>
  <version>1.0.1</version>
</dependency>

Documentation

Please see the Java API docs for the most up-to-date documentation.

You can also refer to the online Javadoc.

Samples

For a sample project, please visit GloballyPaid Java SDK samples.

For a comprehensive list of examples, please visit project's examples.

Quick Start

The following is the minimum needed code to make a charge sale transaction:

Environment Variables

globallypaid-java supports the GloballyPaid Api Key, App ID and Shared Secret values stored in the following environment variables:

  • PUBLISHABLE_API_KEY
  • APP_ID
  • SHARED_SECRET
  • USE_SANDBOX

Setup Environment Variables

Update the development environment with your Environment Variables with the following steps:

Linux users:

  1. Copy the sample environment file env_sample.sh to a new file
cp env_sample.sh globallypaid_env.sh
  1. Edit the new globallypaid_env.sh to add your Environment Variables
  2. Source the globallypaid_env.sh file to set the variables in the current session
source globallypaid_env.sh

Windows users:

  1. Copy the sample environment file env_sample.bat to a new file
cp env_sample.bat globallypaid_env.bat
  1. Edit the new globallypaid_env.bat to add your Environment Variables
  2. Execute the globallypaid_env.bat file to set the variables
globallypaid_env.bat

Initialize the Client

GloballyPaid globallyPaid = new GloballyPaid(
          Config.builder()
              .publishableApiKey(System.getenv("PUBLISHABLE_API_KEY"))
              .appId(System.getenv("APP_ID"))
              .sharedSecret(System.getenv("SHARED_SECRET"))
              .sandbox(System.getenv("USE_SANDBOX")) // true if you need to test through GloballyPaid sandbox
              .build());

or

String publishableApiKey = "pk_live_xxxxx";
String appId = "Your APP ID";
String sharedSecret = "Your Shared Secret";

GloballyPaid globallyPaid = new GloballyPaid(
          Config.builder()
              .publishableApiKey(publishableApiKey)
              .appId(appId)
              .sharedSecret(sharedSecret)              
              .build());

You can also change to sandbox with the following setting:

GloballyPaid.setSandbox(true);
Per-request configuration

All SDK service methods accept an optional RequestOptions object, additionally allowing per-request configuration:

RequestOptions requestOptions = RequestOptions.builder()
            .appId("Your APP ID")
            .sharedSecret("Your Shared Secret")
            .build();

Customer.builder().build().retrieve("customer_id_here", requestOptions);

RequestOptions requestOptions = RequestOptions.builder()
            .publishableApiKey("Your Publishable API Key").build();

GloballyPaid.builder().build().token("token_request_here", requestOptions);

Configuring Timeouts

Connect and read timeouts can be configured globally:

GloballyPaid.setConnectTimeout(50 * 1000); // in milliseconds
GloballyPaid.setReadTimeout(100 * 1000);

Or on a finer grain level using RequestOptions:

RequestOptions options = RequestOptions.builder()
    .connectTimeout(50 * 1000) // in milliseconds
    .readTimeout(100*1000)
    .build();
GloballyPaid.builder().build().charge(ChargeRequest.builder().build(), requestOptions);

Please take care to set conservative read timeouts. Some API requests can take some time, and a short timeout increases the likelihood of a problem within our servers.

Make a Charge Sale Transaction

GloballyPaid Charge Sale Transaction example:

package com.globallypaid.example.payment;

import com.globallypaid.exception.GloballyPaidException;
import com.globallypaid.http.Config;
import com.globallypaid.model.ChargeRequest;
import com.globallypaid.model.ChargeResponse;
import com.globallypaid.service.GloballyPaid;
import java.io.IOException;

public class ChargeSaleTransaction {
  public static void main(String[] args) throws IOException, GloballyPaidException {
    try {
      GloballyPaid globallyPaid =
          new GloballyPaid(
              Config.builder()
                  .publishableApiKey(System.getenv("PUBLISHABLE_API_KEY"))
                  .appId(System.getenv("APP_ID"))
                  .sharedSecret(System.getenv("SHARED_SECRET"))
                  .sandbox(System.getenv("USE_SANDBOX"))
                  .build());

      ChargeRequest chargeRequest =
          ChargeRequest.builder()
              .source("source") // can be the token or payment instrument identifier
              .amount(130)
              .currencyCode("USD")
              .clientCustomerId("XXXXXXX") // set your customer id
              .clientInvoiceId("XXXXXX") // set your invoice id
              .clientTransactionId("XXXXXXXXX")
              .clientTransactionDescription("Charge Sale Transaction") // set your transaction description
              .capture(true) // sale charge
              .savePaymentInstrument(false)
              .build();

      ChargeResponse chargeResponse = globallyPaid.charge(chargeRequest, null);
      System.out.println(chargeResponse);
    } catch (GloballyPaidException e) {
      System.out.println(
          "ChargeSaleTransaction ---> Code: "
              + e.getCode()
              + "\nMsg: "
              + e.getMessage()
              + "\nApi error: "
              + e.getGloballyPaidError());
      throw e;
    }
  }
}

See the project's examples for more examples.

Make a Charge Sale Transaction with Javascript SDK integration

import com.globallypaid.exception.GloballyPaidException;
import com.globallypaid.http.Config;
import com.globallypaid.http.RequestOptions;
import com.globallypaid.model.ChargeRequest;
import com.globallypaid.model.ChargeResponse;
import com.globallypaid.model.PaymentInstrumentToken;
import org.springframework.stereotype.Service;

@Service
public class ChargeService {

  public ChargeResponse charge(PaymentInstrumentToken paymentInstrumentToken)
      throws GloballyPaidException {

    ChargeResponse chargeResponse = null;

    GloballyPaid globallyPaid =
        new GloballyPaid(
            Config.builder()
                .publishableApiKey(System.getenv("PUBLISHABLE_API_KEY"))
                .appId(System.getenv("APP_ID"))
                .sharedSecret(System.getenv("SHARED_SECRET"))
                .sandbox(System.getenv("USE_SANDBOX"))
                .build());

    if (paymentInstrumentToken != null && !paymentInstrumentToken.getId().isEmpty()) {
      RequestOptions requestOptions =
          RequestOptions.builder().connectTimeout(50 * 1000).readTimeout(100 * 1000).build();

      ChargeRequest gpChargeRequest =
          ChargeRequest.builder()
              .source(paymentInstrumentToken.getId())
              .amount(160)
              .currencyCode("USD")
              .clientCustomerId("XXXXXXX")
              .clientInvoiceId("XXXXXX")
              .clientTransactionId("XXXXXXXXX")
              .clientTransactionDescription("Charge Sale sample!")
              .capture(true)
              .savePaymentInstrument(false)
              .build();

      chargeResponse = globallyPaid.charge(gpChargeRequest, requestOptions);
      System.out.println(chargeResponse.toString());
    }
    return chargeResponse;
  }
}

Please visit GloballyPaid Java SDK samples to see sample project.

About

globallypaid-java is maintained and funded by Globally Paid.

If you've found a bug in the library or would like new features added, go ahead and open issues or pull requests!