Module: WechatPay::Direct

Includes:
WechatPayHelper
Defined in:
lib/wechat-pay/direct.rb

Overview

直连商户相关接口封装(常用的已有,待完善)

文档: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml

Constant Summary collapse

QUERY_COMBINE_ORDER_FIELDS =

:nodoc:

%i[combine_out_trade_no].freeze
CLOSE_COMBINE_ORDER_FIELDS =

:nodoc:

%i[combine_out_trade_no sub_orders].freeze
QUERY_ORDER_FIELDS =

:nodoc:

%i[out_trade_no transaction_id].freeze
CLOSE_ORDER_FIELDS =

:nodoc:

%i[out_trade_no].freeze
INVOKE_REFUND_FIELDS =

:nodoc:

%i[transaction_id out_trade_no out_refund_no amount].freeze
QUERY_REFUND_FIELDS =

:nodoc:

%i[sub_mchid refund_id out_refund_no].freeze
TRADEBILL_FIELDS =

:nodoc:

[:bill_date].freeze
FUNDFLOWBILL_FIELDS =

:nodoc:

[:bill_date].freeze

Constants included from WechatPayHelper

WechatPayHelper::GATEWAY_URL

Class Method Summary collapse

Class Method Details

.close_combine_order(params) ⇒ Object

关闭合单

TODO: 与电商平台相同,稍后重构

Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_11.shtml

WechatPay::Direct.close_combine_order(combine_out_trade_no: 'C202104302474')


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/wechat-pay/direct.rb', line 138

def self.close_combine_order(params)
  combine_out_trade_no = params.delete(:combine_out_trade_no)

  url = "/v3/combine-transactions/out-trade-no/#{combine_out_trade_no}/close"

  payload = {
    combine_appid: WechatPay.app_id
  }.merge(params)

  payload_json = payload.to_json

  method = 'POST'

  make_request(
    method: method,
    for_sign: payload_json,
    payload: payload_json,
    path: url
  )
end

.close_order(params) ⇒ Object

关闭订单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_3.shtml

Example:

WechatPay::Direct.close_order(out_trade_no: 'N3344445')


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/wechat-pay/direct.rb', line 212

def self.close_order(params)
  out_trade_no = params.delete(:out_trade_no)
  url = "/v3/pay/transactions/out-trade-no/#{out_trade_no}/close"
  params = params.merge({
                          mchid: WechatPay.mch_id
                        })

  method = 'POST'

  make_request(
    method: method,
    path: url,
    for_sign: params.to_json,
    payload: params.to_json
  )
end

.fundflowbill(params) ⇒ Object

申请资金账单

Todo: 跟商户平台接口相同

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_7.shtml

Example:

WechatPay::Direct.fundflowbill(bill_date: '2021-04-30')


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/wechat-pay/direct.rb', line 332

def self.fundflowbill(params)
  path = '/v3/bill/fundflowbill'
  method = 'GET'

  query = build_query(params)
  url = "#{path}?#{query}"

  make_request(
    path: url,
    method: method,
    extra_headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  )
end

.invoke_combine_transactions_in_appObject

直连合单app下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_1.shtml

params = {
  combine_out_trade_no: 'combine_out_trade_no',
  combine_payer_info: {
    openid: 'client open id'
  },
  sub_orders: [
    {
      mchid: 'mchid',
      sub_mchid: 'sub mchid',
      attach: 'attach',
      amount: {
        total_amount: 100,
        currency: 'CNY'
      },
      out_trade_no: 'out_trade_no',
      description: 'description'
    }
  ],
  notify_url: 'the_url'
}

WechatPay::Direct.invoke_combine_transactions_in_app(params)


93
# File 'lib/wechat-pay/direct.rb', line 93

define_combine_transaction_method('app', 'app', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_1.shtml')

.invoke_combine_transactions_in_h5Object

直连合单h5下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_2.shtml

params = {
  combine_out_trade_no: 'combine_out_trade_no',
  combine_payer_info: {
    openid: 'client open id'
  },
  sub_orders: [
    {
      mchid: 'mchid',
      sub_mchid: 'sub mchid',
      attach: 'attach',
      amount: {
        total_amount: 100,
        currency: 'CNY'
      },
      out_trade_no: 'out_trade_no',
      description: 'description'
    }
  ],
  notify_url: 'the_url'
}

WechatPay::Direct.invoke_combine_transactions_in_h5(params)


95
# File 'lib/wechat-pay/direct.rb', line 95

define_combine_transaction_method('h5', 'h5', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_2.shtml')

.invoke_combine_transactions_in_jsObject

直连合单js下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_3.shtml

params = {
  combine_out_trade_no: 'combine_out_trade_no',
  combine_payer_info: {
    openid: 'client open id'
  },
  sub_orders: [
    {
      mchid: 'mchid',
      sub_mchid: 'sub mchid',
      attach: 'attach',
      amount: {
        total_amount: 100,
        currency: 'CNY'
      },
      out_trade_no: 'out_trade_no',
      description: 'description'
    }
  ],
  notify_url: 'the_url'
}

WechatPay::Direct.invoke_combine_transactions_in_js(params)


94
# File 'lib/wechat-pay/direct.rb', line 94

define_combine_transaction_method('js', 'jsapi', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_3.shtml')

.invoke_combine_transactions_in_miniprogramObject

直连合单miniprogram下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_4.shtml

params = {
  combine_out_trade_no: 'combine_out_trade_no',
  combine_payer_info: {
    openid: 'client open id'
  },
  sub_orders: [
    {
      mchid: 'mchid',
      sub_mchid: 'sub mchid',
      attach: 'attach',
      amount: {
        total_amount: 100,
        currency: 'CNY'
      },
      out_trade_no: 'out_trade_no',
      description: 'description'
    }
  ],
  notify_url: 'the_url'
}

WechatPay::Direct.invoke_combine_transactions_in_miniprogram(params)


96
# File 'lib/wechat-pay/direct.rb', line 96

define_combine_transaction_method('miniprogram', 'jsapi', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_4.shtml')

.invoke_combine_transactions_in_nativeObject

直连合单native下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_5.shtml

params = {
  combine_out_trade_no: 'combine_out_trade_no',
  combine_payer_info: {
    openid: 'client open id'
  },
  sub_orders: [
    {
      mchid: 'mchid',
      sub_mchid: 'sub mchid',
      attach: 'attach',
      amount: {
        total_amount: 100,
        currency: 'CNY'
      },
      out_trade_no: 'out_trade_no',
      description: 'description'
    }
  ],
  notify_url: 'the_url'
}

WechatPay::Direct.invoke_combine_transactions_in_native(params)


97
# File 'lib/wechat-pay/direct.rb', line 97

define_combine_transaction_method('native', 'native', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_5.shtml')

.invoke_refund(params) ⇒ Object

退款申请

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_9.shtml

Example:

WechatPay::Direct.invoke_refund(transaction_id: '4323400972202104305131070170', total: 1, refund: 1, out_refund_no: 'R10000')
WechatPay::Direct.invoke_refund(out_trade_no: 'N2021', total: 1, refund: 1, out_refund_no: 'R10000').body


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/wechat-pay/direct.rb', line 241

def self.invoke_refund(params)
  url = '/v3/refund/domestic/refunds'
  method = 'POST'
  amount = {
    refund: params.delete(:refund),
    total: params.delete(:total),
    currency: 'CNY'
  }

  params = params.merge({
                          amount: amount
                        })

  make_request(
    path: url,
    method: method,
    for_sign: params.to_json,
    payload: params.to_json
  )
end

.invoke_transactions_in_appObject

直连app下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml

Example:

params = {
  appid: 'Your Open id',
  mchid: 'Your Mch id'',
  description: '回流',
  out_trade_no: 'Checking',
  payer: {
    openid: 'oly6s5c'
  },
  amount: {
    total: 1
  },
  notify_url: ENV['NOTIFICATION_URL']
}

WechatPay::Direct.invoke_transactions_in_app(params)


49
# File 'lib/wechat-pay/direct.rb', line 49

define_transaction_method('app', 'app', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml')

.invoke_transactions_in_h5Object

直连h5下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml

Example:

params = {
  appid: 'Your Open id',
  mchid: 'Your Mch id'',
  description: '回流',
  out_trade_no: 'Checking',
  payer: {
    openid: 'oly6s5c'
  },
  amount: {
    total: 1
  },
  notify_url: ENV['NOTIFICATION_URL']
}

WechatPay::Direct.invoke_transactions_in_h5(params)


50
# File 'lib/wechat-pay/direct.rb', line 50

define_transaction_method('h5', 'h5', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml')

.invoke_transactions_in_jsObject

直连js下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml

Example:

params = {
  appid: 'Your Open id',
  mchid: 'Your Mch id'',
  description: '回流',
  out_trade_no: 'Checking',
  payer: {
    openid: 'oly6s5c'
  },
  amount: {
    total: 1
  },
  notify_url: ENV['NOTIFICATION_URL']
}

WechatPay::Direct.invoke_transactions_in_js(params)


47
# File 'lib/wechat-pay/direct.rb', line 47

define_transaction_method('js', 'jsapi', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml')

.invoke_transactions_in_miniprogramObject

直连miniprogram下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml

Example:

params = {
  appid: 'Your Open id',
  mchid: 'Your Mch id'',
  description: '回流',
  out_trade_no: 'Checking',
  payer: {
    openid: 'oly6s5c'
  },
  amount: {
    total: 1
  },
  notify_url: ENV['NOTIFICATION_URL']
}

WechatPay::Direct.invoke_transactions_in_miniprogram(params)


48
# File 'lib/wechat-pay/direct.rb', line 48

define_transaction_method('miniprogram', 'jsapi', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml')

.invoke_transactions_in_nativeObject

直连native下单

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml

Example:

params = {
  appid: 'Your Open id',
  mchid: 'Your Mch id'',
  description: '回流',
  out_trade_no: 'Checking',
  payer: {
    openid: 'oly6s5c'
  },
  amount: {
    total: 1
  },
  notify_url: ENV['NOTIFICATION_URL']
}

WechatPay::Direct.invoke_transactions_in_native(params)


51
# File 'lib/wechat-pay/direct.rb', line 51

define_transaction_method('native', 'native', 'https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml')

.query_combine_order(params) ⇒ Object

合单查询

TODO: 与电商平台相同,稍后重构

Document: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_11.shtml

WechatPay::Direct.query_order(combine_out_trade_no: 'C202104302474')


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wechat-pay/direct.rb', line 111

def self.query_combine_order(params)
  combine_out_trade_no = params.delete(:combine_out_trade_no)

  url = "/v3/combine-transactions/out-trade-no/#{combine_out_trade_no}"

  method = 'GET'

  make_request(
    method: method,
    path: url,
    extra_headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  )
end

.query_order(params) ⇒ Object

直连订单查询

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_2.shtml

Example:

WechatPay::Direct.query_order(transaction_id: '4323400972202104305133344444') # by transaction_id
WechatPay::Direct.query_order(out_trade_no: 'N202104302474') # by out_trade_no


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/wechat-pay/direct.rb', line 172

def self.query_order(params)
  if params[:transaction_id]
    params.delete(:out_trade_no)
    transaction_id = params.delete(:transaction_id)
    path = "/v3/pay/transactions/id/#{transaction_id}"
  else
    params.delete(:transaction_id)
    out_trade_no = params.delete(:out_trade_no)
    path = "/v3/pay/transactions/out-trade-no/#{out_trade_no}"
  end

  params = params.merge({
                          mchid: WechatPay.mch_id
                        })

  method = 'GET'
  query = build_query(params)
  url = "#{path}?#{query}"

  make_request(
    method: method,
    path: url,
    extra_headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  )
end

.query_refund(params) ⇒ Object

直连退款查询

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_10.shtml

Example:

WechatPay::Direct.query_refund(out_refund_no: 'R10000')


274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/wechat-pay/direct.rb', line 274

def self.query_refund(params)
  out_refund_no = params.delete(:out_refund_no)
  url = "/v3/refund/domestic/refunds/#{out_refund_no}"

  method = 'GET'

  make_request(
    method: method,
    path: url,
    extra_headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  )
end

.tradebill(params) ⇒ Object

直连申请交易账单

Todo: 跟商户平台接口相同

Document: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_6.shtml

Example:

WechatPay::direct.tradebill(bill_date: '2021-04-30')


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/wechat-pay/direct.rb', line 302

def self.tradebill(params)
  path = '/v3/bill/tradebill'
  method = 'GET'

  query = build_query(params)
  url = "#{path}?#{query}"

  make_request(
    path: url,
    method: method,
    extra_headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    }
  )
end