Skip to content

Commit 89f534a

Browse files
committed
Version 0.8.0
- Now showing online price after checkout - Correctly embedding weight in embedded codes - Maximum amount of shopping cart is now 99999, not 100000
1 parent 46b4721 commit 89f534a

File tree

7 files changed

+70
-20
lines changed

7 files changed

+70
-20
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ allprojects {
2424
}
2525

2626
project.ext {
27-
sdkVersion='0.7.20'
27+
sdkVersion='0.8.0'
2828
versionCode=1
2929

3030
compileSdkVersion=27

core/src/androidTest/java/io/snabble/sdk/EAN13Test.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,5 @@ public void testInternalChecksum() {
5959

6060
private void checkInternalChecksum(int checksum, String code){
6161
Assert.assertEquals(checksum, EAN13.internalChecksum(code));
62-
63-
ScannableCode scannableCode = ScannableCode.parse(snabbleSdk, code);
64-
Assert.assertEquals(true, scannableCode.isEmbeddedDataOk());
6562
}
6663
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public enum State {
176176
private SignedCheckoutInfo signedCheckoutInfo;
177177
private CheckoutProcessResponse checkoutProcess;
178178
private PaymentMethod paymentMethod;
179+
private int priceToPay;
179180

180181
private List<OnCheckoutStateChangedListener> checkoutStateListeners = new CopyOnWriteArrayList<>();
181182

@@ -301,6 +302,7 @@ public void checkout() {
301302
checkoutProcess = null;
302303
signedCheckoutInfo = null;
303304
paymentMethod = null;
305+
priceToPay = 0;
304306

305307
notifyStateChanged(State.HANDSHAKING);
306308

@@ -325,6 +327,25 @@ public void onResponse(Call call, Response response) throws IOException {
325327
String json = IOUtils.toString(inputStream, Charset.forName("UTF-8"));
326328
signedCheckoutInfo = gson.fromJson(json, SignedCheckoutInfo.class);
327329

330+
Logger.d("Local price: " + shoppingCart.getTotalPrice()
331+
+ ", Remote price: " + signedCheckoutInfo.checkoutInfo.get("price"));
332+
333+
if(signedCheckoutInfo.checkoutInfo.has("price")
334+
&& signedCheckoutInfo.checkoutInfo.get("price").getAsJsonObject().has("price")) {
335+
priceToPay = signedCheckoutInfo.checkoutInfo
336+
.get("price")
337+
.getAsJsonObject()
338+
.get("price")
339+
.getAsInt();
340+
} else {
341+
priceToPay = shoppingCart.getTotalPrice();
342+
}
343+
344+
if(priceToPay != shoppingCart.getTotalPrice()){
345+
Logger.w("Warning local price is different from remotely calculated price! (Local: "
346+
+ shoppingCart.getTotalPrice() + ", Remote: " + priceToPay);
347+
}
348+
328349
PaymentMethod[] availablePaymentMethods = getAvailablePaymentMethods();
329350
if (availablePaymentMethods != null && availablePaymentMethods.length > 0) {
330351
if (availablePaymentMethods.length == 1) {
@@ -596,6 +617,10 @@ public PaymentMethod getSelectedPaymentMethod() {
596617
return paymentMethod;
597618
}
598619

620+
public int getPriceToPay() {
621+
return priceToPay;
622+
}
623+
599624
/**
600625
* Gets all available payment methods, callable after {@link Checkout.State#REQUEST_PAYMENT_METHOD}.
601626
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import io.snabble.sdk.codes.ScannableCode;
1313

1414
public class ShoppingCart {
15-
public static final int MAX_QUANTITY = 100000;
15+
public static final int MAX_QUANTITY = 99999;
1616

1717
private static class Entry {
1818
private Product product;

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutEncodedCodesView.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import android.widget.TextView;
1313

1414
import io.snabble.sdk.Checkout;
15+
import io.snabble.sdk.Product;
1516
import io.snabble.sdk.ShoppingCart;
1617
import io.snabble.sdk.SnabbleSdk;
18+
import io.snabble.sdk.codes.EAN13;
1719
import io.snabble.sdk.ui.PriceFormatter;
1820
import io.snabble.sdk.ui.R;
1921
import io.snabble.sdk.ui.SnabbleUI;
@@ -50,7 +52,6 @@ private void inflateView() {
5052
scrollView = findViewById(R.id.scroll_view);
5153

5254
sdkInstance = SnabbleUI.getSdkInstance();
53-
ShoppingCart shoppingCart = sdkInstance.getShoppingCart();
5455

5556
Button paidButton = findViewById(R.id.paid);
5657
paidButton.setOnClickListener(new OnClickListener() {
@@ -67,7 +68,7 @@ public void onClick(View v) {
6768
});
6869

6970
PriceFormatter priceFormatter = new PriceFormatter(sdkInstance);
70-
String formattedAmount = priceFormatter.format(shoppingCart.getTotalPrice());
71+
String formattedAmount = priceFormatter.format(sdkInstance.getCheckout().getPriceToPay());
7172

7273
TextView textView = findViewById(R.id.pay_amount);
7374
textView.setText(getContext().getString(R.string.Snabble_PaymentSelection_title) + " " + formattedAmount);
@@ -122,21 +123,12 @@ public CodeListView(@NonNull Context context) {
122123

123124
stringBuilder = new StringBuilder();
124125

125-
Checkout checkout = sdkInstance.getCheckout();
126-
ShoppingCart shoppingCart = sdkInstance.getShoppingCart();
126+
127127

128128
int h = scrollContainer.getHeight();
129129
barcodeHeight = h - h / 5;
130130

131-
for (String code : checkout.getCodes()) {
132-
addScannableCode(code);
133-
}
134-
135-
for (int i = 0; i < shoppingCart.size(); i++) {
136-
for (int j = 0; j < shoppingCart.getQuantity(i); j++) {
137-
addScannableCode(shoppingCart.getScannedCode(i));
138-
}
139-
}
131+
addCodes();
140132

141133
if(getChildCount() == 0){
142134
barcodeHeight = h;
@@ -146,6 +138,42 @@ public CodeListView(@NonNull Context context) {
146138
updateExplanationText(getChildCount());
147139
}
148140

141+
private void addCodes() {
142+
Checkout checkout = sdkInstance.getCheckout();
143+
ShoppingCart shoppingCart = sdkInstance.getShoppingCart();
144+
145+
for (String code : checkout.getCodes()) {
146+
addScannableCode(code);
147+
}
148+
149+
for (int i = 0; i < shoppingCart.size(); i++) {
150+
Product product = shoppingCart.getProduct(i);
151+
if(product.getType() == Product.Type.UserWeighed){
152+
String[] weighItemIds = product.getWeighedItemIds();
153+
if(weighItemIds != null && weighItemIds.length > 0){
154+
StringBuilder code = new StringBuilder(weighItemIds[0]);
155+
if(code.length() == 13){
156+
StringBuilder embeddedWeight = new StringBuilder();
157+
String quantity = String.valueOf(shoppingCart.getQuantity(i));
158+
int leadingZeros = 5 - quantity.length();
159+
for(int j=0; j<leadingZeros; j++){
160+
embeddedWeight.append('0');
161+
}
162+
embeddedWeight.append(quantity);
163+
code.replace(7, 12, embeddedWeight.toString());
164+
code.setCharAt(6, Character.forDigit(EAN13.internalChecksum(code.toString()), 10));
165+
code.setCharAt(12, Character.forDigit(EAN13.checksum(code.substring(0, 12)), 10));
166+
addScannableCode(code.toString());
167+
}
168+
}
169+
} else {
170+
for (int j = 0; j < shoppingCart.getQuantity(i); j++) {
171+
addScannableCode(shoppingCart.getScannedCode(i));
172+
}
173+
}
174+
}
175+
}
176+
149177
private void generateView() {
150178
stringBuilder.append(sdkInstance.getEncodedCodesSuffix());
151179
String code = stringBuilder.toString();

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutQRCodePOSView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void onClick(View v) {
4747
SnabbleSdk sdkInstance = SnabbleUI.getSdkInstance();
4848

4949
PriceFormatter priceFormatter = new PriceFormatter(sdkInstance);
50-
String formattedAmount = priceFormatter.format(sdkInstance.getShoppingCart().getTotalPrice());
50+
String formattedAmount = priceFormatter.format(sdkInstance.getCheckout().getPriceToPay());
5151

5252
TextView textView = findViewById(R.id.pay_amount);
5353
textView.setText(getContext().getString(R.string.Snabble_PaymentSelection_title) + " " + formattedAmount);

ui/src/main/java/io/snabble/sdk/ui/checkout/PaymentMethodView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private void inflateView() {
9292
TextView title = findViewById(R.id.choose_payment_title);
9393

9494
PriceFormatter priceFormatter = new PriceFormatter(sdkInstance);
95-
String totalPriceText = priceFormatter.format(sdkInstance.getShoppingCart().getTotalPrice());
95+
String totalPriceText = priceFormatter.format(sdkInstance.getCheckout().getPriceToPay());
9696
title.setText(getResources().getString(R.string.Snabble_PaymentSelection_howToPay, totalPriceText));
9797
}
9898

0 commit comments

Comments
 (0)