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

jp.gopay.sdk.builders.charge.AbstractChargesBuilders Maven / Gradle / Ivy

There is a newer version: 0.11.17
Show newest version
package jp.gopay.sdk.builders.charge;

import jp.gopay.sdk.builders.*;
import jp.gopay.sdk.models.common.*;
import jp.gopay.sdk.models.common.Void;
import jp.gopay.sdk.models.errors.DetailedError;
import jp.gopay.sdk.models.errors.GoPayException;
import jp.gopay.sdk.models.request.charge.CardChargeSearch;
import jp.gopay.sdk.models.response.charge.Charge;
import jp.gopay.sdk.models.response.transactiontoken.TemporaryTransactionToken;
import jp.gopay.sdk.models.response.transactiontoken.TokenAliasKey;
import jp.gopay.sdk.types.MetadataMap;
import jp.gopay.sdk.types.TransactionTokenType;
import jp.gopay.sdk.utils.Backoff;
import jp.gopay.sdk.utils.GoPayCallback;
import jp.gopay.sdk.utils.MetadataAdapter;
import jp.gopay.sdk.utils.functions.ErrorHandler;
import jp.gopay.sdk.utils.functions.Function;
import jp.gopay.sdk.utils.functions.GopayFunctions;
import jp.gopay.sdk.utils.functions.Predicate;
import retrofit2.Retrofit;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Date;
import java.util.concurrent.TimeoutException;

public abstract class AbstractChargesBuilders {

    public static abstract class AbstractListChargesRequestBuilder
            extends RetrofitRequestBuilderPaginated {

        protected StoreId storeId;
        protected CardChargeSearch propertySearch = new CardChargeSearch();
        protected String metadataSearch;

        protected StoreId getStoreId() {
            return storeId;
        }

        public AbstractListChargesRequestBuilder(Retrofit retrofit) {
            super(retrofit);
        }

        public AbstractListChargesRequestBuilder(Retrofit retrofit, StoreId storeId) {
            super(retrofit);
            this.storeId = storeId;
        }

        public B withCardChargeSearch(CardChargeSearch search) {
            this.propertySearch = search;
            return (B) this;
        }

        public B withMetadataSearch(String search) {
            this.metadataSearch = search;
            return (B) this;
        }

    }

    public static abstract class AbstractGetChargeRequestBuilder
            extends RetrofitRequestBuilder implements Polling {

        protected StoreId storeId;
        protected ChargeId chargeId;
        protected Boolean polling;

        protected StoreId getStoreId() {
            return storeId;
        }

        protected ChargeId getChargeId() {
            return chargeId;
        }

        public AbstractGetChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId) {
            super(retrofit);
            this.storeId = storeId;
            this.chargeId = chargeId;
        }

