Complete Bash Example

PORTALKEY, MECHANT_ID, ACCOUNT_ID, PORTAL_ID must be replaced.
Hmac SHA256 generation and request
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

 

Hmac SHA256 Code Example

Node.js
Hmac SHA256
var crypto = require('crypto');
var hash = crypto.createHmac('SHA256', "portalKey").update("dataToBeSigned").digest('base64');
PHP
Hmac SHA256
$s = hash_hmac('sha256', 'dataToBeSigned', 'portalKey', true);
echo base64_encode($s);
Java
Hmac SHA256
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class ApiSecurityExample {
  public static void main(String[] args) {
    String portalKey = "123456789987654321";
    String dataToBeSigned= "18333183342111222LIVEuniqueReference100EUR";

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(portalKey.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);

    String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(dataToBeSigned.getBytes()));
  }
}
Ruby
Hmac SHA256
require 'openssl'
require "base64"

hash  = OpenSSL::HMAC.digest('sha256', "portalKey", "dataToBeSigned")
token = Base64.encode64(hash)
token.delete("\n")
Python
Hmac SHA256
import hashlib
import hmac
import base64

dataToBeSigned= bytes("18333183342111222LIVEuniqueReference100EUR").encode('utf-8')
portalKey = bytes("123456789987654321").encode('utf-8')

signature = base64.b64encode(hmac.new(portalKey, dataToBeSigned, digestmod=hashlib.sha256).digest())