Working checkout_controller.rb that charges Stripe
* Add billing address setting
This commit is contained in:
parent
20a38095dd
commit
d563cd1ddd
|
@ -9,33 +9,10 @@ module DiscourseDonations
|
|||
|
||||
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
|
||||
Rails.logger.debug params.inspect
|
||||
Rails.logger.debug user_params.inspect
|
||||
|
||||
output = { 'messages' => [], 'rewards' => [] }
|
||||
|
||||
if create_account
|
||||
|
|
|
@ -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
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
module DiscourseDonations
|
||||
class Stripe
|
||||
attr_reader :charge, :currency, :description
|
||||
|
@ -9,14 +8,19 @@ module DiscourseDonations
|
|||
@currency = opts[:currency]
|
||||
end
|
||||
|
||||
def checkoutCharge(amount, token)
|
||||
@charge = Stripe::Charge.create(
|
||||
:amount => amount,
|
||||
:currency => opts[:currency],
|
||||
:description => @description,
|
||||
:source => token,
|
||||
def checkoutCharge(email, token, amount)
|
||||
customer = ::Stripe::Customer.create(
|
||||
:email => email,
|
||||
:source => token
|
||||
)
|
||||
@charge
|
||||
|
||||
charge = ::Stripe::Charge.create(
|
||||
:customer => customer.id,
|
||||
:amount => amount,
|
||||
:description => @description,
|
||||
:currency => @currency
|
||||
)
|
||||
charge
|
||||
end
|
||||
|
||||
def charge(email, opts)
|
||||
|
|
|
@ -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_currency) { errors.push("missing currency (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 (!content) { errors.push("missing description"); }
|
||||
return errors;
|
||||
|
@ -47,6 +48,7 @@ function insertCheckout(state, tagInfo, content, siteSettings) {
|
|||
['data-image', tagInfo.attrs['image'] || ''],
|
||||
['data-locale', 'auto'],
|
||||
['data-zip-code', !siteSettings.discourse_donations_hide_zip_code],
|
||||
['data-billing-address', siteSettings.discourse_donations_billing_address],
|
||||
['data-currency', siteSettings.discourse_donations_currency]
|
||||
];
|
||||
|
||||
|
@ -81,6 +83,7 @@ export function setup(helper) {
|
|||
'script[data-description]',
|
||||
'script[data-image]',
|
||||
'script[data-zip-code]',
|
||||
'script[data-billing-address]',
|
||||
'script[data-currency]',
|
||||
'script[data-locale]'
|
||||
]);
|
||||
|
|
|
@ -7,6 +7,7 @@ en:
|
|||
discourse_donations_shop_name: "Stripe Shop Name"
|
||||
discourse_donations_currency: "Currency 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_group_name: "Add the user to this group when a payment is successful"
|
||||
en:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
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 'donate' => 'payments#show'
|
||||
end
|
||||
|
|
|
@ -23,6 +23,9 @@ plugins:
|
|||
discourse_donations_hide_zip_code:
|
||||
default: true
|
||||
client: true
|
||||
discourse_donations_billing_address:
|
||||
default: true
|
||||
client: true
|
||||
discourse_donations_reward_badge_name:
|
||||
client: false
|
||||
default: 'Donation'
|
||||
|
|
Loading…
Reference in New Issue