Skip to content

Commit

Permalink
Merge pull request #1309 from novasamatech/feature/mercuryo-redirect-…
Browse files Browse the repository at this point in the history
…api-transition
  • Loading branch information
svojsu authored Dec 20, 2024
2 parents ee96608 + 2fc8f1a commit 747713b
Show file tree
Hide file tree
Showing 25 changed files with 460 additions and 300 deletions.
67 changes: 45 additions & 22 deletions novawallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Foundation

extension MercuryoCardHookFactory {
func createResponseInterceptingHook(
using params: MercuryoCardParams,
for delegate: PayCardHookDelegate
) -> PayCardHook {
let cardsAction = MercuryoMessageName.onCardsResponse.rawValue
let topUpAction = MercuryoMessageName.onCardTopup.rawValue

let scriptSource = """
let originalXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url) {
if (url === '\(MercuryoCardApi.cardsEndpoint)') {
this.addEventListener('load', function() {
window.webkit.messageHandlers.\(cardsAction).postMessage(this.responseText);
});
}
if (url.includes('\(MercuryoCardApi.topUpEndpoint)')) {
this.addEventListener('load', function() {
window.webkit.messageHandlers.\(topUpAction).postMessage(this.responseText);
});
}
originalXhrOpen.apply(this, arguments);
};
"""

let handlers: [PayCardMessageHandling] = [
MercuryoCardsResponseHandler(
delegate: delegate,
logger: logger
),
MercuryoSellRequestResponseHandler(
delegate: delegate,
chainAsset: params.chainAsset,
logger: logger
)
]

return .init(
script: .init(
content: scriptSource,
insertionPoint: .atDocEnd
),
messageNames: [cardsAction, topUpAction],
handlers: handlers
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

extension MercuryoCardHookFactory {
func createWidgetHook(for delegate: PayCardHookDelegate) -> PayCardHook {
let statusAction = MercuryoMessageName.onCardStatusChange.rawValue

let scriptSource = """
window.addEventListener("message", ({ data }) => {
window.webkit.messageHandlers.\(statusAction).postMessage(data);
});
"""

return .init(
script: .init(
content: scriptSource,
insertionPoint: .atDocEnd
),
messageNames: [statusAction],
handlers: [MercuryoCardStatusHandler(delegate: delegate, logger: logger)]
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation

struct MercuryoCardParams {
let chainAsset: ChainAsset
let refundAddress: AccountAddress
}

enum MercuryoCardApi {
static let widgetUrl = URL(string: "https://exchange.mercuryo.io/")!
static let widgetId = "4ce98182-ed76-4933-ba1b-b85e4a51d75a" // TODO: Change for production
static let theme = "nova"
static let type = "sell"
static let fiatCurrency = "EUR"
static let fixFiatCurrency = "true"
static let fixPaymentMethod = "true"
static let paymentMethod = "fiat_card_open"
static let showSpendCardDetails = "true"
static let hideRefundAddress = "true"
static let cardsEndpoint = "https://api.mercuryo.io/v1.6/cards"
static let topUpEndpoint = "https://api.mercuryo.io/v1.6/widget/sell-request"
static let pendingTimeout: TimeInterval = 5.secondsFromMinutes
}

final class MercuryoCardHookFactory {
let logger: LoggerProtocol

init(logger: LoggerProtocol) {
self.logger = logger
}
}

extension MercuryoCardHookFactory: PayCardHookFactoryProtocol {
func createHooks(
using params: MercuryoCardParams,
for delegate: PayCardHookDelegate
) -> [PayCardHook] {
let responseHook = createResponseInterceptingHook(
using: params,
for: delegate
)
let widgetHook = createWidgetHook(for: delegate)

return [widgetHook, responseHook]
}
}

This file was deleted.

This file was deleted.

78 changes: 0 additions & 78 deletions novawallet/Modules/PayCard/Mercuryo/MercuryoCardHookFactory.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Foundation
import Operation_iOS

protocol MercuryoCardParamsProviderProtocol {
func fetchParamsWrapper() -> CompoundOperationWrapper<MercuryoCardParams>
}

final class MercuryoCardParamsProvider {
let chainRegistry: ChainRegistryProtocol
let wallet: MetaAccountModel
let chainId: ChainModel.Id

init(
chainRegistry: ChainRegistryProtocol,
wallet: MetaAccountModel,
chainId: ChainModel.Id
) {
self.chainRegistry = chainRegistry
self.wallet = wallet
self.chainId = chainId
}
}

// MARK: MercuryoCardParamsProviderProtocol

extension MercuryoCardParamsProvider: MercuryoCardParamsProviderProtocol {
func fetchParamsWrapper() -> CompoundOperationWrapper<MercuryoCardParams> {
let chainFetchWrapper = chainRegistry.asyncWaitChainWrapper(for: chainId)

let resultOperation = ClosureOperation { [weak self] in
guard
let self,
let chain = try chainFetchWrapper.targetOperation.extractNoCancellableResultData(),
let utilityAsset = chain.utilityChainAsset()
else {
throw ChainModelFetchError.noAsset(assetId: AssetModel.utilityAssetId)
}

guard
let selectedAccount = wallet.fetch(for: chain.accountRequest()) else {
throw ChainAccountFetchingError.accountNotExists
}

let refundAddress = try selectedAccount.accountId.toAddress(
using: utilityAsset.chain.chainFormat
)

return MercuryoCardParams(
chainAsset: utilityAsset,
refundAddress: refundAddress
)
}

resultOperation.addDependency(chainFetchWrapper.targetOperation)

return chainFetchWrapper.insertingTail(operation: resultOperation)
}
}
Loading

0 comments on commit 747713b

Please sign in to comment.