From ad1487afdff94dd6e2ab147ad331d4e0cc0082c7 Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Mon, 7 Dec 2020 08:56:57 +0100 Subject: [PATCH] feat: Add server request to register in-app purchase Resolves: https://gitlab.steamware.net/eqn/eqn.ios/-/issues/29 --- Sources/Earthquake Network/Costanti.h | 3 +++ .../Earthquake Network/Models/IAPHelper.swift | 19 +++++++++++++++++++ .../Server Requests/EQNGeneratoreURLServer.h | 1 + .../Server Requests/EQNGeneratoreURLServer.m | 7 +++++++ .../Server Requests/ServerRequest.m | 1 + 5 files changed, 31 insertions(+) diff --git a/Sources/Earthquake Network/Costanti.h b/Sources/Earthquake Network/Costanti.h index e5a2b77..ac76a9a 100644 --- a/Sources/Earthquake Network/Costanti.h +++ b/Sources/Earthquake Network/Costanti.h @@ -35,6 +35,8 @@ static NSString * const EQNServerUrlDownloadRetiSismiche = @"https://srv.earthqu static NSString * const EQNServerUrlOfferTimeRemaining = @"https://srv.earthquakenetwork.it/distquake_download_offer_time_remaining.php"; /// Recupera il numero di sottoscrizioni ancora disponibili per ogni prodotto static NSString * const EQNServerUrlAvailableSubscriptionsCounter = @"https://srv.earthquakenetwork.it/distquake_count_top_redis.php"; +/// Registra l'abbonamento acquistato dall'utente +static NSString * const EQNServerUrlRegisterSubscription = @"https://srv.earthquakenetwork.it/distquake_upload_subscription.php"; /// Carica le impostazioni delle notifiche definite dall'utente static NSString * const EQNServerUrlUploadSettings = @"https://srv.earthquakenetwork.it/distquake_upload_settings_ios.php"; // URL EQNTipoChiamataRegistrazione server @@ -132,6 +134,7 @@ typedef NS_ENUM(NSInteger, EQNTipoChiamata) { EQNTipoChiamataCommentoTerremoto, EQNTipoChiamataImpostazioniNotifiche, EQNTipoChiamataOfferTimeRemaining, + EQNTipoChiamataRegisterSubscription, EQNTipoChiamataAlertPushTest, EQNTipoChiamataAlertSimulator }; diff --git a/Sources/Earthquake Network/Models/IAPHelper.swift b/Sources/Earthquake Network/Models/IAPHelper.swift index 3fff4d7..e7da81b 100644 --- a/Sources/Earthquake Network/Models/IAPHelper.swift +++ b/Sources/Earthquake Network/Models/IAPHelper.swift @@ -174,6 +174,9 @@ extension IAPHelper: SKPaymentTransactionObserver { print("[IAPHelper] Handle purchased transaction (product: \(transaction.payment.productIdentifier))") deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier) SKPaymentQueue.default().finishTransaction(transaction) + + // register subscription + serverPurchaseRegistration(for: transaction.payment.productIdentifier, transactionId:transaction.transactionIdentifier) } private func handleRestored(transaction: SKPaymentTransaction) { @@ -185,6 +188,9 @@ extension IAPHelper: SKPaymentTransactionObserver { print("[IAPHelper] Handle restored transaction (product: \(productIdentifier))") deliverPurchaseNotificationFor(identifier: productIdentifier) SKPaymentQueue.default().finishTransaction(transaction) + + // register subscription + serverPurchaseRegistration(for: productIdentifier, transactionId: transaction.transactionIdentifier) } private func handleFailed(transaction: SKPaymentTransaction) { @@ -206,4 +212,17 @@ extension IAPHelper: SKPaymentTransactionObserver { UserDefaults.standard.set(true, forKey: identifier) NotificationCenter.default.post(name: .IAPHelperPurchaseNotification, object: identifier) } + + private func serverPurchaseRegistration(for productId: String, transactionId: String?) { + let idTransaction = transactionId ?? "NOT_AVAILABLE" + + let url = EQNGeneratoreURLServer.urlRegisterSubscription(forProductId: productId, transactionId: idTransaction) + ServerRequest.default().inviaInformazioniAlServer(with: url, richiesta: .registerSubscription) { (response) in + // nope + print("[IAPHelper] Subscription registered with server") + } failure: { (error) in + // nope + print("[IAPHelper] Error when registering subscription (error: \(String(describing: error?.localizedDescription))") + } + } } diff --git a/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.h b/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.h index 949b247..854b7ad 100644 --- a/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.h +++ b/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.h @@ -22,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN + (NSURL *)urlInvioCommentoTerremoto:(NSString *)commento codeMessage:(NSString *)code; + (NSURL *)urlInvioImpostazioniNotifiche; + (NSURL *)urlDownloadOfferTimeRemaining; ++ (NSURL *)urlRegisterSubscriptionForProductId:(NSString *)productId transactionId:(NSString *)transactionId; + (NSURL *)urlAlertPushTest; + (NSURL *)urlAlertSimulator; diff --git a/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.m b/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.m index f84c27b..22b59ef 100644 --- a/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.m +++ b/Sources/Earthquake Network/Server Requests/EQNGeneratoreURLServer.m @@ -243,6 +243,13 @@ return [NSURL URLWithString:stringUrl]; } ++ (NSURL *)urlRegisterSubscriptionForProductId:(NSString *)productId transactionId:(NSString *)transactionId +{ + NSString *walletSub = @""; // codice associato all'acquisto che conosce anche l'utente (se esiste) + NSString *stringUrl = [NSString stringWithFormat:@"%@?u_id=%@&token_sub=%@&wallet_sub=%@&sku=%@&ios=1", EQNServerUrlRegisterSubscription, [EQNUser defaultUser].user_ID, transactionId, walletSub, productId]; + return [NSURL URLWithString:stringUrl]; +} + + (NSURL *)urlAlertPushTest { EQNGenericValue *radius = [EQNData raggioSismaFor:[EQNAllertaSismica sharedInstance].raggioSismiLievi]; diff --git a/Sources/Earthquake Network/Server Requests/ServerRequest.m b/Sources/Earthquake Network/Server Requests/ServerRequest.m index 71ac1ca..5fcfc25 100644 --- a/Sources/Earthquake Network/Server Requests/ServerRequest.m +++ b/Sources/Earthquake Network/Server Requests/ServerRequest.m @@ -140,6 +140,7 @@ case EQNTipoChiamataCalibrazione: case EQNTipoChiamataImpostazioniNotifiche: case EQNTipoChiamataAlertSimulator: + case EQNTipoChiamataRegisterSubscription: onSuccess(newStr); default: // don't call the callback