merge in master

This commit is contained in:
Rimian Perkins 2017-04-27 20:35:33 +10:00
commit 2aa663d922
10 changed files with 243 additions and 26 deletions

View File

@ -12,22 +12,20 @@ module DiscourseDonations
elsif create_account && params[:username].nil? elsif create_account && params[:username].nil?
response = {'message' => 'Please enter a username'} response = {'message' => 'Please enter a username'}
else else
Stripe.api_key = SiteSetting.discourse_donations_secret_key payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
currency = SiteSetting.discourse_donations_currency response = payment.charge(email, params)
end
customer = Stripe::Customer.create( response['rewards'] = []
:email => email,
:source => params[:stripeToken]
)
response = Stripe::Charge.create( if reward_user?(payment)
:customer => customer.id, reward = DiscourseDonations::Rewards.new(current_user)
:amount => params[:amount], if reward.add_to_group(group_name)
:description => SiteSetting.discourse_donations_description, response['rewards'] << { type: :group, name: group_name }
:currency => currency end
) if reward.grant_badge(badge_name)
response['rewards'] << { type: :badge, name: badge_name }
response['message'] = response['outcome']['seller_message'] end
end end
render :json => response render :json => response
@ -39,6 +37,29 @@ module DiscourseDonations
params[:create_account] == 'true' params[:create_account] == 'true'
end end
def reward_user?(payment)
payment.present? && payment.successful? && current_user.present?
end
def group_name
SiteSetting.discourse_donations_reward_group_name
end
def badge_name
SiteSetting.discourse_donations_reward_badge_name
end
def secret_key
SiteSetting.discourse_donations_secret_key
end
def stripe_options
{
description: SiteSetting.discourse_donations_description,
currency: SiteSetting.discourse_donations_currency
}
end
def email def email
params[:email] || current_user.try(:email) params[:email] || current_user.try(:email)
end end

View File

@ -0,0 +1,29 @@
module DiscourseDonations
class Rewards
attr_reader :user
def initialize(user)
@user = user
end
def add_to_group(name)
grp = ::Group.find_by_name(name)
return if grp.nil?
log_group_add(grp)
grp.add(user)
end
def grant_badge(name)
badge = ::Badge.find_by_name(name)
return if badge.nil?
BadgeGranter.grant(badge, user)
end
def log_group_add(grp)
system_user = User.find(-1)
GroupActionLogger.new(system_user, grp).log_add_user_to_group(user)
end
end
end

View File

@ -0,0 +1,29 @@
module DiscourseDonations
class Stripe
def initialize(secret_key, opts)
::Stripe.api_key = secret_key
@description = opts[:description]
@currency = opts[:currency]
end
def charge(email, opts)
customer = ::Stripe::Customer.create(
email: email,
source: opts[:stripeToken]
)
@charge = ::Stripe::Charge.create(
customer: customer.id,
amount: opts[:amount],
description: @description,
currency: @currency
)
@charge[:message] = @charge[:outcome][:seller_message] if @charge[:outcome]
@charge
end
def successful?
@charge[:paid]
end
end
end

View File

@ -6,3 +6,5 @@ en:
discourse_donations_public_key: Stripe Public Key discourse_donations_public_key: Stripe Public Key
discourse_donations_currency: Currency Code discourse_donations_currency: Currency Code
discourse_donations_hide_zip_code: Hide Zip Code discourse_donations_hide_zip_code: Hide Zip Code
discourse_donations_reward_badge_name: Grant this badge to user when a payment is successful
discourse_donations_reward_group_name: Add the user to this group when a payment is successful

View File

@ -18,3 +18,9 @@ plugins:
discourse_donations_hide_zip_code: discourse_donations_hide_zip_code:
default: true default: true
client: true client: true
discourse_donations_reward_badge_name:
client: false
default: 'Donation'
discourse_donations_reward_group_name:
client: false
default: 'Donation'

View File

@ -1,10 +1,10 @@
# name: discourse-donations # name: discourse-donations
# about: Integrating Discourse with Stripe for donations # about: Integrating Discourse with Stripe for donations
# version: 1.7.3 # version: 1.9.0
# url: https://github.com/choiceaustralia/discourse-donations # url: https://github.com/choiceaustralia/discourse-donations
# authors: Rimian Perkins # authors: Rimian Perkins
gem 'stripe', '2.1.0' gem 'stripe', '2.4.0'
load File.expand_path('../lib/discourse_donations/engine.rb', __FILE__) load File.expand_path('../lib/discourse_donations/engine.rb', __FILE__)
@ -13,16 +13,9 @@ enabled_site_setting :discourse_donations_enabled
after_initialize do after_initialize do
# Must be placed on every page for fraud protection. # Must be placed on every page for fraud protection.
header_script = '<script src="https://js.stripe.com/v3/"></script>' header_script = '<script src="https://js.stripe.com/v3/"></script>'
discourse_donations_theme = Theme.find_or_create_by(name: 'Discourse Donations Header', hidden: false, user_id: -1)
discourse_donations_customization = SiteCustomization.find_or_create_by({ discourse_donations_theme.set_field('common', 'head_tag', header_script)
name: 'Discourse Donations Header', discourse_donations_theme.save
header: header_script,
mobile_header: header_script,
enabled: true,
user_id: -1
})
SiteCustomization.where(name: discourse_donations_customization.name).where.not(id: discourse_donations_customization.id).delete_all
end end
Discourse::Application.routes.prepend do Discourse::Application.routes.prepend do

