Class: GoogleCheckout::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/google-checkout/command.rb

Overview

Abstract class for commands.

https://sandbox.google.com/checkout/cws/v2/Merchant/1234567890/request
https://checkout.google.com/cws/v2/Merchant/1234567890/request

Direct Known Subclasses

Cart, OrderCommand

Constant Summary collapse

SANDBOX_REQUEST_URL =
"https://sandbox.google.com/checkout/cws/v2/Merchant/%s/request"
PRODUCTION_REQUEST_URL =
"https://checkout.google.com/cws/v2/Merchant/%s/request"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merchant_id, merchant_key) ⇒ Command

Returns a new instance of Command.



21
22
23
24
25
26
# File 'lib/google-checkout/command.rb', line 21

def initialize(merchant_id, merchant_key)
  @merchant_id = merchant_id
  @merchant_key = merchant_key

  @currency = "USD"
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



16
17
18
# File 'lib/google-checkout/command.rb', line 16

def currency
  @currency
end

#merchant_idObject

Returns the value of attribute merchant_id.



16
17
18
# File 'lib/google-checkout/command.rb', line 16

def merchant_id
  @merchant_id
end

#merchant_keyObject

Returns the value of attribute merchant_key.



16
17
18
# File 'lib/google-checkout/command.rb', line 16

def merchant_key
  @merchant_key
end

Class Method Details

.x509_storeObject

Class method to return the OpenSSL::X509::Store instance for the CA certificates.

NOTE May not be thread-safe.



80
81
82
83
84
85
86
87
88
89
# File 'lib/google-checkout/command.rb', line 80

def self.x509_store
  return @@x509_store if defined?(@@x509_store)

  cacert_path = File.expand_path(File.dirname(__FILE__) + '/../../support/cacert.pem')

  @@x509_store = OpenSSL::X509::Store.new
  @@x509_store.add_file(cacert_path)

  return @@x509_store
end

Instance Method Details

#postObject

Sends the Command’s XML to GoogleCheckout via HTTPS with Basic Auth.

Returns a GoogleCheckout::RequestReceived or a GoogleCheckout::Error object.



40
41
42
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
# File 'lib/google-checkout/command.rb', line 40

def post
  # Create HTTP(S) POST command and set up Basic Authentication.
  uri = URI.parse(url)

  request = Net::HTTP::Post.new(uri.path)
  request.basic_auth(@merchant_id, @merchant_key)

  # Set up the HTTP connection object and the SSL layer.
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https.cert_store = self.class.x509_store
  https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  https.verify_depth = 5

  # Send the request to Google.
  response = https.request(request, self.to_xml)

  # NOTE Because Notification.parse() is used, the content of objects
  #      will be correctly parsed no matter what the HTTP response code
  #      is from the server.
  case response
  when Net::HTTPSuccess, Net::HTTPClientError
    notification = Notification.parse(response.body)
    if notification.error?
      raise APIError, "#{notification.message} [in #{GoogleCheckout.production? ? 'production' : 'sandbox' }]"
    end
    return notification
  when Net::HTTPRedirection, Net::HTTPServerError, Net::HTTPInformation
    raise "Unexpected response code (#{response.class}): #{response.code} - #{response.message}"
  else
    raise "Unknown response code: #{response.code} - #{response.message}"
  end
end

#urlObject

Returns the appropriate sandbox or production url for posting API requests.



31
32
33
# File 'lib/google-checkout/command.rb', line 31

def url
  GoogleCheckout.sandbox? ? (SANDBOX_REQUEST_URL % @merchant_id) : (PRODUCTION_REQUEST_URL % @merchant_id)
end