Skip to content

Commit

Permalink
Clean state after form submission and add network handler
Browse files Browse the repository at this point in the history
- Add a handler in the tokenisation callback for network request issues
- Perform a state cleanup after the form submission
- Hide keyboard upon clicking the Pay button
- Add a ProgressDialog to the DemoActivity
- Update default date value
  • Loading branch information
ioan-ghisoi-cko committed Aug 8, 2018
1 parent 02e8661 commit 59a3865
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 11 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ dependencies {
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.volley:volley:1.0.0'
implementation 'com.github.checkout:frames-android:v2.0.1'
implementation 'com.github.checkout:frames-android:v2.0.2'
}
```

> You can find more about the installation [here](https://jitpack.io/#checkout/frames-android/v2.0.1)
> You can find more about the installation [here](https://jitpack.io/#checkout/frames-android/v2.0.2)
> Please keep in mind that the Jitpack repository should to be added to the project gradle file while the dependency should be added in the module gradle file. [(see more about gradle files)](https://developer.android.com/studio/build)
Expand Down Expand Up @@ -73,7 +73,11 @@ dependencies {
}
@Override
public void onError(CardTokenisationFail error) {
// your error
// your tokenisation error
}
@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};
```
Expand Down Expand Up @@ -115,6 +119,10 @@ dependencies {
public void onError(CardTokenisationFail error) {
// your error
}
@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};
```

Expand Down Expand Up @@ -240,11 +248,14 @@ The module allows you to handle a Google Pay token payload and retrieve a token,
public void onTokenGenerated(GooglePayTokenisationResponse response) {
// success
}

@Override
public void onError(GooglePayTokenisationFail error) {
// fail
}
@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};
```
**Step2** Pass the callback to the module and generate the token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import com.android.volley.VolleyError;
import com.checkout.android_sdk.Request.CardTokenisationRequest;
import com.checkout.android_sdk.Request.GooglePayTokenisationRequest;
import com.checkout.android_sdk.Response.CardTokenisationFail;
Expand All @@ -26,6 +27,8 @@ public interface OnTokenGenerated {
void onTokenGenerated(CardTokenisationResponse response);

void onError(CardTokenisationFail error);

void onNetworkError(VolleyError error);
}

/**
Expand All @@ -35,6 +38,8 @@ public interface OnGooglePayTokenGenerated {
void onTokenGenerated(GooglePayTokenisationResponse response);

void onError(GooglePayTokenisationFail error);

void onNetworkError(VolleyError error);
}

private Context mContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface OnSubmitForm {
@Override
public void onDetailsCompleted() {
mSubmitFormListener.onSubmit(generateRequest());
customAdapter.clearFields();
mDataStore.cleanState();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.checkout.android_sdk.Utils.CardUtils;

import java.util.Calendar;

/**
* The DataStore
* <p>
Expand All @@ -13,7 +15,7 @@ public class DataStore {
private static DataStore INSTANCE = null;
private String mCardNumber;
private String mCardMonth;
private String mCardYear;
private String mCardYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
private String mCardCvv;
private int mCvvLength = 4;

Expand Down Expand Up @@ -236,6 +238,31 @@ public void cleanBillingData() {
DataStore.getInstance().setCustomerPhone("");
}

public void cleanState() {
this.mCardNumber = "";
this.mCardMonth = "";
this.mCardYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
this.mCardCvv = "";
this.mCvvLength = 4;

this.IsValidCardNumber = false;
this.IsValidCardMonth = false;
this.IsValidCardYear = false;
this.IsValidCardCvv = false;

this.mCustomerName = "";
this.mCustomerCountry = "";
this.mCustomerAddress1 = "";
this.mCustomerAddress2 = "";
this.mCustomerCity = "";
this.mCustomerState = "";
this.mCustomerZipcode = "";
this.mCustomerPhonePrefix = "";
this.mCustomerPhone = "";

this.billingCompleted = false;
}

public CardUtils.Cards[] getAcceptedCards() {
return acceptedCards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public void clearBillingSpinner() {
cardDetailsView.clearBillingSpinner();
}

/**
* Clean form data and state
*/
public void clearFields() {
cardDetailsView.resetFields();
billingDetailsView.resetFields();
}

