> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pesaswap.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Headless SDK

<Warning>**Beta in Progress** – The beta version is under active development and coming soon to our platform. </Warning>

Pesaswap is designed to facilitate the integration and management of payment-related functionalities in a decoupled or headless architecture with flexibility to customize your checkout UI.

## Customize the Payment Experience Using Headless Functions

### 1. Initialize the Pesaswap SDK

Initialize Pesaswap Headless SDK onto your app with your publishable key. To get a Publishable Key please find it here.

<CodeGroup>
  ```javascript HTML + JavaScript theme={"system"}
  // Source Hyperloader on your HTML file using the <script /> tag
  hyper = Hyper.init("YOUR_PUBLISHABLE_KEY", {
      customBackendUrl: "YOUR_BACKEND_URL",
      // You can configure this as an endpoint for all the API calls such as session, payments, confirm call.
  });
  ```

  ```swift iOS theme={"system"}
  // iOS implementation example
  import PesaswapSDK

  // Initialize the SDK
  let Pesaswap = Pesaswap.init("YOUR_PUBLISHABLE_KEY")
  ```

  ```java Android theme={"system"}
  // Android implementation example
  import io.Pesaswap.payments.paymentlauncher.PaymentLauncher;

  // Initialize the SDK
  PaymentLauncher paymentLauncher = new PaymentLauncher(this, "YOUR_PUBLISHABLE_KEY");
  ```

  ```dart Flutter theme={"system"}
  // Flutter implementation example
  import 'package:Pesaswap_sdk/Pesaswap_sdk.dart';

  // Initialize the SDK
  PesaswapSDK.init("YOUR_PUBLISHABLE_KEY");
  ```

  ```javascript React Native (Beta) theme={"system"}
  // React Native implementation example
  import { PesaswapSDK } from '@Pesaswap/react-native-sdk';

  // Initialize the SDK
  PesaswapSDK.init("YOUR_PUBLISHABLE_KEY");
  ```
</CodeGroup>

### 2. Create a Payment Intent

Make a request to the endpoint on your server to create a new payment. The `clientSecret` returned by your endpoint is used to initialize the payment session.

<Warning>Make sure to never share your API key with your client application as this could potentially compromise your security.</Warning>

### 3. Initialize your Payment Session

Initialize a payment session by passing the `clientSecret` to the `initPaymentSession`.

<CodeGroup>
  ```swift iOS theme={"system"}
  paymentSession?.initPaymentSession(paymentIntentClientSecret: paymentIntentClientSecret)
  ```

  ```java Android theme={"system"}
  // Initialize payment session
  paymentSession.initPaymentSession(paymentIntentClientSecret);
  ```

  ```javascript JavaScript theme={"system"}
  // Initialize payment session
  hyper.initPaymentSession({
      clientSecret: paymentIntentClientSecret
  });
  ```

  ```dart Flutter theme={"system"}
  // Initialize payment session
  PesaswapSDK.initPaymentSession(paymentIntentClientSecret);
  ```

  ```javascript React Native (Beta) theme={"system"}
  // Initialize payment session
  PesaswapSDK.initPaymentSession({
      clientSecret: paymentIntentClientSecret
  });
  ```
</CodeGroup>

#### Options (Required)

| Parameter      | Description                                       |
| -------------- | ------------------------------------------------- |
| `clientSecret` | **Required.** Identifier of the payment (string). |

### 4. Craft a Customized Payments Experience

Using the `paymentSession` object, you can fetch the customer's default payment method data and confirm the payment using `confirmWithCustomerDefaultPaymentMethod`.

<CodeGroup>
  ```swift iOS theme={"system"}
  private var handler: PaymentSessionHandler?

  func initSavedPaymentMethodSessionCallback(handler: PaymentSessionHandler) -> Void {
      self.handler = handler
  }

  @objc func launchHeadless(_ sender: Any) {
      hyperViewModel.paymentSession?.getCustomerSavedPaymentMethods(initSavedPaymentMethodSessionCallback)
      getDefault.isEnabled = true
      getLast.isEnabled = true
      getData.isEnabled = true
  }

  @objc func getCustomerDefaultSavedPaymentMethodData(_ sender: Any) {
      let paymentMethod = self.handler?.getCustomerDefaultSavedPaymentMethodData()
  }
  ```

  ```java Android theme={"system"}
  // Get customer saved payment methods
  paymentSession.getCustomerSavedPaymentMethods(new PaymentSessionHandler() {
      @Override
      public void onResult(PaymentMethodData paymentMethodData) {
          // Handle payment method data
      }
  });

  // Get default payment method
  PaymentMethod defaultPaymentMethod = paymentSession.getCustomerDefaultSavedPaymentMethodData();
  ```

  ```javascript JavaScript theme={"system"}
  // Get customer saved payment methods
  hyper.getCustomerSavedPaymentMethods()
      .then((paymentMethods) => {
          // Handle payment methods
      });

  // Get default payment method
  const defaultPaymentMethod = hyper.getCustomerDefaultSavedPaymentMethodData();
  ```

  ```dart Flutter theme={"system"}
  // Get customer saved payment methods
  List<PaymentMethod> paymentMethods = await PesaswapSDK.getCustomerSavedPaymentMethods();

  // Get default payment method
  PaymentMethod defaultPaymentMethod = await PesaswapSDK.getCustomerDefaultSavedPaymentMethodData();
  ```

  ```javascript React Native (Beta) theme={"system"}
  // Get customer saved payment methods
  const paymentMethods = await PesaswapSDK.getCustomerSavedPaymentMethods();

  // Get default payment method
  const defaultPaymentMethod = await PesaswapSDK.getCustomerDefaultSavedPaymentMethodData();
  ```
</CodeGroup>

## Payload for `confirmWithCustomerDefaultPaymentMethod(payload)`

### Options (Required)

| Parameter       | Description                                                                                                                                                        |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `confirmParams` | Object containing parameters that will be passed to the Hyper API.                                                                                                 |
| `redirect`      | **(web only)** Can be either `'always'` or `'if_required'`. If set to `'always'`, user is redirected after confirmation. `'if_required'` only redirects if needed. |

### ConfirmParams Object

| Parameter    | Description                                                         |
| ------------ | ------------------------------------------------------------------- |
| `return_url` | **(web only)** URL your customer will be directed to after payment. |
