Sha256: 4e8caeef6111e7e621f3831d1ef2136b82f2cb976c1ddf872fa90bdbb9fa379c

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

module Pay
  module Paddle
    module Charge
      extend ActiveSupport::Concern

      included do
        scope :paddle, -> { where(processor: :paddle) }

        store_accessor :data, :paddle_receipt_url
      end

      def paddle?
        processor == "paddle"
      end

      def paddle_charge
        return unless owner.subscription
        payments = PaddlePay::Subscription::Payment.list({subscription_id: owner.subscription.processor_id})
        charges = payments.select { |p| p[:id].to_s == processor_id }
        charges.try(:first)
      rescue ::PaddlePay::PaddlePayError => e
        raise Pay::Paddle::Error, e
      end

      def paddle_refund!(amount_to_refund)
        return unless owner.subscription
        payments = PaddlePay::Subscription::Payment.list({subscription_id: owner.subscription.processor_id, is_paid: 1})
        if payments.count > 0
          PaddlePay::Subscription::Payment.refund(payments.last[:id], {amount: amount_to_refund})
          update(amount_refunded: amount_to_refund)
        else
          raise Error, "Payment not found"
        end
      rescue ::PaddlePay::PaddlePayError => e
        raise Pay::Paddle::Error, e
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pay-2.5.0 lib/pay/paddle/charge.rb
pay-2.4.4 lib/pay/paddle/charge.rb
pay-2.4.3 lib/pay/paddle/charge.rb
pay-2.4.2 lib/pay/paddle/charge.rb
pay-2.4.0 lib/pay/paddle/charge.rb