Search code examples
swiftxcodein-app-purchasestorekit

Getting Donations With In-App Purchases with Swift


I'm planning to implement In-App Purchases as a support for app development. I've made an consumable IAP in AppStore connect, and I've used this Code

    @IBAction func supportDevelopmentButtonHasBeenTapped(_ sender: Any) {
    if SKPaymentQueue.canMakePayments() {
        let paymentReq = SKMutablePayment()
        paymentReq.productIdentifier = productID
        SKPaymentQueue.default().add(paymentReq)
    } else {
        print("user cannot make payment")
    }
}

and this function:

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        if transaction.transactionState == .purchased {
            let thanksAlert = UIAlertController(title: "Thanks!", message: "Thanks For Your Support", preferredStyle: UIAlertController.Style.alert)
            thanksAlert(UIAlertAction(title: "Back", style: UIAlertAction.Style.default, handler: nil))
            self.present(thanksAlert, animated: true, completion: nil)
            return 
            
        } else if transaction.transactionState == .failed {
            print("")
        }
    }
}

My issue her is the user cannot purchase more than one time, how am I able to let the user to support as much as he want?


Solution

  • I think you need call in "transaction.transactionState == .purchased"

    SKPaymentQueue.default().finishTransaction(transaction)