diff --git a/RNSquarePos.podspec b/RNSquarePos.podspec index 17c5938..56e7aa4 100644 --- a/RNSquarePos.podspec +++ b/RNSquarePos.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "RNSquarePos" - s.version = "1.0.13" + s.version = "1.1.0" s.summary = "RNSquarePos" s.description = <<-DESC RNSquarePos @@ -10,7 +10,7 @@ Pod::Spec.new do |s| s.license = "MIT" # s.license = { :type => "MIT", :file => "FILE_LICENSE" } s.author = { "author" => "connor@matix.io" } - s.platform = :ios, "7.0" + s.platform = :ios, "12.0" s.source = { :git => "https://github.com/matix-io/react-native-square-pos.git", :tag => "master" } s.source_files = "ios/**/*.{h,m}" s.requires_arc = true diff --git a/android/build.gradle b/android/build.gradle index 84a0ab9..8436987 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -12,12 +12,12 @@ buildscript { apply plugin: 'com.android.library' android { - compileSdkVersion 28 + compileSdkVersion 30 buildToolsVersion "28.0.3" defaultConfig { minSdkVersion 16 - targetSdkVersion 22 + targetSdkVersion 30 versionCode 1 versionName "1.0" } @@ -31,7 +31,7 @@ repositories { } dependencies { - compile 'com.facebook.react:react-native:+' - compile 'com.squareup.sdk:point-of-sale-sdk:2.+' + implementation 'com.facebook.react:react-native:+' + implementation 'com.squareup.sdk:point-of-sale-sdk:2.+' } - \ No newline at end of file + diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index f6cfe39..bb22ded 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,6 +1,8 @@ - + package="io.matix"> + + + \ No newline at end of file diff --git a/index.js b/index.js index 1f2854d..8d31869 100644 --- a/index.js +++ b/index.js @@ -1,130 +1,150 @@ -import { NativeModules, DeviceEventEmitter, Platform, Linking } from 'react-native' -const SquarePOS = NativeModules.RNSquarePos +import { + NativeEventEmitter, + NativeModules, + Platform, + Linking, +} from "react-native"; +const SquarePOS = NativeModules.RNSquarePos; -let callbackUrl +let callbackUrl; const errors = { - CANNOT_OPEN_SQUARE: 'CANNOT_OPEN_SQUARE', - UNKNOWN_IOS_ERROR: 'UNKNOWN_IOS_ERROR', - UNKNOWN_ANDROID_ERROR: 'UNKNOWN_ANDROID_ERROR', - PAYMENT_CANCELLED: 'PAYMENT_CANCELLED', - NOT_LOGGED_IN: 'NOT_LOGGED_IN', - USER_NOT_ACTIVE: 'USER_NOT_ACTIVE', - AMOUNT_TOO_SMALL: 'AMOUNT_TOO_SMALL', - AMOUNT_TOO_LARGE: 'AMOUNT_TOO_LARGE', - INVALID_TENDER_TYPE: 'INVALID_TENDER_TYPE', - UNSUPPORTED_TENDER_TYPE: 'UNSUPPORTED_TENDER_TYPE', - NO_NETWORK_CONNECTION: 'NO_NETWORK_CONNECTION', - TRANSACTION_ALREADY_IN_PROGRESS: 'TRANSACTION_ALREADY_IN_PROGRESS', - INVALID_REQUEST: 'INVALID_REQUEST', -} + CANNOT_OPEN_SQUARE: "CANNOT_OPEN_SQUARE", + UNKNOWN_IOS_ERROR: "UNKNOWN_IOS_ERROR", + UNKNOWN_ANDROID_ERROR: "UNKNOWN_ANDROID_ERROR", + PAYMENT_CANCELLED: "PAYMENT_CANCELLED", + NOT_LOGGED_IN: "NOT_LOGGED_IN", + USER_NOT_ACTIVE: "USER_NOT_ACTIVE", + AMOUNT_TOO_SMALL: "AMOUNT_TOO_SMALL", + AMOUNT_TOO_LARGE: "AMOUNT_TOO_LARGE", + INVALID_TENDER_TYPE: "INVALID_TENDER_TYPE", + UNSUPPORTED_TENDER_TYPE: "UNSUPPORTED_TENDER_TYPE", + NO_NETWORK_CONNECTION: "NO_NETWORK_CONNECTION", + TRANSACTION_ALREADY_IN_PROGRESS: "TRANSACTION_ALREADY_IN_PROGRESS", + INVALID_REQUEST: "INVALID_REQUEST", +}; const iosErrors = { - payment_canceled: errors.PAYMENT_CANCELLED, - not_logged_in: errors.NOT_LOGGED_IN, - user_not_active: errors.USER_NOT_ACTIVE, - amount_too_small: errors.AMOUNT_TOO_SMALL, - amount_too_large: errors.AMOUNT_TOO_LARGE, - invalid_tender_type: errors.INVALID_TENDER_TYPE, - unsupported_tender_type: errors.UNSUPPORTED_TENDER_TYPE, - no_network_connection: errors.NO_NETWORK_CONNECTION, -} + payment_canceled: errors.PAYMENT_CANCELLED, + not_logged_in: errors.NOT_LOGGED_IN, + user_not_active: errors.USER_NOT_ACTIVE, + amount_too_small: errors.AMOUNT_TOO_SMALL, + amount_too_large: errors.AMOUNT_TOO_LARGE, + invalid_tender_type: errors.INVALID_TENDER_TYPE, + unsupported_tender_type: errors.UNSUPPORTED_TENDER_TYPE, + no_network_connection: errors.NO_NETWORK_CONNECTION, +}; const androidErrors = { - TRANSACTION_CANCELED: errors.PAYMENT_CANCELLED, - USER_NOT_LOGGED_IN: errors.NOT_LOGGED_IN, - USER_NOT_ACTIVATED: errors.USER_NOT_ACTIVE, - NO_NETWORK: errors.NO_NETWORK_CONNECTION, - TRANSACTION_ALREADY_IN_PROGRESS: errors.TRANSACTION_ALREADY_IN_PROGRESS, - INVALID_REQUEST: errors.INVALID_REQUEST, -} + TRANSACTION_CANCELED: errors.PAYMENT_CANCELLED, + USER_NOT_LOGGED_IN: errors.NOT_LOGGED_IN, + USER_NOT_ACTIVATED: errors.USER_NOT_ACTIVE, + NO_NETWORK: errors.NO_NETWORK_CONNECTION, + TRANSACTION_ALREADY_IN_PROGRESS: errors.TRANSACTION_ALREADY_IN_PROGRESS, + INVALID_REQUEST: errors.INVALID_REQUEST, +}; const RNSquarePos = { - ERRORS: errors, - configure: (options) => { - SquarePOS.setApplicationId(options.applicationId) - callbackUrl = options.callbackUrl - }, - transaction: (amount, currency, options = {}) => { - return new Promise((resolve, reject) => { - if (Platform.OS === 'android') { - SquarePOS.startTransaction(amount, currency, options, () => { - return reject({ - errorCode: errors.CANNOT_OPEN_SQUARE - }) - }) + ERRORS: errors, + configure: (options) => { + SquarePOS.setApplicationId(options.applicationId); + callbackUrl = options.callbackUrl; + }, + transaction: (amount, currency, options = {}) => { + let subscription = null; + return new Promise((resolve, reject) => { + if (Platform.OS === "android") { + SquarePOS.startTransaction(amount, currency, options, () => { + return reject({ + errorCode: errors.CANNOT_OPEN_SQUARE, + }); + }); - function handleResponse(data) { - DeviceEventEmitter.removeListener('RNSquarePOSResponse', handleResponse); - if (data.errorCode) { - if (androidErrors[data.errorCode]) { - return reject({ - errorCode: androidErrors[data.errorCode], - squareResponse: data - }) - } else { - return reject({ - errorCode: errors.UNKNOWN_ANDROID_ERROR, - originalCode: data.errorCode, - squareResponse: data - }) - } - } else { - return resolve(data) - } - } + function handleResponse(data) { + subscription.remove(); + subscription = null; + if (data.errorCode) { + if (androidErrors[data.errorCode]) { + return reject({ + errorCode: androidErrors[data.errorCode], + squareResponse: data, + }); + } else { + return reject({ + errorCode: errors.UNKNOWN_ANDROID_ERROR, + originalCode: data.errorCode, + squareResponse: data, + }); + } + } else { + return resolve(data); + } + } + const eventEmitter = new NativeEventEmitter(SquarePOS); + subscription = eventEmitter.addListener( + "RNSquarePOSResponse", + handleResponse + ); + } else if (Platform.OS === "ios") { + SquarePOS.startTransaction( + amount, + currency, + options, + callbackUrl, + (errorCode, errorDescription) => { + switch (errorCode) { + case 6: + reject({ + errorCode: errors.CANNOT_OPEN_SQUARE, + }); + break; - DeviceEventEmitter.addListener('RNSquarePOSResponse', handleResponse); - } else if (Platform.OS === 'ios') { - SquarePOS.startTransaction(amount, currency, options, callbackUrl, (errorCode, errorDescription) => { - switch (errorCode) { - case 6: - reject({ - errorCode: errors.CANNOT_OPEN_SQUARE - }) - break + default: + reject({ + errorCode: errors.UNKNOWN_IOS_ERROR, + errorDescription, + }); + break; + } + } + ); - default: - reject({ - errorCode: errors.UNKNOWN_IOS_ERROR, - errorDescription - }) - break - } - }) + function handleIOSResponse(event) { + const url = event.url; + subscription.remove(); + subscription = null; - function handleIOSResponse(event) { - Linking.removeEventListener('url', handleIOSResponse); - const url = event.url + if (url.match(callbackUrl)) { + const data = JSON.parse( + decodeURIComponent(url.split("?data=")[1]) + ); - if (url.match(callbackUrl)) { - const data = JSON.parse(decodeURIComponent(url.split('?data=')[1])) - - if (data.error_code) { - if (iosErrors[data.error_code]) { - return reject({ - errorCode: iosErrors[data.error_code] - }) - } else { - return reject({ - errorCode: errors.UNKNOWN_IOS_ERROR, - originalCode: data.error_code - }) - } - } else { - return resolve({ - transactionId: data.transaction_id, - clientTransactionId: data.client_transaction_id - }) - } - } - } - - Linking.addEventListener('url', handleIOSResponse); - } - }) - }, -} + if (data.error_code) { + if (iosErrors[data.error_code]) { + return reject({ + errorCode: iosErrors[data.error_code], + }); + } else { + return reject({ + errorCode: errors.UNKNOWN_IOS_ERROR, + originalCode: data.error_code, + }); + } + } else { + return resolve({ + transactionId: data.transaction_id, + clientTransactionId: data.client_transaction_id, + }); + } + } + } + subscription = Linking.addEventListener( + "url", + handleIOSResponse + ); + } + }); + }, +}; export default RNSquarePos; - diff --git a/ios/RNSquarePos.m b/ios/RNSquarePos.m index 6496c08..dc0ad99 100644 --- a/ios/RNSquarePos.m +++ b/ios/RNSquarePos.m @@ -46,6 +46,8 @@ @implementation RNSquarePos currType = SCCAPIRequestTenderTypeCash; } else if ([tenderType isEqualToString:@"CARD"]) { currType = SCCAPIRequestTenderTypeCard; + } else if ([tenderType isEqualToString:@"GIFT_CARD"]) { + currType = SCCAPIRequestTenderTypeSquareGiftCard; } else if ([tenderType isEqualToString:@"CARD_ON_FILE"]) { currType = SCCAPIRequestTenderTypeCardOnFile; } else if ([tenderType isEqualToString:@"OTHER"]) { diff --git a/package.json b/package.json index feb5b72..1427897 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-square-pos", - "version": "1.1.2", + "version": "1.1.3", "description": "React Native wrapper around Square's Android / iOS POS SDKs", "main": "index.js", "scripts": {