Skip to content

Commit 447aa8b

Browse files
committed
add receipt auto download config parameter
1 parent 70d8bf5 commit 447aa8b

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

core/src/main/java/io/snabble/sdk/Checkout.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,7 @@ private boolean handleProcessResponse() {
319319

320320
String receiptLink = checkoutProcess.getReceiptLink();
321321
if (receiptLink != null) {
322-
PriceFormatter priceFormatter = new PriceFormatter(project);
323-
receipts.download(project, receiptLink,
324-
project.getCheckedInShop().getName(),
325-
priceFormatter.format(priceToPay), null);
322+
downloadReceipt(receiptLink);
326323

327324
if (paymentMethod.isOfflineMethod()) {
328325
return true;
@@ -351,6 +348,17 @@ private boolean handleProcessResponse() {
351348
return false;
352349
}
353350

351+
private void downloadReceipt(String receiptLink) {
352+
PriceFormatter priceFormatter = new PriceFormatter(project);
353+
ReceiptInfo receiptInfo = receipts.add(project, receiptLink,
354+
project.getCheckedInShop().getName(),
355+
priceFormatter.format(priceToPay));
356+
357+
if(Snabble.getInstance().getConfig().enableReceiptAutoDownload) {
358+
receipts.download(receiptInfo, null);
359+
}
360+
}
361+
354362
private void scheduleNextPollForReceipt() {
355363
handler.postDelayed(pollForReceiptRunnable, 2000);
356364
}
@@ -363,9 +371,7 @@ private void pollForReceipt() {
363371
public void success(CheckoutApi.CheckoutProcessResponse checkoutProcessResponse) {
364372
String receiptLink = checkoutProcessResponse.getReceiptLink();
365373
if (receiptLink != null) {
366-
PriceFormatter priceFormatter = new PriceFormatter(project);
367-
receipts.download(project, receiptLink, project.getCheckedInShop().getName(),
368-
priceFormatter.format(priceToPay), null);
374+
downloadReceipt(receiptLink);
369375
} else {
370376
scheduleNextPollForReceipt();
371377
}

core/src/main/java/io/snabble/sdk/Receipts.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,52 +91,46 @@ public void cancelDownload(final ReceiptInfo receiptInfo) {
9191
receiptInfo.call = null;
9292
}
9393

94+
ReceiptInfo add(final Project project,
95+
final String url,
96+
final String shopName,
97+
final String price) {
98+
final String absoluteUrl = Snabble.getInstance().absoluteUrl(url);
99+
final String id = Utils.sha1Hex(absoluteUrl);
100+
101+
ReceiptInfo receiptInfo = new ReceiptInfo(id, project.getId(), absoluteUrl, shopName, price);
102+
receiptInfo.setProject(project);
103+
saveReceiptInfo(receiptInfo);
104+
105+
return receiptInfo;
106+
}
107+
108+
/**
109+
* Downloads a receipts pdf and stores it in the projects internal storage directory.
110+
*/
94111
public void download(final ReceiptInfo receiptInfo,
95112
final ReceiptDownloadCallback callback) {
96113
if (receiptInfo.isDownloaded()) {
97114
callback.success(receiptInfo);
98115
return;
99116
}
100117

101-
download(receiptInfo.getProject(),
102-
receiptInfo.getUrl(),
103-
receiptInfo.getShopName(),
104-
receiptInfo.getPrice(), callback);
105-
}
106-
107-
public void download(final Project project,
108-
final String url,
109-
final String shopName,
110-
final String price,
111-
final ReceiptDownloadCallback callback) {
112-
if (url == null || project == null) {
118+
if (receiptInfo.getUrl() == null || receiptInfo.getProject() == null) {
113119
callback.failure();
114120
return;
115121
}
116122

117-
final String absoluteUrl = Snabble.getInstance().absoluteUrl(url);
118-
final String id = Utils.sha1Hex(absoluteUrl);
119-
120-
ReceiptInfo receiptInfo = receiptInfoList.get(id);
121-
122-
if (receiptInfo == null) {
123-
receiptInfo = new ReceiptInfo(id, project.getId(), absoluteUrl, shopName, price);
124-
receiptInfo.setProject(project);
125-
}
126-
127-
final ReceiptInfo finalReceiptInfo = receiptInfo;
128-
129123
final Request request = new Request.Builder()
130-
.url(absoluteUrl)
124+
.url(receiptInfo.getUrl())
131125
.get()
132126
.build();
133127

134-
Call call = project.getOkHttpClient().newCall(request);
128+
Call call = receiptInfo.getProject().getOkHttpClient().newCall(request);
135129
call.enqueue(new Callback() {
136130
@Override
137131
public void onResponse(Call call, Response response) throws IOException {
138132
if (response.isSuccessful()) {
139-
finalReceiptInfo.call = null;
133+
receiptInfo.call = null;
140134

141135
ResponseBody body = response.body();
142136
if (body == null) {
@@ -147,14 +141,14 @@ public void onResponse(Call call, Response response) throws IOException {
147141
}
148142

149143
// .pdf extension is needed for adobe reader to work
150-
File file = new File(storageDirectory, finalReceiptInfo.getId() + ".pdf");
144+
File file = new File(storageDirectory, receiptInfo.getId() + ".pdf");
151145
FileOutputStream fos = new FileOutputStream(file);
152146
IOUtils.copy(body.byteStream(), fos);
153-
finalReceiptInfo.setFilePath(file.getAbsolutePath());
154-
saveReceiptInfo(finalReceiptInfo);
147+
receiptInfo.setFilePath(file.getAbsolutePath());
148+
saveReceiptInfo(receiptInfo);
155149

156150
if (callback != null) {
157-
callback.success(finalReceiptInfo);
151+
callback.success(receiptInfo);
158152
}
159153
} else {
160154
if (callback != null) {

core/src/main/java/io/snabble/sdk/Snabble.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ public static class Config {
411411
* Note that this increases setup time of the ProductDatabase, and it may not be
412412
* immediately available offline.
413413
*/
414-
public boolean generateSearchIndex = false;
414+
public boolean generateSearchIndex;
415+
416+
/**
417+
* If set to true, downloads receipts automatically and stores them in the projects
418+
* internal storage folder.
419+
*/
420+
public boolean enableReceiptAutoDownload;
415421
}
416422
}

0 commit comments

Comments
 (0)