class MyMoip::Purchase

Attributes

code[R]
credit_card[RW]
id[RW]
payer[RW]
price[RW]
reason[RW]

Public Class Methods

new(attrs) click to toggle source
# File lib/mymoip/purchase.rb, line 6
def initialize(attrs)
  @id          = attrs.fetch(:id) { rand }
  @price       = attrs.fetch(:price)
  @credit_card = MyMoip::CreditCard.new(attrs.fetch(:credit_card))
  @payer       = MyMoip::Payer.new(attrs.fetch(:payer))
  @reason      = attrs.fetch(:reason)
end

Public Instance Methods

checkout!() click to toggle source
# File lib/mymoip/purchase.rb, line 14
def checkout!
  authorization = get_authorization!
  payment = MyMoip::CreditCardPayment.new(@credit_card,
                                          installments: 1)
  request = MyMoip::PaymentRequest.new(@id)
  request.api_call(payment, token: authorization.token)

  @code = request.code
  request.success?
end

Private Instance Methods

get_authorization!() click to toggle source
# File lib/mymoip/purchase.rb, line 27
def get_authorization!
  instruction = MyMoip::Instruction.new(
    id:             @id,
    payment_reason: @reason,
    values:         [@price],
    payer:          @payer
  )

  request = MyMoip::TransparentRequest.new(@id)
  request.api_call(instruction)
  request
end