Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- has_many :purchases
- class Purchase < ActiveRecord::Base
- belongs_to :tool
- belongs_to :user
- def paypal_url(return_path)
- values = {
- business: "merchant@example.com",
- cmd: "_xclick",
- upload: 1,
- invoice: id,
- amount: tool.price,
- item_name: tool.title,
- notify_url: "http://5947992e.ngrok.io/hook" #Test Server
- }
- "#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
- end
- end
- class PurchasesController < ApplicationController
- def new
- @purchase = Purchase.new(:tool_id => params[:tool_id], :user_id => current_user.id)
- if @purchase.save
- redirect_to @purchase.paypal_url(purchase_path(@purchase))
- else
- render :new
- end
- end
- protect_from_forgery except: [:hook]
- def hook
- params.permit! # Permit all Paypal input params
- status = params[:payment_status]
- if status == "Completed"
- @purchase = Purchase.find(params[:invoice])
- @purchase.update_attributes(status: status, transaction_id: params[:txn_id], purchased_at: Time.now)
- @purchase.save!
- @user = @tool.user
- @user.earned_money += @tool.price
- @user.save!
- end
- render nothing: true
- end
- end
- post "/purchases/:id" => "purchases#show"
- post "/hook" => "purchases#hook"
- = link_to image_tag('paypal.png'), new_purchase_path(:tool_id => @tool.id), target: "_blank"
Add Comment
Please, Sign In to add comment