PaystackHQ / paystack-android

Paystack SDK for Android. Accept payments on Android
Apache License 2.0
126 stars 102 forks source link

"Please send Pin" error after I switched to the Live Key and Made a payment #48

Closed itsibee closed 6 years ago

itsibee commented 6 years ago

Problem

Hello, I used the example codes to build my payment activity in such a way that once a payment is successful, it sends details to my server, the server verifies the payment and provides an input to my app, which uses that to perform some functions, That way I don't need to verify the payment on the app, that can be done on the server all I need to do is wait for the response after making a payment. This work perfectly with the test Key the test card gets charged, Both PIN and OTP are requested, and a payment notification is sent to my mail. But once I used a live key (I made a test account to develop the app. the live key is from another account currently running on a web platform and working) I still get the request for my card PIN and OTP and after I input that, i get an error saying "Please Send Pin" (also the live key is in both my codes and manifest) Below are my codes

Codes

public class BuyPowerActivity extends AppCompatActivity {

    private EditText mEditCardNum;
    private EditText mEditCVC;
    private EditText mEditExpiryAll;

    private Charge charge;
    private Transaction transaction;

    private CircularProgressButton mButtonPerformLocalTransaction;

    private static String newPid;
    private FirebaseDatabase mdata;
    private DocumentReference mDocRef;
    private int selectedprice;
    private String myFormattedPrice;
    private String selectedemail,date_created,time_created,uuid,phone,meternumber,location,acct_name,address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_power);

        //initialize sdk
        PaystackSdk.initialize(getApplicationContext());

        TextView priceget = findViewById(R.id.priceget);
        TextView getemail = findViewById(R.id.emailget);

        mButtonPerformLocalTransaction = findViewById(R.id.chargeid);
        mdata = FirebaseDatabase.getInstance();

        Intent receivedIntent = getIntent();
        selectedprice = receivedIntent.getIntExtra("price",0);
        selectedemail = receivedIntent.getStringExtra("email");
        date_created = receivedIntent.getStringExtra("date_created");
        time_created = receivedIntent.getStringExtra("time_created");
        uuid = receivedIntent.getStringExtra("uuid");
        phone = receivedIntent.getStringExtra("phone");
        meternumber = receivedIntent.getStringExtra("meternumber");
        location = receivedIntent.getStringExtra("location");
        acct_name = receivedIntent.getStringExtra("account_name");
        address = receivedIntent.getStringExtra("address");
        String defaultcNa = BuyPowerActivity.this.getResources().getString(R.string.currency);

        DecimalFormat decimalFormat = new DecimalFormat("#,###,###");

        myFormattedPrice = decimalFormat.format(selectedprice);
        String pricewithCurrency = defaultcNa+myFormattedPrice;
        priceget.setText(pricewithCurrency);
        getemail.setText(selectedemail);

        //Backend Url is only used for build Used but saved for test purposes
        String backend_url = "Backend URl is saved here";
        if (BuildConfig.DEBUG && (backend_url.equals(""))) {
            throw new AssertionError("Please set a backend url before running the sample");
        }
        String paystack_public_key = "My public key is saved here";
        if (BuildConfig.DEBUG && (paystack_public_key.equals(""))) {
            throw new AssertionError("Please set a public key before running the sample");
        }

        PaystackSdk.setPublicKey(paystack_public_key);

        mEditCardNum = findViewById(R.id.cardnum);
        mEditCVC = findViewById(R.id.cvcid);

        mEditExpiryAll = findViewById(R.id.date);

        mEditCardNum.addTextChangedListener(new CreditCardFormattingTextWatcher(mEditCardNum));

        mEditExpiryAll.addTextChangedListener(new CreditCardExpiryTextWatcher(mEditExpiryAll));

        mButtonPerformLocalTransaction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!validateForm()) {
                    return;
                }else{
                    mButtonPerformLocalTransaction.startAnimation();
                    startAFreshCharge(true);
                }
            }
        });

    }

    private void startAFreshCharge(boolean local) {
        // initialize the charge

        if(loadCardFromForm().isValid()) {

            charge = new Charge();
            charge.setCard(loadCardFromForm());
        }

        if (local) {

            Intent receivedIntent = getIntent();

            int selectedprice = receivedIntent.getIntExtra("price", 0);
            String selectedemail = receivedIntent.getStringExtra("email");
            int selectedPriceWkobo = selectedprice*100;

            charge.setAmount(selectedPriceWkobo);
            charge.setEmail(selectedemail);
            //TODO: Intent now

            // charge.setReference("0MA0P" + Calendar.getInstance().getTimeInMillis());

            // this generates a transaction reference for me
            final DatabaseReference ref = mdata.getReference("firebase-generated-reference");
            final DatabaseReference outs = ref.push();
            String s = outs.getKey();

            newPid = s;
            charge.setReference(s);
            try {
                charge.putCustomField("Charged From", "Mobile App");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            chargeCard();
        }
    }

    private Card loadCardFromForm() {
        //validate fields
        Card card;
        String cardNum = mEditCardNum.getText().toString().replaceAll("\\D", "");

        //build card object with ONLY the number, update the other fields later
        //card = new Card.Builder(cardNum, 0, 0, "").build();
        String cvc = mEditCVC.getText().toString().trim();
        //update the cvc field of the card
        // card.setCvc(cvc);

        //validate expiry month;
//        String sMonth = mEditExpiryMonth.getText().toString().trim();
        int month = 0;
        String n = mEditExpiryAll.getText().toString();
        String[] split = n.split("/");
        String firstsub = split[0];
        String secondsub = split[1];
        try {
            month = Integer.parseInt(firstsub);
        } catch (Exception ignored) {
        }

        // card.setExpiryMonth(month);

        //    String sYear = mEditExpiryYear.getText().toString().trim();
        int year = 0;
        try {
            year = Integer.parseInt(secondsub);
        } catch (Exception ignored) {
        }
        //  card.setExpiryYear(20+year);

        card = new Card(cardNum, month, 20+year, cvc);

        return card;
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    private void chargeCard() {
        transaction = null;
        PaystackSdk.chargeCard(BuyPowerActivity.this, charge, new Paystack.TransactionCallback() {
            // This is called only after transaction is successful
            @Override
            public void onSuccess(Transaction transaction) {

                // Move to the other activity, to send reference of
                // Successful payment that will be verified on the server
                // and response received

            }

            @Override
            public void beforeValidate(Transaction transaction) {
                Toast.makeText(BuyPowerActivity.this, "before OTP: "+transaction.getReference(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(Throwable error, Transaction transaction) {

                if (error instanceof ExpiredAccessCodeException) {
                    BuyPowerActivity.this.startAFreshCharge(false);
                    BuyPowerActivity.this.chargeCard();
                    return;
                }

                mButtonPerformLocalTransaction.revertAnimation();

                if (transaction.getReference() != null) {
                    //
                    // This is where I get the "Please send Pin" error
                    //
                } else {
                    Toast.makeText(BuyPowerActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }

        });
    }

    private boolean validateForm() {
        boolean valid = true;

        String cardnum = mEditCardNum.getText().toString();
        if (TextUtils.isEmpty(cardnum)) {
            mEditCardNum.setError("Required.");
            valid = false;
        } else {
            mEditCardNum.setError(null);
        }

        String cardcvc = mEditCVC.getText().toString();
        if (TextUtils.isEmpty(cardcvc)) {
            mEditCVC.setError("Required.");
            valid = false;
        } else {
            mEditCVC.setError(null);
        }

        String password = mEditExpiryAll.getText().toString();
        if (TextUtils.isEmpty(password)) {
            mEditExpiryAll.setError("Required.");
            valid = false;
        } else {
            mEditExpiryAll.setError(null);
        }

        return valid;
    }
}
ibrahimlawal commented 6 years ago

Hello @itsibee does this still persist?

ibrahimlawal commented 6 years ago

We can have a Skype call to test together if so: Skype:profibro . Or TeamViewer.

itsibee commented 6 years ago

Great! We can schedule time to work together on it. And yes it still occurs

Sent from my iPhone

On 4 May 2018, at 5:49 PM, Ibrahim Lawal notifications@github.com wrote:

We can have a Skype call to test together if so: Skype:profibro . Or TeamViewer.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ibrahimlawal commented 6 years ago

@itsibee we discussed this. Thanks for your time!