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

Customer Object Added to Restore API #81

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Chargebee/Classes/Purchase/CBPurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CBPurchase: NSObject {
public static let shared = CBPurchase()
private var productIDs: [String] = []
public var receiveProductsHandler: ((_ result: Result<[CBProduct], CBPurchaseError>) -> Void)?
public var buyProductHandler: ((Result<(status:Bool, subscriptionId:String?, planId:String?), Error>) -> Void)?
public var buyProductHandler: ((Result<(status:Bool, subscriptionId:String?, planId:String?, customerId:String?), Error>) -> Void)?
Copy link
Contributor

Choose a reason for hiding this comment

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

This will be a breaking change for the SDK. If its not related to this PR, can we revert and take it up separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be a breaking change for the SDK. If its not related to this PR, can we revert and take it up separately?

ok Its reverted

private var buyNonSubscriptionProductHandler: ((Result<NonSubscription, Error>) -> Void)?

private var authenticationManager = CBAuthenticationManager()
Expand All @@ -26,6 +26,7 @@ public class CBPurchase: NSObject {
var refreshHandler: RestoreResultCompletion<String>?
var includeInActiveProducts = false
private var productType: ProductType?
var restoreCustomer: CBCustomer?

// MARK: - Init
private override init() {
Expand Down Expand Up @@ -121,24 +122,25 @@ public extension CBPurchase {

//Buy the product
@available(*, deprecated, message: "This will be removed in upcoming versions, Please use this API func purchaseProduct(product: CBProduct, customer : CBCustomer? = nil, completion)")
func purchaseProduct(product: CBProduct, customerId : String? = "",completion handler: @escaping ((_ result: Result<(status:Bool, subscriptionId:String?, planId:String?), Error>) -> Void)) {
func purchaseProduct(product: CBProduct, customerId : String? = "",completion handler: @escaping ((_ result: Result<(status:Bool, subscriptionId:String?, planId:String?, customerId:String?), Error>) -> Void)) {
buyProductHandler = handler
activeProduct = product
self.customer = CBCustomer(customerID: customerId ?? "")
self.purchaseProductHandler(product: product, completion: handler)
}

func purchaseProduct(product: CBProduct, customer : CBCustomer? = nil, completion handler: @escaping ((_ result: Result<(status:Bool, subscriptionId:String?, planId:String?), Error>) -> Void)) {
func purchaseProduct(product: CBProduct, customer : CBCustomer? = nil, completion handler: @escaping ((_ result: Result<(status:Bool, subscriptionId:String?, planId:String?, customerId:String?), Error>) -> Void)) {
buyProductHandler = handler
activeProduct = product
self.customer = customer
self.purchaseProductHandler(product: product, completion: handler)
}

func restorePurchases(includeInActiveProducts:Bool = false ,completion handler: @escaping ((_ result: Result<[InAppSubscription], RestoreError>) -> Void)) {
func restorePurchases(includeInActiveProducts:Bool = false, customer: CBCustomer, completion handler: @escaping ((_ result: Result<[InAppSubscription], RestoreError>) -> Void)) {
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 make the customer as optional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please update the versions in corresponding places as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we make the customer as optional?

Now customerId is mandatory know then why it should be optional ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please update the versions in corresponding places as well.

ok

Copy link
Contributor

Choose a reason for hiding this comment

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

@cb-gadagoju During the purchase flow, the customer object is still optional. And the same goes for validateReceipt as well. Do you think the same can be extended to the restore flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cb-haripriyan yes we can implement optional as purchase flow but when its optional for restore merchant might consider optional and may skip it know any how he will get success after restore even if they didn't pass customer_id ? is't there any hidden problem which customer will know later again if they miss it ? i think we should highlight to customer for passing customerObject ! what you suggest ?

self.restoreResponseHandler = handler
self.includeInActiveProducts = includeInActiveProducts
self.restoredPurchasesCount = 0
self.restoreCustomer = customer
SKPaymentQueue.default().restoreCompletedTransactions()
}

Expand Down Expand Up @@ -212,7 +214,7 @@ extension CBPurchase: SKPaymentTransactionObserver {
SKPaymentQueue.default().finishTransaction(transaction)
if let product = activeProduct {
if let _ = product.product.subscriptionPeriod {
validateReceipt(product,customer: self.customer, completion: buyProductHandler)
validateReceipt(product, customer: self.customer, completion: buyProductHandler)
}else{
validateReceiptForNonSubscriptions(product, self.productType,customer: self.customer, completion: buyNonSubscriptionProductHandler)
}
Expand Down Expand Up @@ -309,7 +311,7 @@ public extension CBPurchase {
}
}

func validateReceipt(_ product: CBProduct?,customer: CBCustomer? = nil,completion: ((Result<(status:Bool, subscriptionId:String?, planId:String?), Error>) -> Void)?) {
func validateReceipt(_ product: CBProduct?,customer: CBCustomer? = nil,completion: ((Result<(status:Bool, subscriptionId:String?, planId:String?, customerId:String?), Error>) -> Void)?) {

guard let receipt = getReceipt(product: product?.product,customer: customer) else {
debugPrint("Couldn't read receipt data with error")
Expand All @@ -327,7 +329,7 @@ public extension CBPurchase {
return
}
self.activeProduct = nil
completion?(.success((true, receipt.subscriptionId, receipt.planId)))
completion?(.success((true, receipt.subscriptionId, receipt.planId,receipt.customerId)))
case .error(let error):
debugPrint(" Chargebee - Receipt Upload - Failure")
completion?(.failure(error))
Expand Down
8 changes: 6 additions & 2 deletions Chargebee/Classes/Restore/CBPurchaseManager+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ extension CBPurchase{
for product in products {
operationQueue?.addOperation{
if let _ = product.product.subscriptionPeriod {
self.validateReceipt(product, completion: nil)
if let customer = self.restoreCustomer {
self.validateReceipt(product,customer: customer, completion: nil)
}
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 pass the customer without having to unwrap? The validateReceipt takes in an optional param.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done Updated

}else{
self.validateReceiptForNonSubscriptions(product, .unknown, completion: nil)
if let customer = self.restoreCustomer {
self.validateReceiptForNonSubscriptions(product, .unknown, customer: customer, completion: nil)
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Example/Chargebee/CBSDKOptionsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ extension CBSDKOptionsViewController: UITableViewDelegate, UITableViewDataSource
}
case .restore:
self.view.activityStartAnimating(activityColor: UIColor.white, backgroundColor: UIColor.black.withAlphaComponent(0.5))
CBPurchase.shared.restorePurchases(includeInActiveProducts: true) { result in
//Ex: customer_id is mandatory field for restoring purchases so please pass customer object as shown example below
let customer = CBCustomer(customerID: "Test123",firstName: "CB",lastName: "Test",email: "cbTest@chargebee.com")
CBPurchase.shared.restorePurchases(includeInActiveProducts: true, customer: customer) { result in
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update the README as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

switch result {
case .success(let response):
if response.count > 0 {
Expand Down
2 changes: 2 additions & 0 deletions Example/Chargebee/CBSDKProductsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ extension CBSDKProductsTableViewController: ProductTableViewCellDelegate {
print(result.status)
print(result.subscriptionId ?? "")
print(result.planId ?? "")
print(result.customerId ?? "")

DispatchQueue.main.async {
self.view.activityStopAnimating()
let alertController = UIAlertController(title: "Chargebee", message: "success", preferredStyle: .alert)
Expand Down
Loading