def signature_payload(headers, body = None):
    """Converts a message into a payload to be fed to the signature algorithm.

    The Checkout API requires every request and response to be signed.
    The first step in the signature process is to convert the request or
    response into a payload string.  This function does that.

    Args:
      headers: A dictionary of headers in the message (must include
          all checkout- headers, others optional)
      body: (optional) A bytes object containing the body to be sent

    Returns:
      A string object ready for signature computation.

    """

    hs = []
    for hn, hd in headers.items():
        hn = hn.lower()
        if hn.startswith("checkout-"):
            hs.append((hn, hd))
    return b"\n".join([ hn.encode() + b":" + hd.encode()
                       for hn, hd in sorted(hs, key=lambda x: x[0])] +
                      ([body] if body is not None else [b""]))