module Alipass::Template

Public Class Methods

add(params) click to toggle source

REQUIRED_PARAMS = [

:method, :timestamp, :app_id, :version, :sign, :sign_type,
:tpl_content, :unique_id

]

# File lib/alipass/template.rb, line 10
def self.add(params)
  gateway = 'https://openapi.alipay.com/gateway.do'
  params = {
    method: 'alipay.pass.tpl.add',
    timestamp: Time.now.strftime('%F %T'),
    format: 'json',
    app_id: Alipass.app_id,
    version: '1.0',
    unique_id: SecureRandom.hex,
    sign_type: 'RSA'
  }.merge(params)

  params[:tpl_content].encode!('GBK', invalid: :replace, undef: :replace, replace: '')
  params[:sign] = Sign.generate(params)

  RestClient.post(gateway, params) do |response|
    JSON.parse(response.body)
  end
end