Plants
& More
Charge ACH Payment Method

This example shows how easily you can create Charge using ACH Payment Method.

Setup Payment Method

To setup payment method see one of:


@AjaxController
@RequestMapping("/ajax/examples/charge-with-ach-payment-method")
class ExamplesAjaxChargeWithPaymentMethodController {

    @PostMapping("/charge")
    Map<String, String> createCharge(@RequestBody ExampleRequest initRequest) throws IOException {
        try (Shift4Gateway shift4Gateway = createShift4Gateway()) {
            validateUsageOfPaymentMethod(initRequest.paymentMethodClientObjectId);
            // find saved `id` of Payment Method using public `clientObjectId`
            var paymentMethodId = getIdOfPaymentMethod(initRequest.paymentMethodClientObjectId);

            // create charge
            var chargeRequest = new ChargeRequest()
                    .amount(1000)
                    .currency("USD")
                    .paymentMethod(new PaymentMethodRequest(paymentMethodId));
            var charge = shift4Gateway.createCharge(chargeRequest);

            // pass clientObjectId of charge to frontend to finish payment
            return singletonMap("clientObjectId", charge.getClientObjectId());

        } catch (Shift4Exception e) {
            throw new BadRequestException(e.getMessage());
        }
    }

    private void validateUsageOfPaymentMethod(String paymentMethodClientObjectId) {
        // validate method usage here, it highly depends on backend implementation
        // you should ensure that clientObjectId is used by the same user that configured it in the previous step
    }
}