pass_server.rb
# create and sign a new pkpass package
create and sign a new pkpass package
create and sign a new pkpass package
def create_pkpass(serial_number, pass_type_identifier, pass_auth_token)
    pass = self.passes.where(:serial_number => serial_number, :pass_type_id => pass_type_identifier).first || halt(404) 
    pass_id = pass[:id]
    passes_folder_path = File.dirname(File.expand_path(__FILE__)) + "/data/passes"
    template_folder_path = passes_folder_path + "/template"
    target_folder_path = passes_folder_path + "/#{pass_id}"
    if (File.exists?(target_folder_path))
      puts "[ ok ] Deleting existing pass data."
      FileUtils.remove_dir(target_folder_path)
    else
      FileUtils.mkdir_p(target_folder_path)
    end
    puts "[ ok ] Creating pass data from template."
    list_of_resources = FileList[template_folder_path + "/*"].exclude(/pass.json$/)
    FileUtils.cp_r(list_of_resources, target_folder_path)
    puts "[ ok ] Updating pass data."
    json_file_path = target_folder_path + "/pass.json"
    begin 
      pass_json_string = PassJson.first(:serial => serial_number, :fields => [:json_data])[:json_data] || raise($!, "wrong or empty json_data")
      pass_json = JSON.parse(pass_json_string) || raise($!, "json parsing error")
    rescue => e 
      raise $!, e.message, $!.backtrace
    end
    pass_json["passTypeIdentifier"] = settings.pass_type_identifier if pass_json["passTypeIdentifier"] == "---"
    pass_json["teamIdentifier"] = settings.team_identifier if pass_json["teamIdentifier"] == "---"
    pass_json["serialNumber"] = serial_number
    pass_json["authenticationToken"] = pass_auth_token
    pass_json["webServiceURL"] = "https://#{settings.hostname}:#{settings.port}/" if pass_json["webServiceURL"] == "---"
    #pass_json["barcode"]["message"] = barcode_string_for_pass(pass_json)
    #pass_json["storeCard"]["primaryFields"][0]["value"] = 1000000
    #pass_json["storeCard"]["secondaryFields"][0]["value"] = 'Test User'
    File.open(json_file_path, "w") do |f|
      f.write JSON.pretty_generate(pass_json)
    end
    pass_folder_path = target_folder_path
    pass_signing_certificate_path = get_certificate_path
    wwdr_certificate_path = get_wwdr_certificate_path
    pass_output_path = passes_folder_path + "/#{pass_id}.pkpass"
    if File.exists?(pass_output_path)
      File.delete(pass_output_path)
    end
    pass_signer = SignPass.new(pass_folder_path, pass_signing_certificate_path, settings.certificate_password, wwdr_certificate_path, pass_output_path)
    pass_signer.sign_pass!
    pass_output_path
  end
def create_pkpass ( serial_number , pass_type_identifier , pass_auth_token ) pass = self . passes . where ( :serial_number => serial_number , :pass_type_id => pass_type_identifier ) . first || halt ( 404 ) pass_id = pass [ :id ] passes_folder_path = File . dirname ( File . expand_path ( __FILE__ ) ) + "/data/passes" template_folder_path = passes_folder_path + "/template" target_folder_path = passes_folder_path + "/#{pass_id}" if ( File . exists? ( target_folder_path ) ) puts "[ ok ] Deleting existing pass data." FileUtils . remove_dir ( target_folder_path ) else FileUtils . mkdir_p ( target_folder_path ) end puts "[ ok ] Creating pass data from template." list_of_resources = FileList [ template_folder_path + "/*" ] . exclude ( / pass.json$ / ) FileUtils . cp_r ( list_of_resources , target_folder_path ) puts "[ ok ] Updating pass data." json_file_path = target_folder_path + "/pass.json" begin pass_json_string = PassJson . first ( :serial => serial_number , :fields => [ :json_data ] ) [ :json_data ] || raise ( $! , "wrong or empty json_data" ) pass_json = JSON . parse ( pass_json_string ) || raise ( $! , "json parsing error" ) rescue => e raise $! , e . message , $! . backtrace end pass_json [ "passTypeIdentifier" ] = settings . pass_type_identifier if pass_json [ "passTypeIdentifier" ] == "---" pass_json [ "teamIdentifier" ] = settings . team_identifier if pass_json [ "teamIdentifier" ] == "---" pass_json [ "serialNumber" ] = serial_number pass_json [ "authenticationToken" ] = pass_auth_token pass_json [ "webServiceURL" ] = "https://#{settings.hostname}:#{settings.port}/" if pass_json [ "webServiceURL" ] == "---" File . open ( json_file_path , "w" ) do | f | f . write JSON . pretty_generate ( pass_json ) end pass_folder_path = target_folder_path pass_signing_certificate_path = get_certificate_path wwdr_certificate_path = get_wwdr_certificate_path pass_output_path = passes_folder_path + "/#{pass_id}.pkpass" if File . exists? ( pass_output_path ) File . delete ( pass_output_path ) end pass_signer = SignPass . new ( pass_folder_path , pass_signing_certificate_path , settings . certificate_password , wwdr_certificate_path , pass_output_path ) pass_signer . sign_pass! pass_output_path end
create and sign a new pkpass package
create and sign a new pkpass package
# Load pass data from database # !!! add DB connection error !!! #, 'cannot find pass with specified serial and type' # this id is from passes db !!! # NOT from passes_json # no need in User Balance - it is a generic pass #user_id = pass[:user_id] #user = self.users.where(:id => user_id).first || halt(404) #, 'cannot find user with specified identifier' # Configure folder paths # Delete pass folder if it already exists # we're storig BLOBs/files on disk for speed concern (not in sqlite) # Copy pass files from template folder # except pass.json # copy all pkpass resources into new folder for archiving # excluding pass.json - because it is a tempalte file, the real json payload data comes from PassJson #the FileUtils class mimics a bash shell's file utilities, so "mv" is "move" and "cp" is "copy". ###################### # here should be 'get json pass template/payload from DB' ###################### #******************************************************** # Modify the pass json #pass_json = JSON.parse(File.read(json_file_path)) #raise $!, "You fool! Look what you have done! #{$!}", $!.backtrace # =StandardError # to allow global 500 error-helper rescue we should re-raise: ## # this check should be a level higher ## # this one already should be the same as pass[:serial_number] # because create_pkpass creating new pass (in json and pass tables) ## # here should be some Hashie for Redeem State ## # this fields are specific # they should be updated in separate biz routine - Redeem State + Biz Rules Editor # !!! ###pass_json["barcode"]["message"] = barcode_string_for_pass(pass_json) #pass_json["storeCard"]["primaryFields"][0]["value"] = user[:account_balance] #pass_json["storeCard"]["secondaryFields"][0]["value"] = user[:name] ###pass_json["storeCard"]["primaryFields"][0]["value"] = 1000000 ###pass_json["storeCard"]["secondaryFields"][0]["value"] = 'Test User' #******************************************************** # Write out the updated JSON ############################ # => to DB (NOT a good idea for SQLite...) # ************************** # Prepare for pass signing # Remove the old pass if it exists # Generate and sign the new pass ############################ # => to DB (NOT a good idea for SQLite...) # ************************** # a full path to created pkpass
serial_number
<not_specific>
pass_type_identifier
<not_specific>
pass_auth_token
<not_specific>