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!