Sha256: 0a283aae67ce541ca77b94e6e74cf250ef0f3261ddb69427ff3cbf9287ddfc77

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

module Pay
  module PaddleClassic
    class Charge
      attr_reader :pay_charge

      delegate :processor_id, :customer, to: :pay_charge

      def initialize(pay_charge)
        @pay_charge = pay_charge
      end

      def charge
        return unless customer.subscription
        payments = PaddleClassic.client.payments.list(subscription_id: customer.subscription.processor_id)
        charges = payments.data.select { |p| p[:id].to_s == processor_id }
        charges.try(:first)
      rescue ::Paddle::Error => e
        raise Pay::PaddleClassic::Error, e
      end

      def refund!(amount_to_refund)
        return unless customer.subscription
        payments = PaddleClassic.client.payments.list(subscription_id: customer.subscription.processor_id, is_paid: 1)
        if payments.total > 0
          PaddleClassic.client.payments.refund(order_id: payments.data.last[:id], amount: amount_to_refund)
          pay_charge.update(amount_refunded: amount_to_refund)
        else
          raise Error, "Payment not found"
        end
      rescue ::Paddle::Error => e
        raise Pay::PaddleClassic::Error, e
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pay-7.3.0 lib/pay/paddle_classic/charge.rb
pay-7.2.1 lib/pay/paddle_classic/charge.rb
pay-7.1.1 lib/pay/paddle_classic/charge.rb
pay-7.1.0 lib/pay/paddle_classic/charge.rb
pay-7.0.0 lib/pay/paddle_classic/charge.rb