-
Notifications
You must be signed in to change notification settings - Fork 12
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
customerId is returned in Purchase API handler #83
Open
cb-gadagoju
wants to merge
3
commits into
chargebee:master
Choose a base branch
from
cb-gadagoju:feature/Handle_CustomerId
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)? | ||
private var buyProductHandler: ((Result<PurchaseSubscription, Error>) -> Void)? | ||
private var buyNonSubscriptionProductHandler: ((Result<NonSubscription, Error>) -> Void)? | ||
|
||
private var authenticationManager = CBAuthenticationManager() | ||
|
@@ -21,7 +21,6 @@ public class CBPurchase: NSObject { | |
var restoredPurchasesCount = 0 | ||
private var activeProduct: CBProduct? | ||
var customer: CBCustomer? | ||
|
||
var restoreResponseHandler: ((Result<[InAppSubscription], RestoreError>) -> Void)? | ||
var refreshHandler: RestoreResultCompletion<String>? | ||
var includeInActiveProducts = false | ||
|
@@ -33,8 +32,7 @@ public class CBPurchase: NSObject { | |
super.init() | ||
startPaymentQueueObserver() | ||
} | ||
|
||
deinit{ | ||
deinit { | ||
stopPaymentQueueObserver() | ||
} | ||
} | ||
|
@@ -111,54 +109,52 @@ public extension CBPurchase { | |
retrieveProducts() | ||
} | ||
} | ||
func purchaseNonSubscriptionProduct(product: CBProduct, customer : CBCustomer? = nil ,productType : ProductType, completion handler: @escaping ((_ result: Result<NonSubscription, Error>) -> Void)) { | ||
|
||
func purchaseNonSubscriptionProduct(product: CBProduct, customer: CBCustomer? = nil, productType: ProductType, completion handler: @escaping ((_ result: Result<NonSubscription, Error>) -> Void)) { | ||
buyNonSubscriptionProductHandler = handler | ||
activeProduct = product | ||
self.productType = productType | ||
self.customer = customer | ||
self.purchaseProductHandler(product: product, completion: handler) | ||
} | ||
|
||
//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<PurchaseSubscription, Error>) -> Void)) { | ||
buyProductHandler = handler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this from the major version. |
||
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<PurchaseSubscription, Error>) -> Void)) { | ||
buyProductHandler = handler | ||
activeProduct = product | ||
self.customer = customer | ||
self.purchaseProductHandler(product: product, completion: handler) | ||
} | ||
func restorePurchases(includeInActiveProducts:Bool = false, customer: CBCustomer? = nil, completion handler: @escaping ((_ result: Result<[InAppSubscription], RestoreError>) -> Void)) { | ||
|
||
func restorePurchases(includeInActiveProducts: Bool = false, customer: CBCustomer? = nil, completion handler: @escaping ((_ result: Result<[InAppSubscription], RestoreError>) -> Void)) { | ||
self.restoreResponseHandler = handler | ||
self.includeInActiveProducts = includeInActiveProducts | ||
self.restoredPurchasesCount = 0 | ||
self.restoreCustomer = customer | ||
SKPaymentQueue.default().restoreCompletedTransactions() | ||
} | ||
func purchaseProductHandler<T>(product: CBProduct,completion handler: @escaping ((_ result: Result<T, Error>) -> Void)) { | ||
|
||
func purchaseProductHandler<T>(product: CBProduct, completion handler: @escaping ((_ result: Result<T, Error>) -> Void)) { | ||
|
||
guard CBAuthenticationManager.isSDKKeyPresent() else { | ||
handler(.failure(CBPurchaseError.cannotMakePayments)) | ||
return | ||
} | ||
|
||
if !CBPurchase.shared.canMakePayments() { | ||
handler(.failure(CBPurchaseError.cannotMakePayments)) | ||
} else { | ||
authenticationManager.isSDKKeyValid { status in | ||
if status { | ||
let payment = SKPayment(product: product.product) | ||
SKPaymentQueue.default().add(payment) | ||
|
||
} else { | ||
handler(.failure(CBPurchaseError.invalidSDKKey)) | ||
} | ||
|
@@ -198,7 +194,7 @@ extension CBPurchase: SKProductsRequestDelegate { | |
debugPrint("Error: \(error.localizedDescription)") | ||
if request is SKReceiptRefreshRequest { | ||
completedRefresh(error: error) | ||
}else{ | ||
} else { | ||
receiveProductsHandler?(.failure(.skRequestFailed)) | ||
} | ||
request.cancel() | ||
|
@@ -216,16 +212,16 @@ extension CBPurchase: SKPaymentTransactionObserver { | |
if let product = activeProduct { | ||
if let _ = product.product.subscriptionPeriod { | ||
validateReceipt(product, customer: self.customer, completion: buyProductHandler) | ||
}else{ | ||
validateReceiptForNonSubscriptions(product, self.productType,customer: self.customer, completion: buyNonSubscriptionProductHandler) | ||
} else { | ||
validateReceiptForNonSubscriptions(product, self.productType, customer: self.customer, completion: buyNonSubscriptionProductHandler) | ||
} | ||
} | ||
case .restored: | ||
SKPaymentQueue.default().finishTransaction(transaction) | ||
receivedRestoredTransaction() | ||
case .failed: | ||
if let error = transaction.error as? SKError{ | ||
debugPrint("Error :",error) | ||
if let error = transaction.error as? SKError { | ||
debugPrint("Error :", error) | ||
switch error.errorCode { | ||
case 0: | ||
self.invokeProductHandler(forProduct: self.activeProduct?.product, error: CBPurchaseError.unknown) | ||
|
@@ -258,7 +254,7 @@ extension CBPurchase: SKPaymentTransactionObserver { | |
default: | ||
if let _ = activeProduct?.product.subscriptionPeriod { | ||
buyProductHandler?(.failure(error)) | ||
}else { | ||
} else { | ||
buyNonSubscriptionProductHandler?(.failure(error)) | ||
} | ||
} | ||
|
@@ -286,19 +282,19 @@ extension CBPurchase: SKPaymentTransactionObserver { | |
|
||
// chargebee methods | ||
public extension CBPurchase { | ||
|
||
func validateReceiptForNonSubscriptions(_ product: CBProduct?,_ productType: | ||
ProductType?,customer: CBCustomer? = nil, completion: ((Result<NonSubscription, Error>) -> Void)?) { | ||
|
||
func validateReceiptForNonSubscriptions(_ product: CBProduct?, | ||
_ productType: ProductType?, | ||
customer: CBCustomer? = nil, | ||
completion: ((Result<NonSubscription, Error>) -> Void)?) { | ||
self.productType = productType | ||
|
||
guard let receipt = getReceipt(product: product?.product,customer: customer) else { | ||
guard let receipt = getReceipt(product: product?.product, customer: customer) else { | ||
debugPrint("Couldn't read receipt data with error") | ||
completion?(.failure(CBError.defaultSytemError(statusCode: 0, message: "Could not read receipt data"))) | ||
return | ||
} | ||
|
||
CBReceiptValidationManager.validateReceiptForNonSubscriptions(receipt: receipt) { | ||
(receiptResult) in DispatchQueue.main.async { | ||
|
||
CBReceiptValidationManager.validateReceiptForNonSubscriptions(receipt: receipt) { (receiptResult) in DispatchQueue.main.async { | ||
switch receiptResult { | ||
case .success(let result): | ||
debugPrint("Receipt: \(result)") | ||
|
@@ -311,34 +307,33 @@ public extension CBPurchase { | |
} | ||
} | ||
} | ||
func validateReceipt(_ product: CBProduct?,customer: CBCustomer? = nil,completion: ((Result<(status:Bool, subscriptionId:String?, planId:String?), Error>) -> Void)?) { | ||
guard let receipt = getReceipt(product: product?.product,customer: customer) else { | ||
|
||
func validateReceipt(_ product: CBProduct?, customer: CBCustomer? = nil, completion: ((Result<PurchaseSubscription, Error>) -> Void)?) { | ||
|
||
guard let receipt = getReceipt(product: product?.product, customer: customer) else { | ||
debugPrint("Couldn't read receipt data with error") | ||
completion?(.failure(CBError.defaultSytemError(statusCode: 0, message: "Could not read receipt data"))) | ||
return | ||
} | ||
|
||
CBReceiptValidationManager.validateReceipt(receipt: receipt) { | ||
(receiptResult) in DispatchQueue.main.async { | ||
|
||
CBReceiptValidationManager.validateReceipt(receipt: receipt) { (receiptResult) in DispatchQueue.main.async { | ||
switch receiptResult { | ||
case .success(let receipt): | ||
debugPrint("Receipt: \(receipt)") | ||
if receipt.subscriptionId.isEmpty { | ||
case .success(let result): | ||
debugPrint("Receipt: \(result)") | ||
if result.subscriptionId.isEmpty { | ||
completion?(.failure(CBError.defaultSytemError(statusCode: 400, message: "Invalid Purchase"))) | ||
return | ||
} | ||
self.activeProduct = nil | ||
completion?(.success((true, receipt.subscriptionId, receipt.planId))) | ||
completion?(.success(PurchaseSubscription(status: true, subscriptionDetails: result))) | ||
case .error(let error): | ||
debugPrint(" Chargebee - Receipt Upload - Failure") | ||
completion?(.failure(error)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func getReceipt(product: SKProduct?, customer: CBCustomer? = nil) -> CBReceipt? { | ||
var receipt: CBReceipt? | ||
guard let appStoreReceiptURL = Bundle.main.appStoreReceiptURL, | ||
|
@@ -351,20 +346,20 @@ public extension CBPurchase { | |
} | ||
do { | ||
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped) | ||
|
||
let receiptString = receiptData.base64EncodedString(options: []) | ||
debugPrint("Apple Purchase - success") | ||
receipt = CBReceipt(name: product.localizedTitle, token: receiptString, productID: product.productIdentifier, price: "\(product.price)", currencyCode: currencyCode, period: product.subscriptionPeriod?.numberOfUnits ?? 0, periodUnit: Int(product.subscriptionPeriod?.unit.rawValue ?? 0),customer: customer,productType: self.productType ?? .unknown) | ||
}catch { | ||
} catch { | ||
print("Couldn't read receipt data with error: " + error.localizedDescription) | ||
} | ||
return receipt | ||
} | ||
|
||
private func invokeProductHandler(forProduct product: SKProduct?, error: CBPurchaseError) { | ||
if let _ = product?.subscriptionPeriod { | ||
buyProductHandler?(.failure(error)) | ||
}else { | ||
} else { | ||
buyNonSubscriptionProductHandler?(.failure(error)) | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be just
Subscription
?