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

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

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

import jp.gopay.sdk.GoPaySDK;
import jp.gopay.sdk.builders.Request;
import jp.gopay.sdk.builders.ResourceMonitor;
import jp.gopay.sdk.builders.ResourcePredicate;
import jp.gopay.sdk.builders.RetrofitRequestCaller;
import jp.gopay.sdk.builders.charge.AbstractChargesBuilders.*;
import jp.gopay.sdk.models.common.*;
import jp.gopay.sdk.models.common.Void;
import jp.gopay.sdk.models.errors.GoPayException;
import jp.gopay.sdk.models.request.charge.CaptureReq;
import jp.gopay.sdk.models.request.charge.ChargesReq;
import jp.gopay.sdk.models.request.charge.PatchReq;
import jp.gopay.sdk.models.response.PaginatedList;
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.resources.ChargesResource;
import jp.gopay.sdk.types.ChargeStatus;
import jp.gopay.sdk.types.TransactionTokenType;
import jp.gopay.sdk.utils.GoPayCallback;
import jp.gopay.sdk.utils.functions.ErrorHandler;
import jp.gopay.sdk.utils.functions.Function;
import jp.gopay.sdk.utils.functions.GopayFunctions;
import retrofit2.Call;
import retrofit2.Retrofit;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public abstract class ChargesBuilders {

    public static class ListChargesRequestBuilder
            extends AbstractListChargesRequestBuilder{

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

        public ListChargesRequestBuilder(Retrofit retrofit, StoreId storeId) {
            super(retrofit, storeId);
        }

        @Override
        protected Call> getRequest(ChargesResource resource) {
            if (storeId != null) {
                return resource.listAllStoreCharges(storeId, getLimit(), getCursorDirection(), getCursor(), propertySearch.asMap(), metadataSearch);
            } else {
                return resource.listAllCharges(getLimit(), getCursorDirection(), getCursor(), propertySearch.asMap(), metadataSearch);
            }
        }
    }

    public static class GetChargeRequestBuilder
            extends AbstractGetChargeRequestBuilder{

        public GetChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId) {
            super(retrofit, storeId, chargeId);
        }

        @Override
        protected Call getRequest(ChargesResource resource) {
            return resource.getStoreCharge(storeId, chargeId, polling);
        }
    }

    public static class CreateChargeRequestBuilder
            extends AbstractCreateChargeRequestBuilder{

        public CreateChargeRequestBuilder(Retrofit retrofit, TransactionTokenId transactionTokenId, MoneyLike money, Boolean capture) {
            super(retrofit, transactionTokenId, money, capture);
        }

        @Override
        protected Call getRequest(ChargesResource resource) {
            return resource.createCharge(new ChargesReq(transactionTokenId, money, capture, captureAt, metadata,
                    onlyDirectCurrency, descriptor), idempotencyKey);
        }
    }

    public static class UpdateChargeRequestBuilder
            extends AbstractUpdateChargeRequestBuilder{

        public UpdateChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId) {
            super(retrofit, storeId, chargeId);
        }

        @Override
        protected Call getRequest(ChargesResource resource) {
            return resource.updateCharge(storeId, chargeId, new PatchReq(metadata), idempotencyKey);
        }
    }

    public static class CaptureAuthorizedChargeRequestBuilder
            extends AbstractCaptureAuthorizedChargeRequestBuilder{

        public CaptureAuthorizedChargeRequestBuilder(Retrofit retrofit, StoreId storeId, ChargeId chargeId, MoneyLike money) {
            super(retrofit, storeId, chargeId, money);
        }

        @Override
        protected Call getRequest(ChargesResource resource) {
            return resource.capture(storeId, chargeId, new CaptureReq(money), idempotencyKey);
        }
    }

    public static ResourceMonitor createChargeCompletionMonitor(Retrofit retrofit, StoreId storeId, ChargeId chargeId) {
        return new ResourceMonitor<>(new GetChargeRequestBuilder(retrofit, storeId, chargeId)
                .withPolling(true),
                new ResourcePredicate() {
                    @Override
                    public boolean test(Charge resource) {
                        return resource.getStatus() != ChargeStatus.PENDING;
                    }
                });
    }

    public static class CreateChargeWithTokenAliasRequestBuilder
            extends AbstractCreateChargeWithTokenAliasRequestBuilder {

        private GoPaySDK gopay;

        public CreateChargeWithTokenAliasRequestBuilder(StoreId storeId, MoneyLike money, Boolean capture, GoPaySDK gopay, TokenAliasKey alias) {
            super(storeId, alias, money, capture);
            this.gopay = gopay;
        }

        @Override
        public Charge dispatch() throws IOException, GoPayException, TimeoutException, InterruptedException {
            if (polling) {
                return pollAndDispatch();
            } else {
                TemporaryTransactionToken temporaryTransactionToken = gopay.getTokenAlias(storeId, alias).build().dispatch();

                Charge charge = getChargeRequestBuilder(temporaryTransactionToken)
                        .build().dispatch();

                deleteTokenAlias(temporaryTransactionToken);

                return charge;
            }
        }

        @Override
        protected Charge pollAndDispatch() throws IOException, GoPayException, TimeoutException, InterruptedException {
            TemporaryTransactionToken temporaryTransactionToken = gopay.getTokenAlias(storeId, alias).build().dispatch();

            Charge charge = getChargeRequestBuilder(temporaryTransactionToken)
                    .build().dispatch();

            deleteTokenAlias(temporaryTransactionToken);

            if (timeout != 0 && backoff != null) {
                charge = gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await(timeout, backoff);
            } else if (timeout != 0) {
                charge = gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await(timeout);
            } else {
                charge = gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await();
            }

            return charge;
        }

        @Override
        public void dispatch(final GoPayCallback callback) {
            if (polling) {
                pollAndDispatch(callback);
            } else {
                gopay.getTokenAlias(storeId, alias).build().dispatch(
                        new GoPayCallback() {
                            @Override
                            public void getResponse(final TemporaryTransactionToken temporaryTransactionToken) {
                                getChargeRequestBuilder(temporaryTransactionToken)
                                        .build().dispatch(new GoPayCallback() {
                                                              //delete a token alias after create a charge
                                                              @Override
                                                              public void getResponse(final Charge charge) {
                                                                  if (temporaryTransactionToken.getType() == TransactionTokenType.RECURRING) {
                                                                      gopay.deleteTokenAlias(storeId, alias).build().dispatch(
                                                                              new GoPayCallback() {
                                                                                  @Override
                                                                                  public void getResponse(Void response) {
                                                                                      callback.getResponse(charge);
                                                                                  }

                                                                                  @Override
                                                                                  public void getFailure(Throwable error) {
                                                                                      callback.getResponse(charge);
                                                                                      callback.getFailure(error);
                                                                                  }
                                                                              }
                                                                      );
                                                                  } else {
                                                                      callback.getResponse(charge);
                                                                  }
                                                              }

                                                              @Override
                                                              public void getFailure(Throwable error) {
                                                                  callback.getFailure(error);
                                                              }
                                                          }
                                );
                            }

                            @Override
                            public void getFailure(Throwable error) {
                                callback.getFailure(error);
                            }
                        }
                );
            }
        }

        @Override
        protected void pollAndDispatch(final GoPayCallback callback) {
            gopay.getTokenAlias(storeId, alias).build().dispatch(
                    new GoPayCallback() {
                        @Override
                        public void getResponse(final TemporaryTransactionToken temporaryTransactionToken) {
                            getChargeRequestBuilder(temporaryTransactionToken)
                                    .build().dispatch(new GoPayCallback() {
                                                          //delete a token alias after create a charge
                                                          @Override
                                                          public void getResponse(final Charge charge) {
                                                              if (temporaryTransactionToken.getType() == TransactionTokenType.RECURRING) {
                                                                  gopay.deleteTokenAlias(storeId, alias).build().dispatch(
                                                                          new GoPayCallback() {
                                                                              @Override
                                                                              public void getResponse(Void response) {
                                                                                  pollingCharge(charge, callback);
                                                                              }

                                                                              @Override
                                                                              public void getFailure(Throwable error) {
                                                                                  pollingCharge(charge, callback);
                                                                                  callback.getFailure(error);
                                                                              }
                                                                          }
                                                                  );
                                                              } else {
                                                                  pollingCharge(charge, callback);
                                                              }
                                                          }

                                                          @Override
                                                          public void getFailure(Throwable error) {
                                                              callback.getFailure(error);
                                                          }
                                                      }
                            );
                        }

                        @Override
                        public void getFailure(Throwable error) {
                            callback.getFailure(error);
                        }
                    }
            );
        }

        private CreateChargeRequestBuilder getChargeRequestBuilder(TemporaryTransactionToken temporaryTransactionToken) {
            checkIsActive(temporaryTransactionToken);
            checkIsNotSubscription(temporaryTransactionToken);
            MoneyLike createMoney = fillMoney(money, temporaryTransactionToken.getAmount(), temporaryTransactionToken.getCurrency());
            return gopay.createCharge(temporaryTransactionToken.getId(), createMoney.getAmount(), createMoney.getCurrency(), capture)
                    .withDescriptor(descriptor, ignoreDescriptorOnError)
                    .withOnlyDirectCurrency(onlyDirectCurrency)
                    .withCaptureAt(captureAt)
                    .withMetadata(metadata)
                    .withIdempotencyKey(idempotencyKey);
        }

        private void deleteTokenAlias(TemporaryTransactionToken temporaryTransactionToken) throws IOException, GoPayException {
            if (temporaryTransactionToken.getType() == TransactionTokenType.RECURRING) {
                gopay.deleteTokenAlias(storeId, alias).build().dispatch();
            }
        }

        private void pollingCharge(Charge charge, GoPayCallback callback) {
            if (timeout != 0 && backoff != null) {
                gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await(callback, timeout, backoff);
            } else if (timeout != 0) {
                gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await(callback, timeout);
            } else {
                gopay.chargeCompletionMonitor(charge.getStoreId(), charge.getId())
                        .await(callback);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy