Module: Alipay::Service

Defined in:
lib/alipay/service.rb

Constant Summary collapse

GATEWAY_URL =
'https://mapi.alipay.com/gateway.do'
CREATE_PARTNER_TRADE_BY_BUYER_REQUIRED_PARAMS =
%w( out_trade_no subject logistics_type logistics_fee logistics_payment price quantity )
TRADE_CREATE_BY_BUYER_REQUIRED_PARAMS =
%w( out_trade_no subject logistics_type logistics_fee logistics_payment price quantity )
CREATE_DIRECT_PAY_BY_USER_REQUIRED_PARAMS =
%w( out_trade_no subject )
CREATE_DIRECT_PAY_BY_USER_WAP_REQUIRED_PARAMS =
%w( out_trade_no subject total_fee )
CREATE_REFUND_URL_REQUIRED_PARAMS =
%w( batch_no data notify_url )
CREATE_FOREX_SINGLE_REFUND_URL_REQUIRED_PARAMS =
%w( out_return_no out_trade_no return_amount currency reason )
SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_PARAMS =
%w( trade_no logistics_name )
SEND_GOODS_CONFIRM_BY_PLATFORM_OPTIONAL_PARAMS =
%w( transport_type create_transport_type )
CREATE_FOREX_TRADE_REQUIRED_PARAMS =
%w( notify_url subject out_trade_no currency )
CREATE_FOREX_TRADE_OPTIONAL_PARAMS =
%w( total_fee rmb_fee )
CLOSE_TRADE_REQUIRED_OPTIONAL_PARAMS =
%w( trade_no out_order_no )
SINGLE_TRADE_QUERY_OPTIONAL_PARAMS =
%w( trade_no out_trade_no )
BATCH_TRANS_NOTIFY_REQUIRED_PARAMS =
%w( notify_url account_name detail_data batch_no batch_num batch_fee email )
CREATE_FOREX_TRADE_WAP_REQUIRED_PARAMS =
%w( out_trade_no subject merchant_url currency )
CREATE_MERCHANT_QR_CODE_REQUIRED_PARAMS =
%w( biz_type biz_data )
CREATE_MERCHANT_QR_CODE_REQUIRED_BIZ_DATA_PARAMS =
%w( secondary_merchant_industry secondary_merchant_id secondary_merchant_name trans_currency currency )
UPDATE_MERCHANT_QR_CODE_REQUIRED_PARAMS =
%w( biz_type biz_data qr_code )
ACQUIRER_OVERSEAS_QUERY_REQUIRED_PARAMS =
%w(partner_trans_id)
ACQUIRER_OVERSEAS_SPOT_REFUND_REQUIRED_PARAMS =
%w( partner_trans_id partner_refund_id refund_amount currency )

Class Method Summary collapse

Class Method Details

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



172
173
174
175
176
177
178
179
180
# File 'lib/alipay/service.rb', line 172

def self.(params, options = {})
  params = {
    service: 'account.page.query',
    _input_charset: 'utf-8',
    partner: options[:pid] || Alipay.pid,
  }.merge(params)

  Net::HTTP.get(request_uri(params, options))
end

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



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/alipay/service.rb', line 271

def self.acquirer_overseas_query(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, ACQUIRER_OVERSEAS_QUERY_REQUIRED_PARAMS)

  params = {
    'service'        => 'alipay.acquire.overseas.query',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
  }.merge(params)

  request_uri(params, options).to_s
end

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



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/alipay/service.rb', line 285

def self.acquirer_overseas_spot_refund_url(params, options= {})
  params = Utils.stringify_keys(params)
  check_required_params(params, ACQUIRER_OVERSEAS_SPOT_REFUND_REQUIRED_PARAMS)

  params = {
    'service'        => 'alipay.acquire.overseas.spot.refund',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
  }.merge(params)

  request_uri(params, options).to_s
end

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



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/alipay/service.rb', line 183

def self.batch_trans_notify_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, BATCH_TRANS_NOTIFY_REQUIRED_PARAMS)

  params = {
    'service'        => 'batch_trans_notify',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
    'pay_date'       => Time.now.strftime("%Y%m%d")
  }.merge(params)

  request_uri(params, options).to_s
end

.check_optional_params(params, names) ⇒ Object



319
320
321
322
323
# File 'lib/alipay/service.rb', line 319

