Module: RailsTrade::PaymentType::Wxpay

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

Instance Method Summary collapse

Instance Method Details

#wxpay_order(trade_type: 'JSAPI', **options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/rails_trade/payment_type/wxpay.rb', line 18

def wxpay_order(trade_type: 'JSAPI', **options)
  prepay = wxpay_prepay(trade_type: trade_type, **options)

  if prepay.success?
    params = {
      noncestr: prepay['nonce_str'],
      prepayid: prepay['prepay_id']
    }
    if trade_type == 'JSAPI'
      ::WxPay::Service.generate_js_pay_req params, options
    else
      ::WxPay::Service.generate_app_pay_req params, options
    end
  else
    prepay.except(:raw)
  end
end

#wxpay_prepay(trade_type: 'JSAPI', spbill_create_ip: '127.0.0.0', notify_url: url_helpers.wxpay_notify_payments_url, **options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/rails_trade/payment_type/wxpay.rb', line 3

def wxpay_prepay(trade_type: 'JSAPI', spbill_create_ip: '127.0.0.0', notify_url: url_helpers.wxpay_notify_payments_url, **options)
  appid = options[:appid] || ::WxPay.appid
  params = {
    body: "订单编号: #{self.uuid}",
    out_trade_no: self.uuid,
    total_fee: (self.amount * 100).to_i,
    spbill_create_ip: spbill_create_ip,
    notify_url: notify_url,
    trade_type: trade_type,
    openid: user.oauth_users.find_by(app_id: appid)&.uid
  }
  
  ::WxPay::Service.invoke_unifiedorder params, options
end

#wxpay_result(options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/rails_trade/payment_type/wxpay.rb', line 36

def wxpay_result(options = {})
  return self if self.payment_status == 'all_paid'

  params = {
    out_trade_no: self.uuid,
  }
  begin
    result = WxPay::Service.order_query params, options
  rescue
    result = { 'err_code_des' => 'network error' }
  end

  if result['trade_state'] == 'SUCCESS'
    self.change_to_paid! type: 'WxpayPayment', payment_uuid: result['transaction_id'], params: result
  else
    self.errors.add :base, result['err_code_des']
  end
end