# Apple Pay in your native app

{% hint style="warning" %}
This document is intented for Apple Pay in your iOS native application. For Apple Pay in your web application please see the [web guide. ](https://docs.payengine.co/developer-docs/processing-payments/apple-pay)
{% endhint %}

## Setup Apple's Developer Account

* Create a developer account with [Apple Developer Program](https://developer.apple.com/programs/).
* Ensure your app is compliant with [Apple's guidelines](https://developer.apple.com/app-store/review/guidelines/).

## Enabe Apple Pay

### 1. Accept Apple Pay in App <a href="#accept-apple-pay-in-app" id="accept-apple-pay-in-app"></a>

Obtain an Apple Merchant ID by [registering for a new identifier](https://developer.apple.com/account/resources/identifiers/add/merchant) in the Apple Developer portal.

Complete the form by providing a description and an identifier. The description is for your reference and can be updated later. It is recommended to use your app's name as the identifier for example  `merchant.{{YOUR_APP_NAME}})`.

### **2. Create an Apple Pay Certificate**

To enable your app to encrypt payment data using Apple Pay, follow these steps:

1. Navigate to the Merchant settings in the PE Partner Portal.
2. Scroll to the Apple Pay section and follow the provided instructions.
3. Enter your Apple Merchant ID and download the Certificate Signing Request (CSR) file.
4. Use the CSR file to obtain a secure certificate from Apple, enabling the use of Apple Pay.

<figure><img src="https://2556322805-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRlN2yYeruyG-6M2wZN%2Fuploads%2FZ2OhzBVIWD0VSnhj5XJn%2Fapple_pay1.png?alt=media&#x26;token=5b807fbc-756f-49f4-ac58-62292650f3bc" alt=""><figcaption></figcaption></figure>

### 3. Upload Apple Pay Processing Certificate

Navigate to the Apple Merchant ID that you registered earlier on the Apple Developer website. Upload the CSR file to generate an Apple Pay Processing Certificate. Once the certificate is active, download it. The certificate file should be named `apple_pay.cer`.

<figure><img src="https://2556322805-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRlN2yYeruyG-6M2wZN%2Fuploads%2FZUiBHHaJp8t4wHKfOP39%2Fapple_pay2.png?alt=media&#x26;token=6603dd45-eac8-41c1-90a0-d6571129de2c" alt=""><figcaption></figcaption></figure>

Upload the `cer` file back to PayEngine as described in the Apple Pay settings.

<figure><img src="https://2556322805-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRlN2yYeruyG-6M2wZN%2Fuploads%2FRd5oFzTOlLrMyIBpKWqr%2Fapple_pay3.png?alt=media&#x26;token=c173db04-062e-4c46-8053-7cc7fbf0ddd4" alt=""><figcaption></figcaption></figure>

### **4. Integrate with Xcode**

To enable Apple Pay in your app:

1. Open your project in Xcode.
2. Go to the **Signing & Capabilities** tab within your project settings.
3. Add the **Apple Pay** capability.
4. If prompted, log in to your Apple Developer account.
5. Select the Merchant ID you previously created.

<figure><img src="https://2556322805-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MRlN2yYeruyG-6M2wZN%2Fuploads%2FMKSOKmA13pApSZT5vREK%2Fapple_pay4.png?alt=media&#x26;token=59effc72-32d2-43eb-8b57-e9ecdb23096d" alt=""><figcaption></figcaption></figure>

Your app is now ready to accept Apple Pay.

## **API Integration (iOS SDK)**

```swift
// Example Swift code for integrating the API
import PayEngine

class ApplePayViewController: UIViewController, PEApplePayDelegate {
    let amount = 1.00
    
    let config = PEConfig(publicKey: "<API Public key")
    let payengine = PayEngine(config: config)
    
    func paymentSheetDidMDismiss() {
        //Handle Payment Sheet dismiss 
    }
    
    func paymentTokenDidReturn(token: String, completion: @escaping (PEApplePayResult) -> Void) {
        print("paymentTokenDidReturn \(token)")
        makePaymentWithToken(token: token, completion: completion)
    }
    
    func paymentTokenNotAvailable() {
        //Handle Payment token not available 
        print("paymentTokenNotAvailable")
    }
    
    //This is just an example of you calling your backend to complete the transaction.
    //Call completion(.success) or completion(.failure) to close the payment sheet
    private func makePaymentWithToken(token: String, completion: @escaping (PEApplePayResult) -> Void) {
        let json: [String: Any] = [
            "merchant_id": MERCHANT_ID,
            "data": [
                "cardToken": token,
                "transactionAmount": amount
            ]
        ]

        let jsonData = try? JSONSerialization.data(withJSONObject: json)

        let url = URL(string: <your-backend-url>)!
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("Basic " + <your-api-key>, forHTTPHeaderField: "Authorization")
        request.setValue("\(String(describing: jsonData?.count))", forHTTPHeaderField: "Content-Length")
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
         request.httpBody = jsonData

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
                return
            }
            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
            if let responseJSON = responseJSON as? [String: Any] {
                print(responseJSON) //Code after Successfull POST Request
                if let responseData = responseJSON["data"] as? [String: Any] {
                    print("responseData: \(responseData)")
                    completion(.success)
                    return
                }
            }
            
            completion(.failure)
        }

        task.resume()
    }
    
    // Sample UI code
    @IBOutlet weak var stackView: UIStackView!
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        payengine.applePay(merchantId: <merchant-id>) { result in
            switch result {
                
            case .success(let applePay):
                // register callbacks to this view controller
                applePay.delegate = self
                
                let request = ApplePayPaymentRequest()
                request.build(paymentItems: [
                    // leave empty or list of PKPaymentSummaryItem
                ], paymentAmount: self.amount, currencyCode: "USD")
                DispatchQueue.main.async {
                    if let button = applePay.setupApplePayButton(for: request, type: .checkout) {
                        /// apple pay is available, add the button to your UI
                        self.stackView.addSubview(button)
                    }
                }
            case .failure(let error):
                print("Unable to setup apple pay: \(error)")
            }
        }
        
    }
}
```

## Handling Multiple Merchant IDs (MIDs)

Manage various merchant IDs in your app by following Apple's guidelines and  ensure your app can switch between MIDs based on the transaction context.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.payengine.co/developer-docs/processing-payments/apple-pay/apple-pay-in-your-native-app.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
