# BLIK payments

Learn how to accept BLIK, a common payment method in Poland.

# iOS


BLIK is a [single use](https://docs.stripe.com/payments/payment-methods.md#usage) payment method that requires customers to [authenticate](https://docs.stripe.com/payments/payment-methods.md#customer-actions) their payments. When customers want to pay online using BLIK, they request a six-digit code from their banking application and enter it into the payment collection form.

The bank sends a push notification to your customer’s mobile phone asking to authorize the payment inside their banking application. The BLIK code is valid for 2 minutes; customers have 60 seconds to authorize the payment after starting a payment. After 60 seconds, it times out and they must request a new BLIK code. Customers typically approve BLIK payments in less than 10 seconds.

## Set up Stripe [Server-side] [Client-side]

First, you need a Stripe account. [Register now](https://dashboard.stripe.com/register).

### Server-side 

This integration requires endpoints on your server that talk to the Stripe API. Use the official libraries for access to the Stripe API from your server:

#### Ruby

```bash
# Available as a gem
sudo gem install stripe
```

```ruby
# If you use bundler, you can add this line to your Gemfile
gem 'stripe'
```

### Client-side 

The [Stripe iOS SDK](https://github.com/stripe/stripe-ios) is open source, [fully documented](https://stripe.dev/stripe-ios/index.html), and compatible with apps supporting iOS 13 or above.

#### Swift Package Manager

To install the SDK, follow these steps:

1. In Xcode, select **File** > **Add Package Dependencies…** and enter `https://github.com/stripe/stripe-ios-spm` as the repository URL.
2. Select the latest version number from our [releases page](https://github.com/stripe/stripe-ios/releases).
3. Add the **StripePaymentsUI** product to the [target of your app](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app).

#### Carthage

1. If you haven’t already, install the latest version of [Carthage](https://github.com/Carthage/Carthage#installing-carthage).
2. Add this line to your `Cartfile`:
   ```cartfile
   github "stripe/stripe-ios"
   ```
3. Follow the [Carthage installation instructions](https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos). Make sure to embed all of the required frameworks listed [here](https://github.com/stripe/stripe-ios/tree/master/StripePaymentsUI/README.md#manual-linking).
4. In the future, to update to the latest version of the SDK, run the following command:
   ```bash
   carthage update stripe-ios --platform ios
   ```

#### Manual Framework

1. Head to our [GitHub releases page](https://github.com/stripe/stripe-ios/releases/latest) and download and unzip **Stripe.xcframework.zip**.
2. Drag **StripePaymentsUI.xcframework** to the **Embedded Binaries** section of the **General** settings in your Xcode project. Make sure to select **Copy items if needed**.
3. Repeat step 2 for all required frameworks listed [here](https://github.com/stripe/stripe-ios/tree/master/StripePaymentsUI/README.md#manual-linking).
4. In the future, to update to the latest version of our SDK, repeat steps 1–3.

> For details on the latest SDK release and past versions, see the [Releases](https://github.com/stripe/stripe-ios/releases) page on GitHub. To receive notifications when a new release is published, [watch releases](https://help.github.com/en/articles/watching-and-unwatching-releases-for-a-repository#watching-releases-for-a-repository) for the repository.

Configure the SDK with your Stripe [publishable key](https://dashboard.stripe.com/test/apikeys) on app start. This enables your app to make requests to the Stripe API.

#### Swift

```swift
import UIKit
import StripePaymentsUI

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        StripeAPI.defaultPublishableKey = "<<YOUR_PUBLISHABLE_KEY>>"
        // do any other necessary launch configuration
        return true
    }
}
```

> Use your [test keys](https://docs.stripe.com/keys.md#obtain-api-keys) while you test and develop, and your [live mode](https://docs.stripe.com/keys.md#test-live-modes) keys when you publish your app.

## Create a PaymentIntent [Server-side] [Client-side]

A [PaymentIntent](https://docs.stripe.com/api/payment_intents/object.md) is an object that represents your intent to collect payment from a customer and tracks the lifecycle of the payment process through each stage.

### Server-side 

First, create a `PaymentIntent` on your server and specify the amount to collect and the `pln` currency (BLIK doesn’t support other currencies). [Enable the payment method](https://dashboard.stripe.com/settings/payment_methods) in your Dashboard. Stripe handles the return of eligible payment methods based on factors such as the transaction’s amount, currency, and payment flow.

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=pln \
  -d "automatic_payment_methods[enabled]=true" \
  --data-urlencode "description=A description of what you're selling" \
  -d statement_descriptor=ORDER_123
```

### Client-side 

On the client, request a PaymentIntent from your server and store its client secret.

#### Swift

```swift
class CheckoutViewController: UIViewController {
    var paymentIntentClientSecret: String?

    func startCheckout() {
        // Request a PaymentIntent from your server and store its client secret
    }
}
```

## Collect payment method details [Client-side]

In your app, collect the required 6-digit BLIK code from the customer. Create a [STPConfirmBLIKOptions](https://stripe.dev/stripe-ios/stripe-payments/Classes/STPConfirmBLIKOptions.html) with the BLIK code.

#### Swift

```swift
let blikOptions = STPConfirmBLIKOptions(code: "777123")
```

## Submit the payment to Stripe [Client-side]

Retrieve the client secret from the PaymentIntent you created in step 2 and the 6-digit BLIK code you collected in step 3 to call [STPPaymentHandler confirmPayment](https://stripe.dev/stripe-ios/stripe-payments/Classes/STPPaymentHandler.html#/c:@M@StripePayments@objc\(cs\)STPPaymentHandler\(im\)confirmPayment:withAuthenticationContext:completion:).

#### Swift

```swift
​​let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)

​​paymentIntentParams.paymentMethodParams = STPPaymentMethodParams(
​​    blik: STPPaymentMethodBLIKParams(),
    billingDetails: nil,
​​    metadata: nil
​​)

let confirmPaymentMethodOptions = STPConfirmPaymentMethodOptions()
confirmPaymentMethodOptions.blikOptions = blikOptions
paymentIntentParams.paymentMethodOptions = confirmPaymentMethodOptions

STPPaymentHandler.shared().confirmPayment(paymentIntentParams, with: self) { (handlerStatus, paymentIntent, error) in
    switch handlerStatus {
    case .succeeded:
        // Show a timer or a dialog to your customer to prompt them to authorize the payment.
        // Poll your server or Stripe API for the PaymentIntent status update

    case .canceled:
        // Payment was canceled

    case .failed:
        // Payment failed
    }
}
```

After confirmation with valid parameters, the status of the PaymentIntent becomes `requires_action` and calls the completion block with succeeded handlerStatus. The `next_action` has the `blik_authorize` type: this means that the next step is completed by your customer, who has 60 seconds to confirm the payment inside their mobile banking app. We recommend:

- Showing a timer or dialog to your customer to prompt them to authorize the payment
- Polling your server or Stripe API for the PaymentIntent status update
- Displaying the payment confirmation to your customer as soon as the PaymentIntent status updates to `succeeded`

## Optional: Handle post-payment events

Stripe sends a [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) event when the payment completes. Use the Dashboard, a custom *webhook* (A configurable endpoint that executes when it receives certain event notifications from an event destination), or a partner solution to receive these events and run actions, like sending an order confirmation email to your customer, logging the sale in a database, or starting a shipping workflow.

Listen for these events rather than waiting on a callback from the client. On the client, the customer could close the browser window or quit the app before the callback executes, and malicious clients could manipulate the response. Setting up your integration to listen for asynchronous events also helps you accept more payment methods in the future. Learn about the [differences between all supported payment methods](https://stripe.com/payments/payment-methods-guide).

- **Handle events manually in the Dashboard**

  Use the Dashboard to [view your payments](https://dashboard.stripe.com/payments), send email receipts, handle payouts, or retry failed payments.

- **Build a custom webhook**

  [Build a custom webhook handler](https://docs.stripe.com/events/set-up-events.md#create-event-handler) to listen for events and build custom asynchronous payment flows. Test and debug your webhook integration locally with the Stripe CLI.

- **Integrate a prebuilt app**

  Handle common business events, such as [automation](https://stripe.partners/?f_category=automation) or [marketing and sales](https://stripe.partners/?f_category=marketing-and-sales), by integrating a partner application.

## Optional: Simulate failures in a sandbox

BLIK payments can fail for different reasons. There are immediate failures (for example, the code is expired or invalid), delayed errors (the bank declines) or timeouts (the customer didn’t respond in time). You can simulate each failure scenarios by passing `email` values matching certain patterns (documented below) when creating the `PaymentIntent`, as part of the [billing details](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_data-billing_details), with any 6-digit as BLIK code.

### Immediate failures

| Error code                         | Decline code   | Explanation                                                                                                    | Email pattern       |
| ---------------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------- | ------------------- |
| `payment_method_invalid_parameter` | `code_invalid` | The code passed wasn’t a valid BLIK code.                                                                      | `.*invalid_code@.*` |
| `payment_method_invalid_parameter` | `code_expired` | The code passed has expired. BLIK codes expire after 2 minutes, please request another code from the customer. | `.*expired_code@.*` |

### Declines (8 second delay)

| Error code                        | Decline code              | Explanation                                                                                    | Email pattern             |
| --------------------------------- | ------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------- |
| `payment_method_provider_decline` | `payment_limit_exceeded`  | The payment has been declined because the payment limit on this bank account has been reached. | `.*limit_exceeded@.*`     |
| `payment_method_provider_decline` | `insufficient_funds`      | The bank account has insufficient funds to complete the purchase.                              | `.*insufficient_funds@.*` |
| `payment_method_provider_decline` | `customer_declined`       | The customer declined this payment.                                                            | `.*customer_declined@.*`  |
| `payment_method_provider_decline` | `partner_generic_decline` | The payment has been declined by the customer’s bank for an unknown reason.                    | `.*bank_declined@.*`      |
| `payment_method_provider_decline` | `partner_generic_decline` | The payment has been declined for an unknown reason.                                           | `.*blik_declined@.*`      |

### Timeouts (60 second delay)

| Error code                        | Decline code      | Explanation                                                               | Email pattern           |
| --------------------------------- | ----------------- | ------------------------------------------------------------------------- | ----------------------- |
| `payment_method_provider_timeout` | `generic_decline` | The customer didn’t approve this payment within the allocated 60 seconds. | `.*customer_timeout@.*` |
| `payment_method_provider_timeout` | `generic_decline` | The request to the customer’s bank timed out.                             | `.*bank_timeout@.*`     |
| `payment_method_provider_timeout` | `generic_decline` | The request to the BLIK network timed out.                                | `.*blik_timeout@.*`     |

