PHP code example of g33kme / adapay

1. Go to this page and download the library: Download g33kme/adapay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

g33kme / adapay example snippets




/*
 * You have to setup some Settings
 */

// Define your URL to API, with version, but NO ending slash!
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

//Optional, you can define an walletid, but you can also pass a walletid in the parameters
define('ADAPAY_WALLETID', '129328d18339990c7398e02975c4513754881337');

// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * First we need to create or restore a wallet on our cardano-wallet server
 * Keep in mind, that the cardano-node have to be fully synced so your wallet comes up
 * 
 * name              = set any name 
 * mnemonic_sentence = create new or add your existing mnemonic recovery phrase to restore a wallet, 24 words for shelly wallet
 * passphrase        = set a password
 * 
 * You will get an "id" back, save this unique id to use the wallet for ADAPay
 */

$wallet = ADAPAY::restoreWallet(array(
    'name' => 'adapay',
    'mnemonic' => 'that are just twenty four words that have no meaning and only for adapay as placeholder so dont try to copy this cheers adapay'
    'passphrase' => 'myAwesomeAdaPayPassword',
));
print_r($wallet);

/*
 * If you already created a wallet you can simply show all created wallets and grab your walletid
 */
$wallets = ADAPAY::wallets(array();
print_r($wallets);

/*
 * List all you receiving wallet cardano addresses
 * Use one  from the list to receive your payments on and create invoices
 */
$addresses = ADAPAY::walletAdresses();
print_r($addresses);

/*
 * Check current network Status, Cardano Node height
 */
$networkStatus = ADAPAY::networkStatus();
print_r($networkStatus);


// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * How to create an invoice
 * 
 * pair     = set a pair like ADAUSD, ADAEUR, ADAGBP
 * amount   = fiat amount, based on the pair your ada amount for the invoice will be calculated
 * identify = how to identify the paid invoice
 * death    = in seconds when the invoice expires
 * address  = set one of your cardano addresses from your created wallet where you want to receive the payment
 * walletid = set a walletid from your cardano-wallet you created in the first step
 * 
 * Each created cardano invoice return an unique hash and the calculated ADA price from your fiat amount
 * We recommend to save your invoice in a database to flag them as paid if you received it
 * 
 * Currently ADAPay will create an unique number on the payment amount to identify incoming payment
 * Creating a new Cardano addresse for each invoice is currently not easy with the Cardano Wallet API, we may provide this in the future 
 */
$invoice = ADAPAY::invoiceCreate(array(
   'pair' => 'ADAUSD',
   'amount' => 10,
   'identify' => array(
        'type' => 'amount'
   ), 
   'death' => 1800,
   'address' => 'addr1qxksn95zhgje7tvdsgfpk9t49sssz4fqewt74neh56cnl4ml8zpc3556jh8exfp70a6f3pva7yf4fmfmw52tdh3dh94sqdvu27',
   'walletid' => '129328d18339990c7398e02975c4513754881337' 
));
print_r($invoice);

// Define your URL to API
define('ADAPAY_API', 'http://<IP4-SERVER-ADDRESS>:8090/v2');

/*
 * Check you invoice was paid and you got the payment to your cardano wallet
 * You can simply pass the return parameters you got in your $invoice
 * 
 */
$verify = ADAPAY::verifyPayment(array(
   'hash' => $invoice['hash'],
   'walletid' => $invoice['walletid'],
   'ada' => $invoice['ada'],
   'created' => $invoice['created'],
   'death' => $invoice['death']
));
print_r($verify);

//Or simply pass the $invoice you created
$verify = ADAPAY::verifyPayment($invoice);
print_r($verify);


if($verify['waiting']) {
    //Still waiting for the payment, not received
    
} 
elseif($verify['expired']) {
    //Invoice is expired
    
}
elseif($verify['paid']) {
     /*
     * Payment received
     * Do your database updates, flag invoice as paid etc ...
     */
    
}

# Get last cardano fiat price, via a pair like: ADAEUR, ADAUSD, ADAGBP ...
$lastPrice = ADAPAY::lastPrice(array('pair' => 'ADAEUR'));
print_r($lastPrice);

$fiat = 10;
$ada = $fiat / $lastPrice;
echo $fiat.' EUR are currently '.$ada.' ADA';

# Get last price in EURO
$lastPriceEuro = ADAPAY::lastPriceEuro();
print_r($lastPriceEuro);

# Get last price in Dollar
$lastPriceDollar = ADAPAY::lastPriceDollar();
print_r($lastPriceDollar);

# Get last price in British Pound
$lastPricePound = ADAPAY::lastPricePound();
print_r($lastPricePound);


//Inspect a single cardano address
$inspect = ADAPAY::addressInspect(array('id' => 'addr1qxksn95zhgje7tvdsgfpk9t49sssz4fqewt74neh56cnl4ml8zpc3556jh8exfp70a6f3pva7yf4fmfmw52tdh3dh94sqdvu27'));
print_r($inspect);

/*
 * List all sent and receive transaction on your wallet
 * 
 * id       = Set one of your wallet ids
 * start    = Optional, set a start date in ISO8601
 * end      = Optional, set an end date in ISO8601
 */
$transactions = ADAPAY::walletTransactions(array(
    'id' => '129328d18339990c7398e02975c4513754881337',

    //List all transaction for walletid in the last hour
    'start' => date(DATE_ISO8601, time()-3600)
));
print_r($transactions);

/*
 * This is some basic example on your ajax.php
 * Of course you can do your checks however you want
 */

//Highly recommend to clean your parameter requests, ADAPay will help
= $request['param2'];
$param3 = $request['param3'];

//First get your created invoice from your database maybe from your set parameters in your Javascript Ajax request
//TODO

//Check invoice from DB
if(!empty($invoice)) {
    if($invoice['paid']) {
        echo 'paid';
    } else {
        $status = ADAPAY::verifyPayment();
        
        if($status['waiting']) {
            echo 'waiting';
        }
        elseif($status['expired']) {
            echo 'expired';
        }
        else($status['paid']) {
            echo 'paid';
            
            //Highly recommend to update your created invoice in
            //TODO
 
        }
    }
}