        public B withPolling(boolean polling) {
            this.polling = polling;
            return (B) this;
        }

    }

    public static abstract class AbstractCreateChargeRequestBuilder
            extends IdempotentRetrofitRequestBuilder implements DescriptorRetry {

        protected TransactionTokenId transactionTokenId;
        protected MoneyLike money;
        protected Boolean onlyDirectCurrency;
        protected Date captureAt;
        protected Boolean capture;
        protected Boolean ignoreDescriptorOnError = false;
        protected String descriptor;
        protected MetadataMap metadata;

        protected TransactionTokenId getTransactionTokenId() {
            return transactionTokenId;
        }

        public MoneyLike getMoney() {
            return money;
        }

        public Boolean getOnlyDirectCurrency() {
            return onlyDirectCurrency;
        }

        public Date getCaptureAt() {
            return captureAt;
        }

        public Boolean getCapture() {
            return capture;
        }

        public String getDescriptor() {
            return descriptor;
        }

        protected MetadataMap getMetadata() {
            return metadata;
        }

        public AbstractCreateChargeRequestBuilder(Retrofit retrofit, TransactionTokenId transactionTokenId, MoneyLike money, Boolean capture) {
            super(retrofit);
            this.transactionTokenId = transactionTokenId;
            this.money = money;
            this.capture = capture;
        }

        public B withOnlyDirectCurrency(Boolean onlyDirectCurrency) {
            this.onlyDirectCurrency = onlyDirectCurrency;
            return (B) this;
        }

        public B withCaptureAt(Date captureAt) {
            this.captureAt = captureAt;
            return (B) this;
        }

        public B withDescriptor(String descriptor) {
            this.descriptor = descriptor;
            return (B) this;
        }

        public B withDescriptor(String descriptor, Boolean ignoreDescriptorOnError) {
            this.descriptor = descriptor;
            this.ignoreDescriptorOnError = ignoreDescriptorOnError;
            return (B) this;
        }

        public B withMetadata(MetadataMap metadata) {
            this.metadata = metadata;
            return (B) this;
        }

        public  B withMetadata(T metadata, MetadataAdapter adapter) {
            this.metadata = adapter.serialize(metadata);
            return (B) this;
        }


        @Override
        public Request build() {

            if(descriptor != null && ignoreDescriptorOnError){
                Request request = new RetrofitRequestCaller<>(retrofit, createCall());
                return retryIgnoringDescriptor(request);
            } else
                return super.build();
        }

        @Override
        public Request retryIgnoringDescriptor(Request originalRequest) {
            return RetryUtils.retryIgnoringDescriptor(originalRequest, this);
        }
    }

    public static abstract class AbstractUpdateChargeRequestBuilder
            extends IdempotentRetrofitRequestBuilder {

        protected StoreId storeId;
        protected ChargeId chargeId;
        protected MetadataMap metadata;

        public StoreId getStoreId() {
            return storeId;
        }

        public ChargeId getChargeId() {
            return chargeId;
        }

        protected MetadataMap getMetadata() {
            return metadata;
        }

        public AbstractUpdateChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId) {
            super(retrofit);
            this.storeId = storeId;
            this.chargeId = chargeId;
        }

        public B withMetadata(MetadataMap metadata) {
            this.metadata = metadata;
            return (B) this;
        }

        public  B withMetadata(T metadata, MetadataAdapter adapter) {
            this.metadata = adapter.serialize(metadata);
            return (B) this;
        }
    }

    public static abstract class AbstractCaptureAuthorizedChargeRequestBuilder
            extends IdempotentRetrofitRequestBuilder {

        protected StoreId storeId;
        protected ChargeId chargeId;
        protected MoneyLike money;

        public MoneyLike getMoney() {
            return money;
        }

        public StoreId getStoreId() {
            return storeId;
        }

        public ChargeId getChargeId() {
            return chargeId;
        }

        public AbstractCaptureAuthorizedChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId, MoneyLike money) {
            super(retrofit);
            this.storeId = storeId;
            this.chargeId = chargeId;
            this.money = money;
        }
    }

    public static abstract class AbstractCreateChargeWithTokenAliasRequestBuilder {
        protected StoreId storeId;
        protected TokenAliasKey alias;
        protected MoneyLike money;
        protected Boolean onlyDirectCurrency;
        protected Date captureAt;
        protected Boolean capture;
        protected String descriptor;
        protected Boolean ignoreDescriptorOnError = false;
        protected MetadataMap metadata;
        protected IdempotencyKey idempotencyKey;

        protected boolean polling = false;
        protected long timeout = 0;
        protected Backoff backoff = null;

        public StoreId getStoreId() {
            return storeId;
        }

        public TokenAliasKey getAlias() {
            return alias;
        }

        protected MoneyLike getMoney() {
            return money;
        }

        protected Boolean getOnlyDirectCurrency() {
            return onlyDirectCurrency;
        }

        protected Date getCaptureAt() {
            return captureAt;
        }

        protected Boolean getCapture() {
            return capture;
        }

        protected String getDescriptor() {
            return descriptor;
        }

        public Boolean getIgnoreDescriptorOnError() {
            return ignoreDescriptorOnError;
        }

        public boolean isPolling() {
            return polling;
        }

        public long getTimeout() {
            return timeout;
        }

        public Backoff getBackoff() {
            return backoff;
        }

        protected MetadataMap getMetadata() {
            return metadata;
        }

        protected IdempotencyKey getIdempotencyKey() {
            return idempotencyKey;
        }

        public B withOnlyDirectCurrency(Boolean onlyDirectCurrency) {
            this.onlyDirectCurrency = onlyDirectCurrency;
            return (B) this;
        }

        public B withCaptureAt(Date captureAt) {
            this.captureAt = captureAt;
            return (B) this;
        }

        public B withDescriptor(String descriptor, Boolean ignoreDescriptorOnError) {
            this.descriptor = descriptor;
            this.ignoreDescriptorOnError = ignoreDescriptorOnError;
            return (B) this;
        }

        public B withDescriptor(String descriptor) {
            this.descriptor = descriptor;
            return (B) this;
        }

        public B withMetadata(MetadataMap metadata) {
            this.metadata = metadata;
            return (B) this;
        }

        public  B withMetadata(T metadata, MetadataAdapter adapter) {
            this.metadata = adapter.serialize(metadata);
            return (B) this;
        }

        public B withIdempotencyKey(IdempotencyKey idempotencyKey) {
            this.idempotencyKey = idempotencyKey;
            return (B) this;
        }

        protected B withPolling(boolean polling, long timeout, Backoff backoff) {
            this.polling = polling;
            this.timeout = timeout;
            this.backoff = backoff;
            return (B) this;
        }

        public B withPolling(long timeout, Backoff backoff) {
            return withPolling(true, timeout, backoff);
        }

        public B withPolling(long timeout) {
            return withPolling(true, timeout, null);
        }

        public B withPolling() {
            return withPolling(true, 0, null);
        }

        public AbstractCreateChargeWithTokenAliasRequestBuilder(StoreId storeId, TokenAliasKey alias, MoneyLike money, Boolean capture) {
            this.storeId = storeId;
            this.alias = alias;
            this.money = money;
            this.capture = capture;
        }

        public abstract E dispatch() throws IOException, GoPayException, TimeoutException, InterruptedException;

        protected abstract E pollAndDispatch() throws IOException, GoPayException, TimeoutException, InterruptedException;

        public abstract void dispatch(final GoPayCallback callback);

        protected abstract void pollAndDispatch(final GoPayCallback callback);

        protected void checkIsActive(M temporaryTransactionToken) {
            if (!temporaryTransactionToken.getActive()) {
                throw new IllegalStateException("Transaction Token is not active");
            }
        }

        protected void checkIsNotSubscription(M temporaryTransactionToken) {
            if (temporaryTransactionToken.getType() == TransactionTokenType.SUBSCRIPTION) {
                throw new IllegalArgumentException("Can't create a charge with a transaction token of type 'subscription'.");
            }
        }

        protected MoneyLike fillMoney(MoneyLike money, BigInteger tokenAmount, String tokenCurrency) {
            if (money == null && (tokenAmount == null || tokenCurrency == null)) {
                throw new IllegalArgumentException("Set charge amount and currency");
            } else if (tokenAmount != null && tokenCurrency != null) {
                return new MoneyLike(tokenAmount, tokenCurrency);
            }
            return new MoneyLike(money.getAmount(), money.getCurrency());
        }
    }
}