Sha256: 97731d07b840b56e74b8e141530d48837f82ac641389e69379c2139328816a6f

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module Moneytree
  module Webhooks
    class StripeController < Moneytree::ApplicationController
      skip_before_action :verify_authenticity_token

      def create
        case webhook_params.type
        when 'charge.succeeded'
          process_charge!
        when 'charge.refunded'
          process_refund!
        else
          puts "Unhandled event type: #{webhook_params.type}"
        end

        head :ok
      end

      private

      def webhook_params
        @webhook_params ||=
          ::Stripe::Event.construct_from(
            JSON.parse(request.body.read, symbolize_names: true)
          )
      end

      def process_charge!
        return if transaction.completed?

        transaction.process_response(
          Moneytree::TransactionResponse.new(:success, '', { charge_id: stripe_object.id })
        )

        if Moneytree.order_status_trigger_method
          transaction.order.send(Moneytree.order_status_trigger_method, transaction)
        end
      end

      def process_refund!
        stripe_object.refunds.data.each do |stripe_refund_object|
          # TODO: Create refund transaction in db for PSP-initiated refunds
          next if stripe_refund_object.metadata[:moneytree_transaction_id].blank?

          refund = transaction.refunds.find(stripe_refund_object.metadata[:moneytree_transaction_id])

          next if refund.completed?

          refund.process_response(
            Moneytree::TransactionResponse.new(:success, '')
          )
          if Moneytree.order_status_trigger_method
            transaction.order.send(Moneytree.order_status_trigger_method, transaction)
          end
        end
      end

      def transaction
        @transaction ||= Moneytree::Transaction.find(stripe_object.metadata[:moneytree_transaction_id])
      end

      def stripe_object
        @stripe_object ||= webhook_params.data.object
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moneytree-rails-0.1.11 app/controllers/moneytree/webhooks/stripe_controller.rb
moneytree-rails-0.1.10 app/controllers/moneytree/webhooks/stripe_controller.rb
moneytree-rails-0.1.9 app/controllers/moneytree/webhooks/stripe_controller.rb