Skip to content
Docs
Last updated

Charging Cards

Once you have a card token representing your user's card from Components - the next step is to charge that card for the money. This has to be done on your server by making a simple request to our API.

Note that a card token is a temporary representation of card details - it has to be used within 24 hours after creation. Otherwise, it will expire.

This example shows how to create a charge from card: cardToken

Shift4Gateway shift4 =
    new Shift4Gateway("pr_test_tXHm9qV9qV9bjIRHcQr9PLPa");

ChargeRequest chargeRequest = new ChargeRequest(799, "USD")
    .card(new CardRequest(cardToken));
Charge charge = shift4.createCharge(chargeRequest);

After successfully creating a charge, you should see it on charges list in your dashboard.

More information about creating charges can be found in API references.

Saving card for later use

Card tokens are for one-time use only. Once you create a charge from a token, that token can't be used to do anything else. To avoid asking the user for their card data every time you create a charge, you must create a customer with a card.

This example shows how to use card token to create a customer with a card and how to create a charge on that customer's card

Shift4Gateway shift4 =
    new Shift4Gateway("pr_test_tXHm9qV9qV9bjIRHcQr9PLPa");

CustomerRequest customerRequest = new CustomerRequest("user@example.com")
    .card(new CardRequest(tokenId));
Customer customer = shift4.createCustomer(customerRequest);

ChargeRequest chargeRequest = new ChargeRequest(799, "USD")
    .customerId(customer.getId());
Charge charge = shift4.createCharge(chargeRequest);

More information about creating customers can be found in the API references.