class Monza::Client

Constants

DEVELOPMENT_URL
PRODUCTION_URL

Attributes

shared_secret[W]
verification_url[RW]

Public Class Methods

development() click to toggle source
# File lib/monza/client.rb, line 13
def self.development
  client = self.new
  client.verification_url = DEVELOPMENT_URL
  client
end
new() click to toggle source
# File lib/monza/client.rb, line 25
def initialize
end
production() click to toggle source
# File lib/monza/client.rb, line 19
def self.production
  client = self.new
  client.verification_url = PRODUCTION_URL
  client
end

Public Instance Methods

verify(data, options = {}) click to toggle source
# File lib/monza/client.rb, line 28
def verify(data, options = {})
  # Post to apple and receive json_response
  json_response = post_receipt_verification(data, options)
  # Get status code of response
  status = json_response['status'].to_i

  case status
  when 0
    begin
      return VerificationResponse.new(json_response)
    rescue
      nil
    end
  else
    raise VerificationResponse::VerificationError.new(status)
  end

end

Private Instance Methods

post_receipt_verification(data, options = {}) click to toggle source
# File lib/monza/client.rb, line 49
def post_receipt_verification(data, options = {})
  parameters = {
    'receipt-data' => data
  }

  parameters['password'] = options[:shared_secret] if options[:shared_secret]
  parameters['exclude-old-transactions'] = options[:exclude_old_transactions] if options[:exclude_old_transactions]

  uri = URI(@verification_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER

  request = Net::HTTP::Post.new(uri.request_uri)
  request['Accept'] = "application/json"
  request['Content-Type'] = "application/json"
  request.body = parameters.to_json

  response = http.request(request)

  JSON.parse(response.body)
end