View File

@ -8,6 +8,8 @@ module DiscourseDonations
before do before do
SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo')
SiteSetting.stubs(:discourse_donations_description).returns('charity begins at discourse plugin')
SiteSetting.stubs(:discourse_donations_currency).returns('AUD')
end end
it 'responds ok for anonymous users' do it 'responds ok for anonymous users' do
@ -40,5 +42,36 @@ module DiscourseDonations
expect(body['message']).to eq('Payment complete.') expect(body['message']).to eq('Payment complete.')
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
describe 'rewards' do
let(:group_name) { 'Zasch' }
let(:badge_name) { 'Beanie' }
let(:response_rewards) { JSON.parse(response.body)['rewards'] }
let(:stripe) { ::Stripe::Charge }
before do
SiteSetting.stubs(:discourse_donations_reward_group_name).returns(group_name)
SiteSetting.stubs(:discourse_donations_reward_badge_name).returns(badge_name)
Fabricate(:group, name: SiteSetting.discourse_donations_reward_group_name)
Fabricate(:badge, name: SiteSetting.discourse_donations_reward_badge_name)
log_in :coding_horror
end
it 'has no rewards' do
stripe.expects(:create).returns({ outcome: { seller_message: 'bummer' } })
post :create
expect(response_rewards).to be_empty
end
it 'awards a group' do
post :create
expect(response_rewards).to include({'type' => 'group', 'name' => group_name})
end
it 'awards a badge' do
post :create
expect(response_rewards).to include({'type' => 'badge', 'name' => badge_name})
end
end
end end
end end

View File

@ -0,0 +1,39 @@
require 'rails_helper'
module DiscourseDonations
RSpec.describe DiscourseDonations::Rewards do
let(:grp) { Fabricate(:group) }
let(:user) { Fabricate(:user) }
subject { described_class.new(user) }
it 'adds the user to a group' do
Group.expects(:find_by_name).with(grp.name).returns(grp)
grp.expects(:add).with(user)
subject.expects(:log_group_add).once
subject.add_to_group(grp.name)
end
it 'does not add the user to a group' do
Group.expects(:find_by_name).with(grp.name).returns(nil)
grp.expects(:add).never
subject.expects(:log_group_add).never
expect(subject.add_to_group(grp.name)).to be_falsy
end
it 'logs the group add' do
GroupActionLogger.any_instance.expects(:log_add_user_to_group)
subject.add_to_group(grp.name)
end
it 'grants the user a badge' do
badge = Fabricate(:badge)
BadgeGranter.expects(:grant).with(badge, user)
subject.grant_badge(badge.name)
end
it 'does not grant the user a badge' do
BadgeGranter.expects(:grant).never
expect(subject.grant_badge('does not exist')).to be_falsy
end
end
end

View File

@ -0,0 +1,64 @@
require 'rails_helper'
require_relative '../../support/dd_helper'
module DiscourseDonations
RSpec.describe DiscourseDonations::Stripe do
before { SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') }
let(:stripe_options) { { description: 'hi there', currency: 'AUD' } }
let(:params) { { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' } }
let(:email) { 'ray-zintoast@example.com' }
let(:customer) { stub(id: 1) }
let!(:subject) { described_class.new('secret-key-yo', stripe_options) }
it 'sets the api key' do
expect(::Stripe.api_key).to eq 'secret-key-yo'
end
it 'creates a customer and charges them an amount' do
::Stripe::Customer.expects(:create).with(
email: email,
source: 'stripe-token'
).returns(customer)
::Stripe::Charge.expects(:create).with(
customer: customer.id,
amount: params[:amount],
description: stripe_options[:description],
currency: stripe_options[:currency]
).returns(
{
paid: true,
outcome: { seller_message: 'yay!' }
}
)
subject.charge(email, params)
end
it 'has a message' do
::Stripe::Customer.expects(:create).returns(customer)
::Stripe::Charge.expects(:create).returns({ outcome: { seller_message: 'yay!' } })
response = subject.charge(email, params)
expect(response[:message]).to eq 'yay!'
end
describe '.successful?' do
let(:charge_options) { { customer: customer.id, amount: params[:amount], description: stripe_options[:description], currency: stripe_options[:currency] } }
before do
::Stripe::Customer.expects(:create).with(email: email, source: 'stripe-token').returns(customer)
end
it 'is successful' do
::Stripe::Charge.expects(:create).with(charge_options).returns({paid: true})
subject.charge(email, params)
expect(subject).to be_successful
end
it 'is not successful' do
::Stripe::Charge.expects(:create).with(charge_options).returns({paid: false})
subject.charge(email, params)
expect(subject).not_to be_successful
end
end
end
end

View File

@ -1,3 +1,4 @@
require 'fakeweb'
#TODO register some fixtures #TODO register some fixtures