So called "block chain based refund" is not reliable with bitcoin.
The sending wallet may be a shared web wallet (blockchain.info, MtGox, e.g. any web site) and thus you cannot guarantee that any of change addresses in the transaction are controlled by the user. Even by exploring the block chain and knowing from which address the bitcoins arrived and where unspent change went, it is not guaranteed that if you make a refund to any of these addresses the sender receives the payment.
Instead, all refunds must be handled off-band and the refund bitcoin address must be asked separately.
E.g. SatoshiDice includes the following disclaimer (players basically must use a desktop wallet where the wallet software generates a change address and the wallet is not shared with other users):
IMPORTANT: Only use wallets that allow you to receive Bitcoin from the same address you sent from. If you're not sure, test by sending a bet for the minimum amount. If you get nothing back, then your wallet is not compatible. Note that some bets may require one confirmation before the win/loss value is sent back.
A just-dice.com FAQ regarding shared web wallet.
In the case you still want to pursuit change-address based refund you might find these links useful
Here is some more info (I have written this down some time ago, but I have not verified whether this is still valid):
Your bitcoind must run in a special: Since bitcoind/Bitcoin-qt 0.8, no transaction index is kept anymore by default, as it is not necessary for validation in the new database model. Instead, there is only a database of unspent transaction outputs, which has enough information to (slowly) locate transactions in block files. getrawtransaction uses this, but it only works for transactions that are not yet spent in the block chain. If you want to be able to look up historical transactions, you need to set the txindex=1 configuration option (or start with the -txindex command line flag). As this setting can only be set when the database is created, you'll need to rebuild it from scratch (using -reindex).
1I know it is unreliable, but just like SatoshiDice, I would include a disclamer. – rrrrrrrrrrrrrrrr – 2013-12-16T21:00:01.250
Ok, let me edit the question for you – Mikko Ohtamaa – 2013-12-16T21:05:42.190
I'm new to StackExchange. Would it be ok to answer my own question with a precise solution that I found out by analyzing the information you provided me? – rrrrrrrrrrrrrrrr – 2013-12-16T21:18:45.690
The last link contains the answer how to do it with API calls (and Python script snippet). However I am reclutant to put it into an answer, as I am not sure if it still works unmodified. – Mikko Ohtamaa – 2013-12-16T22:17:22.113
@RiccardoBestetti its okay to answer your own question and mark it as correct – CQM – 2013-12-16T22:18:01.907
1Tomorrow I will post my own answer base on the information provided by @MikkoOhtamaa and my own (working) code snippets. – rrrrrrrrrrrrrrrr – 2013-12-16T22:21:31.393
1Added some additional info to the answer. – Mikko Ohtamaa – 2013-12-16T22:24:17.330
Any luck yet with the scripting? – Mikko Ohtamaa – 2013-12-19T20:55:45.627
No, I've been really busy in the last days. I'm going to work on it today! – rrrrrrrrrrrrrrrr – 2013-12-21T10:58:30.797
I'm reindexing. – rrrrrrrrrrrrrrrr – 2013-12-21T23:32:56.067