-0.4 C
New York
Saturday, February 22, 2025

ios – an unknown error occurred when processing funds by means of storekit2


Here is the code that I exploit to buy subscriptions utilizing StoreKit2. Generally I get an error saying “an unknown error occurred” and the cost fails to undergo. Can somebody please assist? Is that this the fitting code?

func purchaseProduct(product: Product, supply: String) async -> Bool {
        do {
            // Begin the acquisition
            let end result = strive await product.buy()
            
            // Deal with the results of the acquisition
            swap end result {
            case .success(let verificationResult):
                swap verificationResult {
                case .verified(let transaction):
                    self.transactionState = "Buy Profitable"
                    await transaction.end()
                    return true
                case .unverified(let transaction, let error):
                    self.transactionState = "Buy Unverified: (error.localizedDescription)"
                    await transaction.end()
                  
                    DispatchQueue.major.async {
                        showMessageWithTitle("Error!", "There was an error processing your buy", .error)
                        
                        Amplitude.sharedInstance.observe(
                            eventType: "payment_failed",
                            eventProperties: ["PlanId": product.id, "Source": source, "Error": error.localizedDescription]
                        )
                    }
                    return false
                }
            case .userCancelled:
                self.transactionState = "Person cancelled the acquisition."
               
                DispatchQueue.major.async {
                    Amplitude.sharedInstance.observe(
                        eventType: "payment_cancelled",
                        eventProperties: ["PlanId": product.id, "Source": source]
                    )
                }
                return false
                
            case .pending:
                self.transactionState = "Buy is pending."
                
                DispatchQueue.major.async {
                    showMessageWithTitle("Error!", "There was an error processing your buy", .error)
                }
                return false
                
            @unknown default:
                self.transactionState = "Unknown buy end result."
               
                DispatchQueue.major.async {
                    showMessageWithTitle("Error!", "There was an error processing your buy", .error)
                    
                    Amplitude.sharedInstance.observe(
                        eventType: "payment_failed",
                        eventProperties: ["PlanId": product.id, "Source": source, "Error": "unknown"]
                    )
                }
                
                return false
            }
        } catch {
            self.transactionState = "Buy failed: (error.localizedDescription)"
        
            DispatchQueue.major.async {
                showMessageWithTitle("Error!", "There was an error processing your buy", .error)
                
                Amplitude.sharedInstance.observe(
                    eventType: "payment_failed",
                    eventProperties: ["PlanId": product.id, "Source": source, "Error": error.localizedDescription]
                )
            }
            return false
        }
    }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles