Module: RailsTrade::PaymentType::Stripe

Defined in:
app/models/rails_trade/payment_type/stripe.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stripe_customer(buyer:, **params) ⇒ Object

execute payment required: token



48
49
50
51
52
53
# File 'app/models/rails_trade/payment_type/stripe.rb', line 48

def self.stripe_customer(buyer:, **params)
  payment_method = buyer.payment_methods.build(type: 'StripeMethod')
  payment_method.token = params[:token]
  payment_method.detective_save
  payment_method
end

Instance Method Details

#stripe_charge(params = {}) ⇒ Object



3
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
# File 'app/models/rails_trade/payment_type/stripe.rb', line 3

def stripe_charge(params = {})
  if params[:token]
    TheStripe.stripe_customer(buyer: self.buyer, token: params[:token])
  end

  if params[:payment_method_id]
    stripe_payment_method = buyer.payment_methods.where(type: 'StripeMethod', id: params[:payment_method_id]).first
  else
    stripe_payment_method = buyer.payment_methods.where(type: 'StripeMethod').first
  end

  unless stripe_payment_method
    self.errors.add :base, 'Please add credit card at first.'
    return self
  end
  return self if self.amount <= 0

  begin
    charge = Stripe::Charge.create(amount: self.amount_money.cents, currency: self.currency, customer: stripe_payment_method.)
    self.update payment_type: 'stripe', payment_id: charge.id
    self.stripe_record(charge)
  rescue Stripe::StripeError, Stripe::CardError => ex
    self.errors.add :base, ex.message
  end
  self
end

#stripe_resultObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/rails_trade/payment_type/stripe.rb', line 30

def stripe_result
  return self if self.payment_status == 'all_paid'

  if self.payment_id.present?
    charge = Stripe::Charge.retrieve(self.payment_id)

    if charge.paid && !charge.refunded
       self.change_to_paid! type: 'StripePayment', payment_uuid: charge.id, params: charge
    else
      errors.add :uuid, 'error'
    end

  end
end