diff --git a/app/controllers/discourse_donations/charges_controller.rb b/app/controllers/discourse_donations/charges_controller.rb index f03e6d9..1600d1d 100644 --- a/app/controllers/discourse_donations/charges_controller.rb +++ b/app/controllers/discourse_donations/charges_controller.rb @@ -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 diff --git a/app/controllers/discourse_donations/checkout_controller.rb b/app/controllers/discourse_donations/checkout_controller.rb new file mode 100644 index 0000000..48435aa --- /dev/null +++ b/app/controllers/discourse_donations/checkout_controller.rb @@ -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 diff --git a/app/services/discourse_donations/stripe.rb b/app/services/discourse_donations/stripe.rb index 0acbbe7..7c4f5d2 100644 --- a/app/services/discourse_donations/stripe.rb +++ b/app/services/discourse_donations/stripe.rb @@ -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) diff --git a/assets/javascripts/lib/discourse-markdown/stripe.js.es6 b/assets/javascripts/lib/discourse-markdown/stripe.js.es6 index 2c3c608..adb1e6a 100644 --- a/assets/javascripts/lib/discourse-markdown/stripe.js.es6 +++ b/assets/javascripts/lib/discourse-markdown/stripe.js.es6 @@ -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]' ]); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 6379a4b..36fc3f7 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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: diff --git a/config/routes.rb b/config/routes.rb index a08e196..eba16d8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/config/settings.yml b/config/settings.yml index 9f7a02a..71c4724 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -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'