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 4 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
6 changes: 4 additions & 2 deletions Chargebee/Classes/Purchase/CBPurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -135,10 +136,11 @@ public extension CBPurchase {
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
4 changes: 2 additions & 2 deletions Chargebee/Classes/Restore/CBPurchaseManager+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ extension CBPurchase{
for product in products {
operationQueue?.addOperation{
if let _ = product.product.subscriptionPeriod {
self.validateReceipt(product, completion: nil)
self.validateReceipt(product,customer: self.restoreCustomer, completion: nil)
}else{
self.validateReceiptForNonSubscriptions(product, .unknown, completion: nil)
self.validateReceiptForNonSubscriptions(product, .unknown, customer: self.restoreCustomer, 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: "[email protected]")
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
1 change: 1 addition & 0 deletions Example/Chargebee/CBSDKProductsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ extension CBSDKProductsTableViewController: ProductTableViewCellDelegate {
print(result.status)
print(result.subscriptionId ?? "")
print(result.planId ?? "")

DispatchQueue.main.async {
self.view.activityStopAnimating()
let alertController = UIAlertController(title: "Chargebee", message: "success", preferredStyle: .alert)
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,13 @@ The `restorePurchases()` function helps to recover your app user's previous purc

To retrieve **inactive** purchases along with the **active** purchases for your app user, you can call the `restorePurchases()` function with the `includeInActiveProducts` parameter set to `true`. If you only want to restore active subscriptions, set the parameter to `false`. Here is an example of how to use the `restorePurchases()` function in your code with the `includeInActiveProducts` parameter set to `true`.

To Restore Purchases please pass `CBCustomer` Object for `restorePurchases()` method as shown in below example.
Note: `customerId` is mandatory field to restore purchases.

```swift
CBPurchase.shared.restorePurchases(includeInActiveProducts: true) { result in

let customer = CBCustomer(customerID: "Test123",firstName: "CB",lastName: "Test",email: "[email protected]")
CBPurchase.shared.restorePurchases(includeInActiveProducts: true, customer: customer) { result in
switch result {
case .success(let response):
for subscription in response {
Expand Down
Loading