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

one.credify.sdk.impl.BnplServiceImpl Maven / Gradle / Ivy

There is a newer version: v0.4.9
Show newest version
package one.credify.sdk.impl;

import one.credify.sdk.exception.BnplOrderAlreadyCanceledException;
import one.credify.sdk.exception.CredifyException;
import one.credify.sdk.BnplService;
import one.credify.sdk.dto.*;
import one.credify.sdk.restapi.CredifyRestV1;
import one.credify.sdk.utils.ErrorUtils;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

public class BnplServiceImpl implements BnplService {
    private static final String BnplOrderAlreadyCanceledError = "BnplOrderAlreadyCanceledError";
    private final CredifyRestV1 credifyClient;

    public BnplServiceImpl(CredifyRestV1 credifyClient) {
        this.credifyClient = credifyClient;
    }

    @Override
    public OrderInfo createOrder(String referenceId, FiatCurrency totalAmount, List orderLines, PaymentRecipient paymentRecipient) throws IOException {
        CreateOrderRequest req = new CreateOrderRequest(
                referenceId, totalAmount, orderLines, paymentRecipient
        );
        CredifyApiResponse response = this.credifyClient.createOrder(req).execute().body();
        if (response == null) {
            return null;
        }
        return response.getData();
    }

    @Override
    public OrderInfo getOrder(String orderId) throws IOException {
        CredifyApiResponse response = this.credifyClient.getOrder(orderId).execute().body();
        if (response == null) {
            return null;
        }
        return response.getData();
    }

    @Override
    public void completeOrder(String orderId, CompleteOrderRequest req) throws CredifyException, IOException {
        Response> response = this.credifyClient.completeOrder(orderId, req).execute();
        validateUpdateBnplResponse(response, "Complete");
    }

    @Override
    public void approveOrder(String orderId, ApproveOrderRequest req) throws CredifyException, IOException {
        Response> response = this.credifyClient.approveOrder(orderId, req).execute();
        validateUpdateBnplResponse(response, "Approve");
    }

    @Override
    public void acknowledgeOrder(String orderId) throws Exception {
        Response> response = this.credifyClient.acknowledgeOrder(orderId).execute();
        validateUpdateBnplResponse(response, "Acknowledge");
    }

    @Override
    public void acceptOrder(String orderId, String name, String email, PhoneNumberInfo phone) throws Exception {
        AcceptOrderRequest req = new AcceptOrderRequest(new OrderMessageInfo(name, email, phone));
        Response> response = this.credifyClient.acceptOrder(orderId, req).execute();
        validateUpdateBnplResponse(response, "Accept");
    }

    @Override
    public void cancelOrder(String orderId, String name, String email, PhoneNumberInfo phone, String code, String message) throws Exception {
        CancelOrderRequest req = CancelOrderRequest.builder()
                .message(new OrderMessageInfo(name, email, phone))
                .failedReason(new OrderFailedReasonInfo(code, message))
                .build();
        Response> response = this.credifyClient.cancelOrder(orderId, req).execute();
        validateUpdateBnplResponse(response, "Cancel");
    }

    @Override
    public GetOrderDocumentsResponse getOrderDocuments(String orderId) throws IOException {
        CredifyApiResponse response = this.credifyClient.getOrderDocuments(orderId).execute().body();
        if (response == null || !response.isSuccess()) {
            throw new CredifyException("get order documents failed");
        }
        return response.getData();
    }


    private void validateUpdateBnplResponse(Response> response, String action) throws IOException {
        if (response.isSuccessful()) {
            return;
        }
        CredifyApiErrorResponseV1 errorResponse = ErrorUtils.parseErrorV1(response.errorBody());

        if (response.code() == 409 && errorResponse != null && errorResponse.containsErrorCode(BnplOrderAlreadyCanceledError)) {
            throw new BnplOrderAlreadyCanceledException(action + " order failed");
        }
        throw new CredifyException(action + " order failed");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy