Payments
After integrating the myPOS Glass SDK into your project, you can start using the Payment API via the provided helper classes. This section explains how to retrieve POS terminal information, initiate a payment, and handle the payment result.
1. Retrieve POS Information
To access POS terminal metadata (such as TID, currency, or merchant details), follow these steps:
Step 1: Update AndroidManifest.xml
Add the following query to declare your intent to communicate with the myPOS Glass app:
In your module-level build.gradle file (usually under app/), add the SDK dependency:
<queries>
<package android:name="com.mypos.top" />
</queries>
Step 2: Request POS Info
Use the MyPOSAPI.registerPOSInfo() method to retrieve POS details:
MyPOSAPI.registerPOSInfo(MainActivity.this, new OnPOSInfoListener() {
@Override
public void onReceive(POSInfo info) {
//info is received
}
});
2. Make a Payment
Step 1: Build and Start the Payment
Use the MyPOSPayment.builder() to configure and initiate the transaction:
// Build the payment call
MyPOSPayment payment = MyPOSPayment.builder()
// Mandatory parameters
.productAmount(13.37)
.currency(Currency.EUR)
// Foreign transaction ID. Maximum length: 128 characters
.foreignTransactionId(UUID.randomUUID().toString())
// Optional parameters
// Enable tipping mode
.tippingModeEnabled(true)
.tipAmount(1.55)
// Operator code. Maximum length: 4 characters
.operatorCode("1234")
// Reference number. Maximum length: 20 alpha numeric characters
.reference("asd123asd", ReferenceType.REFERENCE_NUMBER)
// card scheme brandnig
.mastercardSonicBranding(true)
.visaSensoryBranding(true)
// Set receipt mode if printer is paired
.printMerchantReceipt(MyPOSUtil.RECEIPT_ON) // possible options RECEIPT_ON, RECEIPT_OFF
.printCustomerReceipt(MyPOSUtil.RECEIPT_ON) // possible options RECEIPT_ON, RECEIPT_OFF, RECEIPT_AFTER_CONFIRMATION,
// RECEIPT_E_RECEIPT
//set email or phone e-receipt receiver, works with customer receipt configuration RECEIPT_E_RECEIPT or
// RECEIPT_AFTER_CONFIRMATION
.ereceiptreceiver("examplename@example.com")
.build();
// Start the transaction
MyPOSAPI.openPaymentActivity(MainActivity.this, payment, 1);
Step 2: Handle the Payment Result
Override the onActivityResult() method in your calling activity to capture the transaction outcome:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// The same request code as when calling MyPOSAPI.openPaymentActivity
if (requestCode == 1) {
// The transaction was processed, handle the response
if (resultCode == RESULT_OK) {
// Something went wrong in the Payment core app and the result couldn't be returned properly
if (data == null) {
Toast.makeText(this, "Transaction cancelled", Toast.LENGTH_SHORT).show();
return;
}
int transactionResult = data.getIntExtra("status", TransactionProcessingResult.TRANSACTION_FAILED);
Toast.makeText(this, "Payment transaction has completed. Result: " + transactionResult, Toast.LENGTH_SHORT).show();
// TODO: handle each transaction response accordingly
if (transactionResult == TransactionProcessingResult.TRANSACTION_SUCCESS) {
// Transaction is successful
}
} else {
// The user cancelled the transaction
Toast.makeText(this, "Transaction cancelled", Toast.LENGTH_SHORT).show();
}
}
}
3. Check Transaction Approval
To verify if the transaction was approved:
boolean transaction_approved = data.getBooleanExtra("transaction_approved", false);
if (transaction_approved) {
// Transaction is approved
} else {
// Transaction was not approved
// The response code is in the "response_code" string extra
}