Class: Gateway::MollieGateway

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MollieGateway

Returns a new instance of MollieGateway.



16
17
18
19
20
# File 'app/models/spree/gateway/mollie_gateway.rb', line 16

def initialize(options)
  ::Mollie::Client.configure do |config|
    config.api_key  = options[:api_key]
  end
end

Instance Method Details

#authorize(_money, _source, gateway_options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/spree/gateway/mollie_gateway.rb', line 26

def authorize(_money, _source, gateway_options = {})
  begin
    payment = gateway_options[:originator]
    invalidate_previous_orders(payment.order_id)

    # Create a new Mollie order and update the payment source
    mollie_order = create_mollie_order(gateway_options)
    payment.source.update(status: mollie_order.status, payment_id: mollie_order.id, payment_url: mollie_order.checkout_url)

    ActiveMerchant::Billing::Response.new(true, 'Order created')
  rescue ::Mollie::Exception => e
    ActiveMerchant::Billing::Response.new(false, "Order could not be created: #{e.message}")
  end
end

#available_methods(params = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'app/models/spree/gateway/mollie_gateway.rb', line 101

def available_methods(params = nil)
  method_params = {
    include: 'issuers',
    resource: 'orders'
  }
   
  method_params.merge! params if params.present?
   
  ::Mollie::Method.all(method_params)
end

#available_methods_for_order(order) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/spree/gateway/mollie_gateway.rb', line 112

def available_methods_for_order(order)
  params = {
    amount: {
      currency: order.currency,
      value: format_money(order.total)
    },
    resource: 'orders',
    billingCountry: order.billing_address.country.try(:iso)
  }
  available_methods(params)
end

#capture(_money, _response_code, gateway_options = {}) ⇒ Object



41
42
43
44
# File 'app/models/spree/gateway/mollie_gateway.rb', line 41

def capture(_money, _response_code, gateway_options = {})
  authorize(_money, nil, gateway_options)
  # ActiveMerchant::Billing::Response.new(true, 'Mollie will automatically capture the amount.')
end

#credit(_money_cents, _transaction_id, options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/spree/gateway/mollie_gateway.rb', line 64

def credit(_money_cents, _transaction_id, options)
  refund = options[:originator]

  payment = refund.try(:payment)
  order = payment.try(:order)
  reimbursement = refund.try(:reimbursement)

  # binding.pry
  begin
    if reimbursement
      mollie_order = ::Mollie::Order.get(payment.source.payment_id)

      mollie_order_refund_lines = reimbursement.return_items.map do |reimbursement|
        line_item = mollie_order.lines.detect { |line|
   line_item.sku == reimbursement.inventory_unit.line_item.mollie_identifier
 }
        { id: line_item.id, quantity: ri.inventory_unit.line_item.quantity } if line_item
      end.compact

      mollie_order.refund!({lines: mollie_order_refund_lines})
    else
      ::Mollie::Payment::Refund.create(
        payment_id: payment_id,
        amount: {
          value: format_money(refund.amount),
          currency: refund.currency
        },
        description: "Refund Order ID: #{order.number}",

      )
    end
    ActiveMerchant::Billing::Response.new(true, "Successfully refunded #{order.display_total} for order #{order_number}")
  rescue ::Mollie::Exception => e
    ActiveMerchant::Billing::Response.new(false, e.message)
  end
end

#format_money(money) ⇒ Object



22
23
24
# File 'app/models/spree/gateway/mollie_gateway.rb', line 22

def format_money(money)
  Spree::Money.new(money, { symbol: nil, thousands_separator: nil, decimal_mark: '.' }).format
end

#purchase(money, source, gateway_options = {}) ⇒ Object



46
47
48
# File 'app/models/spree/gateway/mollie_gateway.rb', line 46

def purchase(money, source, gateway_options = {})
  authorize(money, source, gateway_options = {})
end

#update_payment_status(payment) ⇒ Object



124
125
126
127
128
# File 'app/models/spree/gateway/mollie_gateway.rb', line 124

def update_payment_status(payment)
  mollie_order = ::Mollie::Order.get(payment.source.payment_id, embed: 'payments')
   
  Spree::Mollie::PaymentStateUpdater.new(mollie_order, payment).call
end

#void(_respoonse_code, gateway_options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/spree/gateway/mollie_gateway.rb', line 50

def void(_respoonse_code, gateway_options = {})
  begin
    payment_id = gateway_options[:originator].source.payment_id

    if cancel_mollie_order!(payment_id)
      ActiveMerchant::Billing::Response.new(true, 'Mollie order has been cancelled.')
    else
      ActiveMerchant::Billing::Response.new(true, 'Spree order has been canceled, could not cancel Mollie order.')
    end
  rescue ::Mollie::Exception => e
    ActiveMerchant::Billing::Response.new(false, 'Order cancellation unsuccessful.')
  end
end