Custom Form enables you to seamlessly integrate card payment directly on your website.
You have full control over the look and feel of your payment flow when using Custom Form, but if you need a faster and easier integration, Checkout is an option.
This example shows a basic Custom Form with only the required fields (card number, expiration date, and CVC).
Use these card details to test different scenarios
@Controller
public class ExamplesController {
private static final String PRIVATE_KEY = "pr_test_tXHm9qV9qV9bjIRHcQr9PLPa";
@RequestMapping(value = "/examples/custom-form", method = GET)
public String customForm(Model model) {
return "examples/custom-form";
}
@RequestMapping(value = "/examples/custom-form/payment", method = POST)
public String customFormSubmit(Model model, @RequestParam String tokenId) throws IOException {
model.addAttribute("tokenId", tokenId);
try (Shift4Gateway shift4Gateway = new Shift4Gateway(PRIVATE_KEY)) {
ChargeRequest request = new ChargeRequest()
.amount(2499)
.currency("USD")
.card(new CardRequest(tokenId));
Charge charge = shift4Gateway.createCharge(request);
model.addAttribute("chargeId", charge.getId());
} catch (Shift4Exception e) {
model.addAttribute("errorType", e.getType());
model.addAttribute("errorCode", e.getCode());
model.addAttribute("errorMessage", e.getMessage());
model.addAttribute("chargeId", e.getChargeId());
}
return "examples/custom-form";
}
}