Class: EricWeixin::RedpackOrder

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/eric_weixin/redpack_order.rb

Constant Summary collapse

SENDTYPE =
{
    "API" => "通过API接口发放",
    "UPLOAD" => "通过上传文件方式发放",
    "ACTIVITY" => "通过活动方式发放"
}
HBTYPE =
{
    "GROUP" => "裂变红包",
    "NORMAL" => "普通红包"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_redpack_order(options) ⇒ Object

必填参数===================

re_openid total_amount wishing client_ip act_name remark send_name

选填参数===================

wxappid mch_id mch_key total_num



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/eric_weixin/redpack_order.rb', line 31

def self.create_redpack_order options
  self.transaction do
    weixin_user = ::Weixin::WeixinUser.find_by_openid(options[:re_openid])
     = weixin_user.||EricWeixin::PublicAccount.find_by_weixin_app_id(params[:wxappid])
    BusinessException.raise '查找不到对应的公众号。' if .blank?
    options[:wxappid] = options[:wxappid]||.weixin_app_id
    options[:mch_id] = options[:mch_id]||.mch_id
    options[:total_num] ||= 1
    options[:mch_key] = options[:mch_key]||.mch_key
    result = EricWeixin::Pay.sendredpack options
    pp "*********************** 发红包结果 **************************"
    pp result
    return result['err_code'] if result['return_code'] == 'FAIL'
    redpack_order = self.new
    redpack_order.mch_billno = result['mch_billno']
    redpack_order. = .id
    redpack_order.save!
    redpack_order
  end
end

Instance Method Details

#get_infoObject

定时使用 redpack_order 实例变量来完善补充红包信息。



53
54
55
56
57
58
59
60
61
62
63
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
# File 'app/models/eric_weixin/redpack_order.rb', line 53

def get_info
  return unless self.redpacks.blank?
  EricWeixin::RedpackOrder.transaction do
    options = {}
    options[:mch_billno] = self.mch_billno
    options[:mch_id] = self..mch_id
    options[:appid] = self..weixin_app_id
    options[:mch_key] = self..mch_key
    result = EricWeixin::Pay.gethbinfo options
    pp "************************ 查询红包结果 *****************************"
    pp result
    return result['err_code'] if result['return_code'] == 'FAIL'
    self.detail_id = result['detail_id']
    self.send_type = result['send_type']
    self.hb_type = result['hb_type']
    self.total_num = result['total_num']
    self.total_amount = result['total_amount']
    self.reason = result['reason']
    self.send_time = result['send_time']
    self.refund_time = result['refund_time']
    self.refund_amount = result['refund_amount']
    self.wishing = result['wishing']
    self.remark = result['emark']
    self.act_name = result['act_name']
    self.save!
    hbinfo = (result['hblist']['hbinfo'] rescue '')

    unless hbinfo.blank?
      options[:status] = hbinfo['status']
      options[:openid] = hbinfo['openid']
      options[:amount] = hbinfo['amount']
      options[:rcv_time] = hbinfo['rcv_time']
      options[:weixin_redpack_order_id] = self.id
      EricWeixin::Redpack.create_redpack options
    end


  end
end