Sha256: 01163f5063ea3276fc2b13e415cdd56cb72b5fc5e7542fd559799b05c80c47cb

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

class Spree::PagseguroProvider
  def initialize(order, user, notification_url, redirect_url)
    @order = order
    @user = user
    @notification_url = notification_url
    @redirect_url = redirect_url
  end

  def init_transaction!
    payment = PagSeguro::PaymentRequest.new
    payment.reference = @order.id
    payment.extra_amount = @order.adjustment_total
    build_items_to payment
    payment.shipping = PagSeguro::Shipping.new shipping_options
    payment.sender = sender
    payment.notification_url = @notification_url
    payment.redirect_url = @redirect_url
    payment.register
  end

  private
  def build_items_to(payment)
    @order.line_items.each do |item|
      payment.items << {
        id: item.product.id,
        description: item.product.name,
        amount: item.price,
        quantity: item.quantity,
        weight: item.product.weight.round
      }
    end
  end

  def sender
    {
      name: @user.bill_address.full_name,
      email: @user.email,
      cpf: @user.cpf,
      phone: {
        area_code: @user.phone_area_code,
        number: @user.phone_number
      }
    }
  end

  def shipping_options
    {
      type_name: @order.shipments.first.selected_shipping_rate.shipping_method.name.downcase,
      cost: @order.shipment_total,
      address: {
        street: @order.bill_address.address1,
        number: @order.bill_address.number,
        complement: @order.bill_address.address2,
        district: @order.bill_address.district,
        city: @order.bill_address.city.name,
        state: @order.bill_address.state.abbr,
        postal_code: @order.bill_address.zipcode
      }
    }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_pagseguro_oficial-1.1.1 app/models/spree/pagseguro_provider.rb
solidus_pagseguro_oficial-1.1.0 app/models/spree/pagseguro_provider.rb