Skip to content

Instantly share code, notes, and snippets.

@kirqe
Created April 19, 2018 22:50
Show Gist options
  • Save kirqe/417ba426d1313a4a2d7f31e70109634c to your computer and use it in GitHub Desktop.
Save kirqe/417ba426d1313a4a2d7f31e70109634c to your computer and use it in GitHub Desktop.
pp
# subscription/payment controller
def execute
payment = PayPal::SDK::REST::Payment.find(params[:paymentId])
if payment.execute(payer_id: params[:PayerID])
current_user.subscription.prolong_for(cookies.signed[:duration])
clear_stash
redirect_to root_path, notice: "Subscribed successfully"
else
redirect_to root_path, error: "Something went wrong"
end
end
private
def clear_stash
cookies.delete(:new_plan_id)
cookies.delete(:duration)
cookies.delete(:amount)
end
end
PayPal::SDK::REST.set_config(
:mode => ENV['pp_mode'], # "sandbox" or "live"
:client_id => ENV['pp_client_id'],
:client_secret => ENV['pp_client_secret'])
class PaymentService
def initialize(host, amount)
@payment = PayPal::SDK::REST::Payment.new(data(host, amount))
end
def accept
@payment.create
end
def response
@payment
end
def data(host, amount)
{
intent: 'sale',
payer: {
payment_method: 'paypal' },
redirect_urls: {
return_url: host + "/execute",
cancel_url: host + "/" },
transactions: [ {
amount: {
total: amount,
currency: 'USD' },
description: 'subscription to plan' } ]
}
end
end
get '/execute', to: 'payment#execute'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment