From 19290a07163d7ceb9061ac9bde3c6c7fa9d7740b Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 10 Dec 2024 15:31:29 -0500 Subject: [PATCH 1/3] Fix VPN Button pay shrinking when progress loader is shown --- .../Components/BuyVPN/Paywall/BraveVPNPaywallView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/brave-ios/Sources/BraveVPN/Components/BuyVPN/Paywall/BraveVPNPaywallView.swift b/ios/brave-ios/Sources/BraveVPN/Components/BuyVPN/Paywall/BraveVPNPaywallView.swift index 2f0bbb4f0b4b..664148ac10bb 100644 --- a/ios/brave-ios/Sources/BraveVPN/Components/BuyVPN/Paywall/BraveVPNPaywallView.swift +++ b/ios/brave-ios/Sources/BraveVPN/Components/BuyVPN/Paywall/BraveVPNPaywallView.swift @@ -202,9 +202,9 @@ public struct BraveVPNPaywallView: View { .font(.body.weight(.semibold)) .foregroundColor(Color(.white)) .padding() - .frame(maxWidth: .infinity) } } + .frame(maxWidth: .infinity) .background( LinearGradient( gradient: From 4cda44c794c458b2879c646bdb19d4f8d26e73f6 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 10 Dec 2024 16:13:11 -0500 Subject: [PATCH 2/3] Inject receipts from a script message instead of only from evaluateJavascript --- .../BraveSkus/BraveSkusAccountLink.swift | 2 +- .../Paged/BraveSkusScriptHandler.swift | 31 +++++++++++++++++++ .../DomainSpecific/Paged/BraveSkusScript.js | 25 +++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/ios/brave-ios/Sources/Brave/BraveSkus/BraveSkusAccountLink.swift b/ios/brave-ios/Sources/Brave/BraveSkus/BraveSkusAccountLink.swift index dfd781677095..14fc4dbdd17d 100644 --- a/ios/brave-ios/Sources/Brave/BraveSkus/BraveSkusAccountLink.swift +++ b/ios/brave-ios/Sources/Brave/BraveSkus/BraveSkusAccountLink.swift @@ -12,7 +12,7 @@ import os.log extension BraveStoreProduct { /// The key to use when storing the receipt in WebKit's LocalStorage - fileprivate var localStorageKey: String { + var localStorageKey: String { switch self { case .vpnMonthly, .vpnYearly: return "braveVpn.receipt" case .leoMonthly, .leoYearly: return "braveLeo.receipt" diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift index fb10e0da2595..a9537fd2b66b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift @@ -6,8 +6,10 @@ import AIChat import BraveCore import BraveShared +import BraveStore import BraveVPN import Foundation +import Preferences import Shared import WebKit import os.log @@ -109,6 +111,30 @@ class BraveSkusScriptHandler: TabContentScript { case .credentialsSummary: let summary = try CredentialSummaryMessage.from(message: message) return await skusManager.credentialSummary(for: summary.domain) + + case .setLocalStorageReceipt: + let storeMessage = try StoreReceiptMessage.from(message: message) + if storeMessage.message == "vpn" { + if let vpnSubscriptionProductId = Preferences.VPN.subscriptionProductId.value, + let product = BraveStoreProduct(rawValue: vpnSubscriptionProductId) + { + let storageKey = product.localStorageKey + let receipt = try BraveSkusSDK.receipt(for: product) + return ["key": storageKey, "data": receipt] + } + } + + if storeMessage.message == "leo" { + if let aiChatSubscriptionProductId = Preferences.AIChat.subscriptionProductId.value, + let orderId = Preferences.AIChat.subscriptionOrderId.value, + let product = BraveStoreProduct(rawValue: aiChatSubscriptionProductId) + { + let storageKey = product.localStorageKey + let receipt = try BraveSkusSDK.receipt(for: product) + return ["key": storageKey, "data": receipt, "braveLeo.orderId": orderId] + } + } + throw SkusWebMessageError.invalidFormat } } } @@ -119,6 +145,7 @@ extension BraveSkusScriptHandler { case fetchOrderCredentials = 2 case prepareCredentialsPresentation = 3 case credentialsSummary = 4 + case setLocalStorageReceipt = 5 static var map: [String: String] { var jsDict = [String: String]() @@ -141,6 +168,10 @@ extension BraveSkusScriptHandler { private struct CredentialSummaryMessage: SkusWebMessage { let domain: String } + + private struct StoreReceiptMessage: SkusWebMessage { + let message: String + } } private enum SkusWebMessageError: Error { diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/BraveSkusScript.js b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/BraveSkusScript.js index 6d21c4f9b5f0..76778062066b 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/BraveSkusScript.js +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/DomainSpecific/Paged/BraveSkusScript.js @@ -17,6 +17,31 @@ window.__firefox__.includeOnce("BraveSkusScript", function($) { if (!window.chrome) { window.chrome = {}; } + + // Request VPN Receipt + sendMessage($, { "message": "vpn" }) + .then(function(jsonData) { + if (jsonData) { + window.localStorage.setItem(jsonData["key"], jsonData["data"]); + } else { + console.error("No VPN Subscription Data"); + } + }).catch(function(err) { + console.error(`An error occurred setting the local storage receipt: ${err}`); + }); + + // Request Leo Receipt + sendMessage($, { "message": "leo" }) + .then(function(jsonData) { + if (jsonData) { + window.localStorage.setItem("braveLeo.orderId", jsonData["braveLeo.orderId"]) + window.localStorage.setItem(jsonData["key"], jsonData["data"]); + } else { + console.error("No Leo Subscription Data"); + } + }).catch(function(err) { + console.error(`An error occurred setting the local storage receipt: ${err}`); + }); Object.defineProperty(window.chrome, 'braveSkus', { enumerable: false, From 0f0015ce428a447cb314d7b876ccdd03cc730362 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 11 Dec 2024 09:57:25 -0500 Subject: [PATCH 3/3] Remove unnecessary code --- .../ScriptHandlers/Paged/BraveSkusScriptHandler.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift index a9537fd2b66b..2d7bd9109f31 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/BraveSkusScriptHandler.swift @@ -38,8 +38,7 @@ class BraveSkusScriptHandler: TabContentScript { return WKUserScript( source: secureScript( - handlerNamesMap: ["$": messageHandlerName] - .merging(Method.map, uniquingKeysWith: { $1 }), + handlerNamesMap: ["$": messageHandlerName], securityToken: scriptId, script: script ),