He declared that he was an apostate in order to avoid having to offer expiation, then he repented and regretted it
Question

                I have an issue I want to ask about, which has been troubling me for some years. When I was fifteen years old, I masturbated during the day in Ramadaan. After that I tried to mend my ways and I researched the ruling on the sin that I had committed, and I thought that I had to offer the expiation for having intercourse. Because I could not do that, I said to myself, I will become a kaafir, Allaah forbid, then I will become Muslim again, thus Allaah will forgive me and I will not have to offer the expiation. So like a fool I said, “Now I am a kaafir and tomorrow I will become Muslim.” Now I am thirty years old and I still think of that incident. Every time I remember it I ask Allaah to forgive me and I bear witness that there is no god but Him and that Muhammad is His slave and Messenger. All the time I pray and fast, until now, praise be to Allaah. But should the hadd punishment, which is execution, be carried out on me so that Allaah will accept my repentance?.
Praise be to Allah.Firstly: 
The Shaytaan 
    keeps on making falsehood appear attractive and gradually makes a person 
    fall into the worst of evil deeds and the gravest of major sins, namely 
    shirk (associating others with Allaah), thinking that he is doing himself a 
    favour. How can a person try to avoid fasting for two consecutive months by 
    disbelieving in Allaah the Almighty, when Allaah has forbidden Paradise to 
    the one who meets Him as a kaafir? The one who does that is like one who 
    jumps out of the frying pan into the fire. He flees from one thing and falls 
    into something that is far worse. 
The trick 
    did not benefit him by relieving him of what was obligatory upon him, 
    because it is a haraam trick, indeed it is the worst of all haraam things. 
    The basic principle according to the scholars is that a trick does not waive 
    an obligation or make something haraam permissible. Can a person be sure 
    that if he commits this major sin, Allaah will give him time to repent and 
    come back? Is it not possible that the last moments of his life may be when 
    he has announced his kufr, Allaah forbid? Then he will be one of those whose 
    deeds are lost in this world and in the Hereafter, and such are the dwellers 
    of the Fire in which they will abide forever.  
Does he not 
    fear that Allaah will punish him for this evil action by preventing him from 
    repenting and coming back to Islam? 
“So when 
    they turned away (from the path of Allaah), Allaah turned their hearts away 
    (from the Right Path). And Allaah guides not the people who are Faasiqoon 
    (rebellious, disobedient to Allaah)”
[al-Saff 
    61:5]
To sum up: 
    The one who has done this has done something serious that makes the flesh of 
    the believers crawl. Praise be to Allaah Who has enabled you to repent. We 
    hope that Allaah will accept your repentance and forgive you your sin. 
Part of 
    perfecting your repentance is to do a great deal of righteous deeds, 
    remembering Allaah (dhikr), reading Qur’aan, asking for forgiveness, 
    acquiring knowledge and teaching it, giving charity, calling people to 
    Allaah, etc. There are many kinds of good deeds, so strive hard and may 
    Allaah forgive you. Allaah says (interpretation of the meaning): 
“And 
    verily, I am indeed forgiving to him who repents, believes (in My Oneness, 
    and associates none in worship with Me) and does righteous good deeds, and 
    then remains constant in doing them (till his death)”
[Ta-Ha 
    20:82]
Secondly: 
The 
    punishment for the one who apostatizes from Islam is death, because the 
    Prophet  (peace and blessings of Allaah be upon him) said: “Whoever 
    changes his religion, execute him.” Narrated by al-Bukhaari (2017). 
The majority 
    of scholars (including the Hanafis, Shaafa’is and Maalikis) are of the view 
    that this punishment is waived in the case of one who repents and comes back 
    to Islam, which is what applies in your case. 
See 
    al-Mughni (9/18) and Sharh Muslim by al-Nawawi (12/208). 
Thirdly: 
With regard 
    to the ruling on masturbating during the day in Ramadaan, it invalidates the 
    fast and what you have to do is make up that day only, not offer the 
    expiation for having intercourse. In the answer to question no.
    50632 we 
    have explained that the expiation for breaking the fast is only required in 
    the case of intercourse.