-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PIMOB: Java sample app updated for CVV tokenization #246
Merged
chintan-soni-cko
merged 2 commits into
master
from
feature/PIMOB-2176_update_cvv_tokenisation_java_sample_app
Oct 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
app/src/main/java/checkout/checkout_android/CVVTokenizationActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package checkout.checkout_android; | ||
|
||
import static checkout.checkout_android.Constants.PUBLIC_KEY_CVV_TOKENIZATION; | ||
|
||
import android.app.AlertDialog; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.LinearLayout; | ||
|
||
import androidx.activity.ComponentActivity; | ||
import androidx.annotation.Nullable; | ||
import androidx.lifecycle.ViewModelProvider; | ||
|
||
import com.checkout.base.model.CardScheme; | ||
import com.checkout.base.model.Environment; | ||
import com.checkout.frames.cvvinputfield.CVVComponentApiFactory; | ||
import com.checkout.frames.cvvinputfield.api.CVVComponentApi; | ||
import com.checkout.frames.cvvinputfield.api.CVVComponentMediator; | ||
import com.checkout.frames.cvvinputfield.models.CVVComponentConfig; | ||
import com.checkout.frames.cvvinputfield.style.DefaultCVVInputFieldStyle; | ||
import com.checkout.frames.model.CornerRadius; | ||
import com.checkout.frames.model.Shape; | ||
import com.checkout.frames.style.component.base.ContainerStyle; | ||
import com.checkout.frames.style.component.base.InputFieldIndicatorStyle; | ||
import com.checkout.frames.style.component.base.InputFieldStyle; | ||
import com.checkout.frames.style.component.base.TextStyle; | ||
import com.checkout.tokenization.model.CVVTokenDetails; | ||
import com.checkout.tokenization.model.CVVTokenizationResultHandler; | ||
|
||
import checkout.checkout_android.viewmodels.CVVTokenizationViewModel; | ||
import kotlin.Unit; | ||
import kotlin.jvm.functions.Function1; | ||
|
||
public class CVVTokenizationActivity extends ComponentActivity { | ||
private Button amexCVVTokenizationButton; | ||
private Button cvvTokenizationButton; | ||
private LinearLayout cvvComponentLinearLayout; | ||
private LinearLayout amexCustomCVVComponentLinearLayout; | ||
private Function1<CVVTokenizationResultHandler, Unit> resultHandler; | ||
private CVVTokenizationViewModel cvvTokenizationViewModel; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_cvv_tokenization); | ||
initViews(); | ||
|
||
cvvTokenizationViewModel = new ViewModelProvider(this).get(CVVTokenizationViewModel.class); | ||
|
||
setupObservers(); | ||
|
||
// Create cvvComponentApi | ||
CVVComponentApi cvvComponentApi = CVVComponentApiFactory.create(PUBLIC_KEY_CVV_TOKENIZATION, Environment.SANDBOX, this); | ||
|
||
// initialise CVVTokenizationResultHandler for tokenization | ||
resultHandler = result -> { | ||
if (result instanceof CVVTokenizationResultHandler.Success) { | ||
CVVTokenDetails tokenDetails = ((CVVTokenizationResultHandler.Success) result).getTokenDetails(); | ||
displayTokenResultDialog(tokenDetails.getToken(), "Token Created Successfully"); | ||
} else if (result instanceof CVVTokenizationResultHandler.Failure) { | ||
String errorMessage = ((CVVTokenizationResultHandler.Failure) result).getErrorMessage(); | ||
displayTokenResultDialog(errorMessage, "Token Failure"); | ||
} | ||
return Unit.INSTANCE; | ||
}; | ||
|
||
createCVVComponentMediator(cvvComponentApi); | ||
|
||
createAMEXCVVComponentMediator(cvvComponentApi); | ||
} | ||
|
||
private void setupObservers() { | ||
cvvTokenizationViewModel.getIsEnteredAMEXCVVValid().observe(this, isCVVValid -> { | ||
if (isCVVValid) | ||
amexCVVTokenizationButton.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark, this.getTheme())); | ||
else { | ||
amexCVVTokenizationButton.setBackgroundColor(getResources().getColor(R.color.colorGray, this.getTheme())); | ||
} | ||
amexCVVTokenizationButton.setEnabled(isCVVValid); | ||
}); | ||
} | ||
|
||
private void initViews() { | ||
amexCVVTokenizationButton = findViewById(R.id.btnAmexCVVTokenization); | ||
cvvTokenizationButton = findViewById(R.id.btnCVVTokenization); | ||
cvvComponentLinearLayout = findViewById(R.id.linearCVVComponent); | ||
amexCustomCVVComponentLinearLayout = findViewById(R.id.linearAMEXCustomCVVComponent); | ||
cvvTokenizationButton.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark, this.getTheme())); | ||
} | ||
|
||
private void createCVVComponentMediator(CVVComponentApi cvvComponentApi) { | ||
// Create config for CVV component | ||
CVVComponentConfig visaCVVComponentConfig = new CVVComponentConfig( | ||
CardScheme.Companion.fromString("unknown"), | ||
isEnteredCVVValid -> Unit.INSTANCE, | ||
DefaultCVVInputFieldStyle.INSTANCE.create() | ||
); | ||
|
||
// Create CVVComponentMediator for CVV component | ||
CVVComponentMediator defaultCVVComponentMediator = cvvComponentApi.createComponentMediator(visaCVVComponentConfig); | ||
View defaultCVVComponentView = defaultCVVComponentMediator.provideCvvComponentContent(cvvComponentLinearLayout); | ||
|
||
// Add defaultCVVComponent as view in parent layout | ||
cvvComponentLinearLayout.addView(defaultCVVComponentView); | ||
|
||
cvvTokenizationButton.setOnClickListener(v -> defaultCVVComponentMediator.createToken(resultHandler)); | ||
} | ||
|
||
|
||
private void createAMEXCVVComponentMediator(CVVComponentApi cvvComponentApi) { | ||
CVVComponentConfig visaCVVComponentConfig = new CVVComponentConfig( | ||
CardScheme.Companion.fromString("American_express"), | ||
isEnteredCVVValid -> { | ||
cvvTokenizationViewModel.setIsAmexCVVValid(isEnteredCVVValid); | ||
return Unit.INSTANCE; | ||
}, | ||
new InputFieldStyle(new TextStyle(), "Enter cvv", | ||
null, new TextStyle(), | ||
new ContainerStyle(Constants.backgroundColor | ||
, Shape.RoundCorner, | ||
new CornerRadius(9)), | ||
new InputFieldIndicatorStyle.Underline() | ||
) | ||
); | ||
|
||
CVVComponentMediator amexCVVComponentMediator = cvvComponentApi.createComponentMediator(visaCVVComponentConfig); | ||
|
||
View amexCVVComponentView = amexCVVComponentMediator.provideCvvComponentContent(amexCustomCVVComponentLinearLayout); | ||
amexCustomCVVComponentLinearLayout.addView(amexCVVComponentView); | ||
|
||
amexCVVTokenizationButton.setOnClickListener(v -> amexCVVComponentMediator.createToken(resultHandler)); | ||
} | ||
|
||
|
||
private void displayTokenResultDialog(String message, String title) { | ||
new AlertDialog.Builder(this).setTitle(title) | ||
.setMessage(message) | ||
.setCancelable(false) | ||
.setNeutralButton("Ok", (dialog, id) -> dialog.dismiss()) | ||
.show(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/checkout/checkout_android/viewmodels/CVVTokenizationViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package checkout.checkout_android.viewmodels; | ||
|
||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
public class CVVTokenizationViewModel extends ViewModel { | ||
|
||
private final MutableLiveData<Boolean> isEnteredAMEXCVVValid = new MutableLiveData<>(); | ||
|
||
public MutableLiveData<Boolean> getIsEnteredAMEXCVVValid() { | ||
return isEnteredAMEXCVVValid; | ||
} | ||
|
||
public void setIsAmexCVVValid(Boolean isCVVValid) { | ||
isEnteredAMEXCVVValid.setValue(isCVVValid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add
final
for all the immutable variables, same for all the other code