Module: Google4R::Checkout

Defined in:
lib/google4r/checkout/utils.rb,
lib/google4r/checkout/shared.rb,
lib/google4r/checkout/commands.rb,
lib/google4r/checkout/frontend.rb,
lib/google4r/checkout/notifications.rb,
lib/google4r/checkout/xml_generation.rb,
lib/google4r/checkout/merchant_calculation.rb

Overview

:nodoc:

Defined Under Namespace

Modules: FinancialOrderState, FulfillmentOrderState Classes: AddMerchantOrderNumberCommand, AddMerchantOrderNumberCommandXmlGenerator, AddTrackingDataCommand, AddTrackingDataCommandXmlGenerator, Address, AnonymousAddress, ArchiveOrderCommand, ArchiveOrderCommandXmlGenerator, Area, AuthorizationAmountNotification, AuthorizeOrderCommand, AuthorizeOrderCommandXmlGenerator, BackorderItemsCommand, BackorderItemsCommandXmlGenerator, CallbackHandler, CancelItemsCommand, CancelItemsCommandXmlGenerator, CancelOrderCommand, CancelOrderCommandXmlGenerator, CancelledSubscriptionNotification, CarrierCalculatedShipping, ChargeAmountNotification, ChargeAndShipOrderCommand, ChargeAndShipOrderCommandXmlGenerator, ChargeFee, ChargeOrderCommand, ChargeOrderCommandXmlGenerator, ChargebackAmountNotification, CheckoutCommand, CheckoutCommandXmlGenerator, CheckoutRedirectResponse, CodeResult, Command, CommandXmlGenerator, CouponResult, CreateOrderRecurrenceRequestCommand, CreateOrderRecurrenceRequestCommandXmlGenerator, DeliverOrderCommand, DeliverOrderCommandXmlGenerator, DeliveryMethod, Dimension, FinancialState, FlatRateShipping, Frontend, FulfillmentState, GiftCertificateResult, GoogleCheckoutError, Item, ItemInfo, ItemsCommand, ItemsCommandXmlGenerator, MarketingPreferences, MerchantCalculatedShipping, MerchantCalculationCallback, MerchantCalculationResult, MerchantCalculationResults, MerchantCalculationResultsXmlGenerator, MerchantCode, NewOrderNotification, Notification, NotificationAcknowledgement, NotificationAcknowledgementXmlGenerator, NotificationDataRequestCommand, NotificationDataRequestCommandXmlGenerator, NotificationDataTokenRequestCommand, NotificationDataTokenRequestCommandXmlGenerator, NotificationHandler, NotificationHistoryReportCommandXmlGenerator, NotificationHistoryRequestCommand, OrderAdjustment, OrderReportCommand, OrderStateChangeNotification, ParameterizedUrl, PickupShipping, PostalArea, PrivateDataParser, ProcessOrderCommand, ProcessOrderCommandXmlGenerator, RefundAmountNotification, RefundOrderCommand, RefundOrderCommandXmlGenerator, ResetItemsShippingInformationCommand, ResetItemsShippingInformationCommandXmlGenerator, ReturnItemsCommand, ReturnItemsCommandXmlGenerator, ReturnOrderReportCommandXmlGenerator, RiskInformationNotification, SendBuyerMessageCommand, SendBuyerMessageCommandXmlGenerator, ShipItemsCommand, ShipItemsCommandXmlGenerator, ShippingAdjustment, ShippingMethod, ShoppingCart, SubscriptionRequestReceivedResponse, TaxRule, TaxTable, TrackingData, UnarchiveOrderCommand, UnarchiveOrderCommandXmlGenerator, Unit, UnknownCallbackType, UnknownNotificationType, UrlParameter, UsCountryArea, UsStateArea, UsZipArea, Weight, WorldArea, XmlGenerator

Class Method Summary collapse

Class Method Details

.sign(params, merchant_key) ⇒ Object

HTML Signing

Args:

  • params – html form parameters

  • merchant_key – Google Checkout merchant key

Returns:

  • signature – The base-64 encoded result of hashing the serialized

    parameters with the merchant key
    

Example


require ‘google4r/checkout/utils’

Google4R::Checkout.sign(:b=>‘456’, ‘merchantkey’)

> “5qBQYatFZk5BMS1hm5gSUS+9yrg=”



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/google4r/checkout/utils.rb', line 53

def self.sign(params, merchant_key)
  raise "params must be a Hash (e.g. {param1 => value1, param2 => value2, ...})" unless params.kind_of? Hash
  raise "merchant_key must be a String" unless merchant_key.kind_of? String
  
  # Remove unwanted parameters
  params.delete_if do |key, value|
    key = key.to_s
    key == '_charset_' || key == 'analyticsdata' ||
    key == 'urchindata' || key =~ /^(.+\.)*[xy]$/
  end
  
  # Strip away whitespaces and url-encode the values
  params.each do |key, value|
    params[key] = CGI::escape(value.to_s.strip)
  end
  
  # Sort parameters alphabetically by value and then key
  params_arr = params.sort do |x, y|
    if x[0] != y[0] then
      x[0].to_s <=> y[0].to_s
    else
      x[1].to_s <=> y[1].to_s
    end
  end

  # Create parameter string to be hashed 
  params_str = ''
  params_arr.each do |x|
    if params_str != '' then params_str += '&' end
    params_str += x[0].to_s + '=' + x[1]
  end

  # Generate hashed signature
  signature = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new,
                                   merchant_key,
                                   params_str)

  # Encode the hash value in Base64 before returning it
  return Base64.encode64(signature).chomp
end