Class: Jieshun::Parking::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jieshun/parking/client.rb

Overview

捷顺订单 API Client, 对应文档为 捷慧通云平台接口规范_2.1.doc 和 捷慧通云平台接口标准协议_http_V3.0.1.doc

Constant Summary collapse

REDIS_AUTH_KEY =
'jieshun.parking.auth.token'
@@max_notify_try_times =
10

Instance Method Summary collapse

Constructor Details

#initialize(configuration, http_client = StandardHttpClient.new) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/jieshun/parking/client.rb', line 11

def initialize(configuration, http_client = StandardHttpClient.new)
	@configuration = configuration
	@http_client = http_client
	@redis_client = RedisClient.new(@configuration.redis_config)
end

Instance Method Details

#create_order_by_car_no(park_code, car_no) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jieshun/parking/client.rb', line 43

def create_order_by_car_no(park_code, car_no)
	payload = {
			"serviceId": "3c.pay.createorderbycarno",
			"requestType": "DATA",
			"attributes": {
					"businesserCode": @configuration.businesser_code,
					"parkCode": park_code,
					"orderType": "VNP",
					"carNo": car_no
			}
	}.to_json
	unify_call(payload) do |result|
		# 0:正常,正常订单
		# 1:安装验证失败,前端设备异常
		# 2:未入场
		# 5:非临时卡
		# 6:未设置收费标准,前端停车场异常
		# 9:已缴费,超时滞留时间内
		# 10:正常免费时间段内
		# 11:打折免费时间段内
		# 12:打折全免时间段内
		# 13:打折减免时间段内
		# 20:超时收费不能使用卡券
		# 3 1:已入场无需缴费
		# 9999:其它未知错误
		attributes = result.body['dataItems'].first['attributes']
		retcode = attributes['retcode']
		retmsg = attributes['retmsg']
		raise RuntimeError.new(retmsg) if retcode != '0'
		yield(result)
	end
end

#generate_signature(payload) ⇒ Object



99
100
101
102
# File 'lib/jieshun/parking/client.rb', line 99

def generate_signature(payload)
	md5_string = Digest::MD5.hexdigest(payload)
	md5_string.upcase
end

#get_auth_tokenObject



17
18
19
# File 'lib/jieshun/parking/client.rb', line 17

def get_auth_token
	@redis_client.redis_get(REDIS_AUTH_KEY)
end

#handle_failure(error) ⇒ Object

捷慧通云平台接口规范_2.1.doc 0:没有异常 6: 无效的令牌或令牌已过期

Raises:

  • (RuntimeError)


127
128
129
130
131
132
133
# File 'lib/jieshun/parking/client.rb', line 127

def handle_failure(error)
	case error.body['resultCode']
		when 6
			@redis_client.redis_del(REDIS_AUTH_KEY)
	end
	raise RuntimeError.new("Unified error handling for client; resultCode:#{error.body['resultCode']}, message: #{error.body['message']}")
end

#login_if_requiredObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jieshun/parking/client.rb', line 21

def 
	auth_token = get_auth_token
	if Jieshun::Parking. || auth_token.nil? || auth_token.empty?
		hash = { cid: @configuration.cid, usr: @configuration.usr, psw: @configuration.psw }
		@http_client.post_form("#{@configuration.server_url}/jsaims/login",
													 {},
													 hash,
													 ->(callback) {
														 if callback.successful
															 response = callback.body
															 auth_token = response['token']
															 @redis_client.redis_set(REDIS_AUTH_KEY, auth_token, 2 * 60 * 60 - 60) # token will be expired after 2 hours
															 yield(auth_token)
														 else
															 handle_failure(callback)
														 end
													 })
	else
		yield(auth_token)
	end
end

#notify_order_result(order_no, pay_type, pay_time, transaction_id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jieshun/parking/client.rb', line 104

def notify_order_result(order_no, pay_type, pay_time, transaction_id)
	Retry.with_retries(max_tries: 10, sleep_seconds: 1, business_info: { order_no: order_no }) do
		payload = {
				"serviceId": "3c.pay.notifyorderresult",
				"requestType": "DATA",
				"attributes": {
						"orderNo": order_no,
						"tradeStatus": 0,
						"isCallBack": 0,
						"payType": pay_type,
						"payTime": pay_time.strftime('%Y-%m-%d %H:%M:%S'),
						"transactionId": transaction_id
				}
		}.to_json
		unify_call(payload) do |result|
			yield(result)
		end
	end
end

#unify_call(payload) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jieshun/parking/client.rb', line 76

def unify_call(payload)
	 do |auth_token|
		sn = generate_signature("#{payload}#{@configuration.sign_key}")
		hash = {
				cid: @configuration.cid,
				tn: auth_token,
				v: 2,
				sn: sn,
				p: payload
		}
		@http_client.post_form("#{@configuration.server_url}/jsaims/as",
													 {},
													 hash,
													 ->(callback) {
														 if callback.successful
															 yield(callback)
														 else
															 handle_failure(callback)
														 end
													 })
	end
end