Module: Od::Payments::Charge

Defined in:
lib/od/payments/resources/charge.rb

Class Method Summary collapse

Class Method Details

.cancel(payment_intent_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/od/payments/resources/charge.rb', line 45

def self.cancel(payment_intent_id)
  case Od::Payments.adapter
  when :stripe
    Stripe::PaymentIntent.cancel payment_intent_id
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end

.capture(payment_intent_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/od/payments/resources/charge.rb', line 34

def self.capture(payment_intent_id)
  case Od::Payments.adapter
  when :stripe
    Stripe::PaymentIntent.capture(payment_intent_id)
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end

.confirm(payment_intent_id, payment_method_id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/od/payments/resources/charge.rb', line 90

def self.confirm(payment_intent_id, payment_method_id)
  case Od::Payments.adapter
  when :stripe
    Stripe::PaymentIntent.confirm(
      payment_intent_id,
      { payment_method: payment_method_id }
    )
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end

.create(amount, option = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/od/payments/resources/charge.rb', line 4

def self.create(amount, option = {})
  case Od::Payments.adapter
  when :stripe
    Stripe::PaymentIntent.create({
      currency: 'usd',
      amount: (amount * 100).to_i
    }.merge(option))
  when :square
    result = Od::Payments.client_square.payments.create_payment(
      body: {
        idempotency_key: SecureRandom.uuid,
        amount_money: {
          amount: amount,
          currency: 'USD'
        }
      }.merge(option)
    )

    if result.success?
      result.data
    elsif result.error?
      raise Od::Payments::CardError.new(result.errors, {})
    end
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end

.show(payment_intent_id) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/od/payments/resources/charge.rb', line 79

def self.show(payment_intent_id)
  case Od::Payments.adapter
  when :stripe
    Stripe::PaymentIntent.retrieve payment_intent_id
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end

.void(payment_intent_id, option = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/od/payments/resources/charge.rb', line 56

def self.void(payment_intent_id, option = {})
  case Od::Payments.adapter
  when :stripe
    Stripe::Refund.create({
      payment_intent: payment_intent_id
    }.merge(option))
  when :square
    result = Od::Payments.client_square.refunds.refund_payment(body: {
      payment_id: payment_intent_id,
      idempotency_key: SecureRandom.uuid
    }.merge(option))
    if result.success?
      result.data
    elsif result.error?
      raise Od::Payments::CardError.new(result.errors, {})
    end
  end
rescue Stripe::CardError => e
  raise Od::Payments::CardError.new(e.error.message, {})
rescue Stripe::StripeError => e
  raise Od::Payments::OdPaymentsError, e.error.message
end