alipay 0.15.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,571 @@
1
+ Quick Start Guide
2
+ =================
3
+
4
+ ### [中文](quick_start_cn.md)
5
+
6
+ ## Table of Contents
7
+ * [Client Setup](#client-setup)
8
+ * [Ruby](#ruby)
9
+ * [Ruby on Rails](#ruby-on-rails)
10
+ * [Create Payment](#create-payment)
11
+ * [Desktop](#desktop)
12
+ * [Mobile](#mobile)
13
+ * [QR Code](#qr-code)
14
+ * [Installment Plan](#installment-plan)
15
+ * [Verify Callback](#verify-callback)
16
+ * [Query Payment Status](#query-payment-status)
17
+ * [Stop Payment](#stop-payment)
18
+ * [Close Payment](#close-payment)
19
+ * [Cancel Payment](#cancel-payment)
20
+ * [Refund Payment](#refund-payment)
21
+ * [Initiate Refund](#initiate-refund)
22
+ * [Query Refund Status](#query-refund-status)
23
+ * [Fund Transfer](#fund-transfer)
24
+ * [Transfer Fund to Customer](#transfer-fund-to-customer)
25
+ * [Query Fund Transfer](#query-fund-transfer)
26
+
27
+ ## Client Setup
28
+
29
+ **All examples in the quick start guide assume you have properly setup the client.**
30
+
31
+ ### Ruby
32
+ ```ruby
33
+ # put your Alipay credentials here
34
+ API_URL: 'https://openapi.alipaydev.com/gateway.do'
35
+ APP_ID: '2016000000000000'
36
+ APP_PRIVATE_KEY: "-----BEGIN RSA PRIVATE KEY-----\nxkbt...4Wt7tl\n-----END RSA PRIVATE KEY-----\n"
37
+ ALIPAY_PUBLIC_KEY: "-----BEGIN PUBLIC KEY-----\nTq43T5...OVUAQb3R\n-----END PUBLIC KEY-----\n"
38
+
39
+ # set up a client to talk to the Alipay API
40
+ @client = Alipay::Client.new(
41
+ url: API_URL,
42
+ app_id: APP_ID,
43
+ app_private_key: APP_PRIVATE_KEY,
44
+ alipay_public_key: ALIPAY_PUBLIC_KEY
45
+ )
46
+ ```
47
+
48
+ ### Ruby on Rails
49
+ You can save your Alipay credentials as environment variables or set them up using initializer.
50
+
51
+ This guide will demonstrate setting up the Alipay client with the `dotenv-rails` gem.
52
+
53
+ In your Gemfile
54
+ ```ruby
55
+ # Gemfile
56
+ gem 'alipay', '~> 0.15.0'
57
+ gem 'dotenv-rails', '~> 2.2', '>= 2.2.1'
58
+ ```
59
+ Then run
60
+ ```bash
61
+ $ bundle install
62
+ ```
63
+
64
+ Create an .env file
65
+ ```ruby
66
+ # .env
67
+ APP_ID=2016000000000000
68
+ ALIPAY_API=https://openapi.alipaydev.com/gateway.do
69
+ APP_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nxkbt...4Wt7tl\n-----END RSA PRIVATE KEY-----\n"
70
+ ALIPAY_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nTq43T5...OVUAQb3R\n-----END PUBLIC KEY-----\n"
71
+ ```
72
+
73
+ In your Ruby on Rails application, you can create an Alipay client instance like this:
74
+ ```ruby
75
+ @client = Alipay::Client.new(
76
+ url: ENV['ALIPAY_API'],
77
+ app_id: ENV['APP_ID'],
78
+ app_private_key: ENV['APP_PRIVATE_KEY'],
79
+ alipay_public_key: ENV['ALIPAY_PUBLIC_KEY']
80
+ )
81
+ ```
82
+
83
+ ## Create Payment
84
+
85
+ ### Desktop
86
+ #### API Method
87
+ ```
88
+ alipay.trade.page.pay
89
+ ```
90
+ This API method is for creating payment transaction that is suitable for customers using various desktop (PC) environment.
91
+
92
+ #### Client Method
93
+ ```ruby
94
+ Alipay::Client.page_execute_url
95
+ ```
96
+ This client method will generate a payment URL for redirecting customers to.
97
+
98
+ #### Example
99
+ ```ruby
100
+ @client.page_execute_url(
101
+ method: 'alipay.trade.page.pay',
102
+ return_url: 'https://mystore.com/orders/20160401000000/return',
103
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
104
+ biz_content: {
105
+ out_trade_no: '20160401000000',
106
+ product_code: 'FAST_INSTANT_TRADE_PAY',
107
+ total_amount: '0.01',
108
+ subject: 'Example #123'
109
+ }.to_json(ascii_only: true)
110
+ )
111
+ # => 'https://openapi.alipaydev.com/gateway.do?app_id=2016...'
112
+ ```
113
+
114
+ #### Notable Parameters
115
+ * `return_url` is where Alipay will redirect customers to after a successful payment is made. The redirect
116
+ will be sent via a `GET` request. This is an *optional* parameter.
117
+ * `notify_url` is where Alipay will send async callbacks to via `POST` request after a successful
118
+ payment. This is an *optional* parameter.
119
+ * `out_trade_no` is a unique string set by you. It is expected to be in reference to a model ID
120
+ in your application. Although it is not a strict requirement.
121
+ * `total_amount` should be in decimal form with a maximum scale of 2. For example `5123.99` will
122
+ be ¥5,123.99 in text form. `5123.999` is not a valid parameter.
123
+
124
+ > For a complete list of the available parameters, please refer to the [API documentation](https://docs.open.alipay.com/270/alipay.trade.page.pay/).
125
+
126
+
127
+ ### Mobile
128
+ #### API Method
129
+ ```
130
+ alipay.trade.wap.pay
131
+ ```
132
+ This API method is for creating payment transaction for customers on a mobile device. It has the ability to transfer the payment process over to the native Alipay application if the application is installed on the customer's device. If not, the customer will be redirected to a mobile HTML version of the payment page.
133
+
134
+ #### Client Method
135
+ ```ruby
136
+ Alipay::Client.page_execute_url
137
+ ```
138
+ This method will generate a payment URL for redirecting customers to.
139
+
140
+ #### Example
141
+ ```ruby
142
+ @client.page_execute_url(
143
+ method: 'alipay.trade.wap.pay',
144
+ return_url: 'https://mystore.com/orders/20160401000000/return',
145
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
146
+ biz_content: {
147
+ out_trade_no: '20160401000000',
148
+ product_code: 'QUICK_WAP_WAY',
149
+ total_amount: '0.01',
150
+ subject: 'Example: 456'
151
+ quit_url: 'https://mystore.com/orders/20160401000000/'
152
+ }.to_json(ascii_only: true)
153
+ )
154
+ # => 'https://openapi.alipaydev.com/gateway.do?app_id=2016...'
155
+ ```
156
+ #### Notebale Parameters
157
+ * `quit_url` is where Alipay will redirect customer to if the customer have completed the payment or have exited the payment process. This redirect only applies to the mobile html verison. This is an *optional* parameter.
158
+
159
+ > For a complete list of the available parameters, please refer to the
160
+ [API documentation](https://docs.open.alipay.com/203/107090/).
161
+
162
+ ### QR Code
163
+ #### API Method
164
+ ```
165
+ alipay.trade.precreate
166
+ ```
167
+ This API method generates a payment URL that can be transformed into a QR code for customers to scan. A callback will be issued to the defined `notify_url` if payment is successful.
168
+
169
+ #### Client Method
170
+ ```ruby
171
+ Alipay::Client.execute
172
+ ```
173
+
174
+ #### Example
175
+ ```ruby
176
+ # Create a QR code based payment
177
+ response = @client.execute(
178
+ method: 'alipay.trade.precreate',
179
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
180
+ biz_content: {
181
+ out_trade_no: '20160401000000',
182
+ total_amount: '50.00',
183
+ subject: 'QR Code Test'
184
+ }.to_json(ascii_only: true)
185
+ )
186
+ # => '{\"alipay_trade_precreate_response\":{\"code\"...'
187
+
188
+ # Get payment url to render as QR code for customer
189
+ qr_code = JSON.parse(response)["alipay_trade_precreate_response"]["qr_code"]
190
+ # => 'https://qr.alipay.com/abcdefggfedcba'
191
+ ```
192
+
193
+ > For a complete list of the available parameters, please refer to the [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.precreate).
194
+
195
+ ### Installment Plan
196
+ #### API Methods
197
+ ```
198
+ alipay.trade.page.pay (Desktop / PC)
199
+ alipay.trade.wap.pay (Mobile)
200
+ alipay.trade.precreate (QR Code)
201
+ ```
202
+
203
+ #### Client Method
204
+ ```ruby
205
+ Alipay::Client.page_execute_url (Desktop / PC / Mobile)
206
+ Alipay::Client.execute (QR Code)
207
+ ```
208
+
209
+ #### Example
210
+ Scenario: Customer pre-select a six-installment payment plan before going through the payment process at Alipay.
211
+
212
+ ```ruby
213
+ @client.page_execute_url(
214
+ method: 'alipay.trade.page.pay',
215
+ return_url: 'https://mystore.com/orders/20160401000000/return',
216
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
217
+ biz_content: {
218
+ out_trade_no: '20160401000000',
219
+ product_code: 'FAST_INSTANT_TRADE_PAY',
220
+ total_amount: '0.01',
221
+ subject: 'Example #654',
222
+ enable_pay_channels: 'balance,pcreditpayInstallment',
223
+ extend_params: {
224
+ hb_fq_num: '6'',
225
+ hb_fq_seller_percent: '0'
226
+ }
227
+ }.to_json(ascii_only: true)
228
+ )
229
+ ```
230
+ Scenario: Customer select an installment plan or their choice at Alipay's payment page.
231
+ ```ruby
232
+ @client.page_execute_url(
233
+ method: 'alipay.trade.page.pay',
234
+ return_url: 'https://mystore.com/orders/20160401000000/return',
235
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
236
+ biz_content: {
237
+ out_trade_no: '20160401000000',
238
+ product_code: 'FAST_INSTANT_TRADE_PAY',
239
+ total_amount: '0.01',
240
+ subject: 'Example #654',
241
+ enable_pay_channels: 'balance,pcreditpayInstallment',
242
+ }.to_json(ascii_only: true)
243
+ )
244
+ ```
245
+ #### Notebale Parameters
246
+ * `enable_pay_channels` defines the funding methods that the customer can use to
247
+ pay for your order. `pcreditpayInstallment` is the value to enable installment plans. Mutiple
248
+ values can be enabled by sperating the values by `,` like the example above.
249
+ * `hb_fq_num` defines the number of installments. Valid values are `3`, `6`, or `12`.
250
+ * `hb_fq_seller_percent` defines the percentage of credit installment fee the seller is
251
+ responsiable for. Valid value is `0` where the customer bare all the fees. Another valid value is
252
+ `100` where you bare all the fees. However it only becomes valid if you have a special contract
253
+ signed with Alipay.
254
+
255
+ > For a complete list of the available parameters, please refer to the [API documentation](https://docs.open.alipay.com/277/106748/).
256
+
257
+
258
+ ### Verify Callback
259
+ #### Client Method
260
+ ```
261
+ Alipay::Client.verify?
262
+ ```
263
+ This client method will verify the validity of response params send by Alipay using Alipay's
264
+ public key.
265
+
266
+ #### Example
267
+ ```ruby
268
+ params = {
269
+ out_trade_no: '20160401000000',
270
+ trade_status: 'TRADE_SUCCESS'
271
+ sign_type: 'RSA2',
272
+ sign: '...'
273
+ }
274
+ @client.verify?(params)
275
+ # => true / false
276
+ ```
277
+
278
+ ##### Verify GET return_url
279
+ Alipay will send your customer via GET request to your specified `return_url`, if it was previously
280
+ defined during the payment creation process. Here is an example of how to verify response params
281
+ from Alipay in your controller.
282
+
283
+ ```ruby
284
+ @client.verify?(request.query_parameters)
285
+ # => true / false
286
+ ```
287
+
288
+ ##### Verify POST notify_url
289
+ Alipay will send POST request to your specified `notify_url`, if it was previously defined during
290
+ the payment creation process. Your controller action should response 'success' in plain text or
291
+ Alipay will keep on sending POST requests in increasing intervals.
292
+
293
+ ```ruby
294
+ if @client.verify?(request.request_parameters)
295
+ render plain: 'success'
296
+ end
297
+ ```
298
+
299
+ ### Query Payment Status
300
+ #### API Method
301
+ ```
302
+ alipay.trade.query
303
+ ```
304
+ This API method is for querying payment status after the payment is created. It suitable for
305
+ situations when callbacks fail or when the created payment did not set callback parameters.
306
+
307
+ #### Client Method
308
+ ```ruby
309
+ Alipay::Client.execute
310
+ ```
311
+
312
+ #### Example
313
+ ```ruby
314
+ response = @client.execute(
315
+ method: 'alipay.trade.query',
316
+ biz_content: {
317
+ trade_no: '2013112611001004680073956707',
318
+ }.to_json(ascii_only: true)
319
+ )
320
+ # => '{\"alipay_trade_query_response\":{\"code\"...'
321
+
322
+ # Get payment status
323
+ result_status = JSON.parse(response)["alipay_trade_query_response"]["trade_status"]
324
+ # => 'TRADE_SUCCESS'
325
+ ```
326
+
327
+ Notable Parameters
328
+ * `trade_no` is the payment identification string provided by Alipay via callback after the payment is
329
+ created. If you do not have this on hand, you can provide the `out_trade_no` instead.
330
+
331
+ > For a complete list of the available parameters, please refer to the
332
+ [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.query).
333
+
334
+ ## Stop Payment
335
+ ### Close Payment
336
+ #### API Method
337
+ ```
338
+ alipay.trade.close
339
+ ```
340
+ This API method is for closing inactive payment if you don't want to wait for Alipay to close
341
+ out the payment at its default expiration time.
342
+
343
+ #### Client Method
344
+ ```ruby
345
+ Alipay::Client.execute
346
+ ```
347
+
348
+ #### Example
349
+ ```ruby
350
+ response = @client.execute(
351
+ method: 'alipay.trade.close',
352
+ notify_url: 'https://mystore.com/orders/20160401000000/notify',
353
+ biz_content: {
354
+ trade_no: '2013112611001004680073956707',
355
+ }.to_json(ascii_only: true)
356
+ )
357
+ # => '{\"alipay_trade_close_response\":{\"code\"...'
358
+
359
+ # Get request result
360
+ result_code = JSON.parse(response)["alipay_trade_close_response"]["code"]
361
+ # => '10000'
362
+ ```
363
+
364
+ #### Notable Parameters
365
+ * `trade_no` is the payment identification that Alipay returned to you after the payment was
366
+ pre-created. If you do not have this parameter on hand, you can stub in the 'out_trade_no' that you
367
+ used using the payment creation process.
368
+
369
+ > For a complete list of the available parameters, please refer to the
370
+ [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.close).
371
+
372
+ ### Cancel Payment
373
+ #### API Method
374
+ ```
375
+ alipay.trade.cancel
376
+ ```
377
+ This API method is for canceling stuck payment or payment that errors out due to either Alipay
378
+ or your own application. If the customer has successfully made the payment. A refund will be made.
379
+ If customers had not pay yet, the payment transaction will be close. You can check if a refund
380
+ had been made by getting the `action` value from the request response.
381
+
382
+ #### Client Method
383
+ ```ruby
384
+ Alipay::Client.execute
385
+ ```
386
+
387
+ #### Example
388
+ ```ruby
389
+ response = @client.execute(
390
+ method: 'alipay.trade.cancel',
391
+ biz_content: {
392
+ out_trade_no: '20160401000000',
393
+ }.to_json(ascii_only: true)
394
+ )
395
+ # => '{\"alipay_trade_cancel_response\":{\"code\"...'
396
+
397
+ # Get cancellation result
398
+ result_action = JSON.parse(response)["alipay_trade_cancel_response"]["action"]
399
+ # => 'close'
400
+ ```
401
+
402
+ #### Notable Parameters
403
+ * `out_trade_no` is a unique string that you provided during the payment creation process. You must
404
+ provide either `out_trade_no` or `trade_no` in order for the API call to be successful.
405
+
406
+ > For a complete list of the available parameters, please refer to the
407
+ [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.cancel/).
408
+
409
+ ## Refund Payment
410
+
411
+ ### Initiate Refund
412
+ #### API Method
413
+ ```
414
+ alipay.trade.refund
415
+ ```
416
+ This API method is for initiating refunds to customers. Only made this API call if the payment has not passed the refund period defined by Alipay. Multiple or partial refunds are also available through this API.
417
+
418
+ #### Client Method
419
+ ```ruby
420
+ Alipay::Client.execute
421
+ ```
422
+
423
+ #### Example
424
+ Secenario: Customer request refund on a ¥10.12 item on a ¥210.85 order(payment).
425
+ ```ruby
426
+ response = @client.execute(
427
+ method: 'alipay.trade.refund',
428
+ biz_content: {
429
+ out_trade_no: '6c50789a0610',
430
+ out_request_no: '6c50789a0610-1',
431
+ refund_amount: '10.12'
432
+ }.to_json(ascii_only: true)
433
+ )
434
+ # => '{\"alipay_trade_refund_response\":{\"code\"...'
435
+
436
+ # Get result
437
+ result_code = JSON.parse(response)["alipay_trade_refund_response"]["code"]
438
+ # => '10000'
439
+
440
+ result_fund_change = JSON.parse(response)["alipay_trade_refund_response"]["fund_change"]
441
+ # => 'Y'
442
+ ```
443
+
444
+ #### Notable Parameters
445
+ * `out_request_no` is an *conditional optional* parameter. It is *required* when you make a partial
446
+ refund or plan to make multiple refunds. It serves as identification for the refund transaction.
447
+ It is *optional* when you refund the entire payment amount.
448
+ * `refund_amount` is the amount you wish to refund to the customer. It can be lower than the original
449
+ payment amount. However, if you have previously issued partial refunds on a payment, the sum
450
+ of `refund_amount` cannot exceed the original payment amount.
451
+
452
+ > For a complete list of the available parameters, please refer to the
453
+ [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.refund/).
454
+
455
+ ### Query Refund Status
456
+ #### API Method
457
+ ```
458
+ alipay.trade.fastpay.refund.query
459
+ ```
460
+ This API method is for querying refund status.
461
+
462
+ #### Client Method
463
+ ```ruby
464
+ Alipay::Client.execute
465
+ ```
466
+
467
+ #### Example
468
+ ```ruby
469
+ response = @client.execute(
470
+ method: 'alipay.trade.fastpay.refund.query',
471
+ biz_content: {
472
+ out_trade_no: '6c50789a0610',
473
+ out_request_no: '6c50789a0610-1'
474
+ }.to_json(ascii_only: true)
475
+ )
476
+ # => '{\"alipay_trade_fastpay_refund_query_response\":{\"code\"...'
477
+
478
+ # Get refund amount
479
+ result_refund_amount = JSON.parse(response)["alipay_trade_fastpay_refund_query_response"]["refund_amount"]
480
+ # => '10.12'
481
+ ```
482
+
483
+ #### Notable Parameters
484
+ * `out_request_no` is the identifying string provided by you provided when you initiated the refund.
485
+ If you did not provide this parameter when the refund was initiated, use the `out_trade_no` as your
486
+ `out_request_no`.
487
+
488
+ > For a complete list of the available parameters, please refer to the
489
+ [API documentation](https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query/).
490
+
491
+
492
+ ## Fund Transfer
493
+ ### Transfer Fund to Customer
494
+ #### API Method
495
+ ```
496
+ alipay.fund.trans.toaccount.transfer
497
+ ```
498
+ This API method is for creating a fund transfer to customers from your Alipay account.
499
+
500
+ #### Client Method
501
+ ```
502
+ Alipay::Client.execute
503
+ ```
504
+
505
+ #### Example
506
+ ```ruby
507
+ response = @client.execute(
508
+ method: 'alipay.fund.trans.toaccount.transfer',
509
+ biz_content: {
510
+ out_biz_no: '3142321423432',
511
+ payee_type: 'ALIPAY_LOGONID',
512
+ payee_account: 'customer@example.com',
513
+ amount: '12.23'
514
+ }.to_json(ascii_only: true)
515
+ )
516
+ # => '{\"alipay_fund_trans_toaccount_transfer_response\":{\"code\"...'
517
+
518
+ # Get order ID
519
+ result_order_id = JSON.parse(response)["alipay_fund_trans_toaccount_transfer_response"]["order_id"]
520
+ # => '20160627110070001502260006780837'
521
+ ```
522
+
523
+ #### Notable Parameters
524
+ * `out_biz_no` is a unique identifying string provided by you for reference purposes.
525
+ * `payee_type` is for defining the type of `payee_account` that is provided. Valid values
526
+ are `ALIPAY_USERID` for customer's Alipay user ID starting with `2088` and `ALIPAY_LOGONID` for
527
+ customer cellular number or email address.
528
+ * `payee_account` is the customer Alipay ID, email or cellular number. It must match the `payee_type`
529
+ provided.
530
+ * `amount` is the amount you wish to transfer to the customer. (e.g. ¥123.50 is `123.50`)
531
+
532
+ > For a complete list of the available parameters, please refer to the
533
+ [API documentation](https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer).
534
+
535
+ ### Query Fund Transfer
536
+ #### API Method
537
+ ```
538
+ alipay.fund.trans.order.query
539
+ ```
540
+ This API method is for querying fund transfer status.
541
+
542
+ #### Client Method
543
+ ```
544
+ Alipay::Client.execute
545
+ ```
546
+
547
+ #### Example
548
+ ```ruby
549
+ response = @client.execute(
550
+ method: 'alipay.fund.trans.order.query',
551
+ biz_content: {
552
+ out_biz_no: '3142321423432',
553
+ }.to_json(ascii_only: true)
554
+ )
555
+ # => '{\"alipay_fund_trans_order_query_response\":{\"code\"...'
556
+
557
+ # Get refund_status
558
+ refund_status = JSON.parse(response)["alipay_fund_trans_order_query_response"]["status"]
559
+ # => 'SUCCESS'
560
+
561
+ # Get expected refund date
562
+ refund_status = JSON.parse(response)["alipay_fund_trans_order_query_response"]["arrival_time_end"]
563
+ # => '2018-01-01 08:08:08'
564
+ ```
565
+
566
+ #### Notable Parameters
567
+ * 'order_id' is the order id provided by Alipay when the transfer request was created. If you
568
+ do not have this on hand, you can stub in `out_biz_no` instead.
569
+
570
+ > For a complete list of the available parameters, please refer to the
571
+ [API documentation](https://docs.open.alipay.com/api_28/alipay.fund.trans.order.query/).