Class: AllinpayCnp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/allinpay_cnp/client.rb

Constant Summary collapse

VERSION =
'V2.0.0'

Instance Method Summary collapse

Instance Method Details

#query(hash_or_opts = nil, **kwargs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/allinpay_cnp/client.rb', line 29

def query(hash_or_opts = nil, **kwargs)
  opts = (hash_or_opts || {}).merge(kwargs).transform_keys(&:to_sym)

  merchant_no = opts.fetch(:merchant_no, config.merchant_id)
  ori_access_order_id = opts.fetch(:ori_access_order_id)
  private_key = opts[:private_key]
  public_key  = opts[:public_key]

  params = {
    version: VERSION,
    instNo: inst_no_param,
    mchtId: merchant_no,
    transType: 'Query',
    accessOrderId: generate_order_id,
    oriAccessOrderId: ori_access_order_id
  }.compact
  request.post(:quickpay, params, private_key: private_key, public_key: public_key)
end

#refund(hash_or_opts = nil, **kwargs) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/allinpay_cnp/client.rb', line 48

def refund(hash_or_opts = nil, **kwargs)
  opts = (hash_or_opts || {}).merge(kwargs).transform_keys(&:to_sym)

  merchant_no = opts.fetch(:merchant_no, config.merchant_id)
  ori_access_order_id = opts.fetch(:ori_access_order_id)
  refund_amount = opts.fetch(:refund_amount)
  access_order_id = opts.fetch(:access_order_id, generate_order_id)
  private_key = opts[:private_key]
  public_key  = opts[:public_key]

  params = {
    version: VERSION,
    instNo: inst_no_param,
    mchtId: merchant_no,
    transType: 'Refund',
    accessOrderId: access_order_id,
    oriAccessOrderId: ori_access_order_id,
    refundAmount: refund_amount.to_s,
    notifyUrl: opts.fetch(:notify_url, nil)
  }.reject { |_, v| v.nil? }
  request.post(:quickpay, params, private_key: private_key, public_key: public_key)
end

#unified_pay(hash_or_opts = nil, **kwargs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/allinpay_cnp/client.rb', line 7

def unified_pay(hash_or_opts = nil, **kwargs)
  opts = (hash_or_opts || {}).merge(kwargs).transform_keys(&:to_sym)

  access_order_id = opts.fetch(:access_order_id)
  amount          = opts.fetch(:amount)
  currency        = opts.fetch(:currency)
  urls            = opts.fetch(:urls)
  private_key     = opts[:private_key]
  public_key      = opts[:public_key]
  options         = opts.except(:access_order_id, :amount, :currency, :urls, :private_key, :public_key)

  params = build_unified_pay_params(
    access_order_id: access_order_id,
    amount: amount,
    currency: currency,
    urls: urls,
    **options
  )

  request.post(:unified_pay, params, private_key: private_key, public_key: public_key)
end

#verify_callback(params, public_key: nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/allinpay_cnp/client.rb', line 71

def verify_callback(params, public_key: nil)
  key = public_key || config.public_key
  return false unless key

  Signature.verify(params, key)
end