  Cosmological analyses using galaxy clusters in optical/NIR photometric
surveys require robust characterization of their galaxy content. Precisely
determining which galaxies belong to a cluster is crucial. In this paper, we
present the COlor Probabilistic Assignment of Clusters And BAyesiaN Analysis
(Copacabana) algorithm. Copacabana computes membership probabilities for {\it
all} galaxies within an aperture centred on the cluster using photometric
redshifts, colours, and projected radial probability density functions.
  We use simulations to validate Copacabana and we show that it achieves up to
89\% membership accuracy with a mild dependency on photometric redshift
uncertainties and choice of aperture size. We find that the precision of the
photometric redshifts has the largest impact on the determination of the
membership probabilities followed by the choice of the cluster aperture size.
We also quantify how much these uncertainties in the membership probabilities
affect the stellar mass--cluster mass scaling relation, a relation that
directly impacts cosmology. Using the sum of the stellar masses weighted by
membership probabilities ($\mu_{\star}$) as the observable, we find that
Copacabana can reach an accuracy of 0.06 dex in the measurement of the scaling
relation. These results indicate the potential of Copacabana and $\mu_{\star}$
to be used in cosmological analyses of optically selected clusters in the
future.
