Are you using PayPal's IPN or PDT? Have you already looked at their code examples?