Picking up from my last post, we will now look at the next PaypalExpressController action: review. We first need to modify our routes.rb file:
Second addition to routes.rb
We are now ready to add the `review` action to the PaypalExpressController.
Based on how the checkout action was setup from the previous post, the review action will be called after the customer returns from Paypal. Now I will explain what this action does:
We check that params contains the :token key since that’s what’s passed back from Paypal. If the request does not have such key, we redirect to the home page.
Next, we use the :token to retrieve order information (such as shipping address, order total, etc.) from Paypal using ActiveMerchant’sdetails_for method.
Next, we check whether the Paypal transaction went through successfully or not. If it failed, we again redirect to the home page.
Lastly, if the transaction was processed successfully, we now extract the order information from the gateway response.
You will notice the review action makes use of get_order_info to extract the order information. The method is defined in the PaypalExpressHelper module here:
The order information includes important data such as shipping address, email, and the customer’s name. Please also note that subtotal, shipping, and total are all in cents and not dollars.
With @order_info populated, here’s a sample review.html.haml to render it:
A few points to notice about the view:
Pay close attention to the data :shipping_address contains.
Also pay close attention to the “Complete Purchase” link. This is the link that the customer needs to click to complete the purchase. The link must contain :token and :payer_id; both of which were sent by Paypal in the review action.
The method we have not defined is paypal_express_purchase_path which is the last controller action we need to implement. That will be covered in Part 3. Stay tuned!
Thanks for your blog, its the simpliest I’ve found on paypal integration.
I’ve used parts 1+2 on a project for university I’m doing but am stuck with you not having part 3 up. When will it be available or could you help me by sending me the next steps.
Thanks for the comment! You are in luck because I plan to publish the last part by this weekend. Will that be alright with you? =)
[…] is the last part of a three-part series. As mentioned in the previous post, the next step is to define `PaypalExpress#purchase` action so that we would get the […]
Did you ever publish part 3?
Oops, just found part 3. It doesn’t have the tags part 1 and 2 have (“paypal”, “paypal-express” etc.) so it doesn’t appear on the tag index page.
Comments are closed.