# Android

{% tabs %}
{% tab title="Prebuilt checkout page" %}

#### 1.Create a Customer

Pass a <mark style="color:blue;">`customerId`</mark> to MoneyCollect and attach it to the customer.

```
   // Use your customerId 
   var customerId = "cus_1452880617225281538"
```

#### 2.Construct dat&#x61;**,** and pay in PaymentSheet model

When creating a payment method, you can specify `customer parameters` to automatically attach the payment method to an existing customer.

```
//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

   //build 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)

   //PayCardActivity contain SaveWithPaymentCardFragment and AddWithPaymentFragment,Support them to switch to each other
   var intent = Intent(this, PayCardActivity::class.java)
   intent.putExtra(CURRENT_PAYMENT_BUNDLE, bundle)   
   //start payment
   startActivityLauncher.launch(intent)
```

{% hint style="info" %}
TestRequestData details please refer to TestRequestData data class under MoneycollectSDK Demo.
{% endhint %}

#### 3.Pay in Hosted Payment Page Mode

![](/files/E6w1dAzk2EJEAQ16nSAZ)

1. Tap <mark style="color:purple;">`Checkout`</mark>button to pay in <mark style="color:purple;">`Hosted Payment Page`</mark> mode.&#x20;
2. In <mark style="color:purple;">`Hosted payment page`</mark>，<mark style="color:purple;">select the previously saved card</mark> or <mark style="color:purple;">add a new card</mark>.&#x20;
3. Finally, tap the <mark style="color:purple;">`Pay Now`</mark> button to complete the payment.

#### 4.Customer selects "save this card" during payment

![](/files/8PBrbVTDgQ9SU7BkDpOi)

4.1 Select <mark style="color:purple;">`Save this card for your future use`</mark> on the Add a new card page.

```
var testRequestPayment = RequestCreatePayment(
       // ...
       paymentMethod = paymentMethodId,
       preAuth = "n",
       receiptEmail = email,
       returnUrl = "http://localhost:8080/return",
       setupFutureUsage = "on",
       // ...
)
```

Select the payment card in the future, when  <mark style="color:blue;">`setupFutureUsage`</mark> set to <mark style="color:blue;">`on`</mark>.

4.2 Click <mark style="color:purple;">`Add a new card`</mark>.

4.3 After creating a new payment method and attaching it to customer,  the card details will be saved to customer.

#### 5.Return the payment result

```
 private val startActivityLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            //selected paymentmethods's list
            if (it.resultCode == Constant.PAYMENT_RESULT_CODE) {
                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)
                        }
                    }
                }
            }

        }
```

{% endtab %}

{% tab title="Custom payment flow" %}

#### **1.Create a Customer**

Pass a <mark style="color:blue;">`customerId`</mark> to MoneyCollect and attach it to the customer.

```
// Use your customerId 
   var customerId = "cus_1452880617225281538"
```

#### **2.**&#x43;onstruct dat&#x61;**,** and add payment methods with PaymentSheet customizable models

When creating a payment method, you can specify `customer parameters` to automatically attach the payment method to an existing customer.

```
   //the address of create paymentmethod
    var address = Address(
          line1 = "123 Main Street",
          line2 = "number456",
          city = "Blackrock",
          state = "Co. Dublin",
          postalCode = "T37 F8HK",
          country = "IE",
    )

    //the BillingDetails of create paymentmethod
    var billingDetails = RequestPaymentMethod.BillingDetails(
          address,
          email = "abc@moneycollect.com",
          firstName = "John",
          lastName = "Doe",
          phone = "+18008675309"
    )

    //RequestPaymentMethod object
    var  testRequestPaymentMethod=RequestPaymentMethod(
         "card",
         billingDetails,
         null
    )

    //support payment credit card
    var testBankIvList= arrayListOf(
         R.drawable.mc_card_visa,
         R.drawable.mc_card_mastercard,
         R.drawable.mc_card_ae,
         R.drawable.mc_card_jcb,
         R.drawable.mc_card_dinner,
         R.drawable.mc_card_discover,
         R.drawable.mc_card_maestro
   )

   //build Bundle 
    var bundle = Bundle()

    //pass currentPaymentModel
    bundle.putSerializable(Constant.CURRENT_PAYMENT_MODEL, MoneyCollectPaymentModel.ATTACH_PAYMENT_METHOD)

    //pass customerId 
    bundle.putString(Constant.CUSTOMER_ID_TAG,customerId)

    //pass default RequestPaymentMethod
    bundle?.putParcelable(Constant.CREATE_PAYMENT_METHOD_REQUEST_TAG, testRequestPaymentMethod)

    //pass default supportBankList
    bundle?.putSerializable(Constant.SUPPORT_BANK_LIST_TAG,testBankIvList)

    //SaveCardActivitycontain SaveCardFragment and AddCardFragment,Support them to switch to each other
    var intent = Intent(this, SaveCardActivity::class.java)
    intent.putExtra(Constant.CURRENT_PAYMENT_BUNDLE, bundle)
    //start payment
    startActivityLauncher.launch(intent)
```

