Module: JdPay::Service

Defined in:
lib/jd_pay/service.rb

Constant Summary collapse

UNIORDER_URL =
'https://paygate.jd.com/service/uniorder'
QUERY_URL =
'https://paygate.jd.com/service/query'
REFUND_URL =
'https://paygate.jd.com/service/refund'
H5_PAY_URL =
'https://h5pay.jd.com/jdpay/saveOrder'
PC_PAY_URL =
'https://wepay.jd.com/jdpay/saveOrder'
REVOKE_URL =
'https://paygate.jd.com/service/revoke'
QRCODE_PAY_URL =
'https://paygate.jd.com/service/fkmPay'
USER_RELATION_URL =
'https://paygate.jd.com/service/getUserRelation'
CANCEL_USER_URL =
'https://paygate.jd.com/service/cancelUserRelation'
WEB_PAY_REQUIRED_FIELDS =
[:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :callbackUrl, :userId]
UNIORDER_REQUIRED_FIELDS =
[:tradeNum, :tradeName, :amount, :orderType, :notifyUrl, :userId]
QRCODE_REQUIRED_FIELDS =
[:tradeNum, :tradeName, :amount, :device, :token]
USER_RELATION_REQUIRED_FIELDS =
[:userId]
REFUND_REQUIRED_FIELDS =
[:tradeNum, :oTradeNum, :amount, :notifyUrl]
QUERY_REQUIRED_FIELDS =
[:tradeNum, :tradeType]
REVOKE_REQUIRED_FIELDS =
[:tradeNum, :oTradeNum, :amount]

Class Method Summary collapse

Class Method Details

.cancel_user(params, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jd_pay/service.rb', line 88

def cancel_user(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
  }.merge(params)

  check_required_options(params, USER_RELATION_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(CANCEL_USER_URL, make_payload(params), options)), options)
end

.h5_pay(params, options = {}) ⇒ Object



22
23
24
# File 'lib/jd_pay/service.rb', line 22

def h5_pay(params, options = {})
  web_pay(params, H5_PAY_URL, options = {})
end

.notify_verify(xml_str, options = {}) ⇒ Object



144
145
146
# File 'lib/jd_pay/service.rb', line 144

def notify_verify(xml_str, options = {})
  JdPay::Result.new(Hash.from_xml(xml_str), options)
end

.pc_pay(params, options = {}) ⇒ Object

the difference between pc and h5 is just request url



18
19
20
# File 'lib/jd_pay/service.rb', line 18

def pc_pay(params, options = {})
  web_pay(params, PC_PAY_URL, options = {})
end

.qrcode_pay(params, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jd_pay/service.rb', line 61

def qrcode_pay(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
    currency: "CNY"
  }.merge(params)

  check_required_options(params, QRCODE_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(QRCODE_PAY_URL, make_payload(params), options)), options)
end

.query(params, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jd_pay/service.rb', line 116

def query(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeType: '0'
  }.merge(params)

  check_required_options(params, QUERY_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(QUERY_URL, make_payload(params), options)), options)
end

.refund(params, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jd_pay/service.rb', line 101

def refund(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
    currency: "CNY"
  }.merge(params)

  check_required_options(params, REFUND_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(REFUND_URL, make_payload(params), options)), options)
end

.revoke(params, options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jd_pay/service.rb', line 130

def revoke(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
    currency: "CNY"
  }.merge(params)

  check_required_options(params, REVOKE_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(REVOKE_URL, make_payload(params), options)), options)
end

.uniorder(params, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jd_pay/service.rb', line 46

def uniorder(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
    currency: "CNY"
  }.merge(params)

  check_required_options(params, UNIORDER_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(UNIORDER_URL, make_payload(params), options)), options)
end

.user_relation(params, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jd_pay/service.rb', line 76

def user_relation(params, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
  }.merge(params)

  check_required_options(params, USER_RELATION_REQUIRED_FIELDS)
  params[:sign] = JdPay::Sign.rsa_encrypt(JdPay::Util.to_xml(params), options)

  JdPay::Result.new(Hash.from_xml(invoke_remote(USER_RELATION_URL, make_payload(params), options)), options)
end

.web_pay(params, url, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jd_pay/service.rb', line 27

def web_pay(params, url, options = {})
  params = {
    version: "V2.0",
    merchant: options[:mch_id] || JdPay.mch_id,
    tradeTime: Time.now.strftime("%Y%m%d%H%M%S"),
    currency: "CNY"
  }.merge(params)

  check_required_options(params, WEB_PAY_REQUIRED_FIELDS)
  sign = JdPay::Sign.rsa_encrypt(JdPay::Util.to_uri(params), options)
  skip_encrypt_params = %i(version merchant)
  params.each do |k, v|
    params[k] = skip_encrypt_params.include?(k) ? v : JdPay::Des.encrypt_3des(v)
  end
  params[:sign] = sign
  JdPay::Util.build_pay_form(url, params)
end