alipay-global

Unofficial gem for linking up to global.alipay for remote payment

Development of this gem is largely influenced by the unofficial alipay gem. Code structure has been replicated from here in some areas.

You can find the docs for global.alipay’s Cross Border Payment service here

Configuration

If you’re using MD5, the api_secret_key needs to be initialized. If RSA is used, the private_key_location needs to be initialized. DSA is currently not supported.

“by AlipayGlobal.api_partner_id = ‘YOUR_PID’ AlipayGlobal.api_secret_key = ‘YOUR_KEY’

AlipayGlobal.private_key_location = ‘YOUR PRIVATE KEY LOCATION’ #Your .pem file location

AlipayGlobal.sign_type = ‘MD5’ # Available values: MD5, RSA. Default is MD5

AlipayGlobal.debug_mode = true # Enable parameter check. Default is true.

Note

Operational Functions

FeatureDescription
AlipayGlobal::Service::Trade.create
AlipayGlobal::Service::Trade.refund
AlipayGlobal::Service::Notification.check
AlipayGlobal::Service::Reconciliation.request
AlipayGlobal::Service::Exchange.current_rates

Incomplete/ Untested Functions

FeatureDescription
AlipayGlobal::SignRSA
AlipayGlobal::SignDSA
AlipayGlobal::Service::Trade.batch_refundAwaiting verification and support from Alipay: Issue with signing for multipartforms

Usage

Service

Service::Trade

“by create

Definition

“by AlipayGlobal::Service::Trade.create(ARGUMENTS)

Issues (awaiting resolution)

  1. create
  2. RSA integration: User needs to work with Alipay team to exchange RSA public keys to properly test the content
  3. testing environment
  4. Alipay mobile site for testing is down. works in production.

Example

