Sha256: 4ae44d03c34599bc26fded1a67a164996cf4660ae1c7e3e4a24e3d621bb09a1f

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

require 'json'
require 'net/https'
require 'uri'

module Monza
  class Client
    attr_accessor :verification_url
    attr_writer :shared_secret

    PRODUCTION_URL = "https://buy.itunes.apple.com/verifyReceipt"
    DEVELOPMENT_URL = "https://sandbox.itunes.apple.com/verifyReceipt"

    def self.development
      client = self.new
      client.verification_url = DEVELOPMENT_URL
      client
    end

    def self.production
      client = self.new
      client.verification_url = PRODUCTION_URL
      client
    end

    def initialize
    end

    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

    def post_receipt_verification(data, options = {})
      parameters = {
        'receipt-data' => data
      }

      parameters['password'] = options[:shared_secret] if options[:shared_secret]

      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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
monza-1.1.2 lib/monza/client.rb
monza-1.1.1 lib/monza/client.rb
monza-1.1.0 lib/monza/client.rb
monza-0.1.4 lib/monza/client.rb
monza-0.1.3 lib/monza/client.rb