Working checkout_controller.rb that charges Stripe

* Add billing address setting
This commit is contained in:
Chris Beach 2017-10-19 23:50:21 +01:00
parent 20a38095dd
commit d563cd1ddd
7 changed files with 117 additions and 36 deletions

View File

@ -9,33 +9,10 @@ module DiscourseDonations
skip_before_action :verify_authenticity_token, only: [:create] skip_before_action :verify_authenticity_token, only: [:create]
def checkout
output = { 'messages' => [], 'rewards' => [] }
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
begin
payment.checkoutCharge(user_params[:amount], user_params[:stripe_options])
rescue ::Stripe::CardError => e
err = e.json_body[:error]
output['messages'] << "There was an error (#{err[:type]})."
output['messages'] << "Error code: #{err[:code]}" if err[:code]
output['messages'] << "Decline code: #{err[:decline_code]}" if err[:decline_code]
output['messages'] << "Message: #{err[:message]}" if err[:message]
render(:json => output) and return
end
if charge['paid'] == true
output['messages'] << I18n.t('donations.payment.success')
output['rewards'] << { type: :group, name: group_name } if group_name
output['rewards'] << { type: :badge, name: badge_name } if badge_name
end
render :json => output
end
def create def create
Rails.logger.debug params.inspect
Rails.logger.debug user_params.inspect
output = { 'messages' => [], 'rewards' => [] } output = { 'messages' => [], 'rewards' => [] }
if create_account if create_account

View File

@ -0,0 +1,92 @@
require_dependency 'discourse'
module DiscourseDonations
class CheckoutController < ActionController::Base
include CurrentUser
protect_from_forgery prepend: true
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token, only: [:create]
def create
Rails.logger.debug params.inspect
Rails.logger.debug user_params.inspect
output = { 'messages' => [], 'rewards' => [] }
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
begin
charge = payment.checkoutCharge(user_params[:stripeEmail],
user_params[:stripeToken],
user_params[:amount])
rescue ::Stripe::CardError => e
err = e.json_body[:error]
output['messages'] << "There was an error (#{err[:type]})."
output['messages'] << "Error code: #{err[:code]}" if err[:code]
output['messages'] << "Decline code: #{err[:decline_code]}" if err[:decline_code]
output['messages'] << "Message: #{err[:message]}" if err[:message]
render(:json => output) and return
end
if charge['paid']
output['messages'] << I18n.t('donations.payment.success')
output['rewards'] << { type: :group, name: group_name } if group_name
output['rewards'] << { type: :badge, name: badge_name } if badge_name
end
render :json => output
end
private
def reward?(payment)
payment.present? && payment.successful?
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 user_params
params.permit(:amount,
:stripeToken,
:stripeTokenType,
:stripeEmail,
:stripeBillingName,
:stripeBillingAddressLine1,
:stripeBillingAddressZip,
:stripeBillingAddressState,
:stripeBillingAddressCity,
:stripeBillingAddressCountry,
:stripeBillingAddressCountryCode,
:stripeShippingName,
:stripeShippingAddressLine1,
:stripeShippingAddressZip,
:stripeShippingAddressState,
:stripeShippingAddressCity,
:stripeShippingAddressCountry,
:stripeShippingAddressCountryCode
)
end
def stripe_options
{
description: SiteSetting.discourse_donations_description,
currency: SiteSetting.discourse_donations_currency
}
end
end
end

View File

@ -1,4 +1,3 @@
module DiscourseDonations module DiscourseDonations
class Stripe class Stripe
attr_reader :charge, :currency, :description attr_reader :charge, :currency, :description
@ -9,14 +8,19 @@ module DiscourseDonations
@currency = opts[:currency] @currency = opts[:currency]
end end
def checkoutCharge(amount, token) def checkoutCharge(email, token, amount)
@charge = Stripe::Charge.create( customer = ::Stripe::Customer.create(
:amount => amount, :email => email,
:currency => opts[:currency], :source => token
:description => @description,
:source => token,
) )
@charge
charge = ::Stripe::Charge.create(
:customer => customer.id,
:amount => amount,
:description => @description,
:currency => @currency
)
charge
end end
def charge(email, opts) def charge(email, opts)

View File

@ -3,7 +3,8 @@ function validationErrors(tagInfo, content, siteSettings) {
if (!siteSettings.discourse_donations_public_key) { errors.push("missing key (site setting)"); } if (!siteSettings.discourse_donations_public_key) { errors.push("missing key (site setting)"); }
if (!siteSettings.discourse_donations_currency) { errors.push("missing currency (site setting)"); } if (!siteSettings.discourse_donations_currency) { errors.push("missing currency (site setting)"); }
if (!siteSettings.discourse_donations_shop_name) { errors.push("missing name (site setting)"); } if (!siteSettings.discourse_donations_shop_name) { errors.push("missing name (site setting)"); }
if (!siteSettings.discourse_donations_hide_zip_code) { errors.push("missing hide zip code (site setting)"); } if (!siteSettings.discourse_donations_hide_zip_code) { errors.push("missing zip code toggle (site setting)"); }
if (!siteSettings.discourse_donations_billing_address) { errors.push("missing billing address toggle (site setting)"); }
if (!tagInfo.attrs['amount']) { errors.push("missing amount"); } if (!tagInfo.attrs['amount']) { errors.push("missing amount"); }
if (!content) { errors.push("missing description"); } if (!content) { errors.push("missing description"); }
return errors; return errors;
@ -47,6 +48,7 @@ function insertCheckout(state, tagInfo, content, siteSettings) {
['data-image', tagInfo.attrs['image'] || ''], ['data-image', tagInfo.attrs['image'] || ''],
['data-locale', 'auto'], ['data-locale', 'auto'],
['data-zip-code', !siteSettings.discourse_donations_hide_zip_code], ['data-zip-code', !siteSettings.discourse_donations_hide_zip_code],
['data-billing-address', siteSettings.discourse_donations_billing_address],
['data-currency', siteSettings.discourse_donations_currency] ['data-currency', siteSettings.discourse_donations_currency]
]; ];
@ -81,6 +83,7 @@ export function setup(helper) {
'script[data-description]', 'script[data-description]',
'script[data-image]', 'script[data-image]',
'script[data-zip-code]', 'script[data-zip-code]',
'script[data-billing-address]',
'script[data-currency]', 'script[data-currency]',
'script[data-locale]' 'script[data-locale]'
]); ]);

View File

@ -7,6 +7,7 @@ en:
discourse_donations_shop_name: "Stripe Shop Name" discourse_donations_shop_name: "Stripe Shop Name"
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_billing_address: "Collect billing address"
discourse_donations_reward_badge_name: "Grant this badge to user when a payment is successful" 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" discourse_donations_reward_group_name: "Add the user to this group when a payment is successful"
en: en:

View File

@ -1,5 +1,6 @@
DiscourseDonations::Engine.routes.draw do DiscourseDonations::Engine.routes.draw do
resources :charges, only: [:create, :checkout] resources :charges, only: [:create]
resources :checkout, only: [:create]
get 'users/:username/payments' => 'payments#show' get 'users/:username/payments' => 'payments#show'
get 'donate' => 'payments#show' get 'donate' => 'payments#show'
end end

View File

@ -23,6 +23,9 @@ plugins:
discourse_donations_hide_zip_code: discourse_donations_hide_zip_code:
default: true default: true
client: true client: true
discourse_donations_billing_address:
default: true
client: true
discourse_donations_reward_badge_name: discourse_donations_reward_badge_name:
client: false client: false
default: 'Donation' default: 'Donation'