Sha256: 72d7c87923450326bd248a69884d7c20196330e5c351200ff41e57e518fb5f5a

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'activemerchant'

module Opay
  module Providers

    class Paypal
      def self.create_payment(session_id, description, ip, confirm_url, cancel_url)
        # for future items list
        payment = Opay::Payment.where(session_id: session_id).first!

        response = geteway.setup_purchase(payment.amount.to_i,
          ip: ip,
          return_url: confirm_url,
          cancel_return_url: cancel_url,
          order_id: session_id,
          currency: Opay.config.paypal_currency,
          items: [{
            name: description,
            quantity: 1,
            amount: payment.amount.to_i
          }]
        )

        # check if response is success
        if response.success?
          payment.update_attribute(:session_id, response.token)
          geteway.redirect_url_for(response.token)
        end
      end

      def self.process(token, payer_id, ip)
        payment = Opay::Payment.where(session_id: token).first!

        if Opay.config.process_payments_localy
          payment.payable.finish
          return true
        end

        response = geteway.purchase(payment.amount, {
          ip: ip,
          token: token,
          payer_id: payer_id
        })

        if response.success?
          payment.payable.finish
          return true
        end

        false
      end

      private

      def self.geteway
        # ActiveMerchant::Billing::BogusGateway.new in test
        @geteway ||= begin
          ::ActiveMerchant::Billing::Base.mode = :test if Opay.config.test_mode == true

          geteway = ::ActiveMerchant::Billing::PaypalExpressGateway.new({
            login: Opay.config.paypal_login,
            password: Opay.config.paypal_password,
            signature: Opay.config.paypal_signature
          })
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opay-1.2.6 lib/opay/providers/paypal.rb
opay-1.2.5 lib/opay/providers/paypal.rb