Search in sources :

Example 1 with Token

use of com.stripe.android.model.Token in project Telegram-FOSS by Telegram-FOSS-Team.

the class PaymentFormActivity method sendCardData.

private boolean sendCardData() {
    Integer month;
    Integer year;
    String date = inputFields[FIELD_EXPIRE_DATE].getText().toString();
    String[] args = date.split("/");
    if (args.length == 2) {
        month = Utilities.parseInt(args[0]);
        year = Utilities.parseInt(args[1]);
    } else {
        month = null;
        year = null;
    }
    Card card = new Card(inputFields[FIELD_CARD].getText().toString(), month, year, inputFields[FIELD_CVV].getText().toString(), inputFields[FIELD_CARDNAME].getText().toString(), null, null, null, null, inputFields[FIELD_CARD_POSTCODE].getText().toString(), inputFields[FIELD_CARD_COUNTRY].getText().toString(), null);
    cardName = card.getType() + " *" + card.getLast4();
    if (!card.validateNumber()) {
        shakeField(FIELD_CARD);
        return false;
    } else if (!card.validateExpMonth() || !card.validateExpYear() || !card.validateExpiryDate()) {
        shakeField(FIELD_EXPIRE_DATE);
        return false;
    } else if (need_card_name && inputFields[FIELD_CARDNAME].length() == 0) {
        shakeField(FIELD_CARDNAME);
        return false;
    } else if (!card.validateCVC()) {
        shakeField(FIELD_CVV);
        return false;
    } else if (need_card_country && inputFields[FIELD_CARD_COUNTRY].length() == 0) {
        shakeField(FIELD_CARD_COUNTRY);
        return false;
    } else if (need_card_postcode && inputFields[FIELD_CARD_POSTCODE].length() == 0) {
        shakeField(FIELD_CARD_POSTCODE);
        return false;
    }
    showEditDoneProgress(true, true);
    try {
        if ("stripe".equals(paymentForm.native_provider)) {
            Stripe stripe = new Stripe(providerApiKey);
            stripe.createToken(card, new TokenCallback() {

                public void onSuccess(Token token) {
                    if (canceled) {
                        return;
                    }
                    paymentJson = String.format(Locale.US, "{\"type\":\"%1$s\", \"id\":\"%2$s\"}", token.getType(), token.getId());
                    AndroidUtilities.runOnUIThread(() -> {
                        goToNextStep();
                        showEditDoneProgress(true, false);
                        setDonePressed(false);
                    });
                }

                public void onError(Exception error) {
                    if (canceled) {
                        return;
                    }
                    showEditDoneProgress(true, false);
                    setDonePressed(false);
                    if (error instanceof APIConnectionException || error instanceof APIException) {
                        AlertsCreator.showSimpleToast(PaymentFormActivity.this, LocaleController.getString("PaymentConnectionFailed", R.string.PaymentConnectionFailed));
                    } else {
                        AlertsCreator.showSimpleToast(PaymentFormActivity.this, error.getMessage());
                    }
                }
            });
        } else if ("smartglocal".equals(paymentForm.native_provider)) {
            AsyncTask<Object, Object, String> task = new AsyncTask<Object, Object, String>() {

                @Override
                protected String doInBackground(Object... objects) {
                    HttpURLConnection conn = null;
                    try {
                        JSONObject jsonObject = new JSONObject();
                        JSONObject cardObject = new JSONObject();
                        cardObject.put("number", card.getNumber());
                        cardObject.put("expiration_month", String.format(Locale.US, "%02d", card.getExpMonth()));
                        cardObject.put("expiration_year", "" + card.getExpYear());
                        cardObject.put("security_code", "" + card.getCVC());
                        jsonObject.put("card", cardObject);
                        URL connectionUrl;
                        if (paymentForm.invoice.test) {
                            connectionUrl = new URL("https://tgb-playground.smart-glocal.com/cds/v1/tokenize/card");
                        } else {
                            connectionUrl = new URL("https://tgb.smart-glocal.com/cds/v1/tokenize/card");
                        }
                        conn = (HttpURLConnection) connectionUrl.openConnection();
                        conn.setConnectTimeout(30 * 1000);
                        conn.setReadTimeout(80 * 1000);
                        conn.setUseCaches(false);
                        conn.setDoOutput(true);
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Content-Type", "application/json");
                        conn.setRequestProperty("X-PUBLIC-TOKEN", providerApiKey);
                        try (OutputStream output = conn.getOutputStream()) {
                            output.write(jsonObject.toString().getBytes("UTF-8"));
                        }
                        int code = conn.getResponseCode();
                        if (code >= 200 && code < 300) {
                            JSONObject result = new JSONObject();
                            JSONObject jsonObject1 = new JSONObject(getResponseBody(conn.getInputStream()));
                            String token = jsonObject1.getJSONObject("data").getString("token");
                            result.put("token", token);
                            result.put("type", "card");
                            return result.toString();
                        } else {
                            if (BuildVars.DEBUG_VERSION) {
                                FileLog.e("" + getResponseBody(conn.getErrorStream()));
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    } finally {
                        if (conn != null) {
                            conn.disconnect();
                        }
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String result) {
                    if (canceled) {
                        return;
                    }
                    if (result == null) {
                        AlertsCreator.showSimpleToast(PaymentFormActivity.this, LocaleController.getString("PaymentConnectionFailed", R.string.PaymentConnectionFailed));
                    } else {
                        paymentJson = result;
                        goToNextStep();
                    }
                    showEditDoneProgress(true, false);
                    setDonePressed(false);
                }
            };
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    return true;
}
Also used : APIConnectionException(com.stripe.android.exception.APIConnectionException) OutputStream(java.io.OutputStream) AsyncTask(android.os.AsyncTask) Token(com.stripe.android.model.Token) TokenCallback(com.stripe.android.TokenCallback) JSONException(org.json.JSONException) APIException(com.stripe.android.exception.APIException) IOException(java.io.IOException) APIConnectionException(com.stripe.android.exception.APIConnectionException) URL(java.net.URL) Card(com.stripe.android.model.Card) APIException(com.stripe.android.exception.APIException) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) Stripe(com.stripe.android.Stripe) JSONObject(org.json.JSONObject) TLObject(org.telegram.tgnet.TLObject) MessageObject(org.telegram.messenger.MessageObject)

Aggregations

AsyncTask (android.os.AsyncTask)1 Stripe (com.stripe.android.Stripe)1 TokenCallback (com.stripe.android.TokenCallback)1 APIConnectionException (com.stripe.android.exception.APIConnectionException)1 APIException (com.stripe.android.exception.APIException)1 Card (com.stripe.android.model.Card)1 Token (com.stripe.android.model.Token)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 MessageObject (org.telegram.messenger.MessageObject)1 TLObject (org.telegram.tgnet.TLObject)1