Sha256: 340c82fde9b7ca57066bfb16a6bb45941fabb3575d60eca551fdfb4b3f4f8ef1

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module Workarea
  class Payment
    module Authorize
      class Afterpay
        include OperationImplementation
        include CreditCardOperation
        include AfterpayPaymentGateway

        def complete!
          response = authorize
          if response.success? && response.body['status'] == 'APPROVED'
            transaction.response = ActiveMerchant::Billing::Response.new(
              true,
              I18n.t(
                'workarea.afterpay.authorize',
                amount: transaction.amount
              ),
              response.body
            )
          else
             transaction.response = ActiveMerchant::Billing::Response.new(
               false,
              I18n.t('workarea.afterpay.capture_failure'),
              response.body
            )
          end
        end

        def cancel!
          return unless transaction.success?

          payment_id = transaction.response.params["id"]
          response = gateway.void(payment_id)

          transaction.cancellation = ActiveMerchant::Billing::Response.new(
            true,
              I18n.t('workarea.afterpay.void'),
              response.body
            )
        end

        private

        def authorize
          request_id = SecureRandom.uuid
          auth_response = response(request_id)
          if Workarea::Afterpay::RETRY_ERROR_STATUSES.include? auth_response.status
            return response(request_id)
          end

          auth_response
        end

        def response(request_id)
          gateway.authorize(tender.token, tender.payment.id, request_id)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-afterpay-2.1.2 app/models/workarea/payment/authorize/afterpay.rb
workarea-afterpay-2.1.1 app/models/workarea/payment/authorize/afterpay.rb