This example demonstrates a payment form using the Swish payment method, which supports three user flows: phone number entry, QR code scan, and mobile app redirection. Swish is a payment method where the customer authorizes the payment using their mobile banking app.
When using phone number entry, you can simulate both successful and unsuccessful scenarios.
For mobile app redirection to work, this example needs to be opened on a mobile phone.
QR code and mobile app redirection test mode simulates Swish application by displaying a confirmation screen for the user. In live mode, user would get redirected to the Swish application.
The following phone numbers result in a successful transaction:
The following phone numbers result in a declined transaction:
The full transaction involves a few straightforward steps:
pending
status and flow.nextAction
equals app_redirect
, qr_code
or mobile_app_confirmation
depending on the chosen flow.
mobile_app_confirmation
, opens their Swish app to confirm the transaction,qr_code
, scans a generated QR code using the Swish app, orapp_redirect
, is redirected directly into the Swish mobile app, if supported.successful
or failed
depending on the payment outcome.
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.
By using shift4.js
, handling the Swish 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 Swish and wait for the result before resolving.
You’ll need to access clientObjectId
in two situations:
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-swish")
@RequiredArgsConstructor
class ExamplesAjaxChargeWithSwishController {
@PostMapping("/payment")
Map ajaxPaymentMethod(@RequestBody PaymentMethodExampleRequest exampleRequest) throws IOException {
try (Shift4Gateway shift4Gateway = createShift4GatewayForPaymentMethods()) {
// create payment method
var paymentMethodRequest = new PaymentMethodRequest(PaymentMethodType.SWISH)
.billing(new BillingRequest()
.name(exampleRequest.name)
.phone(exampleRequest.phone)
.address(new AddressRequest().country("SE")))
.swish(new PaymentMethodRequest.Swish(exampleRequest.swishLinkMethod));
var paymentMethod = shift4Gateway.createPaymentMethod(paymentMethodRequest);
// create charge using payment method
var chargeRequest = new ChargeRequest(100, "USD")
.paymentMethod(new PaymentMethodRequest(paymentMethod.getId()))
.flow(new ChargeFlowRequest()
.returnUrl(getSiteUrl() + "/examples/charge-with-swish"));
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 {
SwishLinkMethod swishLinkMethod;
String name;
String phone;
}
}