iOS应用PassKit框架集成Apple Pay未完成问题求助
Hey there, let's dig into fixing that frustrating "Apple pay not completed" error you're facing. I’ve worked through plenty of Apple Pay integration kinks before, so let’s break this down step by step:
1. Fix Unsafe Amount Parsing (Your Current Code Risk)
Your current code uses forced unwrapping and simple string replacement for the total amount—this is a common crash/error trigger if the label has unexpected characters (like spaces, commas, or is nil). Replace that with safe, robust parsing:
guard let rawTotalText = grandTotalLabel.text, let cleanedTotalText = rawTotalText.replacingOccurrences(of: "$", with: "").trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty, let totalAmount = Decimal(string: cleanedTotalText) else { print("Invalid total amount format—check grandTotalLabel content") return }
This avoids crashes and ensures you’re passing a valid numeric value to Apple Pay.
2. Verify Your PKPaymentRequest is Fully Configured
Your snippet only checks payment network support, but a valid PKPaymentRequest requires several mandatory fields. Make sure you’re setting these correctly:
let paymentRequest = PKPaymentRequest() // Must match the Merchant ID you registered in App Store Connect paymentRequest.merchantIdentifier = "com.yourcompany.yourapp.merchant" // Match your test region (e.g., "US" for United States) paymentRequest.countryCode = "US" // Match your test currency (e.g., "USD" for US Dollars) paymentRequest.currencyCode = "USD" paymentRequest.supportedNetworks = [PKPaymentNetwork.amex] // Sandbox requires this capability for most test scenarios paymentRequest.merchantCapabilities = .capability3DS // Add a valid payment summary item (required for Apple Pay to proceed) let totalSummaryItem = PKPaymentSummaryItem( label: "Order Total", amount: NSDecimalNumber(decimal: totalAmount), type: .final ) paymentRequest.paymentSummaryItems = [totalSummaryItem]
Double-check that your merchantIdentifier exactly matches the one you created in your Apple Developer account—even a tiny typo will break the flow.
3. Sandbox Environment Sanity Checks
Since you’re using a sandbox account, confirm these critical details:
- The test card you added to Wallet is an Amex sandbox card (Apple’s official test cards are network-specific; you can’t use a Visa test card with
PKPaymentNetwork.amex). - Your device is signed into the sandbox Apple ID you created in App Store Connect (Wallet’s test card must be linked to this account).
- Ensure your device supports Apple Pay (check Settings > Wallet & Apple Pay to confirm the feature is enabled).
4. Capture Specific Error Details
The generic "Apple pay not completed" message isn’t helpful. Implement the delegate method to get precise error info:
extension YourViewController: PKPaymentAuthorizationViewControllerDelegate { func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didFailWithError error: Error) { print("Apple Pay Error Details: \(error.localizedDescription)") print("Error Code: \(error._code)") controller.dismiss(animated: true) } // Don't forget the success delegate method too: func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) { controller.dismiss(animated: true) } }
This will tell you exactly what’s failing—whether it’s an invalid merchant ID, unsupported network, or malformed amount.
5. Eliminate Forced Unwrapping
Any forced ! in your code (like self.grandTotalLabel.text!) can crash the app mid-payment flow, leading to the vague "not completed" error. Use optional binding for all UI element access and data parsing.
Start by adding the error capture delegate method first—it’ll give you the most direct clue about what’s going wrong. Then work through the other checks based on the error message you get.
内容的提问来源于stack exchange,提问作者ajeet sharma

