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:
- (Optionally) configure Apple Pay to collect specific billing data.
- Confirm device compatibility with Apple Pay.
- 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 ofCollectibleBillingData
. 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:
- Approach #1: Directly use
proceedWithApplePay
, which requires anintentId
upfront. After completing the payment, it automatically sends the Apple Pay receipt to MoneyHash in one step. - Approach #2: Split the process into two steps—first generate a receipt with
generateApplePayReceipt
, then submit the receipt later usingsubmitPaymentReceipt
.
Approach #1: Single-Step Payment with proceedWithApplePay
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
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:
- Generate the Apple Pay receipt using
generateApplePayReceipt
. - Submit the receipt using
submitPaymentReceipt
once you have theintentId
.
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
-
(Optional) Configure Apple Pay
- Set desired billing data collection through
setApplePayConfiguration(_:)
.
- Set desired billing data collection through
-
Check Compatibility
- Confirm device compatibility by calling
moneyHash.isDeviceCompatible()
.
- Confirm device compatibility by calling
-
Approach #1
- Single-Step: Use
proceedWithApplePay
if you already have anintentId
. The Apple Pay receipt is submitted to MoneyHash right after payment.
- Single-Step: Use
-
Approach #2
- Two-Step:
generateApplePayReceipt
to display Apple Pay and get aNativePayReceipt
.submitPaymentReceipt
once you have theintentId
.
- Two-Step:
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.
Updated 30 days ago