<commit_before>#!/bin/sh

# default options
HTTP_PORT=8080

trap cleanup INT

function cleanup() {
	kill $$ # self pid
}

usage() {
cat << EOF
    Usage: $0 [options] <merchant id> <shared secret>

    This script starts a minimal web server providing an easy to use interface for a Klarna Checkout sandbox.
    
    EXAMPLE:
        $0 1234 YourSharedSecretHere

    OPTIONS:
       -h      Show this help message
       -p      HTTP server port. Default is 8080

EOF
}

# read options
while getopts "hp:" OPTION
do
    case $OPTION in
        h)
            usage
            exit 0
            ;;
        p)
            HTTP_PORT=$OPTARG
            ;;
        ?)
            exit 1
            ;;
    esac
done

# merchant id and shared secret must be passed as arguments
MERCHANT_ID=${@:$OPTIND:1}
SHARED_SECRET=${@:$OPTIND+1:1}

if [ -z "$MERCHANT_ID" ]
then
	echo "You must specify your Klarna Checkout merchant id. For help see -h"
	exit 1
fi

if [ -z "$SHARED_SECRET" ]
then
	echo "You must specify your shared secret for Klarna Checkout. For help see -h"
	exit 1
fi

# Klarna Checkout test drive. DO NOT USE THIS SCRIPT WITH THE PRODUCTION API
API_URL="https://checkout.testdrive.klarna.com/checkout/orders"

create_digest() {
	PAYLOAD=$1
	
	DIGEST=$(php -r '$payload=$argv[1]; $sharedSecret=$argv[2]; echo base64_encode(hash("sha256", "{$payload}{$sharedSecret}", true));' "$PAYLOAD" "$SHARED_SECRET")
	
	echo "$DIGEST"
}

create_order() {
	AUTHORIZATION_STRING=$(create_digest "$PAYLOAD" "$SHARED_SECRET")
	
	CURL_CREATE_ORDER=$(curl -vsX POST \
		--header "Accept: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Content-Type: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Authorization: Klarna $AUTHORIZATION_STRING" \
		-d "$PAYLOAD" \
		"$API_URL" 2>&1)
	
	ORDER_LOCATION=$(echo "$CURL_CREATE_ORDER" | grep -oE "Location: (.*)" | cut -d " " -f 2 | tr -d "\r")
	echo "$ORDER_LOCATION"
}

get_order_snippet() {
	ORDER_LOCATION=$1
	
	PAYLOAD="" # empty payload for GET requests
	AUTHORIZATION_STRING=$(create_digest "$PAYLOAD" "$SHARED_SECRET")
	
	CURL_GET_ORDER=$(curl -vs \
		--header "Accept: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Authorization: Klarna $AUTHORIZATION_STRING" \
		"$ORDER_LOCATION" 2>&1)
	
	KLARNA_CHECKOUT_SNIPPET=$(echo "$CURL_GET_ORDER" | perl -0777 -ne 'print $1 if /\"snippet\":\"(.*?)\"},/s')
	echo "$KLARNA_CHECKOUT_SNIPPET"
}

echo "[`date '+%Y-%m-%d %H:%M:%S'`] Listening for requests. To create a sample order and get it's snippet send a request to http://127.0.0.1:$HTTP_PORT/order"

# creds to http://paulbuchheit.blogspot.ro/2007/04/webserver-in-bash.html which inspired this fifo and nc solution
RESPONSE=/tmp/klarna-checkout-sandbox-response<commit_msg>replacing \" with " in snippet<commit_after>#!/bin/sh

# default options
HTTP_PORT=8080

trap cleanup INT

function cleanup() {
	kill $$ # self pid
}

usage() {
cat << EOF
    Usage: $0 [options] <merchant id> <shared secret>

    This script starts a minimal web server providing an easy to use interface for a Klarna Checkout sandbox.
    
    EXAMPLE:
        $0 1234 YourSharedSecretHere

    OPTIONS:
       -h      Show this help message
       -p      HTTP server port. Default is 8080

EOF
}

# read options
while getopts "hp:" OPTION
do
    case $OPTION in
        h)
            usage
            exit 0
            ;;
        p)
            HTTP_PORT=$OPTARG
            ;;
        ?)
            exit 1
            ;;
    esac
done

# merchant id and shared secret must be passed as arguments
MERCHANT_ID=${@:$OPTIND:1}
SHARED_SECRET=${@:$OPTIND+1:1}

if [ -z "$MERCHANT_ID" ]
then
	echo "You must specify your Klarna Checkout merchant id. For help see -h"
	exit 1
fi

if [ -z "$SHARED_SECRET" ]
then
	echo "You must specify your shared secret for Klarna Checkout. For help see -h"
	exit 1
fi

# Klarna Checkout test drive. DO NOT USE THIS SCRIPT WITH THE PRODUCTION API
API_URL="https://checkout.testdrive.klarna.com/checkout/orders"

create_digest() {
	PAYLOAD=$1
	
	DIGEST=$(php -r '$payload=$argv[1]; $sharedSecret=$argv[2]; echo base64_encode(hash("sha256", "{$payload}{$sharedSecret}", true));' "$PAYLOAD" "$SHARED_SECRET")
	
	echo "$DIGEST"
}

create_order() {
	AUTHORIZATION_STRING=$(create_digest "$PAYLOAD" "$SHARED_SECRET")
	
	CURL_CREATE_ORDER=$(curl -vsX POST \
		--header "Accept: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Content-Type: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Authorization: Klarna $AUTHORIZATION_STRING" \
		-d "$PAYLOAD" \
		"$API_URL" 2>&1)
	
	ORDER_LOCATION=$(echo "$CURL_CREATE_ORDER" | grep -oE "Location: (.*)" | cut -d " " -f 2 | tr -d "\r")
	echo "$ORDER_LOCATION"
}

get_order_snippet() {
	ORDER_LOCATION=$1
	
	PAYLOAD="" # empty payload for GET requests
	AUTHORIZATION_STRING=$(create_digest "$PAYLOAD" "$SHARED_SECRET")
	
	CURL_GET_ORDER=$(curl -vs \
		--header "Accept: application/vnd.klarna.checkout.aggregated-order-v2+json" \
		--header "Authorization: Klarna $AUTHORIZATION_STRING" \
		"$ORDER_LOCATION" 2>&1)
	
	KLARNA_CHECKOUT_SNIPPET=$(echo "$CURL_GET_ORDER" | perl -0777 -ne 'print $1 if /\"snippet\":\"(.*?)\"},/s' | sed 's/\\\"/"/g')
	echo "$KLARNA_CHECKOUT_SNIPPET"
}

echo "[`date '+%Y-%m-%d %H:%M:%S'`] Listening for requests. To create a sample order and get it's snippet send a request to http://127.0.0.1:$HTTP_PORT/order"

# creds to http://paulbuchheit.blogspot.ro/2007/04/webserver-in-bash.html which inspired this fifo and nc solution
RESPONSE=/tmp/klarna-checkout-sandbox-response
{'fraction_non_alphanumeric': 0.13882940756602427, 'fraction_numerical': 0.019628836545324768, 'mean_word_length': 3.365264797507788, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '486582', 'n_tokens_mistral': 2160, 'n_tokens_neox': 1981, 'n_words': 522}