CurrencyCloud / currencycloud-php

MIT License
15 stars 38 forks source link

Specifying currency when using BalancesEntryPoint::find method #117

Closed LanceJenkinZA closed 3 years ago

LanceJenkinZA commented 3 years ago

Good day,

Currently, there is no way to specify the currency when using the BalancesEntryPoint::find method:

public function find(
        $amountFrom = null,
        $amountTo = null,
        $asAtDate = null,
        Pagination $pagination = null,
        $onBehalfOf = null
    ) {

The use case is to check if a user already has a specific currency set up on their account before attempting to create the currency.

For example


$balances = $currencyCloud->balances()->find([currency => $currencyCode]);
if($balances->count() > 0){
  // user already has currency set up on their account
 ....
} else { 
  // Create a balance for the user
  $balance = $currencyCloud->balances()->retrieve($currencyCode);
}

Thanks

gergelykovacs commented 3 years ago

Hello @LanceJenkinZA ,

SDK provides functionality in retrieve($currency, $onBehalfOf = null) under BalancesEntryPoint, also please see in the API schema:

or filter if target currency is in the list of balances.

LanceJenkinZA commented 3 years ago

Thanks @gergelykovacs, but the retrieve method has side effects which I don't want - that is, it will create a new balance if the balance is not already associated with the account.

I'm working with SPARK accounts if that helps.

With reference to the direct-demo platform;

My XY Problem is I would like to know if the user already has the currency associated with their account before I associate the currency with their account.

That way I can show the message "You already have a $currencyCode account".

gergelykovacs commented 3 years ago

@LanceJenkinZA , you can use the BalancesEntryPoint::find (api: /balances/find) to see balances per currency and filter the list to the currency you are interested in (https://www.php.net/array_filter), also you can use the on-behalf-of option to perform the same search for sub-accounts. If the list does not contain the currency you are interested in you can act accordingly. Filtering an array in your code equal to what you would get with providing a specific currency code to the SDK method. SDK method and API does not provide it.

LanceJenkinZA commented 3 years ago

Thanks @gergelykovacs.

I see I miss-read the documentation. When testing with Postman the query did work:

image

But I see that filtering by currency is not documented.

gergelykovacs commented 3 years ago

Hi @LanceJenkinZA , I close this issue, further related documentation can be found here: