How to Implement PayPal Express Checkout for Digital Good in CakePHP - Part 228 Feb 2012 Add Comments Intermediate Level
PayPal Digital Goods for Express Checkout Provides a very easy and user friendly way for users to make payment. All transactions happen in an overlayer modal box, users will not need to leave your site during the whole process unlike other methods, where users will be redirected to PayPal official site to make payment.
If you have not yet read part one yet, go ahead and read it first.
Table Of Content
- Make sure you have required knowledge as discussed in part one.
- Set up a working copy of CakePHP 1.3
Create four view files as follow
views/purchases/payment.ctp: This is a dummy payment page and it is where the PayPal overlayer modal box will show.
- views/purchases/paypal_set_ec.ctp: This is the page to make API call SetExpressCheckout.
This page is very simple. What it does is to reload the parent page of the iframe. Actually this will only happen if the API call does not go through because in the controller action function, we will redirect user to PayPal page if the API call succeeds. If it fails, we will reload the parent page with an error message.
- views/purchases/paypal_back.ctp: This is view page after a successful payment or user will be directed to after clicking on cancel payment button.
As discussed above, this is the page when user returns from PayPal after successful payment, or when he clicks on cancel button. Logic behind this page is to close the modal box and reload the parent page of the modal box after user returns.
This view page will be shared between two action functions, since they will render the same view content.
We have already built our view files above. Now let us create a controller under directory app/controllers/purchases_controller.php copy content below to the file:
As you can see, there are two main functions in this controller, which are paypal_set_ec() and paypal_return(). Let us explain a bit more about these two functions.
- paypal_set_ec(): When user clicks on "Pay now" button on payment page, dg.js will trigger to call this function. And what this function does is to do an API call (setExpressCheckout) with an nvp string. If API server acknowledges the nvp string, it will return a token. Then this function will redirect to a PayPal page with that token, which will render user a PayPal official page.
- paypal_return(): This function is triggered when PayPal redirects user back to our site after successful payment. This function will get two pieces of important data from the URL, which are PayerID and Token. And then it calls PayPal API(doExpressCheckoutPayment) with an nvp string.
Some important notes to take:
- We have used hard coded values for building the nvp string. You should use your own application logic to get those values (normally get it from models), or it makes more sense to build the nvp string using model (fat model).
- All the variables in the nvp string are required to be filled. If you forget or supply wrong values, you may get error message as "We are unable to complete your request at this time.Please try again later. We apologize for the inconvenience."
- If the API call fails, you can check "tmp/logs/error.log" file to investigate.
- You will have to enable express checkout from PayPal.
This is the end of the two parts tutorial. You can always review part one here.
Hopefully this simple tutorial helped you with your development.
If you like our post, please follow us on Twitter and help spread the word. We need your support to continue.
If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.