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 cardToken:

$shift4 = new Shift4Gateway('pr_test_tXHm9qV9qV9bjIRHcQr9PLPa');

$chargeRequest = new ChargeRequest();
$chargeRequest->amount(799)->currency('USD')->card($cardToken);

$charge = $shift4->createCharge($chargeRequest);
Shift4Gateway shift4 =
		new Shift4Gateway("pr_test_tXHm9qV9qV9bjIRHcQr9PLPa");

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

var chargeRequest = new ChargeRequest() { Amount = 799, Currency = "USD",
									  Card = new CardRequest() { Id = token.Id } };
var charge = await gateway.CreateCharge(chargeRequest);

Note that we have placed your test mode API secret key in this example. This is needed to authenticate your requests. Remember to change that key to your live mode in production.

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:

$shift4 = new Shift4Gateway('pr_test_tXHm9qV9qV9bjIRHcQr9PLPa');

$customerRequest = new CustomerRequest();
$customerRequest->email('[email protected]')->card($tokenId);
$customer = $shift4->createCustomer($customerRequest);

$chargeRequest = new ChargeRequest();
$chargeRequest->amount(799)->currency('USD')->customerId($customer->getId());
$charge = $shift4->createCharge($chargeRequest);
Shift4Gateway shift4 =
		new Shift4Gateway("pr_test_tXHm9qV9qV9bjIRHcQr9PLPa");

CustomerRequest customerRequest = new CustomerRequest("[email protected]")
		.card(new CardRequest(tokenId));
Customer customer = shift4.createCustomer(customerRequest);

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

var customerRequest = new CustomerRequest() { Email = "[email protected]",
								      Card = new CardRequest() { Id = token.Id } };
var customer = await gateway.CreateCustomer(customerRequest);

var chargeRequest = new ChargeRequest() { Amount = 799, Currency = "USD",
								          CustomerId = customer.Id };
var charge = await gateway.CreateCharge(chargeRequest);

Note that we have placed your test mode API secret key in this example. This is needed to authenticate your requests. Remember to change that key to your live mode in production.

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