Skip to content

Instantly share code, notes, and snippets.

@victorlucss
Created March 10, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorlucss/099121074efa09c39560a452caecb9f6 to your computer and use it in GitHub Desktop.
Save victorlucss/099121074efa09c39560a452caecb9f6 to your computer and use it in GitHub Desktop.
package br.com.s3bank.recarga.views;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.google.android.material.snackbar.Snackbar;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import br.com.phoebus.android.payments.api.ApplicationInfo;
import br.com.phoebus.android.payments.api.Credentials;
import br.com.phoebus.android.payments.api.ErrorData;
import br.com.phoebus.android.payments.api.Payment;
import br.com.phoebus.android.payments.api.PaymentClient;
import br.com.phoebus.android.payments.api.PaymentRequest;
import br.com.phoebus.android.payments.api.PaymentRequestV2;
import br.com.phoebus.android.payments.api.PaymentType;
import br.com.phoebus.android.payments.api.PaymentV2;
import br.com.phoebus.android.payments.api.client.Client;
import br.com.phoebus.android.payments.api.exception.ClientException;
import br.com.s3bank.R;
import br.com.s3bank.recarga.controller.StationController;
import br.com.s3bank.recarga.controller.TransactionController;
import br.com.s3bank.recarga.models.TransactionModel;
import br.com.s3bank.recarga.models.portion.PortionItem;
import br.com.s3bank.recarga.viewmodel.TransactionDoneViewModel;
public class TransactionDoneActivity extends AppCompatActivity {
private TransactionDoneViewModel viewModel;
private PaymentClient paymentClient;
private PortionItem portion;
private TextView name;
private TextView price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_transaction_done);
this.portion = (PortionItem) getIntent().getSerializableExtra("portion");
this.name = findViewById(R.id.name);
this.price = findViewById(R.id.price);
this.paymentClient = new PaymentClient();
this.doBind();
}
private void doBind() {
this.paymentClient.bind(this, new Client.OnConnectionCallback() {
@Override
public void onConnected() {
showAlert("Conectado!");
doPaymentV2();
}
@Override
public void onDisconnected(boolean forced) {
showAlert("desconectado");
}
});
}
@Override
protected void onResume() {
super.onResume();
paymentClient.bind(this, new Client.OnConnectionCallback() {
@Override
public void onConnected() {
showAlert("Conectado!");
// doPaymentV2();
}
@Override
public void onDisconnected(boolean forced) {
showAlert("desconectado");
}
});
}
@Override
protected void onPause() {
try {
paymentClient.unbind(this);
} catch (Exception e) {
Log.e("ERRROOOOO", e.getMessage());
}
super.onPause();
}
public void doPaymentV2() {
ApplicationInfo appInfo = new ApplicationInfo();
Credentials credentials = new Credentials();
credentials.setApplicationId("0");
credentials.setSecretToken("000000000000000000000000");
appInfo.setCredentials(credentials);
appInfo.setSoftwareVersion("1.0");
PaymentRequestV2 pr;
try {
pr = new PaymentRequestV2();
pr.setValue(new BigDecimal("50"));
pr.setAppTransactionId("0");
pr.setAppInfo(appInfo);
pr.setShowReceiptView(false);
pr.setTokenizeCard(false);
pr.setTokenizeEmail("victorlcmonteiro@gmail.com");
List<PaymentType> types = new LinkedList<PaymentType>();
types.add(PaymentType.CREDIT);
types.add(PaymentType.DEBIT);
types.add(PaymentType.CREDIT_STORE);
pr.setPaymentTypes(types);
} catch (Exception e) {
showAlert("Falha na Solicitação: " + e.getMessage());
Log.e("Falha na solicitação", e.getMessage());
return;
}
//
pr.setInstallments(5);
try {
this.paymentClient.startPaymentV2(pr, new PaymentClient.PaymentCallback<PaymentV2>() {
@Override
public void onSuccess(PaymentV2 data) {
showAlert("Pagamento Realizado!");
Log.d("OK", "TODA CHAMADA DEU CERTO E PAGAMENTO REALIZADO");
}
@Override
public void onError(ErrorData errorData) {
showAlert("Pagamento Não Realizado: " + errorData.getPaymentsResponseCode() + " / "
+ errorData.getAcquirerResponseCode() + " = " + errorData.getResponseMessage());
Log.d("Pagamento não realizado", errorData.getPaymentsResponseCode() + " / "
+ errorData.getAcquirerResponseCode() + " = " + errorData.getResponseMessage());
}
});
} catch (ClientException e) {
showAlert("Falha na Solicitação: " + e.getMessage());
Log.e("Falha na solicitação", e.getMessage());
} catch (Exception e){
Log.e("Falha na solicitação", e.getMessage());
}
}
private void showAlert(String message) {
Snackbar.make(this.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment