Module: WechatPayment::Concern::Goods

Extended by:
ActiveSupport::Concern
Defined in:
lib/wechat_payment/concern/goods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wechat_payment/concern/goods.rb', line 8

def self.included(base)
  base.class_eval do
    cattr_accessor :user_model, :user_ref_field, :goods_ref_field, :user_goods_model, :persist_goods_attrs

    self.user_model = "User"
    self.user_ref_field = "user"
    self.goods_ref_field = self.name.underscore
    self.persist_goods_attrs = []

    # 商品和用户的中间表模型,假设商品模型是 Product,那么中间模型就是 UserProduct
    self.user_goods_model = "#{self.user_model}#{self.name}"

    validates_numericality_of :price
    validates_presence_of :name
  end
end

Instance Method Details

#payment_exec_success(payment_order) ⇒ Object



99
100
101
# File 'lib/wechat_payment/concern/goods.rb', line 99

def payment_exec_success(payment_order)

end

#refund(refund_fee) ⇒ WechatPayment::ServiceResult

退款

Parameters:

  • refund_fee (Integer)

Returns:



95
96
97
# File 'lib/wechat_payment/concern/goods.rb', line 95

def refund(refund_fee)
  payment_orders.paid.last.refund(refund_fee)
end

#sell_to(user, with_info = {}) ⇒ WechatPaymentOrder

售出

Parameters:

Returns:

  • (WechatPaymentOrder)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wechat_payment/concern/goods.rb', line 28

def sell_to(user, with_info = {})
  persist_goods_data = {}.tap do |h|
    self.class.persist_goods_attrs.each do |attr|
      h[attr] = send(attr)
    end
  end

  user_goods_model = self.class.user_goods_model.constantize

  user_goods = user_goods_model.new(
    self.class.goods_ref_field => self,
    self.class.user_ref_field => user,
    **with_info,
    **persist_goods_data,
  )

  unless user_goods.save
    log_content = {
      message: "商品中间表 #{user_goods_model.table_name} 插入数据失败",
      error: user_goods.errors.full_messages
    }.to_json

    WechatPayment::Logger.info { log_content }
    return WechatPayment::FailureResult.new(error: user_goods.errors.full_messages,
                                            message: "商品中间表 #{user_goods_model.table_name} 插入数据失败",
                                            message_kind: :create_user_goods_failed)
  end

  payment_order = user_goods.payment_orders.new(
    body: name,
    total_fee: price,
    trade_type: :JSAPI,
    customer: user
  )

  if payment_order.save
    message = "支付订单创建成功"
    WechatPayment::SuccessResult.new(data: payment_order, message:, message_kind: :create_payment_order_success)
  else
    message = "支付订单创建失败"
    log_content = { message:, error: payment_order.errors.full_messages }.to_json
    WechatPayment::Logger.error { log_content }
    WechatPayment::FailureResult.new(error: user_goods.errors.full_messages, message:, message_kind: :create_payment_order_failed)
  end
end