Class: SolidusOpenPay::Gateway

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_open_pay/gateway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gateway

Returns a new instance of Gateway.



9
10
11
12
13
14
15
16
# File 'app/models/solidus_open_pay/gateway.rb', line 9

def initialize(options)
  @client = OpenpayApi.new(
    options.fetch(:merchant_id, nil),
    options.fetch(:private_key, nil),
    options.fetch(:country, nil).presence || !options.fetch(:test_mode, nil)
  )
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'app/models/solidus_open_pay/gateway.rb', line 7

def client
  @client
end

Instance Method Details

#authorize(amount_in_cents, source, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/solidus_open_pay/gateway.rb', line 18

def authorize(amount_in_cents, source, options = {})
  resource_builder = ::SolidusOpenPay::Builders::Charge.new(
    source: source,
    amount: amount_in_cents,
    options: options
  )

  response = client.create(:charges).create(
    resource_builder.payload
  )

  source&.update(
    authorization_id: response.try(:[], 'id')
  )

  SolidusOpenPay::Response.build(response)
rescue ::OpenpayTransactionException,
       ::OpenpayException,
       ::OpenpayConnectionException => e
  SolidusOpenPay::Response.build(e)
end

#capture(_amount_in_cents, authorization_id, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/solidus_open_pay/gateway.rb', line 40

def capture(_amount_in_cents, authorization_id, options = {})
  response = client.create(:charges).capture(
    authorization_id
  )

  json_response = JSON.parse(response.body)
  source = options[:originator].try(:source)

  source&.update(
    capture_id: json_response.try(:[], 'id'),
    authorization_id: json_response.try(:[], 'authorization')
  )

  SolidusOpenPay::Response.build(json_response)
rescue ::OpenpayTransactionException,
       ::OpenpayException,
       ::OpenpayConnectionException => e
  SolidusOpenPay::Response.build(e)
end

#purchase(amount_in_cents, source, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/solidus_open_pay/gateway.rb', line 60

def purchase(amount_in_cents, source, options = {})
  resource_builder = ::SolidusOpenPay::Builders::Charge.new(
    source: source,
    amount: amount_in_cents,
    options: options.merge({ capture: true })
  )

  response = client.create(:charges).create(
    resource_builder.payload
  )

  source&.update(
    capture_id: response.try(:[], 'id'),
    authorization_id: response.try(:[], 'authorization')
  )

  SolidusOpenPay::Response.build(response)
rescue ::OpenpayTransactionException,
       ::OpenpayException,
       ::OpenpayConnectionException => e
  SolidusOpenPay::Response.build(e)
end

#void(authorization_id, _options = {}) ⇒ Object Also known as: credit



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/solidus_open_pay/gateway.rb', line 83

def void(authorization_id, _options = {})
  response = client.create(:charges).refund(
    authorization_id,
    {}
  )

  SolidusOpenPay::Response.build(response)
rescue ::OpenpayTransactionException,
       ::OpenpayException,
       ::OpenpayConnectionException => e
  SolidusOpenPay::Response.build(e)
end