Android

Set up MoneyCollect Server side Client side

Firstly, you need a MoneyCollect account. Register now.

1.Set up Server-side

If the client side wants to access most of the MoneyConnectServer API, it needs MoneyCollect dashboard to initiate a request through the private key.

Download the mock merchant portal interface code on github.

1.1 Using your own key to replace the public key and private key in the MobilePayController.java file

//Your account PUBLIC_SECRET("Bearer "+PUBLIC_SECRET)
private static final String PUBLIC_SECRET = "Bearer live_pu_OGJ0EidwEg4GjymEiRD7cUBk7IQIYmhwhJlUM****";
//Your account PRIVATE_SECRET("Bearer "+PRIVATE_SECRET)
private static final String PRIVATE_SECRET = "Bearer live_pr_OGJ0EidwEg4GjymEiRD4MRxBCo0OumdH6URv****";

The format of PUBLIC_SECRET & PRIVATE_SECRET is("Bearer"+PUBLIC_SECRET)

1.2 Modify server port(Default to be 9898)

server.port=9898

The merchant replaces the public key and private key in the code with their own, and then switch on the server. (Default server port is 9099 which can be modified).(The dashboard interface address is the local ip:9898)

2.Set up Client-side

Import MoneyCollect android SDK and initialize SDK.

2.1 Add configuration in build.gradle file

To install the SDK, add moneycollect-android to the dependencies block of your build.gradle file:

repositories {
   jcenter()
   maven{ url "https://raw.githubusercontent.com/MoneyCollect/moneycollect-api-android-demo/mcsdk" }
 }

2.2 Add viewbinding and the MoneyCollect library to the app’s main module build.gradle

buildFeatures{
        viewBinding = true
   }
dependencies {
   //The specific version number will be determined according to your needs
   implementation "com.moneycollect.payment:android_mc:0.0.1"
}

2.3 Initialize SDK

Initialize MoneyCollect android sdk() in the project application

/**
* context: Context,            (context)
* publishableKey: String,      (publishable key)
* customerServerUrl: String?   (Local ip:9898)
**/
MoneyCollectSdk.init(this, "live_pu_OGJ0EidwEg4GjymEiRD7cUBk7IQIYmhwhJlUM****","http://192.168.2.100:9898/")

3.Construct the data parameters to initiate transaction, and then start the payment activity

The merchant constructs the transaction request parameters and clicks the Checkout button to start the payment activity.(TestRequestData is data constant class,please check moneycollect-api-android-demo for more details)

// ...
class PaymentSheetDemoActivity: AppCompatActivity() {

    //RequestCreatePayment Object
    var testRequestPayment = TestRequestData.testRequestPayment
    //RequestConfirmPayment Object
    var testConfirmPayment = TestRequestData.testConfirmPayment
    //RequestPaymentMethod Object
    var testRequestPaymentMethod = TestRequestData.testRequestPaymentMethod
    //support payment credit card
    var testBankIvList = TestRequestData.testBankIvList
    //customerId
    var customerId = TestRequestData.customerId
    // ...
    fun presentPaymentSheet() {
        //PayCardActivity contain SaveWithPaymentCardFragment and AddWithPaymentFragment,Support them to switch to each other
        var intent = Intent(this, PayCardActivity::class.java)
        //Bundle Object
        var bundle = Bundle()
        //pass currentPaymentModel
        bundle.putSerializable(Constant.CURRENT_PAYMENT_MODEL, currentPaymentModel)
        //pass RequestCreatePayment
        bundle.putParcelable(Constant.CREATE_PAYMENT_REQUEST_TAG, testRequestPayment)
        //pass RequestConfirmPayment
        bundle.putParcelable(Constant.CONFIRM_PAYMENT_REQUEST_TAG, testConfirmPayment)
        //pass customerId
        bundle.putString(Constant.CUSTOMER_ID_TAG, TestRequestData.customerId)
        //pass default RequestPaymentMethod
        bundle?.putParcelable(Constant.CREATE_PAYMENT_METHOD_REQUEST_TAG, testRequestPaymentMethod)
        //pass default supportBankList
        bundle?.putSerializable(Constant.SUPPORT_BANK_LIST_TAG, testBankIvList)
        intent.putExtra(CURRENT_PAYMENT_BUNDLE, bundle)
        //start payment
        startActivityLauncher.launch(intent)
    }

    private val startActivityLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        }
}

After the customer completes the payment by clicking Pay Now button, the payment activity will be dismissed and return to the PaymentSheetDemoActivity. At the same time,will callback PAYMENT_RESULT_PAYMENT.

// ...
class PaymentSheetDemoActivity: AppCompatActivity() {

     private val startActivityLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Constant.PAYMENT_RESULT_CODE) {
                // resultPayment
                var payment =
                    it.data?.getParcelableExtra<Payment>(Constant.PAYMENT_RESULT_PAYMENT)
                if (payment != null) {
                    when (payment.status) {
                        Constant.PAYMENT_SUCCEEDED -> {
                            Log.e(TAG, Constant.PAYMENT_SUCCEEDED)
                        }
                        Constant.PAYMENT_FAILED -> {
                            payment?.errorMessage?.let { it1 ->
                                Log.e(TAG, it1)
                            }
                        }
                        Constant.PAYMENT_UN_CAPTURED -> {
                            Log.e(TAG, Constant.PAYMENT_UN_CAPTURED_MESSAGE)
                        }
                        Constant.PAYMENT_PENDING -> {
                            Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE)
                        }
                        Constant.PAYMENT_CANCELED -> {
                            Log.e(TAG, Constant.PAYMENT_CANCELED_MESSAGE)
                        }
                        else -> {
                            Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE)
                       }
                    }
                }
            }

        }
}

Use the completion block for handling the payment result.

If payment fails with an error, display the appropriate message to your customer so they can take action and try again. If no error has occurred, tell your customer that the payment was successful.

4.Additional testing resources

There are several test cards you can use to make sure your integration is ready for production. Use them with any CVC, postal code, and future expiration date.

Last updated