Sha256: 874b5bb8dcd026cd71d7f949257aa3dba8c14687eb156626e8859c587ed07c6d

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

module Pay
  module PaddleClassic
    class PaymentMethod
      attr_reader :pay_payment_method

      delegate :customer, :processor_id, to: :pay_payment_method

      # Paddle doesn't provide PaymentMethod IDs, so we have to lookup via the Customer
      def self.sync(pay_customer:, attributes: nil)
        return unless pay_customer.subscription

        payment_method = pay_customer.default_payment_method || pay_customer.build_default_payment_method
        payment_method.processor_id ||= NanoId.generate

        # Lookup payment method from API unless passed in
        attributes ||= payment_method_details_for(subscription_id: pay_customer.subscription.processor_id)

        payment_method.update!(attributes)
        payment_method
      rescue ::Paddle::Error => e
        raise Pay::PaddleClassic::Error, e
      end

      def self.payment_method_details_for(subscription_id:)
        subscription_user = PaddleClassic.client.users.list(subscription_id: subscription_id).data.try(:first)
        payment_information = subscription_user ? subscription_user[:payment_information] : {}

        case payment_information[:payment_method]&.downcase
        when "card"
          {
            payment_method_type: :card,
            brand: payment_information[:card_type],
            last4: payment_information[:last_four_digits],
            exp_month: payment_information[:expiry_date].split("/").first,
            exp_year: payment_information[:expiry_date].split("/").last
          }
        when "paypal"
          {
            payment_method_type: :paypal,
            brand: "PayPal"
          }
        else
          {}
        end
      end

      def initialize(pay_payment_method)
        @pay_payment_method = pay_payment_method
      end

      # Sets payment method as default
      def make_default!
      end

      # Remove payment method
      def detach
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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