/**
* Instantiation function
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void onErrorResponse(VolleyError error) {
} catch (JSONException e) {
e.printStackTrace();
}
} else {
if (mTokenListener != null) {
mTokenListener.onNetworkError(error);
}
}
}
}
Expand All @@ -96,7 +100,7 @@ public Map<String, String> getHeaders() {
}
};
// Enable retry policy since is not enabled by default in the Volley
portRequest.setRetryPolicy(new DefaultRetryPolicy(10000, 10, 1.0f));
portRequest.setRetryPolicy(new DefaultRetryPolicy(10000, 2, 1.0f));
queue.add(portRequest);
}

Expand Down Expand Up @@ -140,6 +144,10 @@ public void onErrorResponse(VolleyError error) {
} catch (JSONException e) {
e.printStackTrace();
}
} else {
if (mTokenListener != null) {
mTokenListener.onNetworkError(error);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ private boolean isValidForm() {
}

/**
* Used to clear the text from teh fields
* Used to clear the text and state of the fields
*/
private void resetFields() {
public void resetFields() {
mName.setText("");
mNameLayout.setError(null);
mNameLayout.setErrorEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkout.android_sdk.View;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
Expand All @@ -8,6 +9,7 @@
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
Expand Down Expand Up @@ -220,7 +222,14 @@ private void init() {
mPayButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCvvInput.clearFocus();
// hide keyboard
try{
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
if(imm != null)
imm.hideSoftInputFromWindow(getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
if (mDetailsCompletedListener != null && isValidForm()) {
mDetailsCompletedListener.onDetailsCompleted();
}
Expand Down Expand Up @@ -400,6 +409,22 @@ public void updateBillingSpinner() {
}
}

/**
* Used to clear the text and state of the fields
* <p>
*/
public void resetFields() {
clearBillingSpinner();
mCvvInput.setText("");
mCvvLayout.setError(null);
mCvvLayout.setErrorEnabled(false);
mYearInput.setSelection(0);
mMonthInput.setSelection(0);
mCardInput.setText("");
mCardLayout.setError(null);
mCardLayout.setErrorEnabled(false);
}

/**
* Used dynamically populate the accepted cards view is the option is used
*/
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/checkout/checkout_android/DemoActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package checkout.checkout_android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.android.volley.VolleyError;
import com.checkout.android_sdk.CheckoutAPIClient;
import com.checkout.android_sdk.CheckoutAPIClient.OnTokenGenerated;
import com.checkout.android_sdk.PaymentForm;
Expand All @@ -19,11 +21,13 @@ public class DemoActivity extends Activity {

private PaymentForm mPaymentForm;
private CheckoutAPIClient mCheckoutAPIClient;
private static ProgressDialog mProgressDialog;

// Callback used when the user completed the form clicked the Pay button
private final OnSubmitForm mSubmitListener = new OnSubmitForm() {
@Override
public void onSubmit(CardTokenisationRequest request) {
mProgressDialog.show(); // slow a spinner
mCheckoutAPIClient.generateToken(request);
}
};
Expand All @@ -32,20 +36,32 @@ public void onSubmit(CardTokenisationRequest request) {
private final OnTokenGenerated mTokenListener = new OnTokenGenerated() {
@Override
public void onTokenGenerated(CardTokenisationResponse token) {
mProgressDialog.dismiss(); // dismiss the spinner
displayMessage("Success!", token.getId());
}

@Override
public void onError(CardTokenisationFail error) {
mProgressDialog.dismiss(); // dismiss the spinner
displayMessage("Error!", error.getEventId());
}

@Override
public void onNetworkError(VolleyError error) {
mProgressDialog.dismiss(); // dismiss the spinner
displayMessage("Error!", "network error"); // handle VolleyError
}
};

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

mProgressDialog = new ProgressDialog(this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Loading...");

mPaymentForm = findViewById(R.id.checkout_card_form);
mPaymentForm
.setSubmitListener(mSubmitListener)
Expand Down
2 changes: 1 addition & 1 deletion demos/examples/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.checkout:frames-android:v2.0.1'
implementation 'com.github.checkout:frames-android:v2.0.2'
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.volley:volley:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.checkout.android_sdk.CheckoutAPIClient;
import com.checkout.android_sdk.CheckoutAPIClient.OnTokenGenerated;
import com.checkout.android_sdk.Request.CardTokenisationRequest;
Expand All @@ -35,6 +36,11 @@ public void onTokenGenerated(CardTokenisationResponse token) {
public void onError(CardTokenisationFail error) {
displayMessage("Error!", error.getEventId());
}

@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};

private EditText mName, mCard, mMonth, mYear, mCvv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.android.volley.VolleyError;
import com.checkout.android_sdk.CheckoutAPIClient;
import com.checkout.android_sdk.PaymentForm;
import com.checkout.android_sdk.Request.CardTokenisationRequest;
Expand Down Expand Up @@ -48,6 +49,11 @@ public void onError(CardTokenisationFail error) {
progress.dismiss();
displayMessage("Error!", error.getEventId());
}

@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.android.volley.VolleyError;
import com.checkout.android_sdk.CheckoutAPIClient;
import com.checkout.android_sdk.Models.BillingModel;
import com.checkout.android_sdk.Models.PhoneModel;
Expand All @@ -30,6 +31,11 @@ public void onTokenGenerated(CardTokenisationResponse token) {
public void onError(CardTokenisationFail error) {
displayMessage("Error!", error.getEventId());
}

@Override
public void onNetworkError(VolleyError error) {
// your network error
}
};

@Override
Expand Down
2 changes: 1 addition & 1 deletion demos/googlepay_example/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.volley:volley:1.0.0'
implementation 'com.github.checkout:frames-android:v2.0.1'
implementation 'com.github.checkout:frames-android:v2.0.2'

// "wallet" is the only module needed for the Google Payment API and this sample app.
// Apps that selectively compile Google Play service APIs need only this dependency.
Expand Down
Loading

0 comments on commit 59a3865

Please sign in to comment.