diff --git a/CHANGELOG.md b/CHANGELOG.md index e6c5da83bb..c3c12228cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. ### Removed ### Fixed +## [0.72.4] +### Added +* core, io: Option to limit payment methods offered to add (#173) + ## [0.72.3] ### Fixed * core, ui: Bind Giropay to its project (#172) diff --git a/core/src/main/java/io/snabble/sdk/PaymentMethod.kt b/core/src/main/java/io/snabble/sdk/PaymentMethod.kt index d1e0d1d123..4e7cfa063d 100644 --- a/core/src/main/java/io/snabble/sdk/PaymentMethod.kt +++ b/core/src/main/java/io/snabble/sdk/PaymentMethod.kt @@ -1,5 +1,7 @@ package io.snabble.sdk +import android.os.Parcel +import android.os.Parcelable import androidx.annotation.Nullable import com.google.gson.annotations.SerializedName @@ -31,7 +33,7 @@ enum class PaymentMethod( * Declares if the payment method can only be aborted with a confirmation by the backend */ val needsAbortConfirmation: Boolean, -) { +) : Parcelable { @SerializedName("qrCodePOS") QRCODE_POS( @@ -168,8 +170,18 @@ enum class PaymentMethod( needsAbortConfirmation = true ); + override fun describeContents(): Int = 0 + + override fun writeToParcel(dest: Parcel, flags: Int) = dest.writeInt(ordinal) + companion object { + @JvmField + val CREATOR = object : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel) = PaymentMethod.values()[parcel.readInt()] + override fun newArray(size: Int) = arrayOfNulls(size) + } + /** * Converts a payment method from its string representation. */ diff --git a/ui/src/main/java/io/snabble/sdk/ui/payment/SelectPaymentMethodFragment.java b/ui/src/main/java/io/snabble/sdk/ui/payment/SelectPaymentMethodFragment.java index 6182cce33f..3ce78d9c8e 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/payment/SelectPaymentMethodFragment.java +++ b/ui/src/main/java/io/snabble/sdk/ui/payment/SelectPaymentMethodFragment.java @@ -1,6 +1,7 @@ package io.snabble.sdk.ui.payment; import android.os.Bundle; +import android.os.Parcelable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -18,6 +19,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import io.snabble.sdk.PaymentMethod; import io.snabble.sdk.Project; @@ -27,11 +29,15 @@ public class SelectPaymentMethodFragment extends BottomSheetDialogFragment { public static final String ARG_PROJECT_ID = "projectId"; + public static final String ARG_SUPPORTED_METHODS = "supportedMethods"; private List entries; private Set paymentMethods; private String projectId; + @Nullable + private List supportedMethods; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -39,6 +45,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) { Bundle args = getArguments(); if (args != null) { projectId = args.getString(ARG_PROJECT_ID, null); + supportedMethods = convertToList(args.getParcelableArray(ARG_SUPPORTED_METHODS), PaymentMethod.class); } } @@ -51,7 +58,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa Set availablePaymentMethods = new HashSet<>(); for (Project project : Snabble.getInstance().getProjects()) { if (project.getId().equals(projectId)) { - availablePaymentMethods.addAll(project.getAvailablePaymentMethods()); + final List methods = project.getAvailablePaymentMethods() + .stream() + .filter(this::isSupportedMethod) + .collect(Collectors.toList()); + availablePaymentMethods.addAll(methods); } } @@ -188,6 +199,13 @@ public void click() { return v; } + private boolean isSupportedMethod(@NonNull final PaymentMethod method) { + if (supportedMethods != null) { + return supportedMethods.contains(method); + } + return true; + } + private String getUsableAtText(PaymentMethod... paymentMethods) { List projects = Snabble.getInstance().getProjects(); if (projects.size() == 1) { @@ -219,6 +237,20 @@ private String getUsableAtText(PaymentMethod... paymentMethods) { return getResources().getString(R.string.Snabble_Payment_usableAt, sb.toString()); } + @SuppressWarnings("unchecked") + @Nullable + private List convertToList(final Parcelable[] parcels, final Class clazz) { + if (parcels == null) return null; + + final List list = new ArrayList<>(parcels.length); + for (final Parcelable parcel : parcels) { + if (parcel.getClass() == clazz) { + list.add(((T) parcel)); + } + } + return list; + } + private static class Entry { int drawableRes; String text;