How to send bitcoins with commission extracted from payment in PHP or bitcoind First, I have simple problem. I want to send customer some bitcoins, but I dont want to pay fees for him. I'd like to substruct fees from payment, so all commissions would be payed by customer.

I thought it should be a simple and common problem for all merchants that making refunds to clients. But I googled for hours and could not find any detailed explanation on how to achieve this feature 'receiver-pays-commissions'.

Here some links that I found usefull, but still they dont have exact answer

How to create raw transaction in bitcoind

How to create raw transaction in PHP

How to calculate transaction size and fee

Simply put. I want to send some bitcoins away so that my account balance will be equal exactly 1.8000000. Now it is slightly higher:



Surprisingly this simple and common problem took hours of googling, so I'm making this question just to share solution with public, since I finally found it. 

(Or at least I think I've found it, so critics and comments are very welcome.)
Here is the algorythm without the code:


createrawtransaction with amount that you want to send assuming that fees are 0
fundrawtransaction to let your bitcoind construct final transaction, put your change address (on where you get change from unspent transaction - very important!) AND to caclulate fees that will be taken of your account for this transaction.
Substract caclulated fees from initial amount
Repeat steps 1-2 with new lowered amount.
Profit.


And here is the PHP code. 

We will need function to create and fund new transaction

function prepareRawTransaction($amountToSend, $addressToSendTo) {

    $bitcoin = new Bitcoin('bitcoinrpc', 'xxx');

    $collectedAmount = 0;
    $sendToArray = [];
    $sendToArray[$addressToSendTo] = $amountToSend;

    $unspentTransactions = $bitcoin-&gt;listunspent(); //First we find out all our unspent transactions from where we can withdraw
    $collectedTransactions = [];

    foreach ($unspentTransactions as $unspentTransaction) {

        if ($collectedAmount &lt; $amountToSend) {
            $collectedTransactions[] = [
                'txid'  =&gt; $unspentTransaction['txid'],
                'vout'  =&gt; $unspentTransaction['vout'],
            ];

            $collectedAmount += $unspentTransaction['amount'];
        } else {

            break;
        }
    }

    //Now we create raw transactions
    $rawTransaction = $bitcoin-&gt;createrawtransaction($collectedTransactions, $sendToArray);

    //And fund it
    $newTransaction = $bitcoin-&gt;fundrawtransaction($rawTransaction);
    return $newTransaction;
}


Now we can use that function to go on algorythm

$bitcoin = new Bitcoin('bitcoinrpc', 'xxx');

$amountToSend = 0.09999556; //That's exact amount that we want to send INCLUDING commission
$addressToSendTo = '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF';

//createrawtransaction with amount that you want to send assuming that fees are 0
$transactionWithoutCommission = prepareRawTransaction($amountToSend, $addressToSendTo);

$decoded = $bitcoin-&gt;decoderawtransaction($transactionWithoutCommission['hex']);
print_r($decoded); //You can see that fees now set up

//Substract caclulated fees from initial amount
$amountToSend =  $amountToSend - $transactionWithoutCommission['fee'];

//Repeat steps 1-2 with new lowered amount
$transactionWithCommission = prepareRawTransaction($amountToSend, $addressToSendTo);

$decoded = $bitcoin-&gt;decoderawtransaction($transactionWithCommission['hex']);
print_r($decoded); //Here you can see that total amount + commission equal needed amount

$signed = $bitcoin-&gt;signrawtransaction($transactionWithCommission['hex']);
print_r($signed); //should say complete:1 if everything was correctly put

$published = $bitcoin-&gt;sendrawtransaction($signed['hex']);
print_r($published); //will output you tx id


And profit: you have sent exactly the amount you wanted INCLUDING the commission