Apple Pay & Native Pay Methods

Apple Pay

In the iOS Swift SDK, Apple Pay can be used to process payments with a smooth, native experience. Before displaying the Apple Pay sheet, you may want to:

  1. (Optionally) configure Apple Pay to collect specific billing data.
  2. Confirm device compatibility with Apple Pay.
  3. Choose an approach for initiating and completing Apple Pay payments.

1. (Optional) Configuring Apple Pay to Collect Billing Data

If you need to collect specific billing data—like an email address—from the user during the Apple Pay flow, configure your MoneyHashSDK instance as follows:

let applePayConfig = ApplePayConfiguration(collectibleBillingData: [.email])

let moneyHash = MoneyHashSDKBuilder()
    .setPublicKey("<YOUR_PUBLIC_KEY>")
    .setApplePayConfiguration(applePayConfig)
    .build()
  • collectibleBillingData is an array of CollectibleBillingData. Currently, only .email is supported.
  • To disable billing data collection, pass an empty array ([]).

2. Check if the Device is Compatible with Apple Pay

To confirm that the device supports Apple Pay, call:

let isCompatible = moneyHash.isDeviceCompatible()

if isCompatible {
    // Proceed with Apple Pay
} else {
    print("This device does not support Apple Pay")
}

Approaches for Apple Pay Payments

You can handle Apple Pay payments in two ways:

  1. Approach #1: Directly use proceedWithApplePay, which requires an intentId upfront. After completing the payment, it automatically sends the Apple Pay receipt to MoneyHash in one step.
  2. Approach #2: Split the process into two steps—first generate a receipt with generateApplePayReceipt, then submit the receipt later using submitPaymentReceipt.

Approach #1: Single-Step Payment with proceedWithApplePay

If you already have an intentId, use proceedWithApplePay to show the Apple Pay sheet and complete the payment in one step. This approach automatically submits the Apple Pay receipt to MoneyHash after payment.

Sample Usage

// Example applePayData extracted from a NativePayData.applePay(...) scenario
do {
    let intentId = "<intent_id>"
    let depositAmount: Float = 10.0
    let merchantIdentifier = "merchant.com.example"
    let currencyCode = "USD"
    let countryCode = "US"

    // Show Apple Pay sheet and complete payment in one step
    let intentDetails = try await moneyHash.proceedWithApplePay(
        intentId: intentId,
        depositAmount: depositAmount,
        merchantIdentifier: merchantIdentifier,
        currencyCode: currencyCode,
        countryCode: countryCode
    )

    // Handle successful payment
    print("Payment completed successfully: \(intentDetails)")
} catch {
    // Handle error during Apple Pay process
    print("Error during Apple Pay: \(error.localizedDescription)")
}

In this flow, the Apple Pay receipt is sent to MoneyHash automatically after the payment is completed.


Approach #2: Two-Step Payment with generateApplePayReceipt & submitPaymentReceipt

If you don’t have an intentId yet—or you want to separate the Apple Pay authorization from finalizing the payment—you can use this two-step approach:

  1. Generate the Apple Pay receipt using generateApplePayReceipt.
  2. Submit the receipt using submitPaymentReceipt once you have the intentId.

Step 1: Generate the Apple Pay Receipt

do {
    let depositAmount: Float = 10.0
    let merchantIdentifier = "merchant.com.example"
    let currencyCode = "USD"
    let countryCode = "US"

    // Generate the Apple Pay receipt (no intentId needed yet)
    let receipt = try await moneyHash.generateApplePayReceipt(
        depositAmount: depositAmount,
        merchantIdentifier: merchantIdentifier,
        currencyCode: currencyCode,
        countryCode: countryCode
    )

    print("Apple Pay Receipt generated: \(receipt)")
    // Store 'receipt' until you're ready to submit the payment

} catch {
    print("Error generating Apple Pay receipt: \(error.localizedDescription)")
}

Step 2: Submit the Payment Receipt

Once you’ve obtained or created the intentId, finalize the payment:

do {
    let intentId = "<intent_id>" // Obtain from your backend
    let intentDetails = try await moneyHash.submitPaymentReceipt(
        intentId: intentId,
        nativePayReceipt: receipt
    )
    print("Payment completed successfully: \(intentDetails)")
} catch {
    print("Error submitting payment receipt: \(error.localizedDescription)")
}

Summary

  1. (Optional) Configure Apple Pay

    • Set desired billing data collection through setApplePayConfiguration(_:).
  2. Check Compatibility

    • Confirm device compatibility by calling moneyHash.isDeviceCompatible().
  3. Approach #1

    • Single-Step: Use proceedWithApplePay if you already have an intentId. The Apple Pay receipt is submitted to MoneyHash right after payment.
  4. Approach #2

    • Two-Step:
      1. generateApplePayReceipt to display Apple Pay and get a NativePayReceipt.
      2. submitPaymentReceipt once you have the intentId.

By choosing the appropriate approach, you can integrate Apple Pay into your iOS Swift application using the MoneyHash SDK, delivering a secure and streamlined payment experience.