Commit 0948f1e9 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Bottomatik transactions

Can now make transaction with Bottomatik QR Code
Add response classes (Bottomatik, article, ginger)
Add cancelAllTransaction
parent fe6e5c97
......@@ -56,7 +56,6 @@ public abstract class ArticleGroupActivity extends BaseActivity {
}
protected void generate() {
try {
if (getIntent().getExtras().getString("categoryList") != null)
createCategories(new ObjectMapper().readTree(getIntent().getExtras().getString("categoryList")), getIntent().getExtras().getIntegerArrayList("categoryListAuthorized"), new ObjectMapper().readTree(getIntent().getExtras().getString("articleList")));
......
......@@ -31,6 +31,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.utc.simde.jessy.tools.Bottomatik;
import fr.utc.simde.jessy.tools.CASConnexion;
import fr.utc.simde.jessy.tools.Config;
import fr.utc.simde.jessy.tools.Dialog;
......@@ -51,6 +52,7 @@ public abstract class BaseActivity extends InternetActivity {
protected static NemopaySession nemopaySession;
protected static Ginger ginger;
protected static Bottomatik bottomatik;
protected static CASConnexion casConnexion;
protected static Config config;
......
......@@ -27,6 +27,7 @@ import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import fr.utc.simde.jessy.tools.Bottomatik;
import fr.utc.simde.jessy.tools.CASConnexion;
import fr.utc.simde.jessy.tools.Config;
import fr.utc.simde.jessy.tools.Ginger;
......@@ -57,6 +58,7 @@ public class MainActivity extends BaseActivity {
nemopaySession = new NemopaySession(MainActivity.this);
ginger = new Ginger(MainActivity.this);
bottomatik = new Bottomatik(MainActivity.this);
casConnexion = new CASConnexion(nemopaySession);
config = new Config(sharedPreferences);
......@@ -102,8 +104,6 @@ public class MainActivity extends BaseActivity {
setConfig();
checkUpdate(false);
startSellByQRCodeActivity(MainActivity.this);
}
@Override
......
......@@ -5,6 +5,7 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import fr.utc.simde.jessy.tools.ExtendedScannerView;
......@@ -41,19 +42,13 @@ public class QRCodeReaderActivity extends BaseActivity implements ZXingScannerVi
@Override
public void onIdentification(final String badgeId) {
this.scannerView.stopCamera();
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.badge_read), "Badge: " + badgeId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
scannerView.startCamera(CAMERA_FACING_BACK);
}
});
handleResult(new Result(badgeId, null, null, BarcodeFormat.DATA_MATRIX));
}
@Override
public void handleResult(Result result) {
Log.d(LOG_TAG, result.getText());
Log.d(LOG_TAG, result.getBarcodeFormat().toString());
dialog.infoDialog(QRCodeReaderActivity.this, getString(R.string.qrcode_reading), result.getText(), new DialogInterface.OnClickListener() {
dialog.infoDialog(QRCodeReaderActivity.this, result.getBarcodeFormat().toString(), result.getText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
......
......@@ -34,10 +34,12 @@ public class SellActivity extends ArticleGroupActivity {
TextView panierText = findViewById(R.id.text_price);
this.panier = new Panier(panierText);
panierText.setOnClickListener(new TextView.OnClickListener() {
panierText.setOnLongClickListener(new TextView.OnLongClickListener() {
@Override
public void onClick(View v) {
public boolean onLongClick(View v) {
startSellByQRCodeActivity(SellActivity.this);
return false;
}
});
......@@ -86,7 +88,7 @@ public class SellActivity extends ArticleGroupActivity {
}
protected void pay(final String badgeId) {
dialog.startLoading(this, getResources().getString(R.string.paiement), getResources().getString(R.string.transaction_in_progress));
dialog.startLoading(this, getString(R.string.paiement), getString(R.string.transaction_in_progress));
final List<Integer> articleList = new ArrayList<Integer>(panier.getArticleList());
clearPanier();
......@@ -102,7 +104,7 @@ public class SellActivity extends ArticleGroupActivity {
@Override
public void run() {
dialog.stopLoading();
Toast.makeText(SellActivity.this, "Paiement effectué", Toast.LENGTH_LONG).show();
Toast.makeText(SellActivity.this, getString(R.string.transaction_realized), Toast.LENGTH_LONG).show();
setBackgroundColor(getResources().getColor(R.color.success));
((Vibrator) getSystemService(ArticleGroupActivity.VIBRATOR_SERVICE)).vibrate(250);
}
......
package fr.utc.simde.jessy;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.zxing.Result;
import fr.utc.simde.jessy.objects.GingerResponse;
import fr.utc.simde.jessy.objects.QRCodeResponse;
import java.io.IOException;
import java.util.List;
import fr.utc.simde.jessy.adapters.GridAdapter;
import fr.utc.simde.jessy.adapters.ListAdapater;
import fr.utc.simde.jessy.responses.ArticleResponse;
import fr.utc.simde.jessy.responses.BottomatikResponse;
import fr.utc.simde.jessy.responses.GingerResponse;
import fr.utc.simde.jessy.responses.QRCodeResponse;
public class SellByQRCodeActivity extends QRCodeReaderActivity {
private static final String LOG_TAG = "_SellByQRCodeActivity";
@Override
public void handleResult(Result result) {
Log.d(LOG_TAG, result.getText());
Log.d(LOG_TAG, result.getBarcodeFormat().toString());
if (result.getBarcodeFormat().toString().equals("QR_CODE")) {
try {
handleQRCode(new ObjectMapper().readValue(result.getText(), QRCodeResponse.class));
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
try {
final QRCodeResponse qrCodeResponse = new ObjectMapper().readValue(result.getText(), QRCodeResponse.class);
dialog.infoDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
}
}
else
resumeReading();
}
protected void handleQRCode(final QRCodeResponse qrCodeResponse) {
try {
dialog.startLoading(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), getString(R.string.user_ginger_info_collecting));
new Thread() {
@Override
public void run() {
GingerResponse gingerResponse = null;
try {
ginger.getInfo(qrCodeResponse.getUsername());
Thread.sleep(100);
gingerResponse = new ObjectMapper().readValue(ginger.getRequest().getResponse(), GingerResponse.class);
}
catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
......@@ -42,23 +76,268 @@ public class SellByQRCodeActivity extends QRCodeReaderActivity {
});
}
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
dialog.infoDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), (new ObjectMapper().readValue(ginger.getRequest().getResponse(), GingerResponse.class)).toString(), new DialogInterface.OnClickListener() {
dialog.changeLoading(getString(R.string.bottomatik_get_transaction));
}
});
BottomatikResponse bottomatikResponse = null;
try {
bottomatik.getTransactionFromId(qrCodeResponse.getId());
Thread.sleep(100);
bottomatikResponse = new ObjectMapper().readValue(bottomatik.getRequest().getResponse(), BottomatikResponse.class);
if (bottomatikResponse.getFoundationId() != nemopaySession.getFoundationId())
throw new Exception(getString(R.string.can_not_sell_other_foundation));
}
catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.infoDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
} catch (Exception e) {
e.printStackTrace();
}
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getString(R.string.article_list_collecting));
}
});
List<ArticleResponse> articleResponseList = null;
List<Integer> articleIdList = bottomatikResponse.getArticleList();
JsonNode articleList = null;
final ArrayNode purchaseList = new ObjectMapper().createArrayNode();
try {
nemopaySession.getArticles();
Thread.sleep(100);
TypeReference<List<ArticleResponse>> articleListType = new TypeReference<List<ArticleResponse>>(){};
articleResponseList = new ObjectMapper().readValue(nemopaySession.getRequest().getResponse(), articleListType);
articleList = nemopaySession.getRequest().getJSONResponse();
int i = 0;
for (ArticleResponse articleResponse : articleResponseList) {
if (articleIdList.contains(articleResponse.getId())) {
purchaseList.add(articleList.get(i));
articleIdList.remove(articleIdList.indexOf(articleResponse.getId()));
if (articleIdList.size() == 0)
break;
}
i++;
}
if (articleIdList.size() != 0)
throw new Exception(getString(R.string.article_not_available));
}
catch (final Exception e) {
Log.e(LOG_TAG, e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.infoDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
}
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getString(R.string.transaction_in_progress));
}
});
try {
nemopaySession.setTransaction(gingerResponse.getBadgeId(), bottomatikResponse.getArticleList());
Thread.sleep(100);
final BottomatikResponse finalBottomatikResponse = bottomatikResponse;
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
Toast.makeText(SellByQRCodeActivity.this, getString(R.string.transaction_realized), Toast.LENGTH_LONG).show();
((Vibrator) getSystemService(SellByQRCodeActivity.VIBRATOR_SERVICE)).vibrate(250);
final LayoutInflater layoutInflater = LayoutInflater.from(SellByQRCodeActivity.this);
final View popupView = layoutInflater.inflate(R.layout.dialog_list, null);
final ListView listView = popupView.findViewById(R.id.list_groups);
try {
listView.setAdapter(new ListAdapater(SellByQRCodeActivity.this, purchaseList, config.getPrintCotisant(), config.getPrint18()));
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
fatal(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage());
}
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SellByQRCodeActivity.this);
alertDialogBuilder
.setTitle(R.string.panier)
.setView(popupView)
.setCancelable(false)
.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialog.startLoading(SellByQRCodeActivity.this, getResources().getString(R.string.paiement), getResources().getString(R.string.transaction_in_validation));
new Thread() {
@Override
public void run() {
try {
bottomatik.setTransaction(finalBottomatikResponse.getId(), true, true);
Thread.sleep(100);
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
Toast.makeText(SellByQRCodeActivity.this, getString(R.string.transaction_validated), Toast.LENGTH_LONG).show();
resumeReading();
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage());
resumeReading();
}
}
}.start();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialog.startLoading(SellByQRCodeActivity.this, getResources().getString(R.string.qrcode_reading), getResources().getString(R.string.transaction_in_cancelation));
new Thread() {
@Override
public void run() {
try {
nemopaySession.cancelTransaction(nemopaySession.getRequest().getJSONResponse().get("transaction_id").intValue());
Thread.sleep(100);
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
Toast.makeText(SellByQRCodeActivity.this, getString(R.string.transaction_refunded), Toast.LENGTH_LONG).show();
((Vibrator) getSystemService(ArticleGroupActivity.VIBRATOR_SERVICE)).vibrate(250);
resumeReading();
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
final JsonNode response = nemopaySession.getRequest().getJSONResponse();
if (response.has("error") && response.get("error").has("message")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), response.get("error").get("message").textValue());
((Vibrator) getSystemService(ArticleGroupActivity.VIBRATOR_SERVICE)).vibrate(500);
resumeReading();
}
});
}
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(SellByQRCodeActivity.this, getString(R.string.qrcode_reading), e.getMessage());
((Vibrator) getSystemService(ArticleGroupActivity.VIBRATOR_SERVICE)).vibrate(500);
resumeReading();
}
});
}
}
}
}.start();
}
});
dialog.createDialog(alertDialogBuilder);
}
});
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
final JsonNode response = nemopaySession.getRequest().getJSONResponse();
if (response.has("error") && response.get("error").has("message")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(SellByQRCodeActivity.this, getString(R.string.paiement), response.get("error").get("message").textValue());
((Vibrator) getSystemService(SellByQRCodeActivity.VIBRATOR_SERVICE)).vibrate(500);
resumeReading();
}
});
}
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(SellByQRCodeActivity.this, getString(R.string.paiement), e.getMessage(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
resumeReading();
}
});
((Vibrator) getSystemService(SellByQRCodeActivity.VIBRATOR_SERVICE)).vibrate(500);
}
});
}
return;
}
}
}.start();
}
......
......@@ -26,24 +26,7 @@ public class GridAdapter extends ArticlesAdapter {
public GridAdapter(final Activity activity, final ArrayNode articleList, final int nbrColumns, final Boolean printCotisant, final Boolean print18) throws Exception {
super(activity, articleList, printCotisant, print18);
switch (nbrColumns) {
case 1:
this.size = 250;
break;
case 2:
this.size = 200;
break;
case 3:
this.size = 150;
break;
case 4:
this.size = 125;
break;
case 5:
default:
this.size = 100;
break;
}
this.size = nbrColumns > 5 ? 100 : 225 - (25 * nbrColumns);
}
@Override
......
package fr.utc.simde.jessy.fragments;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import fr.utc.simde.jessy.R;
import fr.utc.simde.jessy.adapters.ArticlesAdapter;
import fr.utc.simde.jessy.adapters.GridAdapter;
import fr.utc.simde.jessy.adapters.ListAdapater;
import fr.utc.simde.jessy.tools.Config;
import fr.utc.simde.jessy.tools.Panier;
/**
* Created by Samy on 27/10/2017.
*/
public class GroupFragment implements TabHost.TabContentFactory {
private static final String LOG_TAG = "_ArticleGroupFragment";
private int nbrColumns;
private LayoutInflater layoutInflater;
private View view;
private GridView gridView;
private ListView listView;
private ArticlesAdapter articlesAdapter;
private Panier panier;
private Config config;
public GroupFragment(final Activity activity, final ArrayNode articleList, final Panier panier, final Config config) throws Exception { new GroupFragment(activity, articleList, panier, config, 3); }
public GroupFragment(final Activity activity, final ArrayNode articleList, final Panier panier, final Config config, final int gridColumns) throws Exception {
this.layoutInflater = LayoutInflater.from(activity);
this.panier = panier;
this.config = config;
if (config.getInGrid()) {
this.view = this.layoutInflater.inflate(R.layout.fragment_article_group_grid, null);
this.gridView = this.view.findViewById(R.id.grid_articles);
setGridLayout(gridColumns);
createArticleGrid(activity, articleList);
}
else {
this.view = this.layoutInflater.inflate(R.layout.fragment_article_group_list, null);
this.listView = this.view.findViewById(R.id.list_articles);
createArticleList(activity, articleList);
}
}
public void setGridLayout(final int nbrColumns) {
this.nbrColumns = nbrColumns;
this.gridView.setNumColumns(nbrColumns);
}
public void createArticleGrid(final Activity activity, final ArrayNode articleList) throws Exception {
this.articlesAdapter = new GridAdapter(activity, articleList, this.nbrColumns, this.config.getPrintCotisant(), this.config.getPrint18());
this.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
JsonNode article = ((JsonNode) articlesAdapter.getArticle(position));
articlesAdapter.onClick(position);
panier.addArticle(article.get("id").intValue(), article.get("price").intValue());
}
});
this.gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {