I'm working on basically the exact same thing right now. Yes, Express Checkout is the way to go; there are 3 main ways to use Express Checkout: https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/set-up-a-payment/
You'll want to go with the Server-side REST setup. Take a look at their demo page here https://developer.paypal.com/demo/checkout/#/pattern/server
In a nutshell, the flow you'll want to look at is:
1.) User lands on the payment page
2.) User clicks one of the payment buttons
3.) #2 triggers your php page, CREATE_URL (see the demo)
4.) Your CREATE_URL php will need to use the paypal REST API to create a payment
. You'll likely want the intent to be sale
but there are three options (sale, authorized payment, or order)
5.) Once a payment is created, a box pops up which allows the user to make the payment
6.) If the user makes the payment ("authorizes" in paypal terms), you then need to execute the payment. Your EXECUTE_URL php page is triggered
7.) On your EXECUTE_URL php page, you execute the payment again using paypal's REST API. Assuming the payment is successful and the status = completed (meaning, the funds were actually transferred from the user's accuont to your paypal business account) then you can return the unique download id back to the client's browser
A few important notes:
- There's quite a bit of error handling involved, and paypal docs are horrid. Create payment can fail, user might not authorize the payment, execute payment may fail, etc.
- Just because the payment is executed does not mean the funds have been transferred to your account yet. The user may have used an eCheck or other funding instrument that may take a day or two to complete. You need to inspect the
status
value of the sale
to determine this
- You'll also want to set up a listener URL and a webhook, so that you'll be notified of payments which do not complete instantly
It's....taking awhile to get this done in a solid fashion. If PayPal's docs were better, I would've been done a day ago. Good luck.