Airwallex logo

Prepare Payment Session

Copy for LLMView as Markdown

This page covers the required and optional setup steps for integrating the Airwallex iOS SDK.

Environment

When your app starts, configure the SDK with the appropriate mode.

swift
1Airwallex.setMode(.demoMode) // .demoMode, .stagingMode, .productionMode

Create a customer

Generate or retrieve a customer ID for your user on your server-side. Refer to the Airwallex API documentation API for more details.

Optional if you only support guest checkout.

Create payment intent

The payment intent is a required object for all transaction modes in the Airwallex iOS SDK. It represents a specific payment attempt and must be created before initiating a payment from the mobile app.

Create payment intent on your server-side and then pass the payment intent to the mobile-side to confirm the payment intent with the payment method selected.

Refer to the Airwallex API documentation API for details of the payment intent API.

While creating payment intent using payment_intents/create:

  • If amount = 0, only a payment consent will be created (no funds will be deducted).
  • If amount > 0, a payment consent will be created and a deduction will be made at the same time.
  • For guest checkout, customer_id parameter can be omitted.

Create payment session object

Option 1: Initialize with a pre-created payment intent

swift
1let paymentConsentOptions = if /* one-off transaction */ {
2 nil
3} else {
4 /* recurring transaction */
5 PaymentConsentOptions(
6 nextTriggeredBy: ".customer/.merchant",
7 merchantTriggerReason: "nil/.scheduled/.unscheduled/...."
8 )
9}
10let session = Session(
11 paymentIntent: paymentIntent, // payment intent created on your server
12 countryCode: "Your country code",
13 applePayOptions: applePayOptions, // required if you want to support apple pay
14 autoCapture: true, // Only applicable for card payment. If true the payment will be captured immediately after authorization succeeds.
15 billing: billing, // prefilled billing address
16 paymentConsentOptions: paymentConsentOptions, // info for recurring transactions
17 requiredBillingContactFields: [.name, .email], // customize billing contact fields for card payment
18 returnURL: "myapp://payment/return" // App return url
19)

Option 2: Initialize with a payment intent provider (Express Checkout)

Using a PaymentIntentProvider allows the SDK to delay payment intent creation until just before payment confirmation or when clientSecret is required to request some Airwallex API.

swift
1// 1. Implement PaymentIntentProvider
2class MyPaymentIntentProvider: NSObject, PaymentIntentProvider {
3 let amount = NSDecimalNumber(string: "99.99")
4 let currency: String = "USD"
5 let customerId: String? = "customer_123"
6
7 func createPaymentIntent() async throws -> AWXPaymentIntent {
8 // Call your backend to create the payment intent
9 let response = try await MyBackendAPI.createPaymentIntent(
10 amount: amount,
11 currency: currency,
12 customerId: customerId
13 )
14 return response.paymentIntent
15 }
16}
17
18// 2. Create session with the provider
19let provider = MyPaymentIntentProvider()
20let session = Session(
21 paymentIntentProvider: provider, // Payment intent will be created when needed
22 countryCode: "US"
23)

WeChat Pay

To enable WeChat Pay:

  • Make sure you add dependency for AirwallexWeChatPay (Swift Package Manager) or Airwallex/AirwallexWechatPay (CocoaPods)
  • Set up WechatOpenSDK following the WeChat documentation
swift
1class AppDelegate: UIResponder, UIApplicationDelegate {
2 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3 WXApi.registerApp("WeChat app ID", universalLink: "universal link of your app")
4 return true
5 }
6
7 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
8 return WXApi.handleOpen(url, delegate: self)
9 }
10}
11
12extension AppDelegate: WXApiDelegate {
13 func onResp(_ resp: BaseResp) {
14 if let response = resp as? PayResp {
15 switch response.errCode {
16 // handle payment result
17 }
18 }
19 }
20}

After completing payment, WeChat will be redirected to the merchant's app and do a callback using onResp(), then it can retrieve the payment intent status after the merchant server is notified, so keep listening to the notification.

We use internal dynamic framework WechatOpenSDKDynamic.xcframework for WeChat Pay integration, which is a dynamic framework built from original WechatOpenSDK.xcframework 2.0.4. By doing this, we can remove unsafe flag -ObjC, -all_load from SPM target AirwallexWeChatPay and strip architecture armv7 and i386 which is no longer needed for modern apps.

Apple Pay

To enable Apple Pay:

  • Make sure Apple Pay is set up correctly in the app. For more information, refer to Apple's official documentation .
  • Make sure Apple Pay is enabled on your Airwallex account.
  • Prepare the Merchant Identifier and configure applePayOptions on the payment session object.

You can customize the Apple Pay options to restrict it as well as provide extra context. For more information, refer to the AWXApplePayOptions.h header file.

swift
1let options = AWXApplePayOptions(merchantIdentifier: applePayMerchantId)
2options.additionalPaymentSummaryItems = [
3 .init(label: "goods", amount: 10),
4 .init(label: "tax", amount: 1)
5]
6options.merchantCapabilities = [.threeDSecure, .debit]
7options.requiredBillingContactFields = [.postalAddress]
8options.supportedCountries = ["AU"]
9options.totalPriceLabel = "COMPANY, INC."
10
11let session = Session(
12 // ...
13 applePayOptions: options // required for Apple Pay
14)

We currently support the following payment networks for Apple Pay: Visa, MasterCard, ChinaUnionPay, Maestro, Amex, Discover, and JCB. Coupon is not supported at this stage.

Was this page helpful?