def self.check_optional_params(params, names)
  return if !Alipay.debug_mode?

  warn("Alipay Warn: must specify either #{names.join(' or ')}") if names.all? {|name| params[name].nil? }
end

.check_required_params(params, names) ⇒ Object



311
312
313
314
315
316
317
# File 'lib/alipay/service.rb', line 311

def self.check_required_params(params, names)
  return if !Alipay.debug_mode?

  names.each do |name|
    warn("Alipay Warn: missing required option: #{name}") unless params.has_key?(name)
  end
end

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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/alipay/service.rb', line 145

def self.close_trade(params, options = {})
  params = Utils.stringify_keys(params)
  check_optional_params(params, CLOSE_TRADE_REQUIRED_OPTIONAL_PARAMS)

  params = {
    'service'        => 'close_trade',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid
  }.merge(params)

  Net::HTTP.get(request_uri(params, options))
end

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/alipay/service.rb', line 38

def self.create_direct_pay_by_user_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_DIRECT_PAY_BY_USER_REQUIRED_PARAMS)

  if Alipay.debug_mode? and params['total_fee'].nil? and (params['price'].nil? || params['quantity'].nil?)
    warn("Alipay Warn: total_fee or (price && quantity) must be set")
  end

  params = {
    'service'        => 'create_direct_pay_by_user',
    '_input_charset' => 'utf-8',
    'partner'        => options[:partner]   || options[:pid] || Alipay.pid,
    'seller_id'      => options[:seller_id] || options[:pid] || Alipay.pid,
    'payment_type'   => '1'
  }.merge(params)

  request_uri(params, options).to_s
end

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/alipay/service.rb', line 58

def self.create_direct_pay_by_user_wap_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_DIRECT_PAY_BY_USER_WAP_REQUIRED_PARAMS)

  params = {
    'service'        => 'alipay.wap.create.direct.pay.by.user',
    '_input_charset' => 'utf-8',
    'partner'        => options[:partner]   || options[:pid] || Alipay.pid,
    'seller_id'      => options[:seller_id] || options[:pid] || Alipay.pid,
    'payment_type'   => '1'
  }.merge(params)

  request_uri(params, options).to_s
end

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



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

def self.create_forex_trade_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_FOREX_TRADE_REQUIRED_PARAMS)
  check_optional_params(params, CREATE_FOREX_TRADE_OPTIONAL_PARAMS)

  params = {
    'service'         => 'create_forex_trade',
    '_input_charset'  => 'utf-8',
    'partner'         => options[:pid] || Alipay.pid,
  }.merge(params)

  request_uri(params, options).to_s
end

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



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/alipay/service.rb', line 198

def self.create_forex_trade_wap_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_FOREX_TRADE_WAP_REQUIRED_PARAMS)

  params = {
    'service'        => 'create_forex_trade_wap',
    '_input_charset' => 'utf-8',
    'partner'        => options[:partner]   || options[:pid] || Alipay.pid,
    'seller_id'      => options[:seller_id] || options[:pid] || Alipay.pid,
  }.merge(params)

  request_uri(params, options).to_s
end

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/alipay/service.rb', line 214

def self.create_merchant_qr_code(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_MERCHANT_QR_CODE_REQUIRED_PARAMS)
  biz_data = nil

  if params['biz_data']
    params['biz_data'] = Utils.stringify_keys(params['biz_data'])
    check_required_params(params['biz_data'], CREATE_MERCHANT_QR_CODE_REQUIRED_BIZ_DATA_PARAMS)

    data = params.delete('biz_data')
    biz_data = data.map do |key, value|
      "\"#{key}\": \"#{value}\""
    end.join(',')
  end

  biz_data = "{#{biz_data}}"

  params = {
    'service'        => 'alipay.commerce.qrcode.create',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
    'timestamp'      => Time.now.utc.strftime('%Y-%m-%d %H:%M:%S').to_s,
    'biz_data'       => biz_data
  }.merge(params)

  request_uri(params, options).to_s
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/alipay/service.rb', line 6

def self.create_partner_trade_by_buyer_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_PARTNER_TRADE_BY_BUYER_REQUIRED_PARAMS)

  params = {
    'service'        => 'create_partner_trade_by_buyer',
    '_input_charset' => 'utf-8',
    'partner'        => options[:partner]   || options[:pid] || Alipay.pid,
    'seller_id'      => options[:seller_id] || options[:pid] || Alipay.pid,
    'payment_type'   => '1'
  }.merge(params)

  request_uri(params, options).to_s
