Placér dit indhold fhgfvhbher

Integrate the Yourpay mPOS PayButton (Android)

Integrate the Yourpay mPOS PayButton (Android)

Install

The PayButton requires minSdkVersion 17 and compileSdkVersion 26.

  • Add our repository to your project’s build.gradle:
allprojects {
    repositories {
        jcenter()
        maven {
            url "http://releases.payworks.io/artifactory/mpos"
        }
    }
}
  • Add the following exclusion rules to your module’s build.gradle (inside the android section):
packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'LICENSE.txt'
    exclude 'asm-license.txt'
    exclude 'META-INF/ASL2.0'
}
  • Add the libraries to the dependencies section of your module’s build.gradle:
dependencies {
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.google.android.gms:play-services-vision:10.2.1'

 
    implementation 'io.payworks:mpos.android.ui:2.29.0'

    // Add these three dependencies if you want to use a Miura card reader
    implementation 'io.payworks:mpos.java.accessories.miura:2.29.0'
    implementation 'io.payworks:mpos.android.comlinks.bluetooth:2.29.0'
    implementation 'io.payworks:mpos.java.comlinks.tcp:2.29.0'

    // Add these two dependencies if you want to use a Verifone card reader
    implementation 'io.payworks:mpos.java.accessories.vipa:2.29.0'
    implementation 'io.payworks:mpos.java.comlinks.tcp:2.29.0'
}

Perform a payment

This is how you start a payment in mock mode:

void paymentButtonClicked() {
    MposUi ui = MposUi.initialize(this, ProviderMode.MOCK,
            "merchantIdentifier", "merchantSecretKey");

    ui.getConfiguration().setSummaryFeatures(EnumSet.of(
            // Add this line, if you do want to offer printed receipts
            // MposUiConfiguration.SummaryFeature.PRINT_RECEIPT,
            MposUiConfiguration.SummaryFeature.SEND_RECEIPT_VIA_EMAIL)
    );

    // Start with a mocked card reader:
    AccessoryParameters accessoryParameters = new AccessoryParameters.Builder(AccessoryFamily.MOCK)
                                                            .mocked()
                                                            .build();
    ui.getConfiguration().setTerminalParameters(accessoryParameters);

    // Add this line if you would like to collect the customer signature on the receipt (as opposed to the digital signature)
    // ui.getConfiguration().setSignatureCapture(MposUiConfiguration.SignatureCapture.ON_RECEIPT);


    /* When using the Bluetooth Miura, use the following parameters:
    AccessoryParameters accessoryParameters = new AccessoryParameters.Builder(AccessoryFamily.MIURA_MPI)
                                                                     .bluetooth()
                                                                     .build();
    ui.getConfiguration().setTerminalParameters(accessoryParameters);
    */




    /* When using Verifone readers via WiFi or Ethernet, use the following parameters:
    AccessoryParameters accessoryParameters = new AccessoryParameters.Builder(AccessoryFamily.VERIFONE_VIPA)
                                                                     .tcp("192.168.254.123", 16107)
                                                                     .build();
    ui.getConfiguration().setTerminalParameters(accessoryParameters);
    */

    TransactionParameters transactionParameters = new TransactionParameters.Builder()
                                                            .charge(new BigDecimal("5.00"), io.mpos.transactions.Currency.EUR)
                                                            .subject("Bouquet of Flowers")
                                                            .customIdentifier("yourReferenceForTheTransaction")
                                                            .build();

    Intent intent = ui.createTransactionIntent(transactionParameters);
    startActivityForResult(intent, MposUi.REQUEST_CODE_PAYMENT);
}

The result is then delivered to your activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == MposUi.REQUEST_CODE_PAYMENT) {
        if (resultCode == MposUi.RESULT_CODE_APPROVED) {
            // Transaction was approved
            Toast.makeText(this, "Transaction approved", Toast.LENGTH_LONG).show();
        } else {
            // Card was declined, or transaction was aborted, or failed
            // (e.g. no internet or accessory not found)
            Toast.makeText(this, "Transaction was declined, aborted, or failed",
                    Toast.LENGTH_LONG).show();
        }
        // Grab the processed transaction in case you need it
        // (e.g. the transaction identifier for a refund).
        // Keep in mind that the returned transaction might be null
        // (e.g. if it could not be registered).
        Transaction transaction = MposUi.getInitializedInstance().getTransaction();
    }
}

If you have problems with onActivityResult not being called, this might be caused by starting the PayButton Activity from the wrong Activity / Fragment. This Stackoverflow post might help you.

Use a real card reader

You can then retrieve your merchantIdentifier and merchantSecretKey through the Customer Data API and change the ProviderMode to ProviderMode.TEST.

Then create the  AccessoryParameters with the reader family and connection you want to use.

Load merchant data from your backend

Right now, you have hardcoded the merchantIdentifier and merchantSecretKey. This means that all payments would be routed to the same merchant.

For a live solution, you might want to support multiple merchants, e.g. two different restaurants, to route the payment correctly. To support multiple merchants, store the following data on your backend:

  1. merchantIdentifier and merchantSecretKey. They identify to which merchant the payment is routed.
  2. Whether the merchant is a TEST or LIVE merchant.
  3. The reader model the merchant uses.

​You can then fetch this data before a transaction and configure the PayButton correctly:

MposUi ui = MposUi.initialize(this,
    <ProviderMode.TEST or ProviderMode.LIVE, loaded from your backend>,
    <MerchantIdentifier loaded from your backend>, <MerchantSecretKey loaded from your backend>);

Customize the PayButton

UI customization: Change the colors

You can choose the following colors:

  1. setColorPrimary: The icon’s and top action bar’s color
  2. setColorPrimaryDark: The color of the status bar (affects Android L and later)
  3. setTextColorPrimary: The primary text color
ui.getConfiguration().getAppearance()
     .setColorPrimary(Color.parseColor("#9c27b0"))
     .setColorPrimaryDark(Color.parseColor("#7b1fa2"))
     .setTextColorPrimary(Color.WHITE);

 

Was this article helpful?
Dislike 0
Views: 18
Close Menu
×
×

Cart