#### 3.Customer selects <mark style="color:purple;">`save this card for your future use`</mark> during payment

![](/files/WzV60OUZweQBvQmGDTXo)

3.1 Select <mark style="color:purple;">`Save this card for your future use`</mark>on the <mark style="color:purple;">`Add a card`</mark> page.

```
var testRequestPayment = RequestCreatePayment(
       // ...
       paymentMethod = paymentMethodId,
       preAuth = "n",
       receiptEmail = email,
       returnUrl = "http://localhost:8080/return",
       setupFutureUsage = "on",
       // ...
)
```

You can use the payment card in the future when<mark style="color:blue;">`setupFutureUsage`</mark> set to <mark style="color:purple;">`on`</mark>, otherwise setting to <mark style="color:purple;">`off`</mark>.

3.2 Click <mark style="color:purple;">`Add a card`</mark>.

3.3 After creating a payment method and attaching the payment method to customer, the card details will be saved to the customer.

#### 4.Customer selects the card to make payment

![](/files/Y4CjePz4p2g4c5RIHOFA)

Click on the <mark style="color:purple;">`Pay Now`</mark> button to complete the transaction.

### 5.Return the payment result

The payment result is obtained by calling the <mark style="color:blue;">`confirmPayment`</mark> API for payment confirmation, and then the result data is passed to the component.

```
  moneyCollect.confirmPayment(requestConfirmPayment, payment.clientSecret,
                object : ApiResultCallback<Payment> {
                    override fun onSuccess(result: Payment) {
                            isLoadingAnimStatus = false
                            viewBinding?.paymentCheckoutBtn?.setCardConfirmButtonStatus(true)
                            if (result.status.equals(Constant.PAYMENT_SUCCEEDED)) {
                                viewBinding?.paymentCheckoutBtn?.setMoneyCollectButtonViewContext(null)
                                viewBinding?.paymentCheckoutBtn?.setMoneyCollectButtonViewModel(moneyCollectPaymentModel)
                                viewBinding?.paymentCheckoutBtn?.showAnimByPaymentCompleteAndRefresh()
                            } else {
                                viewBinding?.paymentCheckoutBtn?.stopPaymentAnim()
                            }
                            when (result.status) {
                                Constant.PAYMENT_SUCCEEDED -> {
                                }
                                Constant.PAYMENT_FAILED -> {
                                    viewBinding?.paymentErrorMessageTv?.setText(result.errorMessage)
                                }
                                Constant.PAYMENT_UN_CAPTURED -> {
                                    viewBinding?.paymentErrorMessageTv?.setText(Constant.PAYMENT_UN_CAPTURED_MESSAGE)
                                }
                                Constant.PAYMENT_PENDING -> {
                                    viewBinding?.paymentErrorMessageTv?.setText(Constant.PAYMENT_PENDING_MESSAGE)
                                }
                                Constant.PAYMENT_CANCELED -> {
                                    viewBinding?.paymentErrorMessageTv?.setText(Constant.PAYMENT_CANCELED_MESSAGE)
                                }
                                else -> {
                                    viewBinding?.paymentErrorMessageTv?.setText(Constant.PAYMENT_PENDING_MESSAGE)
                                }
                            }
                    }

                    override fun onError(e: Exception) {
                        isLoadingAnimStatus = false
                        viewBinding?.paymentCheckoutBtn?.stopPaymentAnim()
                        viewBinding?.paymentCheckoutBtn?.setCardConfirmButtonStatus(true)
                        viewBinding?.paymentErrorMessageTv?.visibility == View.VISIBLE
                        viewBinding?.paymentErrorMessageTv?.setText(e.message)
                    }
                })
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.moneycollect.com/docs/payment/save-a-card-during-payment/android.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
