In order to add your own API provider and make it work, you need:
There are two ways to create a class:
- You can extend
PostScripton\Money\Clients\RateExchangers\AbstractRateExchanger
where everything is ready, and you need to implement methods. - You can implement
PostScripton\Money\Clients\RateExchangers\RateExchanger
interface methods on your own.
The first way is more convenient and preferred.
You need to implement following methods:
rate()
that returns an exchange rate between two currencies.supports()
checks whether API service supports these currencies or not, and returns only UNSUPPORTED currencies.
The documentation to these methods is in PHPDocs in the code. You can also take a look at already implemented rate exchangers provided by this package because they extend the abstract class.
abstract protected function getRateRequestPath(?Carbon $date = null): string;
abstract protected function getRateRequestOptions(string $from, string|array $to): array;
abstract protected function getRateFromResponse(array $response, string|array $to): float|array;
abstract protected function getSupportsRequestPath(): string;
abstract protected function getSupportedCodesFromResponse(array $response): array;
abstract protected function isErrorInResponse(array $response): bool;
That’s all! All you need to do is to implement these methods!
However, you are free to even change rate()
and supports()
methods 😄
Firstly, you have to find a property rate_exchanger
.
Secondly, add a new array that will represent your provider.
There are some rules that you should follow, otherwise exceptions will be thrown:
- Array must contain a key
class
that has a full name of your API provider class. - Your class must inherit
PostScripton\Money\Clients\RateExchangers\RateExchanger
.
Other options in the array will be available in your class only if you'll specify a constructor:
use GuzzleHttp\RequestOptions;
// ...
public function __constructor(protected readonly array $config)
{
// this is an AbstractClient's constructor
parent::__constructor([
RequestOptions::QUERY => [
'access_key' => $config['key']
],
]);
}
RateExchangerException
- is thrown when:rate_exchangers.*.class
property in the config has a class name that doesn't exist.rate_exchanger
doesn't exist inrate_exchangers
property.rate_exchangers.*.class
property doesn't exist.rate_exchangers.*.class
property class doesn't inherit thePostScripton\Money\Services\ServiceInterface
.
📌 Back to the contents.