Plants
& More

Swish Payment Method

This example demonstrates a payment form using the Blik payment method where the customer authorizes the payment using their mobile banking app.

Test codes

In test mode, including this example, you can use predefined codes to get different final outcome of the charge.

The following payment codes result in a successful charge, each with a different delay before final result is resolved.

  • 111001 - 1 second delay
  • 111010 - 10 seconds delay
  • 111060 - 60 seconds delay
The following payment codes result in a declined charge, each with a different delay before final result is resolved.
  • 999001 - 1 second delay
  • 999010 - 10 seconds delay
  • 999060 - 60 seconds delay

Payment flow technical overview

The full transaction involves a few straightforward steps:

  • The website collects blik payment code and sends it to the backend to initiate a payment.
  • The backend uses Shift4 API to create a Payment Method, and then a Charge using the returned Payment Method ID.
  • The Charge initially has a pending status and flow.nextAction equals mobile_app_confirmation.
  • Customer should accept or decline transaction in their banking app.
  • Soon after, the Charge status is updated to successful or failed depending on the payment outcome.

Client object ID

Every Charge includes a clientObjectId, which can be used with shift4.js. It allows retrieving all necessary payment information in the browser without exposing sensitive data.

Simplified integration with shift4.js

By using shift4.js, handling the Blik with code flow becomes easier. It can manage redirects and automatically track the final payment status for you.

To use this functionality, pass the clientObjectId from your server to the browser and invoke the handleChargeNextAction method with it. This will trigger request to Blik and wait for the result before resolving.

You’ll need to access clientObjectId in two situations:

  • Right after the Charge is created on your backend – return it to the frontend.
  • When the customer is redirected back to your website – it will be part of the return URL's query parameters.

Showing final result

Once the payment is completed, handleChargeNextAction will resolve with a minimal Charge object, exposing only the required result information, ensuring user privacy and security.


@AjaxController
@RequestMapping("/ajax/examples/charge-with-blik-with-code")
@RequiredArgsConstructor
class ExamplesAjaxChargeWithBlikController {

    @PostMapping("/payment")
    Map ajaxPaymentMethod(@RequestBody PaymentMethodExampleRequest exampleRequest,
                                          HttpServletRequest request
    ) throws IOException {
        try (Shift4Gateway shift4Gateway = createShift4GatewayForPaymentMethods()) {

            // create payment method
            var paymentMethodRequest = new PaymentMethodRequest()
                    .set("type", "blik")
                    .set("fraudCheckData", new FraudCheckDataRequest()
                            .setUserAgent(RequestUtils.getUserAgent(request))
                            .setIpAddress(RequestUtils.getIp(request))
                    )
                    .billing(new BillingRequest()
                            .address(new AddressRequest().country("PL"))
                    )
                    .set("blik", new PaymentMethodDetailsRequest.Blik().setCode(exampleRequest.code));
            var paymentMethod = shift4Gateway.createPaymentMethod(paymentMethodRequest);

            // create charge using payment method
            var chargeRequest = new ChargeRequest(100, "PLN")
                    .paymentMethod(new PaymentMethodRequest(paymentMethod.getId()));
            var 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 PaymentMethodExampleRequest {
        String code;
    }
}