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

Added promo offer logic #233

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.12.6"
s.dependency "Qonversion", "5.13.0"
end
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.12.6'
pod 'Qonversion', '5.13.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.12.4):
- Qonversion/Main (= 5.12.4)
- Qonversion/Main (5.12.4)
- QonversionSandwich (5.1.5):
- Qonversion (= 5.12.4)
- Qonversion (5.13.0):
- Qonversion/Main (= 5.13.0)
- Qonversion/Main (5.13.0)
- QonversionSandwich (5.1.7):
- Qonversion (= 5.13.0)

DEPENDENCIES:
- Qonversion (= 5.12.4)
- Qonversion (= 5.13.0)
- QonversionSandwich (from `../`)

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

SPEC CHECKSUMS:
Qonversion: cca480020597aa8bb74d9c5d0bf1916e68b8440e
QonversionSandwich: e8e6d8f9cb72127f4dc2d4ddfff08ec96ea30532
Qonversion: 37addeba74c5b328de9e1173b580c971b6d764ec
QonversionSandwich: 6aa0afcf15dc79817eef20bf5dfe475b32bcff9a

PODFILE CHECKSUM: 1fe656ec677475f37d513dc0b0babde5193ad428
PODFILE CHECKSUM: ea5d129345e5e6f263575a7a6d165488b50b6991

COCOAPODS: 1.15.2
22 changes: 21 additions & 1 deletion ios/sandwich/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ extension Qonversion.SubscriptionPeriod {
}
}

@available(iOS 12.2, macOS 10.14.4, watchOS 6.2, tvOS 12.2, visionOS 1.0, *)
extension Qonversion.PromotionalOffer {
func toMap() -> BridgeData {
return ["productDiscount": productDiscount.toMap(),
"paymentDiscount": paymentDiscount.toMap()]
}
}

extension Qonversion.Entitlement {
func toMap() -> BridgeData {
return [
Expand Down Expand Up @@ -226,7 +234,8 @@ extension Qonversion.Transaction {
"transactionRevocationTimestamp": transactionRevocationDate.map { $0.toMilliseconds() },
"environment": environment.toString(),
"ownershipType": ownershipType.toString(),
"type": type.toString()
"type": type.toString(),
"promoOfferId": promoOfferId
]
}
}
Expand Down Expand Up @@ -607,6 +616,17 @@ extension SKProductSubscriptionPeriod {
}
}

@available(iOS 12.2, macOS 10.14.4, watchOS 6.2, tvOS 12.2, visionOS 1.0, *)
extension SKPaymentDiscount {
func toMap() -> BridgeData {
return ["identifier": identifier,
"nonce": nonce.uuidString,
"signature": signature,
"keyIdentifier": keyIdentifier,
"timestamp": timestamp]
}
}

@available(iOS 11.2, macOS 10.13.2, tvOS 11.2, *)
extension SKProductDiscount {
func toMap() -> BridgeData {
Expand Down
50 changes: 48 additions & 2 deletions ios/sandwich/QonversionSandwich.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,69 @@ public class QonversionSandwich : NSObject {
_ productId: String,
quantity: Int,
contextKeys: [String],
promoOffer: [String: Any],
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)
}

let purchaseOptions: Qonversion.PurchaseOptions

if #available(iOS 12.2, macOS 10.14.4, watchOS 6.2, tvOS 12.2, visionOS 1.0, *),
let productDiscountId = promoOffer["productDiscountId"] as? String,
let productDiscount = product.skProduct?.discounts.first(where: { $0.identifier == productDiscountId }),
let keyIdentifier = promoOffer["keyIdentifier"] as? String,
let nonce = promoOffer["nonce"] as? String,
let nonceUUID = UUID(uuidString: nonce),
let signature = promoOffer["signature"] as? String,
let timestamp = promoOffer["timestamp"] as? Int {
let timestampNumber = NSNumber(value: timestamp)
let paymentDiscount = SKPaymentDiscount(identifier: productDiscountId, keyIdentifier: keyIdentifier, nonce: nonceUUID, signature: signature, timestamp: timestampNumber)

let promotionalOffer = Qonversion.PromotionalOffer(productDiscount: productDiscount, paymentDiscount: paymentDiscount)
purchaseOptions = Qonversion.PurchaseOptions(quantity: quantity, contextKeys: contextKeys, promoOffer: promotionalOffer)
} else {
purchaseOptions = Qonversion.PurchaseOptions(quantity: quantity, contextKeys: contextKeys)
}

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

@objc public func getPromotionalOffer(_ productId: String, productDiscountId: String, completion: @escaping BridgeCompletion) {
if #available(iOS 12.2, macOS 10.14.4, watchOS 6.2, tvOS 12.2, visionOS 1.0, *) {
Qonversion.shared().products { [weak self] result, err in
guard let self = self else { return }

guard let product: Qonversion.Product = result[productId],
let discount: SKProductDiscount = product.skProduct?.discounts.first(where: { $0.identifier == productDiscountId }) else {
let error = productNotFoundError()

return completion(nil, error.toSandwichError())
}
Qonversion.shared().getPromotionalOffer(for: product, discount: discount) { promoOffer, error in
if let error = error as NSError? {
return completion(nil, error.toSandwichError())
}

let bridgeData: [String: Any]? = promoOffer?.toMap().clearEmptyValues()

completion(bridgeData, nil)
}
}
} else {
let error = productNotFoundError()

completion(nil, error.toSandwichError())
}
}

@objc public func purchaseProduct(_ productId: String, offeringId: String?, completion: @escaping BridgeCompletion) {
guard let offeringId = offeringId else {
return purchase(productId, completion: completion)
Expand Down
Loading