Creating Customers

You can create a customer by itself, with a credit card, or with a credit card with a billing address.

This page describes creating customers using the server-to-server API. Customers can also be created using Transparent Redirect. The server-to-server API is easier to understand, so even if you’re planning to use TR, you may want to read this page first.

Just a Customer

Here is an example of creating a customer without credit card information. If all customer validations pass, success will be true.

CustomerRequest request = new CustomerRequest().
    firstName("Mark").
    lastName("Jones").
    company("Jones Co.").
    email("mark.jones@example.com").
    fax("419-555-1234").
    phone("614-555-1234").
    website("http://example.com");
Result<Customer> result = gateway.customer().create(request);

result.isSuccess();
// true

result.getTarget().getId();
// e.g. 594019

Specify Your Own Customer ID

If you do not specify the customer ID, as in the example above, the gateway will generate one. You can also choose what you would like the ID to be.

CustomerRequest request = new CustomerRequest().
    id("customer_123").
    firstName("Mark");
Result<Customer> result = gateway.customer().create(request);

Blank Customer

None of the parameters are required, so if you’re only interested in storing credit card details without any customer information, you can create a blank customer.

Result<Customer> result = gateway.customer().create(new CustomerRequest());

Customer with Credit Card

You can also create a credit card along with a customer. If all customer validations and credit card validations pass, and the card is verified (if requested), success will return true.

CustomerRequest request = new CustomerRequest().
    firstName("Fred").
    lastName("Jones").
    creditCard().
        cardholderName("Fred Jones").
        number("5105105105105100").
        expirationDate("05/2012").
        done();

Result<Customer> result = gateway.customer().create(request);

result.isSuccess();
// true

Customer customer = result.getTarget();
customer.getId();
// e.g. 160923

customer.getCreditCards().get(0).getToken();
// e.g. f28w

If you do not specify a token for the credit card, as in the example above, the gateway will generate one. You can also choose what you want the token to be.

CustomerRequest request = new CustomerRequest().
    creditCard().
        token("credit_card_123").
        number("5105105105105100").
        expirationDate("05/2012").
        done();

Result<Customer> result = gateway.customer().create(request);

By default, the credit card number is not verified to be a valid, open account. If you want to check for that, use the card verification API.

Customer with Credit Card with Billing Address

You can also create a credit card with billing address along with a customer.

CustomerRequest request = new CustomerRequest().
    firstName("Fred").
    lastName("Jones").
    creditCard().
        number("5105105105105100").
        expirationDate("05/2012").
        billingAddress().
            streetAddress("1 E Main St").
            extendedAddress("Unit 2").
            locality("Chicago").
            region("Illinois").
            postalCode("60607").
            countryCodeAlpha2("US").
            done().
        done();

Result<Customer> result = gateway.customer().create(request);

Customer customer = result.getTarget();
customer.getCreditCards().get(0).getBillingAddress().getStreetAddress();
// e.g. "1 E Main St"

See Also