This is a PHP library to enable BitCoin Cash (BCH) and SLP token payments. You can easily use this with WordPress, Laravel and other PHP frameworks.
With composer (recommended):
composer require "ekliptor/cashp"
Manual installation:
cashp.php from the root directory of this library.src/phpqrcodePHP >= 7.1
Take a look at code examples.
Create the main API class.
CashpOptions $options - (optional) API options (see below)Return the exchange rate API.
Return the Blockchain API to generate addresses, check balances, transactions,...
Generate a QR code for a payment.
string $fileLocal - A path on your local filesystem to store the QR code file. This should be accessible from the web if you want to display the QR code to users.
If the given file already exists it will NOT be overwritten (QR codes are meant to be generated & cached in your web temp directory).string $address - The (1-time) BCH (or SLP) address created for this payment.float $amountBCH - The amount in BCH. Can be 0 if the user pays the full amount in SLP tokens.float $amountToken - (optional) The amount of the optional token to be received.string $tokenID - (optional) The hex ID of the SLP token. Required if $amountToken > 0.returns bool - true on success, false otherwise
Return a payment URI (starting with "bitcoincash:" or "simpleledger:" if $amountToken > 0) for the given $address.
string $address - The receiving BCH (or SLP) address.float $amountBCH - The amount in BCH to receive.float $amountToken - (optional) The amount of SLP tokens to receive.string $tokenID - (optional) The hex ID of the SLP token. Required if $amountToken > 0.returns string
Get the HTML code of a BadgerButton. See https://badger.bitcoin.com/
array $btnConf - associative array with buttom config
string $address - The receiving BCH (or SLP) address.float $amountBCH - The amount in BCH to receive.float $amountToken - (optional) The amount of SLP tokens to receive.string $tokenID - (optional) The hex ID of the SLP token. Required if $amountToken > 0.returns string - The button HTML.
Check if a BCH address is valid.
string $bchAddress - The address in CashAddress format starting with 'bitcoincash:'returns bool - True if the address is valid, false otherwise.
Check if a SLP address is valid.
string $slpAddress - The address starting with 'simpleledger:'returns bool - True if the address is valid, false otherwise.
Gets the return BCH address (belonging to the sender senders address) defined as the last address in transaction outputs.
Transaction $tx - The transaction from getTransaction() call.returns string - the address
Gets the return SLP address (belonging to the sender senders address) defined as the last address in transaction outputs.
Transaction $tx - The transaction from getTransaction() call.returns string - the address
A set of advanced config properties.
$httpAgent = null - The HTTP implementation used to make HTTP requests.
Values: BasicHttpAgent|CurlHttpAgent|WordpressHttpAgent
Defaults to BasicHttpAgent, but you should use a better one according to your PHP setup.$exchangeRateExpirationMin = 60 - How long the crawled exchange rate shall stay in cache. Currently rates are not saved beyond script execution.$httpTimeoutSec = 10 - The timeout for HTTP requests to the REST API backend.$blockchainApiImplementation = "BitcoinComRestApi" - The REST API backend implementation to use. Allowed values: BitcoinComRestApi|BchdProtoGatewayApi|SlpDbApiAn API to get BCH exchanges rates to fiat currencies.
Get the current exchange rate for BCH.
string $currency - A fiat currency such as USD|EUR|JPYreturns float
The Blockchain API to generate addresses, check balances, transactions,...
Set a logger function for errors and debug output. Use this to write to a logfile or database. If no function is provided everything will be printed using 'echo'.
callable $loggerFn(string $subject, mixed $error, mixed $data = null) - parameters of the PHP callableSet a a HTTP implementation for requests (cURL, Wordpress HTTP API,...)
AbstractHttpAgent $agent -Return the number of confirmation for the given blockchain transaction ID.
string $transactionID -returns int - The number of confirmations or -1 if the $transactionID doesn't exist.
Creates a new address from the xPub.
string $xPub - The extended public key. Called 'Master Public Key' in Electron Cash.int $addressCount - The number of the next address to generate a unique address. Usually this should be an incrementing integer.string $hdPathFormat - (optional) The HD path to be used for creating address children.returns BchAddress - the address or null on failure
Get general (network-wide) info about a SLP token.
string $tokenID -returns SlpToken - The token or null on failure
Return the BCH balance of the given address (including unconfirmed transactions).
string $address - The BCH address in CashAddress format.returns float - The balance or -1 if the address doesn't exist.
Return the token balance of the given SLP address (including unconfirmed transactions).
string $address -string $tokenID -returns float - The balance or -1 if the address doesn't exist.
Return the BCH Address with all its properties such as balance, TXIDs,...
string $address - The BCH address in CashAddress format.returns BchAddress - the address or null on failure
Return the SLP token details of a given address include balance, TXIDs,...
string $address -string $tokenID -returns SlpTokenAddress - The token or null on failure
Returns a transaction with all inputs and outputs including SLP data.
string $transactionID -returns Transaction - The transaction or null on failure
To run unit tests type the following command in the project root directory (requires PHPUnit, installed automatically with Composer):
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests
WordPress plugin