end

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



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/alipay/service.rb', line 98

def self.forex_refund_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_FOREX_SINGLE_REFUND_URL_REQUIRED_PARAMS)

  params = {
    'service'        => 'forex_refund',
    'partner'        => options[:pid] || Alipay.pid,
    '_input_charset' => 'utf-8',
    'gmt_return'     => Time.now.getlocal('+08:00').strftime('%Y%m%d%H%M%S')
  }.merge(params)

  request_uri(params, options).to_s
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/alipay/service.rb', line 74

def self.refund_fastpay_by_platform_pwd_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, CREATE_REFUND_URL_REQUIRED_PARAMS)

  data = params.delete('data')
  detail_data = data.map do|item|
    item = Utils.stringify_keys(item)
    "#{item['trade_no']}^#{item['amount']}^#{item['reason']}"
  end.join('#')

  params = {
    'service'        => 'refund_fastpay_by_platform_pwd',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
    'seller_user_id' => options[:pid] || Alipay.pid,
    'refund_date'    => Time.now.strftime('%Y-%m-%d %H:%M:%S'),
    'batch_num'      => data.size,
    'detail_data'    => detail_data
  }.merge(params)

  request_uri(params, options).to_s
end

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



298
299
300
301
302
# File 'lib/alipay/service.rb', line 298

def self.request_uri(params, options = {})
  uri = URI(GATEWAY_URL)
  uri.query = URI.encode_www_form(sign_params(params, options))
  uri
end

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



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

def self.send_goods_confirm_by_platform(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_PARAMS)
  check_optional_params(params, SEND_GOODS_CONFIRM_BY_PLATFORM_OPTIONAL_PARAMS)

  params = {
    'service'        => 'send_goods_confirm_by_platform',
    'partner'        => options[:pid] || Alipay.pid,
    '_input_charset' => 'utf-8'
  }.merge(params)

  Net::HTTP.get(request_uri(params, options))
end

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



304
305
306
307
308
309
# File 'lib/alipay/service.rb', line 304

def self.sign_params(params, options = {})
  params.merge(
    'sign_type' => (options[:sign_type] || Alipay.sign_type),
    'sign'      => Alipay::Sign.generate(params, options)
  )
end

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



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/alipay/service.rb', line 159

def self.single_trade_query(params, options = {})
  params = Utils.stringify_keys(params)
  check_optional_params(params, SINGLE_TRADE_QUERY_OPTIONAL_PARAMS)

  params =   {
    "service"         => 'single_trade_query',
    "_input_charset"  => "utf-8",
    "partner"         => options[:pid] || Alipay.pid,
  }.merge(params)

  Net::HTTP.get(request_uri(params, options))
end

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alipay/service.rb', line 22

def self.trade_create_by_buyer_url(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, TRADE_CREATE_BY_BUYER_REQUIRED_PARAMS)

  params = {
    'service'        => 'trade_create_by_buyer',
    '_input_charset' => 'utf-8',
    'partner'        => options[:partner]   || options[:pid] || Alipay.pid,
    'seller_id'      => options[:seller_id] || options[:pid] || Alipay.pid,
    'payment_type'   => '1'
  }.merge(params)

  request_uri(params, options).to_s
end

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/alipay/service.rb', line 243

def self.update_merchant_qr_code(params, options = {})
  params = Utils.stringify_keys(params)
  check_required_params(params, UPDATE_MERCHANT_QR_CODE_REQUIRED_PARAMS)
  biz_data = nil

  if params['biz_data']
    params['biz_data'] = Utils.stringify_keys(params['biz_data'])

    data = params.delete('biz_data')
    biz_data = data.map do |key, value|
      "\"#{key}\": \"#{value}\""
    end.join(',')
  end

  biz_data = "{#{biz_data}}"

  params = {
    'service'        => 'alipay.commerce.qrcode.modify',
    '_input_charset' => 'utf-8',
    'partner'        => options[:pid] || Alipay.pid,
    'timestamp'      => Time.now.utc.strftime('%Y-%m-%d %H:%M:%S').to_s,
    'biz_data'       => biz_data
  }.merge(params)

  request_uri(params, options).to_s
end