Search in sources :

Example 1 with BankAccount

use of com.squareup.protos.tbd.pfi.BankAccount in project tbdex-protocol by TBD54566975.

the class PaymentProcessor method createBankAccount.

private BankAccount createBankAccount(PaymentProcessorRequest request) {
    CreateBankAccountRequest createBankAccountRequest = new CreateBankAccountRequest.Builder().accountNumber(request.account_number).routingNumber(request.routing_number).billingDetails(request.billing_details).bankAddress(request.bank_address).idempotencyKey(UUID.randomUUID().toString()).build();
    BankAccount bankAccount;
    try {
        bankAccount = circleClient.createBankAccount(createBankAccountRequest);
    } catch (Exception e) {
        // TODO: add more error handling here
        bankAccount = null;
    }
    return bankAccount;
}
Also used : CreateBankAccountRequest(com.squareup.protos.tbd.pfi.CreateBankAccountRequest) BankAccount(com.squareup.protos.tbd.pfi.BankAccount)

Example 2 with BankAccount

use of com.squareup.protos.tbd.pfi.BankAccount in project tbdex-protocol by TBD54566975.

the class RealCircleClient method createBankAccount.

@Override
public BankAccount createBankAccount(CreateBankAccountRequest createBankAccountRequest) throws Exception {
    OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, gson.toJson(createBankAccountRequest));
    Request request = requestBuilder.url("https://api-sandbox.circle.com/v1/businessAccount/banks/wires").post(body).build();
    Response response = client.newCall(request).execute();
    JSONObject data = new JSONObject(response.body().string()).getJSONObject("data");
    return new BankAccount.Builder().trackingRef(data.getString("trackingRef")).id(data.getString("id")).build();
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) Request(okhttp3.Request) TransferRequest(com.squareup.protos.tbd.pfi.TransferRequest) PayoutRequest(com.squareup.protos.tbd.pfi.PayoutRequest) CreateWirePaymentRequest(com.squareup.protos.tbd.pfi.CreateWirePaymentRequest) CreateBankAccountRequest(com.squareup.protos.tbd.pfi.CreateBankAccountRequest) MediaType(okhttp3.MediaType) BankAccount(com.squareup.protos.tbd.pfi.BankAccount) RequestBody(okhttp3.RequestBody)

Example 3 with BankAccount

use of com.squareup.protos.tbd.pfi.BankAccount in project tbdex-protocol by TBD54566975.

the class PaymentProcessor method process.

public void process(SettlementDetails settlementDetails, String threadToken) {
    // Get ASK from thread store
    // TODO: change to get conditional offer and also add source and target amounts in offer
    HibernateMessageThreadStore threadStore = new HibernateMessageThreadStore();
    MessageThread messageThread = threadStore.getThread(threadToken);
    Ask ask = messageThread.getAsk();
    Preconditions.checkNotNull(ask);
    String body = parser.toJson(settlementDetails.body);
    PaymentProcessorRequest request = parser.fromJson(body, PaymentProcessorRequest.class);
    // Register Bank Account with Circle.
    // TODO: Do not store bank account in our database
    BankAccount bankAccount = createBankAccount(request);
    System.out.println(bankAccount);
    Amount amount = new Amount.Builder().amount(ask.sourceAmount.toString()).currency(CurrencyCode.valueOf(ask.sourceCurrency)).build();
    // On-Ramp
    if (CurrencyCode.valueOf(ask.targetCurrency) == CurrencyCode.USDC) {
        CreateWirePaymentRequest createWirePaymentRequest = new CreateWirePaymentRequest.Builder().amount(amount).trackingRef(bankAccount.trackingRef).build();
        TransferRequest transferRequest = new TransferRequest.Builder().source(WALLET_SOURCE).destination(new Destination.Builder().type("blockchain").address(request.wallet_address).chain("ETH").build()).idempotencyKey(UUID.randomUUID().toString()).amount(amount).build();
        try {
            // Create a WIRE payment Request.
            circleClient.createWirePayment(createWirePaymentRequest);
            // Transfer USDC to external wallet address.
            circleClient.transfer(transferRequest);
        } catch (Exception e) {
            System.out.println("wire payment failed");
        }
    // Off-Ramp
    } else if (CurrencyCode.valueOf(ask.targetCurrency) == CurrencyCode.USD) {
        try {
            PayoutRequest payoutRequest = new PayoutRequest.Builder().source(WALLET_SOURCE).destination(new Destination.Builder().type("wire").id(bankAccount.id).build()).idempotencyKey(UUID.randomUUID().toString()).metadata(new Metadata.Builder().beneficiaryEmail(request.email_address).build()).amount(amount).build();
            circleClient.payout(payoutRequest);
        } catch (Exception e) {
            System.out.println("payout failed");
        }
    }
}
Also used : PayoutRequest(com.squareup.protos.tbd.pfi.PayoutRequest) Destination(com.squareup.protos.tbd.pfi.Destination) HibernateMessageThreadStore(io.tbd.tbdex.pfi_mock_impl.store.HibernateMessageThreadStore) Amount(com.squareup.protos.tbd.pfi.Amount) PaymentProcessorRequest(com.squareup.protos.tbd.pfi.PaymentProcessorRequest) MessageThread(io.tbd.tbdex.protocol.core.MessageThread) BankAccount(com.squareup.protos.tbd.pfi.BankAccount) CreateWirePaymentRequest(com.squareup.protos.tbd.pfi.CreateWirePaymentRequest) TransferRequest(com.squareup.protos.tbd.pfi.TransferRequest) Ask(io.tbd.tbdex.protocol.messages.Ask)

Aggregations

BankAccount (com.squareup.protos.tbd.pfi.BankAccount)3 CreateBankAccountRequest (com.squareup.protos.tbd.pfi.CreateBankAccountRequest)2 CreateWirePaymentRequest (com.squareup.protos.tbd.pfi.CreateWirePaymentRequest)2 PayoutRequest (com.squareup.protos.tbd.pfi.PayoutRequest)2 TransferRequest (com.squareup.protos.tbd.pfi.TransferRequest)2 Amount (com.squareup.protos.tbd.pfi.Amount)1 Destination (com.squareup.protos.tbd.pfi.Destination)1 PaymentProcessorRequest (com.squareup.protos.tbd.pfi.PaymentProcessorRequest)1 HibernateMessageThreadStore (io.tbd.tbdex.pfi_mock_impl.store.HibernateMessageThreadStore)1 MessageThread (io.tbd.tbdex.protocol.core.MessageThread)1 Ask (io.tbd.tbdex.protocol.messages.Ask)1 MediaType (okhttp3.MediaType)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 JSONObject (org.json.JSONObject)1