“by AlipayGlobal::Service::Trade.create( out_trade_no: ‘20150401000-0001’, subject: ‘Subject’, currency: ‘USD’, total_fee: ‘10.00’, notify_url: ‘https://example.com/orders/20150401000-0001/notify’ )

=> ‘https://mapi.alipay.com/gateway.do?service=create_forex_trade…’ #for production

ARGUMENTS

KeyRequirementData TypeDescription
mobileoptional (unique to this gem)booleantrue/false boolean value. Tells the gem whether you want to direct the user to Alipay’s mobile friendly site
out_trade_norequiredstringOrder number in your application. (Unique inside the partner system)
subjectrequiredstringThe name of the goods. Cannot contain special symbols
currencyrequiredstringThe settlement currency merchant named in contract. Refer to abbreviation of currencies
total_feerequired *floatA floating number ranging 0.01~1000000.00 If use the rmb_fee, don’t use the total_fee
notify_urlrequiredstringThe URL for receiving notifications after the payment process
rmb_feeoptionalfloat0.01~1000000.00 Use this parameter to replace total_fee if partner wish to price their product in RMB. If use the total_fee, don’t use the rmb_fee.
return_urloptionalstringAfter the payment transaction is done, the result is returned via the URL (only suitable for interfaces with directly returned results).
bodyoptionalstringA detailed description of the goods. Cannot contain special symbols
order_gmt_createoptionalstringYYYY-MM-DD HH:MM:SS please use China local time in order to sync with Alipay sustem, this parameter can only be used with order_valid_time together in order to control the valid time from redirect to login
order_valid_timeoptionalstringMaximum is 21600,unit is second,this parameter can only be used with order_gmt_create together in order to control the valid time from redirection to login
timeout_ruleoptionalstringOptions: 5m 10m 15m 30m 1h 2h 3h 5h 10h 12h. default is 12h. this parameter control the valid time from login to completion, please contact Alipay technical service if you want to enable this param
auth_tokenoptionalstringThe secure token from accessing express login API Mandatory for express login
supplieroptionalstringseller name shown in the cashier
seller_idoptional*stringFor PSG only The unique ID for PSG to identify a sub-merchant.
seller_nameoptional*stringFor PSG only. The name of sub-merchant who initiate the request.
seller_industryoptional*stringFor PSG only The industry type of sub-merchant who initiate the request.

* Normal Mechant: optional. FOR PSG: compulsory

*** Attention1:In the case a merchant wish to price their products in RMB, the merchant shall not change the value of the currency parameter, instead he shall just replace the total_fee with rmb_fee, and then his trade will appears in RMB in front of user.

*** Attention2:The request parameters can only be accepted by the Alipay system if they are signed according to the signature mechanism specified in this document.The parameter “timeout_rule”, the default value is 12h. If you want to use this parameter to change timing, you need to contact Alipay technical. Otherwise you will have an error.

“by status

Definition

“by AlipayGlobal::Service::Trade.status(ARGUMENTS)

Example

“by AlipayGlobal::Service::Trade.status( out_trade_no: “SAMPLE_TRANSACTION_ID” )

success => { success: true, message {

trade_no: “ALIPAYS_INTERNAL_TRADE_NO”, out_trade_no: “YOUR_TRADE_NO_USED”, subject: “SUBJECT CONTENT USED IN TRANSACTION”, trade_status: “WAIT_BUYER_PAY/TRADE_FINISHED/TRADE_CLOSED” } }

error =>

for production

ARGUMENTS

KeyRequirementData TypeDescription
out_trade_norequiredstringThe ID for the original transaction.

RESPONSE

Success response
Trade statusDescription
WAIT_BUYER_PAYThe buyer is expected to make the payment
TRADE_FINISHEDThe payment has been made, transaction closes.
TRADE_CLOSEDTransaction closed without payment.
Error response

Shown when there’s an issue with the payment uri created or if the transaction does not exist

Error CodeDescription
TRADE_NOT_EXISTThis trade number does not exist in Alipay’s transaction DB
ILLEGAL_ARGUMENTIllegal parameters
ILLEGAL_SIGNIllegal signature
HASH_NO_PRIVILEGENo sufficient rights to complete the query
ILLEGAL_SERVICEService Parameter is incorrect
ILLEGAL_PARTNERIncorrect Partner ID
ILLEGAL_SIGN_TYPESignature is of wrong type.
ILLEGAL_CHARSETIllegal charset

“by refund

Definition

“by AlipayGlobal::Service::Trade.refund(ARGUMENTS)

Example

“by AlipayGlobal::Service::Trade.refund( out_return_no: “SAMPLE_REFUND_ID”, out_trade_no: “SAMPLE_TRANSACTION_ID”, return_rmb_amount: 200.00, reason: “hello”, gmt_return: (Time.parse(“2015-03-20 12:00”).getlocal(“+08:00”)).strftime(“%Y%m%d%H%M%S”), currency: “USD” )

=> ‘https://mapi.alipay.net/gateway.do?service=forex_refund…’ #for production

ARGUMENTS

KeyRequirementData TypeDescription
out_return_norequiredstringThe new ID which created for refund.
out_trade_norequiredstringThe ID for the original transaction.
return_amountrequiredfloat
currencyrequiredstring
reasonrequiredstringReason for refund, out of supply e.g.
return_rmb_amountoptional*floatThis parameter related the RMB price function in the payment interface

* return_rmb_amount: if used, then leave return_amount empty

Service::Exchange

“by current_rates

Definition

“by AlipayGlobal::Service::Exchange.current_rates()

Example

“by AlipayGlobal::Service::Exchange.current_rates()

Results: hash with following content

=>

Results

Hash contains objects representing each currency. Each object is represented by it’s named currency code.

AttributeData TypeDescription
timeDateTimeWhen Alipay retrieved the date and time of this currency rate
ratefloatRate for 1 FOREX Unit : Units of RMB/CNY

Service::Notification

It is necessary for the partner system to verify the integrity and correctness of Alipay’snotification. Intheinterestofsystem’shealthiness,itisstrongly recommended that the partner system apply such verification mechanism.

In order to guarantee the interface will be used legally, the partner system can only verify the notifications within the last 1 minute (this configuration is subject to change, and such change will not be notified).

“by check

Definition

“by AlipayGlobal::Service::Notification.check({ notify_id: ‘your_notification_id_received’ })

Example

“by AlipayGlobal::Service::Notification.check({ notify_id: ‘your_notification_id_received’ })

Results: Alipay will respond with whether this notification is valid or not?

=> “false”

Parameters

KeyRequirementDescription
notify_idrequiredThe ID of Alipay system’s notification.

* Provided: provisioned by setting these environment variables from the start

Results

Notification verification output result list:

ResultDescription
invalidThe input parameter is invalid.
trueAuthentication passed.
falseAuthentication failed.

Service::Reconciliation

After certain period of time, the oversea merchant partner needs to verify the past transaction details in order to make sure the correctness and integrity of transaction records. This can be performed by using the reconciliation file downloading interface. The starting and end dates should be provided within a 10-day interval. Same-day transaction details cannot be accessed by this interface.

“by check

Definition

“by AlipayGlobal::Service::Reconciliation.request({ ‘start_date’=> ‘20120202’, ‘end_date’=> ‘20120205’ })

Parameters

KeyRequirementDescription
start_daterequiredThe start date of the reconciliation period, formatted as YYYYMMDD
end_daterequiredThe end date of the reconciliation period, formatted as YYYYMMDD

Example

“by AlipayGlobal::Service::Reconciliation.request({ ‘start_date’=> ‘20120202’, ‘end_date’=> ‘20120205’ })

Results: Alipay will respond with either the file’s array of results, false, or throw an ArgumentError?

=> “false”

Results

Failure Case 1 : No transactions found

“by AlipayGlobal::Service::Reconciliation.request(params)

Results: If no transaction records found

=> “false”

Failure Case 2 : Errors in arguments Argument Error with the following messages will be thrown

Error MessageDescription
File download failed:Over 10 days to Date periodThe supplied date range exceeds 10 days
\nFILLEGAL_PARTNERPartner id that is set does not exist in Alipay’s system/ or isn’t active in the production/testing environment yet
File download failed:Finish date ahead of begin dateInvalid date types supplied
File download failed:Illegal Date period!No date/ Invalid date data
File download failed:Finish date not ahead of todayDates supplied for reconciliations should be before current date (Alipay’s probably)

Untested Cases The following cases have yet to be tested

ErrorsDescription
Date format incorrect YYYYMMDDUnable to recreate scenario
System exception
Internet connected exception ,please try later

Success Case : Array containing the results of transactions in these periods.

“by AlipayGlobal::Service::Reconciliation.request(params)

Results: If no transaction records found

=> []

, , ,

]

Contributing

Feel free to fork this repo and make a PR.