Checkout improvements: linking customers to orders

Jonathan Steele profile picture Written by: Jonathan Steele - Published on: 07 Dec 2017

We’ve introduced some improvements to the cart-checkout process that’ll make associating orders with customers simple and efficient.

Linking an order to a customer

Previously, checking out a cart required that you provided a customer object, with a name and email address in the payload. Now, you can check out with an existing Moltin customer.

Check out this basic example using cURL. Here we are checking out a cart and attaching an existing Moltin customer:

  curl -X "POST" "https://api.moltin.com/v2/carts/{CART_ID}/checkout" \
     -H "Authorization: Bearer XXXX" \
     -d $'{
   "data": {
     "customer": {
 "id": {CUSTOMER_ID}
     },
     "billing_address": {
 "first_name": "John",
 "last_name": "Doe",
 "company_name": "Moltin",
 "line_1": "2nd Floor British India House",
 "line_2": "15 Carliol Square",
 "city": "Newcastle upon Tyne",
 "postcode": "NE1 6UF",
 "county": "Tyne & Wear",
 "country": "UK"
     },
     "shipping_address": {
 "first_name": "John",
 "last_name": "Doe",
 "company_name": "Moltin",
 "line_1": "2nd Floor British India House",
 "line_2": "15 Carliol Square",
 "city": "Newcastle upon Tyne",
 "postcode": "NE1 6UF",
 "county": "Tyne & Wear",
 "country": "UK"
     }
   }
}'

This is just as easy to do when using our JavaScript SDK:

  Moltin.Cart({CART_ID}).Checkout({CUSTOMER_ID}, {
  first_name: "John",
  last_name: "Doe",
  company_name: "Moltin",
  line_1: "2nd Floor British India House",
  line_2: "15 Carliol Square",
  city: "Newcastle upon Tyne",
  postcode: "NE1 6UF",
  county: "Tyne & Wear",
  country: "UK"
}).then(() => {
  // Returns a new order object
});

The returned order object will have a snapshot of the customer’s -mail address and name at the time of the order, but also a relationship to the customer record.

  {
  "type": "order",
  "id": "a2b027ec-f49d-4ce7-94e7-f1848b619c8b",
  "status": "incomplete",
  "payment": "unpaid",
  "shipping": "unfulfilled",
  "customer": {
  	"name": "Jonathan Steele",
  	"email": "jonathan@moltin.com"
  },
  "shipping_address": {
  	"first_name": "Jonathan",
  	"last_name": "Steele",
  	"company_name": "Moltin",
  	"line_1": "British India House",
  	"line_2": "15 Carliol Square",
  	"city": "Newcastle upon Tyne",
  	"postcode": "NE1 6UF",
  	"county": "Tyne & Wear",
  	"country": "UK",
  	"instructions": ""
  },
  "billing_address": {
  	"first_name": "Jonathan",
  	"last_name": "Steele",
  	"company_name": "Moltin",
  	"line_1": "British India House",
  	"line_2": "15 Carliol Square",
  	"city": "Newcastle upon Tyne",
  	"postcode": "NE1 6UF",
  	"county": "Tyne & Wear",
  	"country": "UK"
  },
  "relationships": {
  	"customer": {
  		"data": {
  			"type": "customer",
  			"id": "7b9678e4-a52a-469b-bfdd-6ad9b36d8dc4"
  		}
  	}
  }
}

Retrieving a customer’s orders

Now that the relationship between your customers and orders exist, it’s easy to collate an order history for a specific customer using tokens.

When making a request for all orders, just add the customer token to the request header (X-Moltin-Customer-Token). This will return all orders associated with that customer.

  curl -X "GET" "https://api.moltin.com/v2/orders" \
  -H "Authorization: Bearer XXXX" \
  -H "X-Moltin-Customer-Token: eyJhbGciOi.AiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiI3OWN"

Or if you’re using theJavaScript SDK, just pass the customer token to the All method:

  Moltin.Orders.All("eyJhbGciOi.AiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJzdWIiOiI3OWN")

Let's build something amazing with Moltin