Skip to content

Commit c53da3a

Browse files
committed
add redeemed coupons api
1 parent 51d066b commit c53da3a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.45.0]
5+
6+
### Added
7+
- Added API to get redeemed coupons
8+
49
## [0.44.4]
510

611
### Added

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.snabble.sdk;
22

3+
import androidx.annotation.NonNull;
4+
35
import java.util.ArrayList;
46
import java.util.Collection;
57
import java.util.Collections;
@@ -129,6 +131,7 @@ public enum State {
129131
private final PaymentOriginCandidateHelper paymentOriginCandidateHelper;
130132
private CheckoutApi.AuthorizePaymentRequest storedAuthorizePaymentRequest;
131133
private boolean authorizePaymentRequestFailed;
134+
private List<Coupon> redeemedCoupons;
132135

133136
Checkout(Project project) {
134137
this.project = project;
@@ -271,6 +274,7 @@ public void checkout(long timeout) {
271274
storedAuthorizePaymentRequest = null;
272275
shop = project.getCheckedInShop();
273276
paymentOriginCandidateHelper.reset();
277+
redeemedCoupons = null;
274278

275279
notifyStateChanged(Checkout.State.HANDSHAKING);
276280

@@ -663,6 +667,7 @@ private void approve() {
663667
shoppingCart.backup();
664668
}
665669

670+
redeemedCoupons = signedCheckoutInfo.getRedeemedCoupons(project.getCoupons().get());
666671
shoppingCart.invalidate();
667672
clearCodes();
668673
notifyStateChanged(Checkout.State.PAYMENT_APPROVED);
@@ -671,6 +676,15 @@ private void approve() {
671676
}
672677
}
673678

679+
@NonNull
680+
public List<Coupon> getRedeemedCoupons() {
681+
if (redeemedCoupons == null) {
682+
return new ArrayList<>();
683+
}
684+
685+
return redeemedCoupons;
686+
}
687+
674688
public void approveOfflineMethod() {
675689
if (paymentMethod != null && paymentMethod.isOfflineMethod()
676690
|| paymentMethod == PaymentMethod.CUSTOMERCARD_POS) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ public PaymentMethodInfo[] getAvailablePaymentMethods(PaymentMethod[] clientAcce
106106

107107
return new PaymentMethodInfo[0];
108108
}
109+
110+
public List<Coupon> getRedeemedCoupons(List<Coupon> availableCoupons) {
111+
ArrayList<Coupon> redeemedCoupons = new ArrayList<>();
112+
113+
if (checkoutInfo != null && checkoutInfo.has("lineItems")) {
114+
JsonArray jsonArray = checkoutInfo.getAsJsonArray("lineItems");
115+
if (jsonArray != null) {
116+
List<LineItem> lineItems = new Gson().fromJson(jsonArray,
117+
new TypeToken<List<LineItem>>() {}.getType());
118+
for (LineItem lineItem : lineItems) {
119+
if (lineItem.type == LineItemType.COUPON && lineItem.redeemed) {
120+
for (Coupon coupon : availableCoupons) {
121+
if (coupon.getId().equals(lineItem.couponId)) {
122+
redeemedCoupons.add(coupon);
123+
}
124+
}
125+
}
126+
}
127+
}
128+
}
129+
130+
return redeemedCoupons;
131+
}
109132
}
110133

111134
public static class CheckoutInfo {

0 commit comments

Comments
 (0)