-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1309 from novasamatech/feature/mercuryo-redirect-…
…api-transition
- Loading branch information
Showing
25 changed files
with
460 additions
and
300 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
novawallet/Modules/PayCard/Mercuryo/Hooks/MercuryoCardHookFactory+ResponseIntercept.swift
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 |
---|---|---|
@@ -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 | ||
) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
novawallet/Modules/PayCard/Mercuryo/Hooks/MercuryoCardHookFactory+Widget.swift
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 |
---|---|---|
@@ -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)] | ||
) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
novawallet/Modules/PayCard/Mercuryo/Hooks/MercuryoCardHookFactory.swift
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 |
---|---|---|
@@ -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] | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
novawallet/Modules/PayCard/Mercuryo/MercuryoCardHookFactory+ResponseIntercept.swift
This file was deleted.
Oops, something went wrong.
55 changes: 0 additions & 55 deletions
55
novawallet/Modules/PayCard/Mercuryo/MercuryoCardHookFactory+Widget.swift
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
novawallet/Modules/PayCard/Mercuryo/MercuryoCardHookFactory.swift
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
novawallet/Modules/PayCard/Mercuryo/MercuryoCardParamsProvider.swift
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 |
---|---|---|
@@ -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) | ||
} | ||
} |
Oops, something went wrong.