Photochemistry and molecular ions in carbon-rich circumstellar envelopes