Module: RailsTrade::Payment

Extended by:
ActiveSupport::Concern
Included in:
Payment
Defined in:
app/models/rails_trade/payment.rb

Defined Under Namespace

Modules: AlipayPayment, ApplePayment, BankPayment, HandPayment, PaypalPayment, StripePayment, WxpayPayment

Instance Method Summary collapse

Instance Method Details

#analyze_adjust_amountObject



92
93
94
95
96
# File 'app/models/rails_trade/payment.rb', line 92

def analyze_adjust_amount
  self.adjust_amount = self.checked_amount.to_d - self.total_amount.to_d
  self.state = 'all_checked'
  self.save
end

#analyze_payment_methodObject



52
53
54
55
56
57
58
59
60
61
# File 'app/models/rails_trade/payment.rb', line 52

def analyze_payment_method
  if self.buyer_name.present? || self.buyer_identifier.present?
    pm = PaymentMethod.find_or_initialize_by(account_name: self.buyer_name.to_s, account_num: self.buyer_identifier.to_s)
    pm.bank = self.buyer_bank
    self.payment_method = pm

    pm.save
    self.save
  end
end

#check_order(order_id) ⇒ Object



98
99
100
101
102
103
104
105
# File 'app/models/rails_trade/payment.rb', line 98

def check_order(order_id)
  order = Order.find order_id
  payment_order = self.payment_orders.build(order_id: order.id)
  payment_order.check_amount = order.unreceived_amount
  payment_order.save

  payment_order
end

#check_stateObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/rails_trade/payment.rb', line 107

def check_state
  if self.checked_amount == self.total_amount
    self.state = 'all_checked'
  elsif self.checked_amount == 0
    self.state = 'init'
  elsif self.checked_amount < self.total_amount
    self.state = 'part_checked'
  elsif self.checked_amount > self.total_amount
    self.state = 'adjust_checked'
  else
    self.state = 'abusive_checked'
  end
end

#check_state!Object



121
122
123
124
# File 'app/models/rails_trade/payment.rb', line 121

def check_state!
  self.check_state
  self.save!
end

#compute_amountObject



67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/rails_trade/payment.rb', line 67

def compute_amount
  if income_amount.blank? && fee_amount.present?
    self.income_amount = self.total_amount - self.fee_amount
  end

  if fee_amount.blank? && income_amount.present?
    self.fee_amount = self.total_amount - self.income_amount
  end

  self.check_state
end

#compute_checked_amountObject



79
80
81
# File 'app/models/rails_trade/payment.rb', line 79

def compute_checked_amount
  self.payment_orders.where(state: :confirmed).sum(:check_amount)
end

#pending_ordersObject



83
84
85
86
87
88
89
90
# File 'app/models/rails_trade/payment.rb', line 83

def pending_orders
  if self.payment_method
    buyer_ids = self.payment_method.payment_references.pluck(:buyer_id)
    Order.where.not(id: self.payment_orders.pluck(:order_id)).where(buyer_id: buyer_ids, payment_status: ['unpaid', 'part_paid'], state: 'active')
  else
    Order.none
  end
end

#unchecked_amountObject



63
64
65
# File 'app/models/rails_trade/payment.rb', line 63

def unchecked_amount
  total_amount.to_d - payment_orders.sum(:check_amount)
end