From 468ea5a51a28476a5a8de6d8ea8ef0a3bd89e500 Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Tue, 25 Jul 2023 15:08:41 +0530 Subject: [PATCH 1/9] Handling Next_offset issue While retrieving Subscriptions. --- .../Classes/Configuration/Chargebee.swift | 4 ++-- .../Classes/Subscription/CBSubscription.swift | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Chargebee/Classes/Configuration/Chargebee.swift b/Chargebee/Classes/Configuration/Chargebee.swift index a9601fc..23154be 100644 --- a/Chargebee/Classes/Configuration/Chargebee.swift +++ b/Chargebee/Classes/Configuration/Chargebee.swift @@ -39,10 +39,10 @@ public class Chargebee { CBSubscriptionManager().retrieveSubscription(network: request, logger: logger, handler: handler) } - public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping SubscriptionHandler) { + public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping SubscriptionHandler2) { let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId") logger.info() - + let request = CBAPIRequest(resource: SubscriptionResource(queryParams: queryParams)) CBSubscriptionManager().retrieveSubscriptions(network: request, logger: logger, handler: handler) } diff --git a/Chargebee/Classes/Subscription/CBSubscription.swift b/Chargebee/Classes/Subscription/CBSubscription.swift index 3cd128a..4ea04fa 100644 --- a/Chargebee/Classes/Subscription/CBSubscription.swift +++ b/Chargebee/Classes/Subscription/CBSubscription.swift @@ -19,7 +19,7 @@ public struct SubscriptionList: Codable { public struct CBSubscriptionWrapper: Codable { public let list: [SubscriptionList] - public let nextOffset: [String]? + public let nextOffset: String? enum CodingKeys: String, CodingKey { case nextOffset = "next_offset" case list @@ -58,6 +58,8 @@ public struct Subscription: Codable { public typealias CBSubscriptionHandler = (CBResult) -> Void public typealias SubscriptionHandler = (CBResult<[SubscriptionList]>) -> Void +public typealias SubscriptionHandler2 = (CBResult) -> Void + class CBSubscriptionManager { func retrieveSubscription(network: T, logger: CBLogger, handler: @escaping CBSubscriptionHandler) { @@ -69,14 +71,14 @@ class CBSubscriptionManager { }, onError: onError) } - func retrieveSubscriptions(network: T, logger: CBLogger, handler: @escaping SubscriptionHandler) { + func retrieveSubscriptions(network: T, logger: CBLogger, handler: @escaping SubscriptionHandler2) { let (onSuccess, onError) = CBResult.buildResultHandlers(handler, logger) network.load(withCompletion: { status in if let data = status as? CBSubscriptionWrapper { if data.list.isEmpty { onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found")) }else { - onSuccess(data.list) + onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset)) } } else { onError(CBError.defaultSytemError(statusCode: 480, message: "json serialization failure")) @@ -85,6 +87,16 @@ class CBSubscriptionManager { }, onError: onError) } + func queryParamSanitizer(queryParam:[String:String]) -> [String:String] { + var params = [String:String]() + if queryParam.isEmpty{ + for (key, value) in queryParam { + debugPrint("Key:\(key) , value:\(value)") + } + } + return params + } + } extension String { From 9cce7e571ac5839f0ba9698570e21ca591c97b28 Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Fri, 11 Aug 2023 15:14:57 +0530 Subject: [PATCH 2/9] Next_offset value is not handle hence response is not getting parsed properly its handled now new handler and old method mentioned as deprecated --- .../Classes/Configuration/Chargebee.swift | 12 ++- .../Classes/Subscription/CBSubscription.swift | 31 ++++---- ...BSDKSubscriptionStatusViewController.swift | 76 +++++++++++++++---- 3 files changed, 91 insertions(+), 28 deletions(-) diff --git a/Chargebee/Classes/Configuration/Chargebee.swift b/Chargebee/Classes/Configuration/Chargebee.swift index 23154be..aa8c7a8 100644 --- a/Chargebee/Classes/Configuration/Chargebee.swift +++ b/Chargebee/Classes/Configuration/Chargebee.swift @@ -39,7 +39,17 @@ public class Chargebee { CBSubscriptionManager().retrieveSubscription(network: request, logger: logger, handler: handler) } - public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping SubscriptionHandler2) { + + @available(*, deprecated, message: "This will be removed in upcoming versions, Please use this API func retrieveSubscriptionsList(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler)") + public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping SubscriptionHandler) { + let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId") + logger.info() + + let request = CBAPIRequest(resource: SubscriptionResource(queryParams: queryParams)) + CBSubscriptionManager().retrieveSubscriptions(network: request, logger: logger, handler: handler) + } + + public func retrieveSubscriptionsList(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler) { let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId") logger.info() diff --git a/Chargebee/Classes/Subscription/CBSubscription.swift b/Chargebee/Classes/Subscription/CBSubscription.swift index 4ea04fa..8f21030 100644 --- a/Chargebee/Classes/Subscription/CBSubscription.swift +++ b/Chargebee/Classes/Subscription/CBSubscription.swift @@ -58,7 +58,7 @@ public struct Subscription: Codable { public typealias CBSubscriptionHandler = (CBResult) -> Void public typealias SubscriptionHandler = (CBResult<[SubscriptionList]>) -> Void -public typealias SubscriptionHandler2 = (CBResult) -> Void +public typealias RetrieveSubscriptionHandler = (CBResult) -> Void class CBSubscriptionManager { @@ -71,14 +71,14 @@ class CBSubscriptionManager { }, onError: onError) } - func retrieveSubscriptions(network: T, logger: CBLogger, handler: @escaping SubscriptionHandler2) { + func retrieveSubscriptions(network: T, logger: CBLogger, handler: @escaping SubscriptionHandler) { let (onSuccess, onError) = CBResult.buildResultHandlers(handler, logger) network.load(withCompletion: { status in if let data = status as? CBSubscriptionWrapper { if data.list.isEmpty { - onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found")) - }else { - onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset)) + onError(CBError.defaultSytemError(statusCode: 480, message: "Subscription Not found")) + }else{ + onSuccess(data.list) } } else { onError(CBError.defaultSytemError(statusCode: 480, message: "json serialization failure")) @@ -87,16 +87,21 @@ class CBSubscriptionManager { }, onError: onError) } - func queryParamSanitizer(queryParam:[String:String]) -> [String:String] { - var params = [String:String]() - if queryParam.isEmpty{ - for (key, value) in queryParam { - debugPrint("Key:\(key) , value:\(value)") + func retrieveSubscriptions(network: T, logger: CBLogger, handler: @escaping RetrieveSubscriptionHandler) { + let (onSuccess, onError) = CBResult.buildResultHandlers(handler, logger) + network.load(withCompletion: { status in + if let data = status as? CBSubscriptionWrapper { + if data.list.isEmpty { + onError(CBError.defaultSytemError(statusCode: 480, message: "Subscription Not found")) + }else { + onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset)) + } + } else { + onError(CBError.defaultSytemError(statusCode: 480, message: "json serialization failure")) } - } - return params - } + }, onError: onError) + } } extension String { diff --git a/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift b/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift index 2bcfee1..3c2bbaa 100644 --- a/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift +++ b/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift @@ -57,37 +57,85 @@ final class CBSDKSubscriptionStatusViewController: UIViewController { } @IBAction func getStatusUsingCustomerId(_ sender: Any) { - self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5)) - + callRetreiveSubscriptions() + } + + func callRetreiveSubscriptions(){ + self.activityStartAnimating() + guard let id = subscriptioniDTextField.text, id.isNotEmpty else { + return + } + Chargebee.shared.retrieveSubscriptionsList(queryParams: ["customer_id": id,"offset":""]) { result in + switch result { + case let .success(result): + debugPrint("Subscription Status Fetched: \(result)") + if let value = result.nextOffset { + let offset = value + DispatchQueue.main.async { + self.getSubcription(offset: offset) + } + }else{ + DispatchQueue.main.async { + if let status = result.list.first?.subscription.status, let amount = result.list.first?.subscription.planAmount { + let alertController = UIAlertController(title: "Chargebee", message: "Status :\(status)\n Plan amount:\(amount).", preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) + self.present(alertController, animated: true, completion: nil) + } + } + } + self.activityStopAnimating() + case let .error(error): + debugPrint("Error Fetched: \(error)") + self.showError(error: error) + } + } + } + + func getSubcription(offset: String){ + self.activityStartAnimating() guard let id = subscriptioniDTextField.text, id.isNotEmpty else { return } - //Sample Query Params - //subscription_id ="id" - //status = "active" - Chargebee.shared.retrieveSubscriptions(queryParams: ["customer_id": id]) { result in + + Chargebee.shared.retrieveSubscriptionsList(queryParams: ["customer_id": id,"offset":offset]) { result in switch result { case let .success(result): debugPrint("Subscription Status Fetched: \(result)") DispatchQueue.main.async { - if let status = result.first?.subscription.status, let amount = result.first?.subscription.planAmount { + if let status = result.list.first?.subscription.status, let amount = result.list.first?.subscription.planAmount { let alertController = UIAlertController(title: "Chargebee", message: "Status :\(status)\n Plan amount:\(amount).", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) self.present(alertController, animated: true, completion: nil) } - self.view.activityStopAnimating() } + self.activityStopAnimating() case let .error(error): debugPrint("Error Fetched: \(error)") - DispatchQueue.main.async { - self.view.activityStopAnimating() - self.statusLabel.text = error.localizedDescription - self.subscriptioniDTextField.resignFirstResponder() - - } + self.showError(error: error) + } } } + + func activityStartAnimating(){ + DispatchQueue.main.async { + self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5)) + } + } + func activityStopAnimating(){ + DispatchQueue.main.async { + self.view.activityStopAnimating() + } + } + + func showError(error:Error){ + DispatchQueue.main.async { + self.view.activityStopAnimating() + self.statusLabel.text = error.localizedDescription + self.subscriptioniDTextField.resignFirstResponder() + } + self.activityStopAnimating() + } @IBAction private func textFieldDidEndEdit(_ sender: UITextField) { enableFetchButton(shouldEnable: sender.isNotEmpty) } From e66e396059ffe4768bfe27c67ec4ee4ef96526de Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Fri, 11 Aug 2023 15:27:18 +0530 Subject: [PATCH 3/9] Reverted Error code --- Chargebee/Classes/Subscription/CBSubscription.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chargebee/Classes/Subscription/CBSubscription.swift b/Chargebee/Classes/Subscription/CBSubscription.swift index 8f21030..811e5fe 100644 --- a/Chargebee/Classes/Subscription/CBSubscription.swift +++ b/Chargebee/Classes/Subscription/CBSubscription.swift @@ -76,7 +76,7 @@ class CBSubscriptionManager { network.load(withCompletion: { status in if let data = status as? CBSubscriptionWrapper { if data.list.isEmpty { - onError(CBError.defaultSytemError(statusCode: 480, message: "Subscription Not found")) + onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found")) }else{ onSuccess(data.list) } @@ -92,7 +92,7 @@ class CBSubscriptionManager { network.load(withCompletion: { status in if let data = status as? CBSubscriptionWrapper { if data.list.isEmpty { - onError(CBError.defaultSytemError(statusCode: 480, message: "Subscription Not found")) + onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found")) }else { onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset)) } From 65fa743daf15c3721968dae95648bd82822636a4 Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 15:53:13 +0530 Subject: [PATCH 4/9] Renamed retrieveSubscriptionsList to retrieveSubscriptions as Existing name for new method also --- Chargebee/Classes/Configuration/Chargebee.swift | 2 +- Example/Chargebee/CBSDKSubscriptionStatusViewController.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Chargebee/Classes/Configuration/Chargebee.swift b/Chargebee/Classes/Configuration/Chargebee.swift index aa8c7a8..04c83b0 100644 --- a/Chargebee/Classes/Configuration/Chargebee.swift +++ b/Chargebee/Classes/Configuration/Chargebee.swift @@ -49,7 +49,7 @@ public class Chargebee { CBSubscriptionManager().retrieveSubscriptions(network: request, logger: logger, handler: handler) } - public func retrieveSubscriptionsList(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler) { + public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler) { let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId") logger.info() diff --git a/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift b/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift index 3c2bbaa..0346043 100644 --- a/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift +++ b/Example/Chargebee/CBSDKSubscriptionStatusViewController.swift @@ -65,7 +65,7 @@ final class CBSDKSubscriptionStatusViewController: UIViewController { guard let id = subscriptioniDTextField.text, id.isNotEmpty else { return } - Chargebee.shared.retrieveSubscriptionsList(queryParams: ["customer_id": id,"offset":""]) { result in + Chargebee.shared.retrieveSubscriptions(queryParams: ["customer_id": id,"offset":""]) { result in switch result { case let .success(result): debugPrint("Subscription Status Fetched: \(result)") @@ -97,7 +97,7 @@ final class CBSDKSubscriptionStatusViewController: UIViewController { return } - Chargebee.shared.retrieveSubscriptionsList(queryParams: ["customer_id": id,"offset":offset]) { result in + Chargebee.shared.retrieveSubscriptions(queryParams: ["customer_id": id,"offset":offset]) { result in switch result { case let .success(result): debugPrint("Subscription Status Fetched: \(result)") From 29a15544e662d507d30af2860e4b8564a38bb52f Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 16:04:50 +0530 Subject: [PATCH 5/9] Code Refactoring as per Pr Review "onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset))" to this "onSuccess(data)" --- Chargebee/Classes/Subscription/CBSubscription.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chargebee/Classes/Subscription/CBSubscription.swift b/Chargebee/Classes/Subscription/CBSubscription.swift index 811e5fe..d09878a 100644 --- a/Chargebee/Classes/Subscription/CBSubscription.swift +++ b/Chargebee/Classes/Subscription/CBSubscription.swift @@ -94,7 +94,7 @@ class CBSubscriptionManager { if data.list.isEmpty { onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found")) }else { - onSuccess(CBSubscriptionWrapper.init(list: data.list, nextOffset: data.nextOffset)) + onSuccess(data) } } else { onError(CBError.defaultSytemError(statusCode: 480, message: "json serialization failure")) From ab09ee7385afe1de1e324cf41ec7b2a88df8e1d2 Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 17:38:04 +0530 Subject: [PATCH 6/9] Code refactoring --- Chargebee/Classes/Configuration/Chargebee.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chargebee/Classes/Configuration/Chargebee.swift b/Chargebee/Classes/Configuration/Chargebee.swift index 04c83b0..bfb0b78 100644 --- a/Chargebee/Classes/Configuration/Chargebee.swift +++ b/Chargebee/Classes/Configuration/Chargebee.swift @@ -40,7 +40,7 @@ public class Chargebee { } - @available(*, deprecated, message: "This will be removed in upcoming versions, Please use this API func retrieveSubscriptionsList(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler)") + @available(*, deprecated, message: "This will be removed in upcoming versions, Please use this API func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler)") public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping SubscriptionHandler) { let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId") logger.info() From 2644b7cdbfb5fc3fe35760dca702443b4a308282 Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 18:44:31 +0530 Subject: [PATCH 7/9] Updated Version Number --- Chargebee.podspec | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Chargebee.podspec b/Chargebee.podspec index 6273f77..0c3efd3 100644 --- a/Chargebee.podspec +++ b/Chargebee.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'Chargebee' - s.version = '1.0.26' + s.version = '1.0.27' s.summary = 'Chargebee iOS SDK' # This description is used to generate tags and improve search results. diff --git a/README.md b/README.md index d46bd88..aa51661 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Choose from the following options to install Chargeee iOS SDK. Add the following snippet to the Podfile to install directly from Github. ```swift -pod 'Chargebee', :git => 'https://github.com/chargebee/chargebee-ios', :tag => '1.0.26' +pod 'Chargebee', :git => 'https://github.com/chargebee/chargebee-ios', :tag => '1.0.27' ``` ### CocoaPods From d3af3f2990dda2ca67ed345af5a8b9fb2e85e63f Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 20:42:39 +0530 Subject: [PATCH 8/9] Updated readme file --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aa51661..0e24df8 100644 --- a/README.md +++ b/README.md @@ -317,12 +317,13 @@ Use query parameters - Subscription ID, Customer ID, or Status for checking the ```swift Chargebee.shared.retrieveSubscriptions(queryParams :["String" : "String"]") { result in - switch result { - case let .success(resultarray): - print("Status \(resultarray.first.subscription.status)") - case let .error(error): - // Handle error here - } + switch result { + case let .success(result): + print("Next offset \(result.nextOffset)") + print("Subscriptions: \(result.list)") + case let .error(error): + // Handle error here + } } ``` From 16c4af7bd0beda35d6c1454e7ec9b6efb0da068b Mon Sep 17 00:00:00 2001 From: Ramesh Gadagoju Date: Mon, 14 Aug 2023 21:02:45 +0530 Subject: [PATCH 9/9] Updated Version Number in CBLogger File --- Chargebee/Classes/Logger/CBLoggerResource.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chargebee/Classes/Logger/CBLoggerResource.swift b/Chargebee/Classes/Logger/CBLoggerResource.swift index 85f9490..19b1f28 100644 --- a/Chargebee/Classes/Logger/CBLoggerResource.swift +++ b/Chargebee/Classes/Logger/CBLoggerResource.swift @@ -24,7 +24,7 @@ var osVersion: String { } var sdkVersion: String { - return "1.0.26" + return "1.0.27" } class CBLoggerResource: CBAPIResource {