DEV Community

Apironе
Apironе

Posted on

[DEV: PHP] How to create and process an invoice in cryptocurrency

If you do not want to handle payments at your site, check facts of under-/overpayment, then you just need to use a ready-made decision for creating invoices on the side of a processing service. https://apirone.com/docs/invoices/

Image description

The parameters necessary for creating an invoice are below.

The account can be unlimited or time-limited. For example, an unlimited invoice can be used to replenish game balances, and a time-limited one to pay for goods or services to reduce price volatility. This characteristic is controlled with the lifetime parameter which is indicated in seconds.

The amount for payment in an invoice is specified in the amount field, in which minor units of the cryptocurrencies are used, e.g. Satoshi for Bitcoin. The currency parameter is written in major cryptocurrency abbreviations, e.g. btc, tbtc (testnet), ltc, doge, tron, etc.
In the user-data parameter, you can put the name of your store, its link on the Internet, or the cost of goods or services. It is important to know that we do not convert the amount in the user-data array, so you can calculate the conversion from your sources or even specify any fictitious game currency in the currency field.

<?php

    $AccountID = "apr-e729d9982f079fa86b10a0e3aa6ff37b";
    $price_in_usd = 140;


    // getting price in BTC
    $ticker = file_get_contents("https://apirone.com/api/v2/ticker?currency=btc");
    $ticker = json_decode($ticker,true);
    $price_in_btc = round($price_in_usd / $ticker["usd"], 8) * 100000000;

    $payload = '{
    "amount": '. $price_in_btc .',
    "currency": "btc",
    "lifetime": 3600,
    "callback_url": "https://shop.spacex.com/handler.php",
    "user-data": {
        "merchant": "Space X flight",
        "url": "https://shop.spacex.com/collections/mens/products/mens-bomber-jacket",
        "price": {
            "currency": "LamboCoins",
            "amount": '. $price_in_usd .'
            }},
    "linkback": "https://shop.spacex.com"
    }';

    $api_base = "https://apirone.com/api/v2/accounts/" . $AccountID . "/invoices";

    $ch = curl_init( $api_base );
    # Setup request to send json via POST
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    # Return response instead of printing.
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    # Send request.
    $result = curl_exec($ch);
    curl_close($ch);
    # Print response.
    // echo "<pre>$result</pre>";
    $invoice = json_decode($result,true);

    echo '<a href="https://apirone.com/invoice?id='. $invoice["invoice"] .'" target="blank">Link to invoice '. $invoice["invoice"] .'</a>';

?>
Enter fullscreen mode Exit fullscreen mode

An invoice example is here https://apirone.com/invoice?id=KDDBhhSTYEbKOW83

Callback processing

When an invoice is paid to the specified address, a callback is provided, in the request this parameter is callback_url. Callback statuses for invoices are different from ordinary invoice processing. Statuses can be as follows:

  • created (when an invoice is created)
  • paid
  • part-paid (when the payment is confirmed, it is underpaid, and the invoice lifetime is over)
  • overpaid
  • completed (when the transfer of funds is confirmed in the blockchain)
  • expired (the invoice is overdue, i.e. it is not paid when the lifetime is over).

You can also request the invoice status independently https://apirone.com/api/v2/invoices/KDDBhhSTYEbKOW83

These parameters are enough to issue an invoice in cryptocurrency and to track its state.

Top comments (0)