Flutter iOS集成PhonePe Payment SDK遇HTTPClientError error 0求解决
Flutter集成PhonePe Payment SDK iOS端
PhonePePayment.HTTPClientError error 0错误解决方案 问题背景
在Flutter应用中集成phonepe_payment_sdk: ^1.0.4,Android端支付流程正常,但iOS端触发支付时出现PhonePePayment.HTTPClientError error 0错误。已配置info.plist相关项,并构造了Base64格式的交易请求体:
当前info.plist配置
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleURLSchemes</key> <array> <string>amzn-$(PRODUCT_BUNDLE_IDENTIFIER)</string> <string>juspay-$(PRODUCT_BUNDLE_IDENTIFIER)</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).cred</string> <string>paytm-$(PRODUCT_BUNDLE_IDENTIFIER)</string> </array> </dict> <dict> <key>CFBundleURLName</key> <string>com.PhonePe-iOS-Intent-SDK-Integration</string> <key>CFBundleURLSchemes</key> <array> <string>iOSIntentIntegration</string> </array> </dict> </array> <key>CFBundleVersion</key> <string>$(FLUTTER_BUILD_NUMBER)</string> <key>LSApplicationQueriesSchemes</key> <array> <string>ppemerchantsdkv1</string> <string>ppemerchantsdkv2</string> <string>ppemerchantsdkv3</string> <string>paytmmp</string> <string>gpay</string> <string>credpay</string> <string>phonepe</string> <string>tez</string> <string>paytm</string> <string>bhim</string> <string>devtools</string> <string>myairtel</string> </array>
交易请求体(Base64编码前)
body = { "merchantId": merchantId, "merchantTransactionId": uuid.v1().toString().substring(0, 18), "merchantUserId": user.userId, "amount": amount * 100, "redirectUrl": "https://webhook.site/redirect-url", "redirectMode": "REDIRECT", "callbackUrl": callBackUrl, "mobileNumber": user.phoneNumber, "paymentInstrument": { "type": "PAY_PAGE", } };
解决方案
1. 修复请求体JSON语法错误
请求体中paymentInstrument对象的最后一个键值对后多了一个逗号,属于无效JSON语法。iOS端对JSON格式校验更严格,会导致Base64编码后的请求体无效,触发HTTP错误。修改后的请求体如下:
body = { "merchantId": merchantId, "merchantTransactionId": uuid.v1().toString().substring(0, 18), "merchantUserId": user.userId, "amount": amount * 100, "redirectUrl": "https://webhook.site/redirect-url", "redirectMode": "REDIRECT", "callbackUrl": callBackUrl, "mobileNumber": user.phoneNumber, "paymentInstrument": { "type": "PAY_PAGE" } };
2. 补充PhonePe专属URL Scheme配置
当前CFBundleURLSchemes中缺少PhonePe回调所需的自定义Scheme,需添加与你的App Bundle ID关联的Scheme,并确保与商户后台配置一致:
在CFBundleURLTypes的第一个字典的CFBundleURLSchemes数组中添加:
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <string>phonepe-$(PRODUCT_BUNDLE_IDENTIFIER)</string>
同时,更新LSApplicationQueriesSchemes,添加SDK最新版本所需的Scheme:
<string>ppemerchantsdkv4</string>
3. 验证回调URL的有效性
- 确保
callbackUrl是可公开访问的HTTPS地址,且未被防火墙或安全策略拦截 - 若使用App内回调,需将
redirectUrl改为自定义Scheme格式(如your-bundle-id://payment-callback),并在info.plist中配置对应Scheme,同时在AppDelegate中实现URL回调处理逻辑:// AppDelegate.swift import UIKit import Flutter import phonepe_payment_sdk @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { if PhonePePaymentSdk.handleOpen(url) { return true } return super.application(app, open: url, options: options) } }
4. 检查ATS配置
iOS端需确保允许访问PhonePe的API域名,若使用测试环境,可临时添加ATS例外(生产环境建议配置具体域名):
在info.plist中添加:
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
5. 确认SDK初始化环境
确保iOS端初始化SDK时使用了正确的环境(测试/生产),测试环境需指向PhonePe的测试API地址,生产环境使用正式地址,避免环境不匹配导致请求失败。
内容的提问来源于stack exchange,提问作者J Bulls Infotech
相关产品推荐
相关产品推荐

