Skip to content

Update Android to Current Square Documentation #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RNSquarePos.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.license = "MIT"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
s.author = { "author" => "[email protected]" }
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
Expand Down
10 changes: 5 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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.+'
}


6 changes: 4 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.matix">

package="io.matix">
<queries>
<package android:name="com.squareup" />
</queries>
</manifest>

248 changes: 134 additions & 114 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;

2 changes: 2 additions & 0 deletions ios/RNSquarePos.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"]) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down