Ir para o conteúdo

Pix Payment


Create a pix payment charge

Info

The automatic printing feature is available starting from version 1.3.3.0 of the Pix App.

startPixPayment()

To create a charge, the startPixPayment function is used, it receives a string in json format with the following fields:

Request

{
  "cob_value": "20.00",
  "pix_client_id": "228dbb16-1b8c-4503-a1ac-0531d0202b35",
  "preview_customer_receipt": true,
  "preview_merchant_receipt": true,
  "print_customer_receipt": true,
  "print_merchant_receipt": true
}

Parameters

Name Type Mandatory Description
request startPixPayment Yes Data transfer object that will contain the pix request information. Note that not all parameters are mandatory.
callback StartPixPaymentCallback Yes Interface that will be executed for notifications of success or error in the pix process.

Parameter Details

request (startPixPayment)

Name Type Required Description Default
cob_value String No Charge amount reported by the user. If it is not filled in (null), the interface will request the value. The charge must be sent in a numeric format with two decimal places, as shown in the example Request .
pix_client_id String Yes The pix_client_id is a unique identifier used by the integration app to identify charges created. It supports up to 36 characters.
preview_customer_receipt Boolean No Indicates whether the customer's receipt should be displayed. true
preview_merchant_receipt Boolean No Indicates whether the merchant's receipt should be displayed. true
print_customer_receipt Boolean No Indicates whether the customer's receipt should be printed automatically.
Note: Automatic printing will only occur if the receipt preview is disabled (false).
true
print_merchant_receipt Boolean No Indicates whether the merchant's receipt should be printed automatically.
Note: Automatic printing will only occur if the receipt preview is disabled (false).
true

Parameter Details

callback (PaymentCallback)

Name Type Required Description
onSuccess Cadena Yes Notification method in case of success. The return response will be a string in json format that will contain the fields that are displayed in the class PixResponse
onError Cadena Yes Notification method in case of error. The return response will be a string in json format that will have the fields shown in the class PixErrorResponse
fun cobCreateService(
    pixClient: PixClient,
    value: String?,
    pixClientId: String,
    previewCustomerReceipt: Boolean,
    previewMerchantReceipt: Boolean,
    printCustomerReceipt: Boolean,
    printMerchantReceipt: Boolean,
    context: Context,
    viewModel: CobCreateViewModel
) {
    val gson: Gson = Gson()
    if (pixClient.isBound()) {
        val createCobRequest =
            CreateCobRequest(value, pixClientId, previewCustomerReceipt, previewMerchantReceipt, printCustomerReceipt, printMerchantReceipt)
        val callback = object : PixClient.StartPixPaymentCallback {

            override fun onError(response: String?) {
                println("Pagamento error: $response")

                if (response != null) {
                    val responseError = gson.fromJson(response, PixErrorResponse::class.java)
                    showAlertDialog(context, responseError.errorMessage)
                } else {
                    showAlertDialog(context, "")
                }
                viewModel.resetFilds()
            }

            override fun onSuccess(response: String?) {
                val pixResponse = gson.fromJson(response, PixResponse::class.java)
                println("Pagamento: $pixResponse")

                if (pixResponse != null && pixResponse.status == ChargeStatus.REMOVED_BY_USER ) {
                    Toast.makeText(context, R.string.payment_cancelled, Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(context, R.string.payment_successfully_made, Toast.LENGTH_SHORT)
                        .show()
                }
                viewModel.resetFilds()
            }
        }

        pixClient.startPixPayment(
            gson.toJson(createCobRequest),
            callback
        )

    }
}

fun showAlertDialog(context: Context, message: String) {
    val builder = AlertDialog.Builder(context)
    builder.setTitle(R.string.cob_gen)
    builder.setMessage(message)
    builder.setPositiveButton("OK") { dialog, _ ->
        dialog.dismiss()
    }
    val dialog = builder.create()
    dialog.show()
}

Example of the CreateCobRequest class:

data class CreateCobRequest(
    @SerializedName("cob_value")
    val cobValue: String?,
    @SerializedName("pix_client_id")
    val pixClientId: String?,
    @SerializedName("preview_customer_receipt")
    val previewCustomerReceipt: Boolean,
    @SerializedName("preview_merchant_receipt")
    val previewMerchantReceipt: Boolean,
    @SerializedName("print_customer_receipt")
    val printCustomerReceipt: Boolean,
    @SerializedName("print_merchant_receipt")
    val printMerchantReceipt: Boolean
)

ChargeStatus class example:

enum class ChargeStatus(private val value: String) {
    @SerializedName("ATIVA")
    ACTIVE("ATIVA"),
    @SerializedName("CONCLUIDA")
    CONCLUDED("CONCLUIDA"),
    @SerializedName("DEVOLVIDO")
    REFUNDED("DEVOLVIDO"),
    @SerializedName("EM_PROCESSAMENTO")
    REFUND_PROCESSING("EM_PROCESSAMENTO"),
    @SerializedName("NAO_REALIZADO")
    REFUND_NOT_DONE("NAO_REALIZADO"),
    @SerializedName("REMOVIDA_PELO_USUARIO_RECEBEDOR")
    REMOVED_BY_USER("REMOVIDA_PELO_USUARIO_RECEBEDOR"),
    @SerializedName("REMOVIDA_PELO_PSP")
    REMOVED_BY_PSP("REMOVIDA_PELO_PSP"),
    @SerializedName("EXPIRADA")
    EXPIRED("EXPIRADA"),
    UNKNOWN("UNKNOWN");

    fun getValue(): String {
        return value
    }
}

PixResponse

data class PixResponse(
    @SerializedName("cob_value")
    val cobValue: String,
    @SerializedName("status")
    var status: String,
    @SerializedName("tx_id")
    val txID: String,
)

PixErrorResponse

class PixErrorResponse (
    @SerializedName("error_message")
    val errorMessage: String
)