TAAP Working Paper: Using Square POS API from Android
Square’s own developer site
Configure app security on Square Developer Portal.
Security is via a combination of Android Package Name and Fingerprint (SHA11) which need to be registered with Square and are validated on each transaction.
Android Example
To Build
Add to Gradle build process
Initiate a transaction
Set the application ID.
private static final String APPLICATION_ID = "q0idp-pv9tU96ReVLMH99LV5djmQ";
Add a constructor that configures an instance of PosClient.
public class MainActivity extends Activity {
private PosClient posClient;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Replace APPLICATION_ID with a Square-assigned application ID
posClient = PosSdk.createClient(this, APPLICATION_ID);
}
}
Add code that creates a new charge request and uses it to initiate a transaction in the Point of Sale app. The function below uses ChargeRequest.Builder to create a charge for 1 GBP, then uses that request to create a ChargeRequest and initiate the transaction with an Intent object.
// create a new charge request and initiate a Point of Sale transaction
//
private static final int CHARGE_REQUEST_CODE = 1;
public void startTransaction() {
ChargeRequest request = new ChargeRequest.Builder(
100, // £1.00
CurrencyCode.GBP)
.build();
try {
Intent intent = posClient.createChargeIntent(request);
startActivityForResult(intent, CHARGE_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
AlertDialogHelper.showDialog(
this,
"Error",
"Square Point of Sale is not installed"
);
posClient.openPointOfSalePlayStoreListing();
}
}
AlertDialogHelper
is a helper class to create an alert dialog box, I assume we already have this or can return a failure DQ result.
Receive data from the Square Point of Sale app
When the Square Point of Sale app completes a transaction (or encounters an error), it returns UI focus to your app and calls the onActivityResult()
method of the activity that initiated the transaction.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Handle unexpected errors
if (data == null || requestCode != CHARGE_REQUEST_CODE) {
AlertDialogHelper.showDialog(this,
"Error: unknown",
"Square Point of Sale was uninstalled or stopped working");
return;
}
// Handle expected results
if (resultCode == Activity.RESULT_OK) {
// Handle success
ChargeRequest.Success success = posClient.parseChargeSuccess(data);
AlertDialogHelper.showDialog(this,
"Success",
"Client transaction ID: "
+ success.clientTransactionId);
} else {
// Handle expected errors
ChargeRequest.Error error = posClient.parseChargeError(data);
AlertDialogHelper.showDialog(this,
"Error" + error.code,
"Client transaction ID: "
+ error.debugDescription);
}
return;
}
Misc. developer notes
Enable developer support on Android device via Settings > About phone > Build number
& tap 7 times.
Build & install on attached Android device via:
npm run android-linux
Arbirary APKs can be installed via:
adb install name_of.apk
Settings | Value |
---|---|
API access account | rik.watson@ontaap.com |
Payment account | rik.watson@gmail.com |
Application name | com.androidexample |
Application ID | sq0idp-pv9tU96ReVLMH99LV5djmQ |
Sandbox Application ID | sandbox-sq0idp-pv9tU96ReVLMH99LV5djmQ |
Sandbox Access Token | sandbox-sq0atb-n_PdDUuQ-J7usnhDEADBEEF |
Personal Access Token | sq0atp-ZHE5_L-3DJZ9Ti7iDEADBEEF - note that these expire every 6 weeks |
API Version | 2018-09-18 (Latest) |
SquarePosModule.java
keytool -list -v -keystore ~/.android/debug.keystore \
-alias androiddebugkey \
-storepass android \
-keypass android | grep SHA1:
Which will result in something like this:
SHA1: 0D:87:6E:5B:BE:A6:DE:AD:BE:EF:AC:BE:01:5A:0D:A4:AC:5D:07:7C
Use the 0D:...:7C
& add it to the Square Developer Portal
-
Determine SHA1 via: ↩