How to attach PVC piping to a flat surface
I would suggest Plasticweld epoxy putty. It's designed to work with most plastics, including PVC. 

Because it's a putty, it's moldable and can be used to attach to a greater amount of surface area of the pipe to the gun.

You'll be most successful by sanding the portion of the pipe and gun where you plan to use the adhesive, giving the putty a better sticking area. I would also wipe clean with some rubbing alcohol to remove  dust particles (alcohol because it dries quickly). 

In the end, the putty will be flat against the gun surface, but U-shaped against the pipe. 

The putty can also be sanded, drilled, painted, etc. once dry.