Class: Runa::Order

Inherits:
Response show all
Defined in:
lib/runa/models/order.rb

Constant Summary collapse

PATH =
'/order'
DELIVERY_METHODS =
{ direct: 'direct', email: 'email' }.freeze
DELIVERY_FORMATS =
{ code: 'raw', url: 'url-instant' }.freeze

Constants inherited from Response

Response::STATUS

Instance Attribute Summary collapse

Attributes inherited from Response

#error_code, #error_details, #error_string

Instance Method Summary collapse

Methods inherited from Response

#is_successful?

Constructor Details

#initialize(params = {}) ⇒ Order

Returns a new instance of Order.



16
17
18
19
20
21
# File 'lib/runa/models/order.rb', line 16

def initialize(params = {})
  super(params)
  # default/fallback: 'direct'/'raw'
  @delivery_method = params[:delivery_method] || DELIVERY_METHODS[:direct]
  @delivery_format = params[:delivery_format] || DELIVERY_FORMATS[:code]
end

Instance Attribute Details

#codeObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def code
  @code
end

#created_atObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def created_at
  @created_at
end

#currencyObject

response/success



10
11
12
# File 'lib/runa/models/order.rb', line 10

def currency
  @currency
end

#distribution_typeObject

request/payload



10
11
12
# File 'lib/runa/models/order.rb', line 10

def distribution_type
  @distribution_type
end

#external_refObject

request/payload



10
11
12
# File 'lib/runa/models/order.rb', line 10

def external_ref
  @external_ref
end

#face_valueObject

request/payload



10
11
12
# File 'lib/runa/models/order.rb', line 10

def face_value
  @face_value
end

#itemsObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def items
  @items
end

#order_idObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def order_id
  @order_id
end

#payment_methodObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def payment_method
  @payment_method
end

#payment_typeObject

request/payload



10
11
12
# File 'lib/runa/models/order.rb', line 10

def payment_type
  @payment_type
end

#product_codeObject

request/payload



10
11
12
# File 'lib/runa/models/order.rb', line 10

def product_code
  @product_code
end

#redemption_urlObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def redemption_url
  @redemption_url
end

#statusObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def status
  @status
end

#total_discountObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def total_discount
  @total_discount
end

#total_priceObject

response/success



13
14
15
# File 'lib/runa/models/order.rb', line 13

def total_price
  @total_price
end

Instance Method Details

#parse(response) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/runa/models/order.rb', line 51

def parse(response)
  super(response)

  if @payload['status'].eql?(STATUS[:failed])
    @status = @payload['status']
    unless @payload['type'].blank?
      @type = @payload['type']
    end
    unless @payload['message'].blank?
      @message = @payload['message']
    end
    unless @payload['help'].blank?
      @help = @payload['help']
    end
  end

  # set valid data
  if @payload['status'] && @payload['status'].eql?(STATUS[:completed])
    @status = @payload['status']
    @total_price = @payload['total_price']
    @items = @payload['items']
    @redemption_url = @items.first['redemption_url']
  end

  @order_id = @payload['id']

  self
end

#payloadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/runa/models/order.rb', line 23

def payload
  {
    payment_method: {
      type: @payment_type,
      currency: @currency
    },
    items: [
      {
        face_value: @face_value,
        distribution_method: {
          type: @distribution_type
        },
        products: {
            type: "SINGLE",
            value: @product_code
        }
      }
    ],
  }
end

#post(ctx) ⇒ Object

Create a new order POST /v2/order



46
47
48
49
# File 'lib/runa/models/order.rb', line 46

def post(ctx)
  response = ctx.request(:post, PATH, payload, @external_ref)
  parse(response)
end