Class: IAPServer::Receipt
- Inherits:
-
Object
- Object
- IAPServer::Receipt
- Defined in:
- lib/iap/receipt.rb
Instance Method Summary collapse
- #get_inputs(options, args) ⇒ Object
- #input(message) ⇒ Object
- #request(receipt, sandbox, password) ⇒ Object
- #run(options, args) ⇒ Object
- #status_declare(status) ⇒ Object
- #verify(receipt, sandbox = false, password = nil) ⇒ Object
Instance Method Details
#get_inputs(options, args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/iap/receipt.rb', line 14 def get_inputs(, args) receipt_data = args.first || begin file = .file || '' File.read(file) if File.exist?(file) && File.file?(file) rescue raise "文件读取失败".red end if receipt_data.nil? path = self.input('Path to receipt-data file: ') receipt_data = File.read(path).chomp if File.exist? path end sandbox = .sandbox password = .password return receipt_data, sandbox, password end |
#input(message) ⇒ Object
31 32 33 34 |
# File 'lib/iap/receipt.rb', line 31 def input() print "#{}".red STDIN.gets.chomp.strip end |
#request(receipt, sandbox, password) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/iap/receipt.rb', line 36 def request(receipt, sandbox, password) uri = sandbox ? 'sandbox.itunes.apple.com' : 'buy.itunes.apple.com' http = Net::HTTP.new(uri, 443) http.use_ssl = true headers = { ##定义http请求头信息 'Content-Type' => 'application/json' } params = { 'receipt-data' => "#{receipt}", 'exclude-old-transactions' => true, } params['password'] = password unless password.nil? resp = http.post("/verifyReceipt", params.to_json, headers) resp end |
#run(options, args) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/iap/receipt.rb', line 6 def run(, args) # get the command line inputs and parse those into the vars we need... receipt_data, sandbox, password = get_inputs(, args) raise "必须有票据才能验证。".red if receipt_data.nil? || receipt_data.empty? verify(receipt_data, sandbox, password) end |
#status_declare(status) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/iap/receipt.rb', line 70 def status_declare(status) error_status = { '21000' => 'App Store无法读取你提供的JSON数据', '21002' => '收据数据不符合格式', '21003' => '收据无法被验证', '21004' => '你提供的共享密钥和账户的共享密钥不一致', '21005' => '收据服务器当前不可用', '21006' => '收据是有效的,但订阅服务已经过期。当收到这个信息时,解码后的收据信息也包含在返回内容中', '21007' => '收据信息是测试用(sandbox),但却被发送到产品环境中验证', '21008' => '收据信息是产品环境中使用,但却被发送到测试环境中验证', } error_msg = error_status[status.to_s] || "未知的status类型,请对照https://developer.apple.com/documentation/appstorereceipts/status排查" error_msg end |
#verify(receipt, sandbox = false, password = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/iap/receipt.rb', line 53 def verify(receipt, sandbox=false, password=nil) resp = request(receipt, sandbox, password) if resp.code == '200' body = JSON.parse(resp.body) status = body['status'] if status == 0 puts resp.body else error_msg = status_declare(status) puts "status: #{status}, #{error_msg}" end else puts "Code: #{resp.code}" puts "Message: #{resp.}" end end |