Class: DonationsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- DonationsController
- Defined in:
- app/controllers/donations_controller.rb
Instance Method Summary collapse
Methods inherited from ApplicationController
#dashboard, #keyboard_tools, #upload
Instance Method Details
#callback ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/controllers/donations_controller.rb', line 49 def callback secret = Rails.application.credentials.stripe_webhook_secret payload = request.body.read signature = request.headers['Stripe-Signature'] begin event = Stripe::Webhook.construct_event(payload, signature, secret) rescue JSON::ParserError respond_to do |format| format.json do render status: 400, json: { error: 'Check yo JSON syntax. Fam.' } end format.any do render status: 400, plain: 'Check yo JSON syntax. Fam.' end end return rescue Stripe::SignatureVerificationError respond_to do |format| format.json do render status: 400, json: { error: "You're not Stripe. Go away." } end format.any do render status: 400, plain: "You're not Stripe. Go away." end end return end if event.nil? respond_to do |format| format.json do render status: 500, json: { error: 'Webhook event not created. ???' } end format.any do render status: 500, plain: 'Webhook event not created. ???' end end return end object = event.data.object method = event.type.gsub('.', '_') if StripeEventProcessor.respond_to?(method) begin result = StripeEventProcessor.send(method, object, event) render status: 200, json: { status: 'Accepted for processing.', result: result } rescue Stripe::StripeError => e error_id = SecureRandom.uuid ErrorLog.create(community: RequestContext.community, user: current_user, klass: e&.class, message: e&., backtrace: e&.backtrace&.join("\n"), request_uri: request.original_url, host: request.raw_host_with_port, uuid: error_id, user_agent: request.user_agent) render status: 500, json: { error: "#{e&.class}: #{error_id} created." } end else render status: 202, json: { status: 'Accepted, not processed.' } end end |
#index ⇒ Object
9 |
# File 'app/controllers/donations_controller.rb', line 9 def index; end |
#intent ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/donations_controller.rb', line 11 def intent @referrer = params[:return_to] currencies = ['GBP', 'USD', 'EUR'] @currency = currencies.include?(params[:currency]) ? params[:currency] : 'GBP' @symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '€' }[@currency] begin amount = params[:amount].to_f rescue flash[:danger] = 'Invalid amount. Is there a typo somewhere?' redirect_to donate_path return end if amount < 0.50 flash[:danger] = "Sorry, we can't accept amounts below #{symbol}0.50. We appreciate your generosity, but the " \ 'processing fees make it prohibitive.' redirect_to donate_path return end # amount * 100 because Stripe takes amounts in pence @amount = amount @intent = Stripe::PaymentIntent.create({ amount: (amount * 100).to_i, currency: @currency, metadata: { user_id: current_user&.id }, description: params[:desc] }, { idempotency_key: params[:authenticity_token] }) end |
#success ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/donations_controller.rb', line 38 def success @amount = params[:amount] @symbol = params[:currency] @referrer = params[:return_to] Stripe::PaymentIntent.update(params[:intent], { metadata: { public_name: params[:public_name], public_comment: params[:public_comments] } }) DonationMailer.with(amount: @amount, currency: @symbol, email: params[:billing_email], name: current_user&.username || params[:billing_name]) .donation_successful.deliver_now end |