paymentQueue - 交易失败

时间:2017-07-30 23:42:21

标签: ios swift swift3 in-app-purchase

我正在使用应用内购买构建应用。该应用程序有6个待售产品(所有耗材)。

该应用程序能够使用以下代码识别可供购买的产品(来自itunes connect):

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    //Apple Store
    appleStoreProducts = [productID, product5ID, product25ID, product100ID, product500ID, product1000ID]
    SKPaymentQueue.default().add(self)
    self.getProductInfo()
}

func getProductInfo() {
    if SKPaymentQueue.canMakePayments() {

        for product in appleStoreProducts {
         let request = SKProductsRequest(productIdentifiers: NSSet(objects: product) as! Set<String>)
        request.delegate = self
        request.start()
        }
    } else {
        // Tell user that In-App Purchase is disabled in settings

        print("In-App Purchase is disabled in settings")
    }
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

    var products = response.products

    if (products.count != 0) {
        let newProduct = products[0]
        switch newProduct.localizedTitle {
        case "DOLLAR":
            product = newProduct
        case "five dollars":
            product2 = newProduct
        case "twenty five":
            product3 = newProduct
        case "one hundred dollars":
            product4 = newProduct
        case "thousand dollars":
            product5 = newProduct
        default:
            print("none of the above")
        }

        print("titile : \(newProduct.localizedTitle) \n description: \(newProduct.localizedDescription)")
    }
    else {
        print("Product not found")
    }

    let invalids = response.invalidProductIdentifiers

    for product in invalids {
        print("Product not found: \(product)")
    }
}

然后,您可以单击按钮购买其中一个项目,例如:

  @IBAction func productOne(_ sender: Any) {
    let payment = SKPayment(product: product!)
    SKPaymentQueue.default().add(payment)
}

当调用paymentQueue方法时,接下来会出现我的问题:

  func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    for transaction in transactions {

        switch transaction.transactionState {

        case SKPaymentTransactionState.purchased:
        //  self.pointsBought()
            SKPaymentQueue.default().finishTransaction(transaction)

        case SKPaymentTransactionState.failed:
             SKPaymentQueue.default().finishTransaction(transaction)
            print("transaction failed")
        default:
            print("ESTDO:  \(transaction.transactionState)")
            break
        }

    }
}

单击一次按钮后,此方法被调用两次。 transactionState第一次既没有“购买”也没有“失败”,因此它调用默认选项。第二次结果是“失败”。但是,我不知道我做错了什么。

相关资料: 我正在使用iOS设备(iphone 6)来运行该应用程序 我正在使用我的iphone中的开发者帐户(应用程序)

1 个答案:

答案 0 :(得分:3)

您未能涵盖所有案例。您需要涵盖所有这些内容,并且在某些情况下需要致电finishTransaction。这是正确的updatedTransactions方法的格式:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for t in transactions {
        switch t.transactionState {
        case .purchasing, .deferred: break // do nothing
        case .purchased, .restored:
            let p = t.payment
            if p.productIdentifier == whatever {
                // ... do stuff ...
                queue.finishTransaction(t)
            }
        case .failed:
            queue.finishTransaction(t)
        }
    }
}