|
@@ -75,11 +75,11 @@ public class PurchaseManager: NSObject {
|
|
|
|
|
|
initializeForFree()
|
|
|
|
|
|
-#if DEBUG
|
|
|
- verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: true)
|
|
|
-#else
|
|
|
- verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: false)
|
|
|
-#endif
|
|
|
+//#if DEBUG
|
|
|
+// verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: true)
|
|
|
+//#else
|
|
|
+// verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: false)
|
|
|
+//#endif
|
|
|
|
|
|
}
|
|
|
|
|
@@ -400,8 +400,10 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
case .failed:
|
|
|
|
|
|
SKPaymentQueue.default().finishTransaction(transaction)
|
|
|
-
|
|
|
- if let error = transaction.error as NSError? {
|
|
|
+
|
|
|
+ //非购买次数类(就是订阅续费类)报错处理
|
|
|
+ if self.purchaseNumProducts.contains(transaction.payment.productIdentifier) == false,
|
|
|
+ let error = transaction.error as NSError? {
|
|
|
// 1. 检查内层错误
|
|
|
if let underlyingError = error.userInfo[NSUnderlyingErrorKey] as? NSError {
|
|
|
if underlyingError.domain == "ASDServerErrorDomain" && underlyingError.code == 3532 {
|
|
@@ -422,11 +424,21 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
}
|
|
|
|
|
|
// Transaction was cancelled or failed before being added to the server queue.
|
|
|
+// var message = "Payment Failed"
|
|
|
+// if let error = transaction.error as? SKError,
|
|
|
+// error.code == SKError.paymentCancelled {
|
|
|
+// message = "The subscription was canceled"
|
|
|
+// }
|
|
|
+
|
|
|
var message = "Payment Failed"
|
|
|
- if let error = transaction.error as? SKError,
|
|
|
- error.code == SKError.paymentCancelled {
|
|
|
- message = "The subscription was canceled"
|
|
|
+ if let error = transaction.error as? SKError{
|
|
|
+ if error.code == SKError.paymentCancelled {
|
|
|
+ message = "The subscription was canceled"
|
|
|
+ }else{
|
|
|
+ message = error.localizedDescription
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
purchase(self, didChaged: .payFail, object: message)
|
|
|
subscriptionApple(type: .result, jsonString: message)
|
|
|
case .restored:
|
|
@@ -728,4 +740,31 @@ extension PurchaseManager {
|
|
|
当用户决定购买某个产品后,根据产品信息(SKProduct对象)创建SKPayment对象,然后使用SKPaymentQueue的add(_:)方法将支付请求添加到支付队列。
|
|
|
同时,在应用启动等合适的时机,通过SKPaymentQueue的addTransactionObserver(_:)方法添加交易观察者。当支付状态发生变化时,paymentQueue(_:updatedTransactions:)方法会被调用,在这里可以根据交易状态(如购买成功、失败、恢复等)进行相应的处理。
|
|
|
|
|
|
+
|
|
|
+ func handleSKError(_ error: SKError) {
|
|
|
+ switch error.code {
|
|
|
+ case .paymentCancelled:
|
|
|
+ print("用户取消支付")
|
|
|
+ case .paymentNotAllowed:
|
|
|
+ print("设备不允许支付(如未登录 Apple ID)")
|
|
|
+ case .paymentInvalid:
|
|
|
+ print("商品 ID 无效或未配置")
|
|
|
+ case .storeProductNotAvailable:
|
|
|
+ print("商品在当前地区不可用")
|
|
|
+ default:
|
|
|
+ print("其他错误: \(error.localizedDescription)")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取详细的错误信息
|
|
|
+ print("错误域: \(error.errorDomain)")
|
|
|
+ print("错误码: \(error.errorCode)")
|
|
|
+ print("本地化描述: \(error.localizedDescription)")
|
|
|
+ print("失败原因: \(error.localizedFailureReason ?? "")")
|
|
|
+ }
|
|
|
+ if let userInfo = (error as NSError).userInfo {
|
|
|
+ print("完整错误信息: \(userInfo)")
|
|
|
+ // 示例输出:
|
|
|
+ // ["NSLocalizedDescription": "此设备不允许应用内购买",
|
|
|
+ // "NSLocalizedFailureReason": "未登录 Apple ID"]
|
|
|
+ }
|
|
|
*/
|