Sha256: 2a8e8dd53e7755c2c7718f8bbbf88878907e821362421cf3ebbb5309cff913fe

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

module Pay
  module Webhooks
    class BraintreeController < Pay::ApplicationController
      if Rails.application.config.action_controller.default_protect_from_forgery
        skip_before_action :verify_authenticity_token
      end

      def create
        case verified_event.kind
        when "subscription_charged_successfully"
          subscription_charged_successfully(verified_event)
        when "subscription_canceled"
          subscription_canceled(verified_event)
        end

        render json: {success: true}
      rescue Braintree::InvalidSignature => e
        head :ok
      end

      private

      def subscription_charged_successfully(event)
        subscription = event.subscription
        return if subscription.nil?

        user = Pay.user_model.find_by(processor: :braintree, processor_id: subscription.id)
        return unless user.present?

        charge = user.save_braintree_transaction(subscription.transactions.first)

        if Pay.send_emails
          Pay::UserMailer.receipt(user, charge).deliver_later
        end
      end

      def subscription_canceled(event)
        subscription = event.subscription
        return if subscription.nil?

        user = Pay.user_model.find_by(processor: :braintree, processor_id: subscription.id)
        return unless user.present?

        # User canceled or failed to make payments
        user.update(braintree_subscription_id: nil)
      end

      def verified_webhook
        @webhook_notification ||= Braintree::WebhookNotification.parse(
          params[:bt_signature],
          params[:bt_payload]
        )
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pay-1.0.0.rc3 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.rc2 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.rc1 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.beta5 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.beta4 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.beta3 app/controllers/pay/webhooks/braintree_controller.rb
pay-1.0.0.beta2 app/controllers/pay/webhooks/braintree_controller.rb