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 web site.
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:
@RestController
@RequestMapping("/ajax/examples")
class ExamplesAjaxController {
@PostMapping("/charge-with-apple-pay/payment")
Map<String, String> chargeWithApplePayPayment(@RequestBody ApplePayExampleRequest exampleRequest) throws IOException {
try (Shift4Gateway shift4Gateway = createShift4GatewayForPaymentMethods()) {
// create payment method using ApplePay token
PaymentMethodRequest paymentMethodRequest = new PaymentMethodRequest(APPLE_PAY)
.applePay(new PaymentMethodRequest.ApplePay(exampleRequest.applePayToken));
PaymentMethod paymentMethod = shift4Gateway.createPaymentMethod(paymentMethodRequest);
// create charge using created payment method
ChargeRequest chargeRequest = new ChargeRequest(100, "USD")
.paymentMethod(new PaymentMethodRequest(paymentMethod.getId()));
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 ApplePayExampleRequest {
Object applePayToken;
}
}