Ir para conteúdo

willianlq

Members
  • Total de itens

    2
  • Registro em

  • Última visita

Reputação

0 Comum

Sobre willianlq

  1. willianlq

    Integração API google pay

    Boa tarde a todos, Estou realizando uma integração da API do google pay, basicamente trabalho com duas classes(MainActivity e GooglePay.java) e estou com alguns problemas na classe principal, mais especificamente no método "possiblyShowGooglePayButton" e "onActivityResult", no qual enfrento problema de NullPointer e erros na conversão de Objetos JSON. package com.example.testeapi; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.common.api.Status; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.gms.wallet.AutoResolveHelper; import com.google.android.gms.wallet.IsReadyToPayRequest; import com.google.android.gms.wallet.PaymentData; import com.google.android.gms.wallet.PaymentDataRequest; import com.google.android.gms.wallet.PaymentsClient; import com.google.android.gms.wallet.Wallet; import com.google.android.gms.wallet.WalletConstants; import org.json.JSONObject; import java.util.Optional; public class MainActivity extends AppCompatActivity { public PaymentsClient mPaymentsClient; private View mGooglePayButton; private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 42; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPaymentsClient = Wallet.getPaymentsClient( this, new Wallet.WalletOptions.Builder() .setEnvironment(WalletConstants.ENVIRONMENT_TEST) .build()); possiblyShowGooglePayButton(); } private void possiblyShowGooglePayButton() { final Optional<JSONObject> isReadyToPayJson = GooglePay.getIsReadyToPayRequest(); if (!isReadyToPayJson.isPresent()) { return; } IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(isReadyToPayJson.get().toString()); if (request == null) { return; } Task<Boolean> task = mPaymentsClient.isReadyToPay(request); task.addOnCompleteListener( new OnCompleteListener<Boolean>() { @Override public void onComplete(@NonNull Task<Boolean> task) { try { boolean result = task.getResult(ApiException.class); if (result) { // show Google as a payment option mGooglePayButton = findViewById(R.id.googlepay); mGooglePayButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { requestPayment(view); } }); mGooglePayButton.setVisibility(View.VISIBLE); } } catch (ApiException exception) { // Erro } } }); } public void requestPayment(View view) { Optional<JSONObject> paymentDataRequestJson = GooglePay.getPaymentDataRequest(); if (!paymentDataRequestJson.isPresent()) { return; } PaymentDataRequest request = PaymentDataRequest.fromJson(paymentDataRequestJson.get().toString()); if (request != null) { AutoResolveHelper.resolveTask( mPaymentsClient.loadPaymentData(request), this, LOAD_PAYMENT_DATA_REQUEST_CODE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // valor passado ao AutoResolveHelper if (requestCode == LOAD_PAYMENT_DATA_REQUEST_CODE) { switch (resultCode) { case Activity.RESULT_OK: PaymentData paymentData = PaymentData.getFromIntent(data); String json = paymentData.toJson(); String paymentMethodData = new JSONObject(json) .getJSONObject(paymentMethodData); String paymentToken = ((JSONObject) paymentMethodData) .getJSONObject("tokenizationData") .getString("token"); break; case Activity.RESULT_CANCELED: break; case AutoResolveHelper.RESULT_ERROR: Status status = AutoResolveHelper.getStatusFromIntent(data); Log.i("Status", "Erro na requisição"); // Generally, there is no need to show an error to the user. // The Google Pay payment sheet will present any account errors. break; default: } } } } Se alguém puder me ajudar agradeço, já estou tentando resolver a um bom tempo.
×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.