-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PayPal Pro Payments #528
Draft
NuckChorris
wants to merge
11
commits into
the-future
Choose a base branch
from
feature/pro-paypal
base: the-future
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
PayPal Pro Payments #528
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
17d999d
Add paypal-sdk-rest
NuckChorris b79ee4c
Add a crappy rake task to generate the PayPal Plans
NuckChorris 6d9839d
Add state and error columns to ProSubscription
NuckChorris 32b0b00
Convince PayPal Checkout to accept our Subscriptions
NuckChorris 90ad749
Activate PayPal Plans after creation
NuckChorris 43a782a
Add ProSubscription::PayPalSubscription
NuckChorris 167b834
Allow hitting PayPal Sandbox from tests
NuckChorris c4af2d8
Add PayPal Sandbox credentials
NuckChorris fadef5c
Add CreatePayPalAgreement action
NuckChorris 8ee37df
Add SubscribeWithPayPal action
NuckChorris 3f5b5bd
Add enum for ProSubscription#state
NuckChorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Create a PayPal Billing Agreement to be authorized by a user and return the token which can be | ||
# used to authorize it on the client side. | ||
module Pro | ||
class CreatePaypalAgreement < Action | ||
include PayPal::SDK::REST | ||
|
||
parameter :user, required: true, load: User | ||
parameter :tier, required: true | ||
|
||
validates :tier, inclusion: { in: %w[pro patron] } | ||
|
||
def call | ||
agreement = Agreement.new( | ||
start_date: 30.seconds.from_now.iso8601, | ||
payer: { payment_method: 'paypal' }, | ||
name: "Kitsu #{tier.upcase}", | ||
description: "Yearly Kitsu #{tier.upcase} Subscription", | ||
plan: paypal_plan | ||
) | ||
|
||
agreement.create! | ||
|
||
{ token: agreement.token } | ||
end | ||
|
||
private | ||
|
||
def paypal_plan_id | ||
ENV["PAYPAL_#{tier.upcase}_PLAN"] | ||
end | ||
|
||
def paypal_plan | ||
Plan.new(id: paypal_plan_id) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Execute a PayPal Billing Agreement based on a provided token, and set up the User's | ||
# ProSubscription to match it. | ||
module Pro | ||
class SubscribeWithPayPal < Action | ||
include PayPal::SDK::REST | ||
|
||
parameter :user, required: true, load: User | ||
parameter :tier, required: true | ||
parameter :token, required: true | ||
|
||
validates :tier, inclusion: { in: %w[pro patron] } | ||
|
||
def call | ||
user.pro_subscription&.destroy! | ||
|
||
agreement = Agreement.new(token: token) | ||
agreement.execute! | ||
|
||
subscription = ProSubscription::PayPalSubscription.create!( | ||
user: user, | ||
tier: tier, | ||
billing_id: agreement.id | ||
) | ||
|
||
{ subscription: subscription } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class ProSubscription | ||
class PayPalSubscription < ProSubscription | ||
PayPal = PayPal::SDK::REST | ||
|
||
def billing_service | ||
:paypal | ||
end | ||
|
||
def agreement | ||
@agreement ||= PayPal::Agreement.find(billing_id) | ||
end | ||
|
||
alias_method :cancel!, :destroy! | ||
|
||
after_destroy do | ||
agreement.cancel!(note: 'Cancelled on website') | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
inflect.acronym 'AMA' | ||
inflect.acronym 'ANN' | ||
inflect.acronym 'MAL' | ||
inflect.acronym 'PayPal' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'paypal-sdk-rest' | ||
|
||
PayPal::SDK.configure( | ||
mode: ENV['PAYPAL_ENVIRONMENT'], | ||
client_id: ENV['PAYPAL_CLIENT_ID'], | ||
client_secret: ENV['PAYPAL_CLIENT_SECRET'] | ||
) | ||
PayPal::SDK.logger = Rails.logger |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddStateToProSubscriptions < ActiveRecord::Migration | ||
def change | ||
add_column :pro_subscriptions, :state, :integer, default: 0, null: false | ||
add_column :pro_subscriptions, :error, :string | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace :paypal_pro do | ||
desc 'Resave all paperclip attachments' | ||
task setup: :environment do | ||
[ | ||
{ | ||
name: 'PRO', | ||
cost: '19.00' | ||
}, | ||
{ | ||
name: 'PATRON', | ||
cost: '49.00' | ||
} | ||
].each do |tier| | ||
PayPal::SDK.logger = Logger.new(nil) | ||
plan = PayPal::SDK::REST::Plan.new( | ||
name: "Kitsu #{tier[:name]}", | ||
description: "Yearly subscription to Kitsu #{tier[:name]}", | ||
type: 'INFINITE', | ||
payment_definitions: [{ | ||
name: "Kitsu #{tier[:name]} Yearly", | ||
type: 'REGULAR', | ||
frequency_interval: '1', | ||
frequency: 'YEAR', | ||
amount: { | ||
currency: 'USD', | ||
value: tier[:cost] | ||
} | ||
}], | ||
merchant_preferences: { | ||
cancel_url: 'https://www.paypal.com/checkoutnow/error', | ||
return_url: 'https://www.paypal.com/checkoutnow/error', | ||
max_fail_attempts: '0', | ||
auto_bill_amount: 'YES', | ||
initial_fail_amount_action: 'CANCEL' | ||
} | ||
) | ||
plan.create! | ||
|
||
# They won't let you activate in the same request as creation | ||
patch = PayPal::SDK::REST::Patch.new(op: 'replace', path: '/', value: { state: 'ACTIVE' }) | ||
plan.update!(patch) | ||
|
||
puts "PAYPAL_#{tier[:name]}_PLAN=#{plan.id}" | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Pro::CreatePaypalAgreement do | ||
it 'should create an agreement and return the token' do | ||
response = Pro::CreatePaypalAgreement.call(user: build(:user), tier: 'pro') | ||
expect(response.token).to start_with('EC-') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Pro::SubscribeWithPayPal do | ||
it 'should execute an agreement based on a token' do | ||
agreement = instance_double(PayPal::SDK::REST::Agreement) | ||
expect(agreement).to receive(:execute!) | ||
allow(agreement).to receive(:id).and_return('IH8PAYPAL') | ||
allow(PayPal::SDK::REST::Agreement).to receive(:new).and_return(agreement) | ||
|
||
result = Pro::SubscribeWithPayPal.call(user: build(:user), tier: 'pro', token: 'POOP') | ||
expect(result.subscription.billing_id).to eq('IH8PAYPAL') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ProSubscription::PayPalSubscription, type: :model do | ||
describe '#billing_service' do | ||
it 'should return :paypal' do | ||
expect(ProSubscription::PayPalSubscription.new.billing_service).to eq(:paypal) | ||
end | ||
end | ||
|
||
context 'after destruction' do | ||
it 'should cancel the agreement on PayPal' do | ||
user = create(:user) | ||
sub = ProSubscription::PayPalSubscription.create!(user: user, tier: 'pro', billing_id: 'POOP') | ||
agreement = instance_double(PayPal::SDK::REST::Agreement) | ||
allow(sub).to receive(:agreement).and_return(agreement) | ||
expect(sub.agreement).to receive(:cancel!).once | ||
sub.destroy! | ||
end | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this source file (paypal-sdk-rest.rb) should use snake_case. (https://github.com/bbatsov/ruby-style-guide#snake-case-files)