Sha256: 00227a8d154a520fc3b00c5d055ed93b5d90eca26890ecf17e1d5c7e905ba972

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 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: @order.bill_address.full_name,
      email: @order.email,
      cpf: @user.cpf,
      phone: {
        area_code: @order.bill_address.phone_area_code,
        number: @order.bill_address.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

1 entries across 1 versions & 1 rubygems

Version Path
solidus_pagseguro_oficial-1.1.2 app/models/spree/pagseguro_provider.rb