diff --git a/doc/legacy_api.md b/doc/legacy_api.md index 6c2b7c4..7fc30be 100644 --- a/doc/legacy_api.md +++ b/doc/legacy_api.md @@ -539,6 +539,202 @@ notify_params = params.except(*request.path_parameters.keys) Alipay::Notify.verify?(notify_params, options = {}) ``` +### QR Code 生成二维码 + +#### Name + +```ruby +alipay.commerce.qrcode.create +``` + +#### Definition + +```ruby +Alipay::Service.create_merchant_qr_code({PARAMS}, {OPTIONS}) +``` + +#### Example + +```ruby +create_qr_code_params = { + biz_type: "OVERSEASHOPQRCODE", + biz_data: { + address: "No.278, Road YinCheng, Shanghai, China", + country_code: "CN", + currency: "USD", + secondary_merchant_id: "xxx001", + secondary_merchant_industry: "7011", + secondary_merchant_name: "xxx Store", + store_id: "0001", + store_name: "Apple store", + trans_currency: "USD" + } +} + +Alipay::Service.create_merchant_qr_code(create_qr_code_params) +# => 'https://mapi.alipay.com/gateway.do?service=alipay.commerce.qrcode.create...' +``` + +#### ARGUMENTS + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| notify_url | optional | Alipay asyn notify url. | +| biz_type | required | Business type that is defined by Alipay, this case is “OVERSEASHOPQRCODE” | +| biz_data | required | Business data. Format:JSON | + +#### BIZ_DATA ARGUMENTS (required) + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| secondary_merchant_industry | required | Business category code of the secondary merchant. | +| secondary_merchant_id | required | The unique ID assigned by the partner to identify a secondary merchant. | +| secondary_merchant_name | required | Registration legal name of the secondary merchant, shown in the Alipay Wallet and the reconciliation file to identify a secondary merchant. | +| store_id | required | The unique ID that is assigned by the partner to identify a store of a merchant. | +| store_name | required | The name of the store. | +| trans_currency | required | The pricing currency | +| currency | required | The currency to settle with the merchant. The default value is CNY. If the pricing currency is not CNY, then the settlement currency must be either CNY or the pricing currency. | +| country_code | required | The country code that consists of two letters (alpha-2 code) | +| address | required | The address of the store where the code is created. | + +This is not a complete list of arguments, please read official document: https://global.alipay.com/docs/ac/global/qrcode_create#biz_data + +### QR Code 修改二维码 + +#### Name + +```ruby +alipay.commerce.qrcode.modify +``` + +#### Definition + +```ruby +Alipay::Service.update_merchant_qr_code({PARAMS}, {OPTIONS}) +``` + +#### Example + +```ruby +update_qr_code_params = { + biz_type: "OVERSEASHOPQRCODE", + qrcode: "https://qr.alipay.com/baxxxxx", + biz_data: { + address: "No.278, Road YinCheng, Shanghai, China", + country_code: "CN", + currency: "USD", + secondary_merchant_id: "xxx001", + secondary_merchant_industry: "7011", + secondary_merchant_name: "xxx Store", + store_id: "0001", + store_name: "Apple store", + trans_currency: "USD" + } +} + +Alipay::Service.update_merchant_qr_code(update_qr_code_params) +# => 'https://mapi.alipay.com/gateway.do?service=alipay.commerce.qrcode.modify...' +``` + +#### ARGUMENTS + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| notify_url | optional | Alipay asyn notify url. | +| biz_type | required | Business type that is defined by Alipay, this case is “OVERSEASHOPQRCODE” | +| biz_data | required | Business data. Format:JSON | +| qrcode | required | The returned QR code value after the code is generated successfully. | + +#### BIZ_DATA ARGUMENTS (required) + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| secondary_merchant_industry | required | Business category code of the secondary merchant. | +| secondary_merchant_id | required | The unique ID assigned by the partner to identify a secondary merchant. | +| secondary_merchant_name | required | Registration legal name of the secondary merchant, shown in the Alipay Wallet and the reconciliation file to identify a secondary merchant. | +| store_id | required | The unique ID that is assigned by the partner to identify a store of a merchant. | +| store_name | required | The name of the store. | +| trans_currency | required | The pricing currency | +| currency | required | The currency to settle with the merchant. The default value is CNY. If the pricing currency is not CNY, then the settlement currency must be either CNY or the pricing currency. | +| country_code | required | The country code that consists of two letters (alpha-2 code) | +| address | required | The address of the store where the code is created. | + +This is not a complete list of arguments, please read official document: https://global.alipay.com/docs/ac/global/qrcode_modify#Qb0Hc + +### 境外线下交易查询接口 + +#### Name + +```ruby +alipay.acquire.overseas.query +``` + +#### Definition + +```ruby +Alipay::Service.acquirer_overseas_query({PARAMS}, {OPTIONS}) +``` + +#### Example + +```ruby +acquirer_overseas_query_params = { + partner_trans_id: "2010121000000002" +} + +Alipay::Service.acquirer_overseas_query(acquirer_overseas_query_params) +# => 'https://mapi.alipay.com/gateway.do?service=alipay.acquire.overseas.query...' +``` + +#### ARGUMENTS + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| partner_trans_id | required | The original partner transaction ID given in the payment request | +| alipay_trans_id | optional | The transaction ID assigned by Alipay for the partner's payment request, which follows a mapping relation with the partner field plus the partner_trans_id field. When both of the fields are specified, alipay_trans_id will be verified first. | + +Document: https://global.alipay.com/docs/ac/global/overseas_query + +### 境外线下单笔退款接口 + +#### Name + +```ruby +alipay.acquire.overseas.spot.refund +``` + +#### Definition + +```ruby +Alipay::Service.acquirer_overseas_spot_refund_url({PARAMS}, {OPTIONS}) +``` + +#### Example + +```ruby +acquirer_overseas_spot_refund_params = { + partner_trans_id: "2010121000000002", + partner_refund_id: "301012133000002", + currency: "USD", + refund_amount: "0.01" +} + +Alipay::Service.acquirer_overseas_spot_refund_url(acquirer_overseas_spot_refund_params) +# => 'https://mapi.alipay.com/gateway.do?service=alipay.acquire.overseas.spot.refund...' +``` + +#### ARGUMENTS + +| Key | Requirement | Description | +| --- | ----------- | ----------- | +| partner_trans_id | required | The original partner transaction ID given in the payment request | +| partner_refund_id | required | The refund order ID in the partner system. The value of partner_refund_id cannot be the same as that of partner_trans_id. The partner_refund_id field plus the partner field identifies a refund transaction. | +| currency | required | The currency of the refund amount. | +| refund_amount | required | Refund amount, which must be less than or equal to the original transaction amount or the left transaction amount if ever refunded. | +| is_sync | optional | Indicates that the refund request is processed synchronously or asynchronously with a value of Y or N. The default value is N, which means an asynchronous notification from Alipay is returned to the merchant if the merchant has set the value of the notify_url field when sending the refund request. If the value is set as Y, it means only a synchronous response is returned to the merchant. | + +This is not a complete list of arguments, please read official document: https://global.alipay.com/docs/ac/global/spot_refund#92fa0c95 + ## Mobile::Service ### 移动支付接口 diff --git a/lib/alipay/service.rb b/lib/alipay/service.rb index 21ca205..1789887 100644 --- a/lib/alipay/service.rb +++ b/lib/alipay/service.rb @@ -209,6 +209,92 @@ def self.create_forex_trade_wap_url(params, options = {}) request_uri(params, options).to_s end + 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 ) + 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 + + UPDATE_MERCHANT_QR_CODE_REQUIRED_PARAMS = %w( biz_type biz_data qr_code ) + 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 + + ACQUIRER_OVERSEAS_QUERY_REQUIRED_PARAMS = %w(partner_trans_id) + 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_REQUIRED_PARAMS = %w( partner_trans_id partner_refund_id refund_amount currency ) + 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 + def self.request_uri(params, options = {}) uri = URI(GATEWAY_URL) uri.query = URI.encode_www_form(sign_params(params, options)) diff --git a/test/alipay/service_test.rb b/test/alipay/service_test.rb index 26e538d..4d73062 100644 --- a/test/alipay/service_test.rb +++ b/test/alipay/service_test.rb @@ -262,4 +262,69 @@ def test_batch_trans_notify_url assert_equal 'https://mapi.alipay.com/gateway.do?service=batch_trans_notify&_input_charset=utf-8&partner=1000000000000000&pay_date=20080107¬ify_url=https%3A%2F%2Fexample.com%2Forders%2F20150401000-0001%2Fnotify&account_name=%E6%AF%9B%E6%AF%9B&detail_data=0315006%5Etestture0002%40126.com%5E%E5%B8%B8%E7%82%9C%E4%B9%B0%E5%AE%B6%5E1000.00%5Ehello&batch_no=20080107001&batch_num=1&batch_fee=1000.0&email=biz_932%40alitest.com&sign_type=MD5&sign=59c611607daafd1337e96b22404bd521', Alipay::Service.batch_trans_notify_url(options) end + + def test_create_merchant_qr_code + params = { + biz_type: "OVERSEASHOPQRCODE", + biz_data: { + address: "No.278, Road YinCheng, Shanghai, China", + country_code: "CN", + currency: "USD", + secondary_merchant_id: "xxx001", + secondary_merchant_industry: "7011", + secondary_merchant_name: "xxx Store", + store_id: "0001", + store_name: "Apple store", + trans_currency: "USD" + } + } + + current_time = Time.new(2023, 12, 12, 1, 1, 1) + Time.stub :now, current_time do + assert_equal 'https://mapi.alipay.com/gateway.do?service=alipay.commerce.qrcode.create&_input_charset=utf-8&partner=1000000000000000×tamp=2023-12-11+17%3A01%3A01&biz_data=%7B%22address%22%3A+%22No.278%2C+Road+YinCheng%2C+Shanghai%2C+China%22%2C%22country_code%22%3A+%22CN%22%2C%22currency%22%3A+%22USD%22%2C%22secondary_merchant_id%22%3A+%22xxx001%22%2C%22secondary_merchant_industry%22%3A+%227011%22%2C%22secondary_merchant_name%22%3A+%22xxx+Store%22%2C%22store_id%22%3A+%220001%22%2C%22store_name%22%3A+%22Apple+store%22%2C%22trans_currency%22%3A+%22USD%22%7D&biz_type=OVERSEASHOPQRCODE&sign_type=MD5&sign=fbfcc8c9316cc209c385eefcbceb2105', Alipay::Service.create_merchant_qr_code(params) + end + end + + def test_update_merchant_qr_code + params = { + biz_type: "OVERSEASHOPQRCODE", + qr_code: "https://qr.alipay.com/baxxxxx", + biz_data: { + address: "No.278, Road YinCheng, Shanghai, China", + country_code: "CN", + currency: "USD", + secondary_merchant_id: "xxx001", + secondary_merchant_industry: "7011", + secondary_merchant_name: "xxx Store", + store_id: "0001", + store_name: "Apple store", + trans_currency: "USD" + } + } + + current_time = Time.new(2023, 12, 12, 1, 1, 1) + Time.stub :now, current_time do + assert_equal 'https://mapi.alipay.com/gateway.do?service=alipay.commerce.qrcode.modify&_input_charset=utf-8&partner=1000000000000000×tamp=2023-12-11+17%3A01%3A01&biz_data=%7B%22address%22%3A+%22No.278%2C+Road+YinCheng%2C+Shanghai%2C+China%22%2C%22country_code%22%3A+%22CN%22%2C%22currency%22%3A+%22USD%22%2C%22secondary_merchant_id%22%3A+%22xxx001%22%2C%22secondary_merchant_industry%22%3A+%227011%22%2C%22secondary_merchant_name%22%3A+%22xxx+Store%22%2C%22store_id%22%3A+%220001%22%2C%22store_name%22%3A+%22Apple+store%22%2C%22trans_currency%22%3A+%22USD%22%7D&biz_type=OVERSEASHOPQRCODE&qr_code=https%3A%2F%2Fqr.alipay.com%2Fbaxxxxx&sign_type=MD5&sign=5a72863d023163147f2909e74b741e52', Alipay::Service.update_merchant_qr_code(params) + end + end + + def test_acquirer_overseas_query + params = { + partner_trans_id: "2010121000000002" + } + + assert_equal 'https://mapi.alipay.com/gateway.do?service=alipay.acquire.overseas.query&_input_charset=utf-8&partner=1000000000000000&partner_trans_id=2010121000000002&sign_type=MD5&sign=2a7f598bbb13d02f7de819ae689f80ba', Alipay::Service.acquirer_overseas_query(params) + end + + def test_acquirer_overseas_spot_refund_url + params = { + partner_trans_id: "2010121000000002", + partner_refund_id: "301012133000002", + currency: "USD", + refund_amount: "0.01", + is_sync: "Y" + } + + assert_equal 'https://mapi.alipay.com/gateway.do?service=alipay.acquire.overseas.spot.refund&_input_charset=utf-8&partner=1000000000000000&partner_trans_id=2010121000000002&partner_refund_id=301012133000002¤cy=USD&refund_amount=0.01&is_sync=Y&sign_type=MD5&sign=397685a0c6b2d71d0d1f374ddba331a0', Alipay::Service.acquirer_overseas_spot_refund_url(params) + end end