Kotlin - Google Pay integration

mail@pastecode.io avatar
unknown
kotlin
a year ago
2.8 kB
1
Indexable
Never
    val isReadyToPayJson = "{\n" + " \"apiVersion\": 2,\n" + " \"apiVersionMinor\": 0,\n" + " \"allowedPaymentMethods\": [\n" + " {\n" + " \"type\": \"CARD\",\n" + " \"parameters\": {\n" + " \"allowedAuthMethods\": [\"PAN_ONLY\", \"CRYPTOGRAM_3DS\"],\n" + " \"allowedCardNetworks\": [\"AMEX\", \"DISCOVER\", \"INTERAC\", \"JCB\", \"MASTERCARD\", \"VISA\"]\n" + " },\n" + " \"tokenizationSpecification\": {\n" + " \"type\": \"PAYMENT_GATEWAY\",\n" + " \"parameters\": {\n" + " \"gateway\": \"your_gateway_name\",\n" + " \"gatewayMerchantId\": \"your_gateway_merchant_id\"\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}" 
    val isReadyToPayRequest = IsReadyToPayRequest.fromJson(isReadyToPayJson) 
    val task = paymentsClient.isReadyToPay(isReadyToPayRequest) task.addOnCompleteListener { task -> try 
        { 
            val result = task.getResult(ApiException::class.java)!! 
            // Dispositivo compatível com o Google Pay 
        } catch (exception: ApiException) { 
        // Dispositivo incompatível com o Google Pay 
        } 
    }

val paymentDataRequestJson = "{\n" + " \"apiVersion\": 2,\n" + " \"apiVersionMinor\": 0,\n" + " \"allowedPaymentMethods\": [\n" + " {\n" + " \"type\": \"CARD\",\n" + " \"parameters\": {\n" + " \"allowedAuthMethods\": [\"PAN_ONLY\", \"CRYPTOGRAM_3DS\"],\n" + " \"allowedCardNetworks\": [\"AMEX\", \"DISCOVER\", \"INTERAC\", \"JCB\", \"MASTERCARD\", \"VISA\"]\n" + " },\n" + " \"tokenizationSpecification\": {\
" \"type\": \"PAYMENT_GATEWAY\",\n" + " \"parameters\": {\n" + " \"gateway\": \"your_gateway_name\",\n" + " \"gatewayMerchantId\": \"your_gateway_merchant_id\"\n" + " }\n" + " }\n" + " }\n" + " ],\n" + " \"transactionInfo\": {\n" + " \"totalPriceStatus\": \"FINAL\",\n" + " \"totalPrice\": \"100.00\",\n" + " \"currencyCode\": \"USD\"\n" + " },\n" + " \"merchantInfo\": {\n" + " \"merchantName\": \"Your Merchant Name\",\n" + " \"merchantId\": \"your_merchant_id\",\n" + " \"merchantOrigin\": \"https://your-merchant-website.com\"\n" + " }\n" + "}"

    val paymentDataRequest = PaymentDataRequest.fromJson(paymentDataRequestJson)
    val task = paymentsClient.loadPaymentData(paymentDataRequest)
    task.addOnCompleteListener { task ->
    try {
        val paymentData = task.getResult(ApiException::class.java)!!
        // Use o paymentData para processar o pagamento
        } catch (exception: ApiException) {
        // Erro ao carregar o paymentData
    }
}

fun processPayment(paymentData: PaymentData) 
{ 
    val paymentMethodData = paymentData.cardInfo!!.paymentMethod!! 
    val tokenizationData = paymentMethodData.tokenizationData!! 
    val paymentToken = tokenizationData.token!! // Envie o paymentToken para o seu gateway de pagamento para processar o pagamento 
}