From bacd251f5e685b0a5f9a9bd57350c62e65efd0d9 Mon Sep 17 00:00:00 2001 From: MahmoudAllam Date: Thu, 3 Aug 2023 07:52:44 +0300 Subject: [PATCH] feat(Wrapper): update keys --- example/src/App.tsx | 8 +- ios/ApplePayRn.swift | 394 +++++++++++++++--------------- tap-payments-apple-pay-rn.podspec | 2 +- 3 files changed, 208 insertions(+), 196 deletions(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index d6206c8..a1e3e35 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -21,8 +21,8 @@ export default function App() { const tapToken = useCallback(async () => { try { const config = { - sandboxKey: 'sk_test_cvSHaplrPNkJO7dhoUxDYjqA', - productionKey: 'sk_live_QglH8V7Fw6NPAom4qRcynDK2', + sandboxKey: 'pk_test_Vlk842B1EA7tDN5QbrfGjYzh', + productionKey: 'pk_live_UYnihb8dtBXm9fDSw1kFlPQA', countryCode: 'US', transactionCurrency: TapCurrencyCode.USD, allowedCardNetworks: [AllowedCardNetworks.VISA], @@ -46,8 +46,8 @@ export default function App() { const applePayToken = useCallback(async () => { try { const config = { - sandboxKey: 'sk_test_cvSHaplrPNkJO7dhoUxDYjqA', - productionKey: 'sk_live_QglH8V7Fw6NPAom4qRcynDK2', + sandboxKey: 'pk_test_Vlk842B1EA7tDN5QbrfGjYzh', + productionKey: 'pk_live_UYnihb8dtBXm9fDSw1kFlPQA', countryCode: 'US', transactionCurrency: TapCurrencyCode.USD, allowedCardNetworks: [AllowedCardNetworks.VISA], diff --git a/ios/ApplePayRn.swift b/ios/ApplePayRn.swift index 6a66762..84d4944 100644 --- a/ios/ApplePayRn.swift +++ b/ios/ApplePayRn.swift @@ -4,231 +4,243 @@ import CommonDataModelsKit_iOS @objc(ApplePayRn) class ApplePayRn: NSObject { - - let controller = RCTPresentedViewController() - let tapApplePay: TapApplePay = .init() - - var argsDataSource:[String:Any]? = [:] - - - public var sdkMode: SDKMode { - if let sdkModeInt: Int = argsDataSource?["environmentMode"] as? Int { - switch (sdkModeInt) { - case 0: - return .production - case 1: - return .sandbox - default: + + let controller = RCTPresentedViewController() + let tapApplePay: TapApplePay = .init() + var argsDataSource:[String:Any]? = [:] + + + public var sdkMode: SDKMode { + if let sdkModeInt: Int = argsDataSource?["environmentMode"] as? Int { + switch (sdkModeInt) { + case 0: + return .production + case 1: + return .sandbox + default: + return .sandbox + } + } return .sandbox - } - } - return .sandbox - } - - public var sandboxKey: String? { - if let secretKeyString: String = argsDataSource?["sandboxKey"] as? String { - return secretKeyString } - return nil - } - - public var productionKey: String? { - if let secretKeyString: String = argsDataSource?["productionKey"] as? String { - return secretKeyString - } - return nil - } - - public var transactionCurrency: TapCurrencyCode? { - if let transactionCurrencyString: String = argsDataSource?["transactionCurrency"] as? String { - return TapCurrencyCode(appleRawValue: transactionCurrencyString) - } - return nil - } - - public var merchantID: String? { - if let merchantIDString: String = argsDataSource?["merchantId"] as? String { - return merchantIDString - } - return nil - } - - - public var amount: Double? { - if let amountDouble: Double = argsDataSource?["amount"] as? Double { - return amountDouble + + public var sandboxKey: String? { + if let secretKeyString: String = argsDataSource?["sandboxKey"] as? String { + return secretKeyString + } + return nil } - return nil - } - - - public var countryCode: TapCountryCode? { - if let countryCodeString: String = argsDataSource?["countryCode"] as? String { - return TapCountryCode(rawValue: countryCodeString.uppercased()) + + public var productionKey: String? { + if let secretKeyString: String = argsDataSource?["productionKey"] as? String { + return secretKeyString + } + return nil } - return nil - } - - public var paymentNetworks: [TapApplePayPaymentNetwork] { - if let arrayString: [String] = argsDataSource?["allowedCardNetworks"] as? [String] { - var tapApplePayPaymentNetwork: [TapApplePayPaymentNetwork] = [] - arrayString.forEach { - if let method = TapApplePayPaymentNetwork(rawValue: $0){ - tapApplePayPaymentNetwork.append(method) + + public var transactionCurrency: TapCurrencyCode? { + if let transactionCurrencyString: String = argsDataSource?["transactionCurrency"] as? String { + return TapCurrencyCode(appleRawValue: transactionCurrencyString) } - } - return tapApplePayPaymentNetwork + return nil } - return [] - } - - public var merchantCapability: PKMerchantCapability { - var merchantCapabilityArray: PKMerchantCapability = [] - if let intArray: [Int] = argsDataSource?["merchantCapabilities"] as? [Int] { - intArray.forEach { - switch($0) { - case 0: - merchantCapabilityArray.update(with: .capability3DS) - break - case 1: - merchantCapabilityArray.update(with:.capabilityCredit) - break - case 2: - merchantCapabilityArray.update(with:.capabilityDebit) - break - case 3: - merchantCapabilityArray.update(with:.capabilityEMV) - break - default: break + + public var merchantID: String? { + if let merchantIDString: String = argsDataSource?["merchantId"] as? String { + return merchantIDString } - } + return nil } - return merchantCapabilityArray - } - - - - @objc(generateApplePayRawToken:withResolver:withRejecter:) - func generateApplePayRawToken(arguments: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void { - guard let request = generateRequest(arguments: arguments) else { - reject("Please Enter correct values", "Please Enter correct values", "wrong params") - return + + + public var amount: Double? { + if let amountDouble: Double = argsDataSource?["amount"] as? Double { + return amountDouble + } + return nil } - - tapApplePay.authorizePayment(in: controller!, for: request) { token in - resolve(["stringAppleToken": token.stringAppleToken]) + + + public var countryCode: TapCountryCode? { + if let countryCodeString: String = argsDataSource?["countryCode"] as? String { + return TapCountryCode(rawValue: countryCodeString.uppercased()) + } + return nil } - } - - - @objc(generateTapApplePayToken:withResolver:withRejecter:) - func generateTapApplePayToken(arguments: NSDictionary, resolve: @escaping RCTPromiseResolveBlock,reject: @escaping RCTPromiseRejectBlock) -> Void { - guard let request = generateRequest(arguments: arguments) else { - reject("Please Enter correct values", "Please Enter correct values", "wrong params") - return + + public var paymentNetworks: [TapApplePayPaymentNetwork] { + if let arrayString: [String] = argsDataSource?["allowedCardNetworks"] as? [String] { + var tapApplePayPaymentNetwork: [TapApplePayPaymentNetwork] = [] + arrayString.forEach { + if let method = TapApplePayPaymentNetwork(rawValue: $0){ + tapApplePayPaymentNetwork.append(method) + } + } + return tapApplePayPaymentNetwork + } + return [] } - - tapApplePay.authorizePayment(in: controller!, for: request) { token in - self.tapApplePay.createTapToken(for: token, onTokenReady: { tapToken in - resolve(tapToken.dictionary) - }, onErrorOccured: { (session, result, error) in - reject("createTapTokenError", error.debugDescription, "createTapTokenError") - }) + + public var merchantCapability: PKMerchantCapability { + var merchantCapabilityArray: PKMerchantCapability = [] + if let intArray: [Int] = argsDataSource?["merchantCapabilities"] as? [Int] { + intArray.forEach { + switch($0) { + case 0: + merchantCapabilityArray.update(with: .capability3DS) + break + case 1: + merchantCapabilityArray.update(with:.capabilityCredit) + break + case 2: + merchantCapabilityArray.update(with:.capabilityDebit) + break + case 3: + merchantCapabilityArray.update(with:.capabilityEMV) + break + default: break + } + } + } + return merchantCapabilityArray } - } - - - private func generateRequest(arguments: NSDictionary) -> TapApplePayRequest? { - guard let arguments = arguments as? [String: Any] else { - return nil + + + + @objc(generateApplePayRawToken:withResolver:withRejecter:) + func generateApplePayRawToken(arguments: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void { + self.generateRequest(arguments: arguments) { result in + guard let request = result else { + reject("Please Enter correct values", "Please Enter correct values", "wrong params") + return + } + self.tapApplePay.authorizePayment(in: self.controller!, for: request) { token in + resolve(["stringAppleToken": token.stringAppleToken]) + } onErrorOccured: { error in + reject("createTapTokenError", error.TapApplePayRequestValidationErrorRawValue(), "createTapTokenError") + } + } } - argsDataSource = arguments - guard let productionKey = productionKey, - let sandboxKey = sandboxKey, - let amount = amount, - let merchantID = merchantID, - let transactionCurrency = transactionCurrency, - let countryCode = countryCode else { - return nil + @objc(generateTapApplePayToken:withResolver:withRejecter:) + func generateTapApplePayToken(arguments: NSDictionary, resolve: @escaping RCTPromiseResolveBlock,reject: @escaping RCTPromiseRejectBlock) -> Void { + self.generateRequest(arguments: arguments) { request in + guard let request = request else { + reject("Please Enter correct values", "Please Enter correct values", "wrong params") + return + } + self.tapApplePay.authorizePayment(in: self.controller!, for: request) { token in + self.tapApplePay.createTapToken(for: token, onTokenReady: { tapToken in + resolve(tapToken.dictionary) + }, onErrorOccured: { (session, result, error) in + reject("createTapTokenError", error.debugDescription, "createTapTokenError") + }) + } onErrorOccured: { error in + reject("createTapTokenError", error.TapApplePayRequestValidationErrorRawValue(), "createTapTokenError") + } + } } - TapApplePay.sdkMode = sdkMode - TapApplePay.secretKey = .init(sandbox: sandboxKey, - production: productionKey) - let myTapApplePayRequest:TapApplePayRequest = .init() - myTapApplePayRequest.build(with: countryCode, paymentNetworks: paymentNetworks, paymentItems: [], paymentAmount: amount, currencyCode: transactionCurrency,merchantID:merchantID, merchantCapabilities: merchantCapability) - return myTapApplePayRequest - } + + + private func generateRequest(arguments: NSDictionary, callback: @escaping (TapApplePayRequest?) -> Void) { + guard let arguments = arguments as? [String: Any] else { + callback(nil) + return + } + + argsDataSource = arguments + + guard let productionKey = productionKey, + let sandboxKey = sandboxKey, + let amount = amount, + let merchantID = merchantID, + let transactionCurrency = transactionCurrency else { + callback(nil) + return + } + + TapApplePay.sdkMode = sdkMode + TapApplePay.secretKey = .init(sandbox: sandboxKey, + production: productionKey) + TapApplePay.setupTapMerchantApplePay(merchantKey: .init(sandbox: sandboxKey, + production: productionKey)) { [self] in + let myTapApplePayRequest:TapApplePayRequest = .init() + myTapApplePayRequest.build(paymentNetworks: paymentNetworks, paymentItems: [], paymentAmount: amount, currencyCode: transactionCurrency,merchantID:merchantID, merchantCapabilities: self.merchantCapability) + callback(myTapApplePayRequest) + } onErrorOccured: { error in + callback(nil) + } + } } private enum CodingKeys: String, CodingKey { - case identifier = "id" - case object = "object" - case card = "card" - case type = "type" - case creationDate = "creationDate" - case clientIPAddress = "clientIPAddress" - case isLiveMode = "isLiveMode" - case isUsed = "isUsed" - case lastFourDigits = "lastFour" - case expirationMonth = "expMonth" - case expirationYear = "expYear" - case binNumber = "firstSix" - case brand = "brand" - case funding = "funding" - case cardholderName = "name" - case customerIdentifier = "customer" - case fingerprint = "fingerprint" - case address = "address" + case identifier = "id" + case object = "object" + case card = "card" + case type = "type" + case creationDate = "creationDate" + case clientIPAddress = "clientIPAddress" + case isLiveMode = "isLiveMode" + case isUsed = "isUsed" + case lastFourDigits = "lastFour" + case expirationMonth = "expMonth" + case expirationYear = "expYear" + case binNumber = "firstSix" + case brand = "brand" + case funding = "funding" + case cardholderName = "name" + case customerIdentifier = "customer" + case fingerprint = "fingerprint" + case address = "address" } extension TokenizedCard: Encodable { - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(identifier, forKey: .identifier) - try container.encode(object, forKey: .object) - try container.encode(lastFourDigits, forKey: .lastFourDigits) - try container.encode(expirationMonth, forKey: .expirationMonth) - try container.encode(expirationYear, forKey: .expirationYear) - try container.encode(binNumber, forKey: .binNumber) - try container.encode(brand, forKey: .brand) - try container.encode(funding, forKey: .funding) - try container.encode(cardholderName, forKey: .cardholderName) - try container.encode(customerIdentifier, forKey: .customerIdentifier) - try container.encode(fingerprint, forKey: .fingerprint) - try container.encode(address, forKey: .address) - } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(identifier, forKey: .identifier) + try container.encode(object, forKey: .object) + try container.encode(lastFourDigits, forKey: .lastFourDigits) + try container.encode(expirationMonth, forKey: .expirationMonth) + try container.encode(expirationYear, forKey: .expirationYear) + try container.encode(binNumber, forKey: .binNumber) + try container.encode(brand, forKey: .brand) + try container.encode(funding, forKey: .funding) + try container.encode(cardholderName, forKey: .cardholderName) + try container.encode(customerIdentifier, forKey: .customerIdentifier) + try container.encode(fingerprint, forKey: .fingerprint) + try container.encode(address, forKey: .address) + } } extension TokenType: Encodable { - + } extension Token: Encodable { - - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(identifier, forKey: .identifier) - try container.encode(object, forKey: .object) - try container.encode(card, forKey: .card) - try container.encode(type, forKey: .type) - try container.encode(creationDate, forKey: .creationDate) - try container.encode(clientIPAddress, forKey: .clientIPAddress) - try container.encode(isLiveMode, forKey: .isLiveMode) - try container.encode(isUsed, forKey: .isUsed) - } + + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(identifier, forKey: .identifier) + try container.encode(object, forKey: .object) + try container.encode(card, forKey: .card) + try container.encode(type, forKey: .type) + try container.encode(creationDate, forKey: .creationDate) + try container.encode(clientIPAddress, forKey: .clientIPAddress) + try container.encode(isLiveMode, forKey: .isLiveMode) + try container.encode(isUsed, forKey: .isUsed) + } } extension Encodable { - var dictionary: [String: Any]? { - guard let data = try? JSONEncoder().encode(self) else { return nil } - return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] } - } + var dictionary: [String: Any]? { + guard let data = try? JSONEncoder().encode(self) else { return nil } + return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] } + } } diff --git a/tap-payments-apple-pay-rn.podspec b/tap-payments-apple-pay-rn.podspec index 401670c..37804d9 100644 --- a/tap-payments-apple-pay-rn.podspec +++ b/tap-payments-apple-pay-rn.podspec @@ -17,7 +17,7 @@ Pod::Spec.new do |s| s.source_files = "ios/**/*.{h,m,mm,swift}" s.dependency "React-Core" - s.dependency 'TapApplePayKit-iOS' + s.dependency 'TapApplePayKit-iOS', '1.0.20' # Don't install the dependencies when we run `pod install` in the old architecture. if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then