You are presuming that the church will be open when you arrive, and that they will have credencials available. In my experience, never presume - but verify in advance. And, always have a Plan B.
The recommendations in several posts above, to pre-purchase from Ivar (@CasaIvar) or from your national Camino friends association, are very good ideas. In the US, this is from
www.americanpilgrims.org. In the UK it is from the Confraternity of St. James -
https://www.csj.org.uk/.
You can find your national group - or ask here. Someone will give you the direct link.
In fact, it is likely that the credencial sold at
Real Iglesia Santiago y San Juan Bautista in Madrid is the identical version sold by Ivar. Some national groups have their own style of credential which has been approved for use by the Pilgrim Office.
That said, most organizations and churches obtain their supplies in case quantities, from the Pilgrim Office at Santiago, just like Ivar does.
IMHO, it is preferable to already have your credencial - front inside cover completed as appropriate - to receive that first sello (rubber stamp). it makes the anticipation greater, at least in my view.
Hope this helps.
Tom