All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.minlessika.membership.integration.RestPaymentMethods Maven / Gradle / Ivy

Go to download

It's a library to help developers to integration membership services to another project.

There is a newer version: 0.3.1
Show newest version
package com.minlessika.membership.integration;

import com.google.common.collect.ImmutableMap;
import com.minlessika.http.client.RestClient;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.cactoos.scalar.Sticky;

public final class RestPaymentMethods implements PaymentMethods {

    private final Sticky response;

    private final Organization organization;

    private final RestClient client;

    public RestPaymentMethods(final RestClient client, final Organization organization) {
        this.organization = organization;
        this.client = client;
        this.response = new Sticky<>(
            () -> this.client.get(
                "/api/organization/payment-methods/active",
                ImmutableMap.of("org", this.organization.uid().toString())
            ).asJsonArray()
        );
    }

    @Override
    public PaymentMethod get(final UUID method) {
        for (final PaymentMethod mth : this.iterate()) {
            if (mth.uid().equals(method)) {
                return mth;
            }
        }
        throw new IllegalArgumentException("Le moyen de paiement n'a pas été trouvé !");
    }

    @Override
    public Iterable iterate() {
        try {
            final List items = new LinkedList<>();
            for (javax.json.JsonValue jsonValue : this.response.value()) {
                items.add(
                    new JsonPaymentMethod(jsonValue.asJsonObject())
                );
            }
            return items;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean find(final String filter) {
        for (final PaymentMethod mth : this.iterate()) {
            if (StringUtils.containsIgnoreCase(mth.name(), filter)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void add(final String name, final PaymentMethodType type) {
        final JsonObject body = Json.createObjectBuilder()
            .add("name", name)
            .add("typeId", type.id())
            .build();
        this.client.post(
            "/api/organization/payment-method",
            ImmutableMap.of("org", this.organization.uid().toString()),
            body
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy