Guest User

Untitled

a guest
Sep 25th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. has_many :purchases
  2.  
  3. class Purchase < ActiveRecord::Base
  4. belongs_to :tool
  5. belongs_to :user
  6.  
  7. def paypal_url(return_path)
  8. values = {
  9. business: "merchant@example.com",
  10. cmd: "_xclick",
  11. upload: 1,
  12. invoice: id,
  13. amount: tool.price,
  14. item_name: tool.title,
  15. notify_url: "http://5947992e.ngrok.io/hook" #Test Server
  16. }
  17. "#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
  18. end
  19. end
  20.  
  21. class PurchasesController < ApplicationController
  22. def new
  23. @purchase = Purchase.new(:tool_id => params[:tool_id], :user_id => current_user.id)
  24. if @purchase.save
  25. redirect_to @purchase.paypal_url(purchase_path(@purchase))
  26. else
  27. render :new
  28. end
  29. end
  30.  
  31. protect_from_forgery except: [:hook]
  32. def hook
  33. params.permit! # Permit all Paypal input params
  34. status = params[:payment_status]
  35. if status == "Completed"
  36. @purchase = Purchase.find(params[:invoice])
  37. @purchase.update_attributes(status: status, transaction_id: params[:txn_id], purchased_at: Time.now)
  38. @purchase.save!
  39. @user = @tool.user
  40. @user.earned_money += @tool.price
  41. @user.save!
  42. end
  43. render nothing: true
  44. end
  45. end
  46.  
  47. post "/purchases/:id" => "purchases#show"
  48. post "/hook" => "purchases#hook"
  49.  
  50. = link_to image_tag('paypal.png'), new_purchase_path(:tool_id => @tool.id), target: "_blank"
Add Comment
Please, Sign In to add comment