Plants
& More

Apple Pay

Apple Pay enables Apple device owner to pay with card information securely stored by Apple without cumbersome process of filling card and payment information on website.

This example shows how you can process Apple Pay payments using our shift4.js.

It is based on Apple Pay PaymentRequest API. If you're curious, you can find the details of the API under following locations:

Payment with the Apple Pay API JavaScript library

The entire process is detailed in the code snippets below and can be organized into the following main sections:

  • Load shift4.js JavaScript library [1]
  • Setup Shift4.js with your public key [2]
  • Detect if Apple Pay is supported [3]
  • Create Apple Pay token on button click [4]
    • Configure PaymentRequest method details [4a]
    • Configure details of shopping cart details [4b]
    • Create PaymentRequest using shift4.js [4c]
    • Show payment sheet [4d]
  • Proceed with payment on user verification [5]
    • Create Shift4 token [5a]
    • Create charge [5b]
    • Go to payment the result page [5c]

Apple Pay and 3D Secure

Apple Pay payment does not require additional 3D Secure verification.


@AjaxController
@RequestMapping("/ajax/examples/charge-with-apple-pay")
class ExamplesAjaxChargeWithApplePayWithTokenController {

    @PostMapping("/payment")
    Map chargeWithApplePayPayment(@RequestBody ApplePayTokenPaymentExampleRequest exampleRequest) throws IOException {
        try (Shift4Gateway shift4Gateway = createShift4Gateway()) {
            // create charge using received token ID
            ChargeRequest chargeRequest = new ChargeRequest(100, "USD")
                    .paymentMethod(new PaymentMethodRequest(exampleRequest.token));
            Charge charge = shift4Gateway.createCharge(chargeRequest);

            // pass clientObjectId of charge to frontend
            return singletonMap("clientObjectId", charge.getClientObjectId());
        } catch (Shift4Exception e) {
            throw new BadRequestException(e.getMessage());
        }
    }

    static class ApplePayTokenPaymentExampleRequest {
        String token;
    }
}