Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/io/snabble/sdk/PaymentMethod.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<PaymentMethod> {
override fun createFromParcel(parcel: Parcel) = PaymentMethod.values()[parcel.readInt()]
override fun newArray(size: Int) = arrayOfNulls<PaymentMethod>(size)
}

/**
* Converts a payment method from its string representation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -27,18 +29,23 @@

public class SelectPaymentMethodFragment extends BottomSheetDialogFragment {
public static final String ARG_PROJECT_ID = "projectId";
public static final String ARG_SUPPORTED_METHODS = "supportedMethods";

private List<Entry> entries;
private Set<PaymentMethod> paymentMethods;
private String projectId;

@Nullable
private List<PaymentMethod> supportedMethods;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle args = getArguments();
if (args != null) {
projectId = args.getString(ARG_PROJECT_ID, null);
supportedMethods = convertToList(args.getParcelableArray(ARG_SUPPORTED_METHODS), PaymentMethod.class);
}
}

Expand All @@ -51,7 +58,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
Set<PaymentMethod> availablePaymentMethods = new HashSet<>();
for (Project project : Snabble.getInstance().getProjects()) {
if (project.getId().equals(projectId)) {
availablePaymentMethods.addAll(project.getAvailablePaymentMethods());
final List<PaymentMethod> methods = project.getAvailablePaymentMethods()
.stream()
.filter(this::isSupportedMethod)
.collect(Collectors.toList());
availablePaymentMethods.addAll(methods);
}
}

Expand Down Expand Up @@ -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<Project> projects = Snabble.getInstance().getProjects();
if (projects.size() == 1) {
Expand Down Expand Up @@ -219,6 +237,20 @@ private String getUsableAtText(PaymentMethod... paymentMethods) {
return getResources().getString(R.string.Snabble_Payment_usableAt, sb.toString());
}

@SuppressWarnings("unchecked")
@Nullable
private <T> List<T> convertToList(final Parcelable[] parcels, final Class<T> clazz) {
if (parcels == null) return null;

final List<T> 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;
Expand Down