Created
April 19, 2018 22:50
-
-
Save kirqe/417ba426d1313a4a2d7f31e70109634c to your computer and use it in GitHub Desktop.
pp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/execute', to: 'payment#execute' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment