Skip to content

HIPS example code using the "Checkout API"

Patrick Bolger edited this page Nov 4, 2017 · 2 revisions

This code is provided by HIPS as a simple example of invoking order create and get actions.

Note that this code uses an HTTParty option debug_output (set to $stdout). This option should not be used in production code, as it calls the NET::HTTP method set_debug_output.

See note here.

#
# Hips API Communications example
#
# Documentation: https://static.hips.com/doc/api/index.html

# REQUIRES HTTPARTY
# https://github.com/jnunemaker/httparty

class IntegrationTest

  def self.create_order
    #https://static.hips.com/doc/api/index.html#create-a-new-order

    payload={
      "order_id": "1233ds9",
      "purchase_currency": "SEK",
      "cart": {
        "items": [
          {
            "type": "physical",
            "sku": "3123123",
            "name": "Hips cup",
            "quantity": 1,
            "unit_price": 250,
            "discount_rate": 0,
            "var_amount": 50,
          }, {
            "type": "physical",
            "sku": "456778",
            "name": "Hips T-shirt",
            "quantity": 1,
            "unit_price": 750,
            "discount_rate": 0,
            "vat_amount": 150
          }
        ]
      },
      "require_shipping": false
    }

    HTTParty.post(
      'https://api.hips.com/v1/orders',
      headers: {
        "Authorization" => "Token token=private_YOUR_PRIVATE_KEY",
        "Content-Type" => "application/json"
      },
      debug_output: $stdout,
      body:  payload.to_json
    ).parsed_response
  end

  def self.retrieve_order(id)
    #https://static.hips.com/doc/api/index.html#view-an-order

    url=URI::join('https://api.hips.com/v1/orders/', id)
    HTTParty.get(url).parsed_response
  end

  def self.run_example
    puts "Creating new order"
    created_order=create_order
    puts "Created order #{created_order["id"]}"

    puts "Retrieving order"
    retrieved_order=retrieve_order(created_order["id"])
    puts "Successfully retrieved order" if retrieved_order && retrieved_order["id"]==created_order["id"]

    # created_order["id"] is the ID that can be used to do the client-side integration
    # see https://static.hips.com/doc/api/index.html#checkout-api-integration-guide
  end
end
Clone this wiki locally