Sha256: b4ea9207c0c062b0c0af87c1d85edf2e340297fde4e21be7b27ef4d7cd97826c

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'bodega/payment_method/base'

module Bodega
  module PaymentMethod
    class Paypal < Base
      options :username, :password, :signature

      def checkout_url(success_url, cancel_url, params = {})
        response = client.setup(request, success_url, cancel_url)
        response.redirect_uri
      end

      def complete!(options = {})
        response = client.checkout!(
          options[:token],
          options[:PayerID],
          request
        )
        response.payment_info.last.transaction_id
      end

      protected
      def client
        ::Paypal.sandbox! if Bodega.config.test_mode
        @client ||= ::Paypal::Express::Request.new(
          username:  Bodega.config.paypal.username,
          password:  Bodega.config.paypal.password,
          signature: Bodega.config.paypal.signature
        )
      end

      def request
        @request ||= ::Paypal::Payment::Request.new(
          amount: order.total.to_f,
          description: order.summary,
          items: order.order_products.map {|order_product|
            {
              name: order_product.name,
              amount: order_product.price.to_f,
              quantity: order_product.quantity
            }
          },
          shipping_amount: order.shipping.to_f,
          tax_amount: order.tax.to_f
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bodega-0.4.12 lib/bodega/payment_method/paypal.rb
bodega-0.4.9 lib/bodega/payment_method/paypal.rb