You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 3下Braintree Drop-in UI集成Apple Pay技术求助

Integrating Apple Pay with Braintree Drop-in UI in Swift 3

Hey there! I’ve helped set up Apple Pay with Braintree’s Drop-in UI in Swift 3 before, so let’s get this working for you. The good news is that Drop-in handles most of the heavy lifting—you just need to add a few configuration steps to your existing code.

Step 1: Confirm Your Apple Pay Setup

First, double-check the following (since you mentioned you’ve already gone through the configuration guide):

  • Your merchant ID is correctly registered in Apple Developer Portal and linked to your Braintree account
  • You’ve added the Apple Pay entitlement to your Xcode project
  • Your simulator has a test Apple Pay card added (you can do this via Settings > Wallet & Apple Pay)

Step 2: Modify Your Drop-in Request to Include Apple Pay

Update your pay(_:) method to configure the Apple Pay settings in the BTDropInRequest. Here’s how your updated code will look:

import PassKit // Don't forget to add this import for Apple Pay classes

@IBAction func pay(_ sender: Any) {
    // 1. Create a basic Drop-in request
    let request = BTDropInRequest()
    
    // 2. Configure Apple Pay for the request
    let applePayRequest = PKPaymentRequest()
    applePayRequest.merchantIdentifier = "your.merchant.id" // Replace with your actual merchant ID
    applePayRequest.countryCode = "US" // Adjust to your country code (e.g., "GB" for UK)
    applePayRequest.currencyCode = "USD" // Adjust to your currency code (e.g., "GBP" for British Pounds)
    
    // 3. Validate and set the payment amount for Apple Pay
    guard let amountText = amountTextField.text, 
          let amount = NSDecimalNumber(string: amountText), 
          amount.compare(NSDecimalNumber.zero) == .orderedDescending else {
        self.show(message: "Please enter a valid amount")
        return
    }
    
    // 4. Set the payment summary that appears in the Apple Pay sheet
    let paymentSummaryItem = PKPaymentSummaryItem(
        label: "Your Organization Name", 
        amount: amount, 
        type: .final
    )
    applePayRequest.paymentSummaryItems = [paymentSummaryItem]
    
    // 5. Assign the Apple Pay request to the Drop-in request
    request.applePayPaymentRequest = applePayRequest
    
    // 6. Present the Drop-in controller as before
    let dropIn = BTDropInController(authorization: toKinizationKey, request: request) { [unowned self] (controller, result, error) in
        if let error = error {
            self.show(message: error.localizedDescription)
        } else if (result?.isCancelled == true) {
            self.show(message: "Transaction Cancelled")
        } else if let nonce = result?.paymentMethod?.nonce, let amount = self.amountTextField.text {
            // Reuse your existing payment logic—Apple Pay nonces work the same way as credit card nonces!
            self.sendRequestPaymentToServer(nonce: nonce, amount: amount)
        }
        controller.dismiss(animated: true, completion: nil)
    }
    
    self.present(dropIn!, animated: true, completion: nil)
}

Key Notes:

  • Merchant ID: Replace "your.merchant.id" with the exact merchant ID you registered with Apple and Braintree.
  • Payment Summary: The label in PKPaymentSummaryItem will display in the Apple Pay sheet, so use your organization’s name here for clarity.
  • Nonce Handling: You don’t need to modify your sendRequestPaymentToServer method at all! Braintree’s Drop-in returns a valid payment method nonce for Apple Pay transactions, just like it does for credit cards and PayPal. Your server-side processing stays identical.
  • Swift 3 Compatibility: This code is tailored for Swift 3 and works with Braintree v4 SDKs, matching your existing implementation.

Troubleshooting Tips

If Apple Pay doesn’t appear in the Drop-in UI:

  • Ensure your simulator is running iOS 10 or later (Apple Pay support in Drop-in requires this)
  • Verify that your Braintree sandbox account has Apple Pay enabled for your merchant ID
  • Check that your Xcode project’s entitlements include the correct merchant ID
  • Confirm your test Apple Pay card is added to the simulator’s Wallet

That should do it! Once you add this configuration, the Drop-in UI will automatically display the Apple Pay option when available, and your existing payment flow will handle the rest.

内容的提问来源于stack exchange,提问作者Mamta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:35:04