Skip to content

Commit

Permalink
Merge pull request #209 from qonversion/feature/purchaseOptions
Browse files Browse the repository at this point in the history
Added purchase options
  • Loading branch information
suriksarkisyan authored Aug 22, 2024
2 parents f24d944 + 17d5757 commit 64b52ca
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 35 deletions.
2 changes: 1 addition & 1 deletion QonversionSandwich.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ Pod::Spec.new do |s|
s.source_files = 'ios/sandwich/**/*.{h,m,swift}'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }

s.dependency "Qonversion", "5.11.1"
s.dependency "Qonversion", "5.12.0"
end
2 changes: 1 addition & 1 deletion android/sandwich/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.qonversion_version = '8.0.2'
ext.qonversion_version = '8.1.0'
}

plugins {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.qonversion.android.sdk.dto.QAttributionProvider
import com.qonversion.android.sdk.dto.QEnvironment
import com.qonversion.android.sdk.dto.QLaunchMode
import com.qonversion.android.sdk.dto.QPurchaseModel
import com.qonversion.android.sdk.dto.QPurchaseOptions
import com.qonversion.android.sdk.dto.QPurchaseUpdateModel
import com.qonversion.android.sdk.dto.QPurchaseUpdatePolicy
import com.qonversion.android.sdk.dto.QRemoteConfig
Expand Down Expand Up @@ -92,6 +93,7 @@ class QonversionSandwich(
productId: String,
offerId: String?,
applyOffer: Boolean?,
contextKeys: List<String>?,
resultListener: ResultListener
) {
val currentActivity = activityProvider.currentActivity
Expand All @@ -100,13 +102,24 @@ class QonversionSandwich(
return
}

val purchaseModel = QPurchaseModel(productId, offerId)
if (applyOffer == false) {
purchaseModel.removeOffer()
}

val purchaseCallback = getEntitlementsCallback(resultListener)
Qonversion.shared.purchase(currentActivity, purchaseModel, purchaseCallback)

Qonversion.shared.products(object: QonversionProductsCallback {
override fun onSuccess(products: Map<String, QProduct>) {
val product = products[productId]
if (product != null) {
val purchaseOptions = configurePurchaseOptions(offerId, applyOffer, contextKeys = contextKeys)

Qonversion.shared.purchase(currentActivity, product, purchaseOptions, purchaseCallback)
} else {
purchaseCallback.onError(QonversionError(QonversionErrorCode.ProductNotFound))
}
}

override fun onError(error: QonversionError) {
purchaseCallback.onError(error)
}
})
}

fun updatePurchase(
Expand All @@ -115,6 +128,7 @@ class QonversionSandwich(
applyOffer: Boolean?,
oldProductId: String,
updatePolicyKey: String?,
contextKeys: List<String>?,
resultListener: ResultListener
) {
val currentActivity = activityProvider.currentActivity
Expand All @@ -123,24 +137,25 @@ class QonversionSandwich(
return
}

val updatePolicy = updatePolicyKey?.let {
try {
QPurchaseUpdatePolicy.valueOf(it)
} catch (e: IllegalArgumentException) {
null
val purchaseCallback = getEntitlementsCallback(resultListener)

Qonversion.shared.products(object: QonversionProductsCallback {
override fun onSuccess(products: Map<String, QProduct>) {
val product = products[productId]
val oldProduct = products[oldProductId]
if (product != null && oldProduct != null) {
val purchaseOptions = configurePurchaseOptions(offerId, applyOffer, oldProduct, updatePolicyKey, contextKeys)

Qonversion.shared.updatePurchase(currentActivity, product, purchaseOptions, purchaseCallback)
} else {
purchaseCallback.onError(QonversionError(QonversionErrorCode.ProductNotFound))
}
}
}
val purchaseUpdateModel = QPurchaseUpdateModel(productId, oldProductId, updatePolicy, offerId)
if (applyOffer == false) {
purchaseUpdateModel.removeOffer()
}

val purchaseCallback = getEntitlementsCallback(resultListener)
Qonversion.shared.updatePurchase(
currentActivity,
purchaseUpdateModel,
purchaseCallback
)
override fun onError(error: QonversionError) {
purchaseCallback.onError(error)
}
})
}

fun checkEntitlements(resultListener: ResultListener) {
Expand Down Expand Up @@ -365,6 +380,46 @@ class QonversionSandwich(
return mapOf("success" to success)
}

private fun configurePurchaseOptions(
offerId: String?,
applyOffer: Boolean?,
oldProduct: QProduct? = null,
updatePolicyKey: String? = null,
contextKeys: List<String>? = null,
): QPurchaseOptions {
val builder = QPurchaseOptions.Builder()

oldProduct?.let {
builder.setOldProduct(it)
}

updatePolicyKey?.let {
try {
QPurchaseUpdatePolicy.valueOf(it)
} catch (e: IllegalArgumentException) {
null
}
}?.let {
builder.setUpdatePolicy(it)
}

offerId?.let {
builder.setOfferId(it)
}

if (applyOffer == false) {
builder.removeOffer()
}

contextKeys?.let {
builder.setContextKeys(it)
}

val purchaseOptions = builder.build()

return purchaseOptions
}

private interface ProductCallback {
fun onProductLoaded(product: QProduct)

Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ platform :ios, '9.0'
use_frameworks!

target 'QonversionSandwich' do
pod 'Qonversion', '5.10.0'
pod 'Qonversion', '5.12.0'
end

target 'Sample' do
Expand Down
18 changes: 9 additions & 9 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PODS:
- Qonversion (5.10.0):
- Qonversion/Main (= 5.10.0)
- Qonversion/Main (5.10.0)
- QonversionSandwich (4.5.0):
- Qonversion (= 5.10.0)
- Qonversion (5.12.0):
- Qonversion/Main (= 5.12.0)
- Qonversion/Main (5.12.0)
- QonversionSandwich (5.0.3):
- Qonversion (= 5.12.0)

DEPENDENCIES:
- Qonversion (= 5.10.0)
- Qonversion (= 5.12.0)
- QonversionSandwich (from `../`)

SPEC REPOS:
Expand All @@ -18,9 +18,9 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Qonversion: 879e542136f238e963d5488ef31983f28bd315ef
QonversionSandwich: 40d9bb9065d0cc693b959552ad053e3d4bfc1a33
Qonversion: ac2da69e497cb1f01cea13d82513fde14f338ca3
QonversionSandwich: 11c3c29fccb4c0d22127df6e00a43ffbe461ab96

PODFILE CHECKSUM: 47571a17dacda3f48496b805e608b6699cf2c2a7
PODFILE CHECKSUM: 270a709ed1c745b2fbcd44832cb6e9be9fbbafe3

COCOAPODS: 1.15.2
29 changes: 28 additions & 1 deletion ios/sandwich/QonversionSandwich.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ public class QonversionSandwich : NSObject {
Qonversion.shared().purchase(productId, completion: purchaseCompletion)
}

@objc public func purchase(
_ productId: String,
quantity: Int,
contextKeys: [String],
completion: @escaping BridgeCompletion
) {
Qonversion.shared().products { [weak self] result, err in
guard let self = self else { return }

let purchaseCompletion = getPurchaseCompletionHandler(for: completion)
let purchaseOptions = Qonversion.PurchaseOptions(quantity: quantity, contextKeys: contextKeys)

guard let product: Qonversion.Product = result[productId] else {
let error = self.productNotFoundError()
return purchaseCompletion([:], error, false)
}

Qonversion.shared().purchaseProduct(product, options: purchaseOptions, completion: purchaseCompletion)
}
}

@objc public func purchaseProduct(_ productId: String, offeringId: String?, completion: @escaping BridgeCompletion) {
guard let offeringId = offeringId else {
return purchase(productId, completion: completion)
Expand All @@ -100,7 +121,7 @@ public class QonversionSandwich : NSObject {
self?.handleEntitlementsResult(entitlements, error, completion: completion)
}
} else {
let error = NSError.init(domain: QonversionErrorDomain, code: Qonversion.Error.productNotFound.rawValue, userInfo: nil)
let error = productNotFoundError()

completion(nil, error.toSandwichError())
}
Expand Down Expand Up @@ -361,6 +382,12 @@ public class QonversionSandwich : NSObject {

isSubscribedOnAsyncEvents = true
}

private func productNotFoundError() -> NSError {
let error = NSError.init(domain: QonversionErrorDomain, code: Qonversion.Error.productNotFound.rawValue, userInfo: nil)

return error
}
}

// MARK: - PurchasesDelegate
Expand Down

0 comments on commit 64b52ca

Please sign in to comment.