Skip to content

Instantly share code, notes, and snippets.

@jasminsuljic
Created January 24, 2017 11:30
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 jasminsuljic/89f892b67f18d74d54bf57908a13d232 to your computer and use it in GitHub Desktop.
Save jasminsuljic/89f892b67f18d74d54bf57908a13d232 to your computer and use it in GitHub Desktop.
package hr.webtech.pay2.example;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import ba.leftor.android.pikpay.BuildConfig;
import ba.leftor.android.pikpay.R;
import hr.webtech.pay2.api.PgwModule;
import hr.webtech.pay2.builders.AddCardModelBuilder;
import hr.webtech.pay2.builders.PayCvvModelBuilder;
import hr.webtech.pay2.data.AddCardModel;
import hr.webtech.pay2.data.PayCvvModel;
import hr.webtech.pay2.data.ShowOrderResponse;
import hr.webtech.pay2.data.TransactionErrorDetailed;
import hr.webtech.pay2.data.TransactionSuccess;
import hr.webtech.pay2.view.TransactionLifecycleDelegate;
import hr.webtech.pay2.view.TransactionLifecycleAdapter;
import hr.webtech.pay2.view.WebtehWebView;
/**
* Example activity with {@link #webtehWebView}.
* <p>
* Use {@link #createIntent(Context, boolean)} to load example
*/
public class WebtehWebViewExampleActivity extends AppCompatActivity {
private static final String BNLD_PAY = "bnld_pay";
private static final String TAG = WebtehWebViewExampleActivity.class.getSimpleName();
private View progressBar;
private WebtehWebView webtehWebView;
private PgwModule pgwModule;
public static Intent createIntent(Context context, boolean payCvv) {
final Intent intent = new Intent(context, WebtehWebViewExampleActivity.class);
intent.putExtra(BNLD_PAY, payCvv);
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webteh_web_view_example);
this.webtehWebView = (WebtehWebView) findViewById(R.id.webView);
this.progressBar = findViewById(R.id.progressBar);
final boolean isPay = getIntent().getBooleanExtra(BNLD_PAY, false);
this.pgwModule = new PgwModule("00001111222233330000111122223333", BuildConfig.DEBUG);
final String orderNumber = String.valueOf(System.currentTimeMillis());
final TransactionLifecycleDelegate transactionLifecycleDelegate = new TransactionLifecycleDelegateImpl();
final TransactionLifecycleAdapter.ResponseType responseType = TransactionLifecycleAdapter.ResponseType.TransactionSuccess;
final TransactionLifecycleAdapter transactionLifecycleAdapter = new TransactionLifecycleAdapter(orderNumber, pgwModule.getApi(), "TestKeyXULLyvgWyPJSwOHe", responseType, transactionLifecycleDelegate);
webtehWebView.initialize(transactionLifecycleAdapter, BuildConfig.DEBUG, 10000);
if (!isPay) {
addCardExample(orderNumber);
} else {
payCvvExample(orderNumber);
}
}
private void payCvvExample(String orderNumber) {
PayCvvModel payCvvModel = new PayCvvModelBuilder()
.setAmount("1250")
.setMerchantId("007007007007")
.setAuthToken(pgwModule.getAuthToken())
.setCurrency(AddCardModel.Currency.EUR.toString())
.setOrderInfo("order ifno")
.setOrderNumber(orderNumber)
.setSign("signature")
.setClientSecret("MegaSecretKey")
.setPmAlias("xoAVCN1-4nzfgOP6uKlH6LWgZXYuk7IY4kJ8JxYa")
.setClient("cvv_android")
.createPayCvvModel();
webtehWebView.payCvv(payCvvModel);
}
private void addCardExample(String orderNumber) {
AddCardModel cardModel = new AddCardModelBuilder()
.setAmount("1250")
.setMerchantId("007007007007")
.setAuthToken(pgwModule.getAuthToken())
.setCurrency(AddCardModel.Currency.EUR.toString())
.setOrderInfo("order ifno")
.setOrderNumber(orderNumber)
.setSign("signature")
.setClientSecret("MegaSecretKey")
.setClient("android")
.createAddCardModel();
webtehWebView.addCard(cardModel);
}
/**
* To prevent stopping ongoing transaction add code bellow in {@link #onBackPressed()}
*/
@Override
public void onBackPressed() {
if (webtehWebView.isBackAllowed()) {
super.onBackPressed();
}
}
/**
* For best behaviour invoke {@link WebtehWebView#onDestroy()} in appropriate lifecycle event
*/
@Override
protected void onDestroy() {
super.onDestroy();
webtehWebView.onDestroy();
}
final class TransactionLifecycleDelegateImpl implements TransactionLifecycleDelegate {
@Override
public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void hideProgress() {
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void transactionSuccess(TransactionSuccess transaction) {
Log.d(TAG, "transactionSuccess: " + transaction);
}
@Override
public void backPressedTransactionInProgress() {
Log.d(TAG, "backPressedTransactionInProgress");
}
@Override
public void threeDsSubmitted() {
Log.d(TAG, "threeDsSubmitted");
}
@Override
public void threeDsLoaded() {
Log.d(TAG, "threeDsLoaded");
}
@Override
public void handleTransactionError(TransactionErrorDetailed transactionError) {
Log.d(TAG, "handleTransactionError: " + transactionError);
}
@Override
public void handleTransactionError(Throwable throwable) {
Log.e(TAG, "handleTransactionError: " + throwable, throwable);
}
@Override
public void checkOrderIdSuccess(ShowOrderResponse showOrderResponse) {
Log.d(TAG, "checkOrderIdSuccess: " + showOrderResponse);
}
@Override
public void checkOrderIdFailure(Throwable throwable) {
Log.d(TAG, "checkOrderIdFailure: " + throwable);
hideProgress();
}
@Override
public void stopWebViewLoad() {
Log.d(TAG, "stopWebViewLoad");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment