Module: WechatPay::Direct

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

Overview

# 直连商户相关接口封装(常用的已有,待完善) 文档: 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

Instance Method Summary collapse

Class Method Details

.close_combine_order(params) ⇒ Object

关闭合单

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

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

“‘ ruby WechatPay::Direct.close_combine_order(combine_out_trade_no: ’C202104302474’) “‘



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/wechat-pay/direct.rb', line 233

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: pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_3.shtml

Example:

“‘ ruby WechatPay::Direct.close_order(out_trade_no: ’N3344445’) “‘



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/wechat-pay/direct.rb', line 454

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: pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_7.shtml

Example:

“‘ ruby WechatPay::Direct.fundflowbill(bill_date: ’2021-04-30’) “‘



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/wechat-pay/direct.rb', line 574

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_refund(params) ⇒ Object

退款申请

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

Example:

“‘ ruby 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 “‘



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/wechat-pay/direct.rb', line 483

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

.query_combine_order(params) ⇒ Object

合单查询

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

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

“‘ ruby WechatPay::Direct.query_order(combine_out_trade_no: ’C202104302474’) “‘



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/wechat-pay/direct.rb', line 206

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: pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_2.shtml

Example:

“‘ ruby WechatPay::Direct.query_order(transaction_id: ’4323400972202104305133344444’) # by transaction_id WechatPay::Direct.query_order(out_trade_no: ‘N202104302474’) # by out_trade_no “‘



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/wechat-pay/direct.rb', line 414

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: pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_10.shtml

Example:

“‘ ruby WechatPay::Direct.query_refund(out_refund_no: ’R10000’) “‘



516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/wechat-pay/direct.rb', line 516

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: pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_6.shtml

Example:

“‘ ruby WechatPay::direct.tradebill(bill_date: ’2021-04-30’) “‘



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/wechat-pay/direct.rb', line 544

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

Instance Method Details

#keyObject

:singleton-method: invoke_transactions_in_native

直连native下单

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

Example:

“‘ ruby 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) “‘



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/wechat-pay/direct.rb', line 180

{
  js: 'jsapi',
  app: 'app',
  h5: 'h5',
  native: 'native',
  miniprogram: 'jsapi'
}.each do |key, _value|
  const_set("INVOKE_COMBINE_TRANSACTIONS_IN_#{key.upcase}_FIELDS",
            %i[combine_out_trade_no scene_info sub_orders notify_url].freeze)
  define_singleton_method("invoke_combine_transactions_in_#{key}") do |params|
    WechatPay::Ecommerce.send("invoke_combine_transactions_in_#{key}", params)
  end
end