Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextOffet Value is Handled for Retrieve Subscriptions API #80

Merged
merged 11 commits into from
Aug 14, 2023
10 changes: 10 additions & 0 deletions Chargebee/Classes/Configuration/Chargebee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ public class Chargebee {
CBSubscriptionManager().retrieveSubscription(network: request, logger: logger, handler: handler)
}


@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()

let request = CBAPIRequest(resource: SubscriptionResource(queryParams: queryParams))
CBSubscriptionManager().retrieveSubscriptions(network: request, logger: logger, handler: handler)
}

public func retrieveSubscriptions(queryParams: [String: String]? = nil, handler: @escaping RetrieveSubscriptionHandler) {
let logger = CBLogger(name: "Subscription", action: "Fetch Subscription using customerId")
logger.info()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the README as well?

let request = CBAPIRequest(resource: SubscriptionResource(queryParams: queryParams))
CBSubscriptionManager().retrieveSubscriptions(network: request, logger: logger, handler: handler)
}
Expand Down
21 changes: 19 additions & 2 deletions Chargebee/Classes/Subscription/CBSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,6 +58,8 @@ public struct Subscription: Codable {

public typealias CBSubscriptionHandler = (CBResult<Subscription>) -> Void
public typealias SubscriptionHandler = (CBResult<[SubscriptionList]>) -> Void
public typealias RetrieveSubscriptionHandler = (CBResult<CBSubscriptionWrapper>) -> Void


class CBSubscriptionManager {
func retrieveSubscription<T: CBNetworkRequest>(network: T, logger: CBLogger, handler: @escaping CBSubscriptionHandler) {
Expand All @@ -75,7 +77,7 @@ class CBSubscriptionManager {
if let data = status as? CBSubscriptionWrapper {
if data.list.isEmpty {
onError(CBError.defaultSytemError(statusCode: 404, message: "Subscription Not found"))
}else {
}else{
onSuccess(data.list)
}
} else {
Expand All @@ -85,6 +87,21 @@ class CBSubscriptionManager {
}, onError: onError)
}

func retrieveSubscriptions<T: CBNetworkRequest>(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: 404, message: "Subscription Not found"))
}else {
onSuccess(data)
}
} else {
onError(CBError.defaultSytemError(statusCode: 480, message: "json serialization failure"))
}

}, onError: onError)
}
}

extension String {
Expand Down
76 changes: 62 additions & 14 deletions Example/Chargebee/CBSDKSubscriptionStatusViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.retrieveSubscriptions(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.retrieveSubscriptions(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)
}
Expand Down
Loading