set -ueo pipefail
command -v jq > /dev/null || echo "this script needs jq to extract fields from the link_json"
command -v openssl > /dev/null || echo "this script needs openssl to generate the hmac"
portal_key=PORTALKEY
link_json=$(cat << EOF
{
"currency": "EUR",
"intent": "authorization",
"paymentMethods": [
"visa",
"mastercard",
"paypal",
"paydirekt",
"giropay",
"sepa"
],
"merchantId": "MECHANT_ID",
"accountId": "ACCOUNT_ID",
"portalId": "PORTAL_ID",
"description": "Danke für Ihren Einkauf in meinem Shop",
"reference": "ref-$(date +%s)",
"mode": "test",
"shoppingCart": [
{
"type": "goods",
"number": "PO747035",
"description": "FliegendeUntertassen",
"price": 399,
"quantity": 1,
"vatRate": 19
},
{
"type": "goods",
"number": "PO747035",
"description": "Koffeeintabletten",
"price": 5,
"quantity": 9001,
"vatRate": 19
}
],
"billing": {
"country": "DE",
"lastName": "somebody"
}
}
EOF
)
function sum_all_article_prices(){
local total_amount article_price article_quantity article_amount
total_amount=0
for article in $(echo "$link_json" | jq --compact-output .shoppingCart[]); do
article_price=$(echo $article | jq .price)
article_quantity=$(echo $article | jq .quantity)
article_amount=$(( article_price * article_quantity ))
total_amount=$(( total_amount + article_amount ))
done
echo $total_amount
}
merchant_id=$(echo "$link_json" | jq --raw-output .merchantId)
account_id=$(echo "$link_json" | jq --raw-output .accountId)
portal_id=$(echo "$link_json" | jq --raw-output .portalId)
mode=$(echo "$link_json" | jq --raw-output .mode)
reference=$(echo "$link_json" | jq --raw-output .reference)
total_amount=$(sum_all_article_prices)
currency=$(echo "$link_json" | jq --raw-output .currency)
data_to_be_signed="${merchant_id}${account_id}${portal_id}${mode}${reference}${total_amount}${currency}"
signature=$(echo -n $data_to_be_signed | openssl dgst -sha256 -hmac ${portal_key} -binary | base64)
echo "${link_json}" | curl --fail \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: payone-hmac-sha256 $signature" \
-d @- \
https://onelink.pay1.de/api/v1/payment-links
|