Subscriptions
Code samples to generate Signature hash while creating Plan and Subscription
Regular Plan#
- Golang
- PHP
- NodeJS
- C#
- Java
- Python
package main
import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
    "net/url"
    "strconv"
)
type RequestObj struct {
    Amount           float64
    ClientKey        string
    Currency         string
    Frequency        int64
    MerchantOrderRef string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
    // Create a url.Values map and add the necessary parameters
    params := make(url.Values)
    params.Add("amount", strconv.FormatFloat(requestObj.Amount, 'f', -1, 64))
    params.Add("client_key", requestObj.ClientKey)
    params.Add("currency", requestObj.Currency)
    params.Add("frequency", strconv.Itoa(int(requestObj.Frequency)))
    params.Add("merchant_order_ref", requestObj.MerchantOrderRef)
    // Encode the parameters
    data := params.Encode()
    // fmt.Println("data is:", data)
    // Create the HMAC hash using SHA-256
    secret := []byte(portOneSecret)
    message := []byte(data)
    hash := hmac.New(sha256.New, secret)
    hash.Write(message)
    // Convert the hash to a base64 string
    hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
    return hashValue
}
func main() {
    portOneKey := "PORTONE_KEY"
    portOneSecret := "PORTONE_SECRET"
    // The unique merchant order reference generated by the merchant
    merchantOrderRef := ""
    requestObj := RequestObj{
        Amount:           100.00,
        ClientKey:        portOneKey,
        Currency:         "USD",
        Frequency:        12,
        MerchantOrderRef: merchantOrderRef,
    }
    // Generate the signature
    signature := GenerateSignature(requestObj, portOneSecret)
    // Output the signature
    fmt.Println("Signature is:", signature)
}
<?php
function GenerateSignature($requestObj, $secretKey) {
  $data = array(
      'amount' => $requestObj->Amount,
      'client_key' => $requestObj->ClientKey,
      'currency' => $requestObj->Currency,
      'frequency' => $requestObj->Frequency,
      'merchant_order_ref' => $requestObj->MerchantOrderRef
  );
  ksort($data);
  $data = http_build_query($data);
  $message = $data;
  return base64_encode(hash_hmac('sha256', $message, $secretKey, true));
}
function main() {
    $key = "PORTONE_KEY";
    $secret_key = "PORTONE_SECRET";
    // The unique merchant order reference generated by the merchant
    $merchant_order_ref = "";
    // Example request object
    $requestObj = (object) [
        'Amount' => 100.00,
        'ClientKey' => $key,
        'Currency' => 'USD',
        'Frequency' => 12,
        'MerchantOrderRef' => $merchant_order_ref
    ];
    $signature = GenerateSignature($requestObj, $secret_key);
    echo "Signature: " . $signature;
}
main();
?>
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function GenerateSignature(requestObj, secretKey) {
    const params = new URLSearchParams();
    params.append('amount', requestObj.Amount);
    params.append('client_key', requestObj.ClientKey);
    params.append('currency', requestObj.Currency);
    params.append('frequency', requestObj.Frequency);
    params.append('merchant_order_ref', requestObj.MerchantOrderRef);
    params.sort();
    const message = params.toString();
    const hashValue = crypto.createHmac('sha256', secretKey).update(message).digest('base64');
    return hashValue;
}
const clientKey = 'PORTONE_KEY';
const secretKey = 'PORTONE_SECRET';
// The unique merchant order reference generated by the merchant
const merchantOrderRef = '';
const requestObj = {
    Amount: 100.00,
    ClientKey: clientKey,
    Currency: 'USD',
    Frequency: 12,
    MerchantOrderRef: merchantOrderRef
};
const signature = GenerateSignature(requestObj, secretKey);
console.log(`Signature: ${signature}\n`);
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
    class PaymentRequest
    {
        public double Amount;
        public string ClientKey;
        public string Currency;
        public int Frequency;
        public string MerchantOrderRef;
    }
    class ApiSecurityExample
    {
        public static string GenerateSignature(PaymentRequest paymentRequest, string secret)
        {
            var map = new SortedDictionary<string, string>()
            {
                { "amount", RemoveTrailingZeros(paymentRequest.Amount) },
                { "client_key", paymentRequest.ClientKey },
                { "currency", paymentRequest.Currency },
                { "frequency", paymentRequest.Frequency.ToString() },
                { "merchant_order_ref", paymentRequest.MerchantOrderRef }
            };
            var stringBuilder = new StringBuilder();
            foreach (var key in map.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append("&");
                }
                var value = map[key];
                try
                {
                    stringBuilder.Append((key != null ? Uri.EscapeDataString(key) : ""));
                    stringBuilder.Append("=");
                    stringBuilder.Append(value != null ? Uri.EscapeDataString(value) : "");
                }
                catch (ArgumentNullException e)
                {
                    throw new Exception("The key or value is null.", e);
                }
                catch (UriFormatException e)
                {
                    throw new Exception("Invalid format for key or value.", e);
                }
            }
            var message = stringBuilder.ToString();
            // Console.WriteLine("message: " + message);
            var encoding = new ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string hash_value = Convert.ToBase64String(hashmessage);
            return hash_value;
        }
        private static string RemoveTrailingZeros(double amount)
        {
            return amount.ToString("0.###################");
        }
    }
  class Program
    {
       static void Main(string[] args)
        {
            string key = "PORTONE_KEY";
            string secret = "PORTONE_SECRET";
            // The unique merchant order reference generated by the merchant
            string merchantOrderRef = "";
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Amount = 100.00,
                ClientKey = key,
                Currency = "USD",
                Frequency = 12,
                MerchantOrderRef = merchantOrderRef
            };
            string signature = ApiSecurityExample.GenerateSignature(paymentRequest, secret);
            Console.WriteLine("Signature: " + signature);
        }
    }
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
public class Main {
    public static void main(String[] args) {
        String key = "PORTONE_KEY";
        String secret = "PORTONE_SECRET";
        // The unique merchant order reference generated by the merchant
        String merchantOrderRef = "";
        String currency = "USD";
        Double amount = 100.00;
        // Create an instance of RequestObj using the default constructor
        RequestObj requestObj = new RequestObj();
        // Use setter methods to set the values
        requestObj.setAmount(amount);
        requestObj.setClientKey(key);
        requestObj.setCurrency(currency);
        requestObj.setFrequency(12);
        requestObj.setMerchantOrderRef(merchantOrderRef);
        String signature = generateSignature(requestObj, secret);
        System.out.println("Signature: " + signature);
    }
    public static String generateSignature(RequestObj requestObj, String secretKey) {
        try {
            Map<String, String> params = new TreeMap<>();
            params.put("amount", requestObj.getAmount());
            params.put("client_key", requestObj.getClientKey());
            params.put("currency", requestObj.getCurrency());
            params.put("frequency", requestObj.getFrequency().toString());
            params.put("merchant_order_ref", requestObj.getMerchantOrderRef());
            // Encode the parameters
            String data = encodeParams(params);
            // System.out.println("data: " + data);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secretKeySpec);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate hmac-sha256", e);
        }
    }
    public static String encodeParams(Map<String, String> params) {
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (encodedParams.length() > 0) {
                encodedParams.append("&");
            }
            encodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
        }
        return encodedParams.toString();
    }
}
class RequestObj {
    private Double amount;
    private String clientKey;
    private String currency;
    private Integer frequency;
    private String merchantOrderRef;
    // Default constructor
    public RequestObj() {
    }
    // Setter methods
    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    public void setMerchantOrderRef(String merchantOrderRef) {
        this.merchantOrderRef = merchantOrderRef;
    }
    public void setFrequency(Integer frequency) {
        this.frequency = frequency;
    }
    // Getter methods
    public String getClientKey() {
        return clientKey;
    }
    public String getCurrency() {
        return currency;
    }
    public String getAmount() {
        DecimalFormat df = new DecimalFormat("0.##");
        return df.format(amount);
    }
    public String getMerchantOrderRef() {
        return merchantOrderRef;
    }
    public Integer getFrequency() {
        return frequency;
    }
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.parse
import hashlib
import hmac
import base64
class RequestObj:   
    def __init__(self, Amount, ClientKey, Currency, Frequency, MerchantOrderRef):
        # Instance Variables    
        self.Amount = Amount
        self.ClientKey = ClientKey
        self.Currency = Currency    
        self.Frequency = Frequency 
        self.MerchantOrderRef = MerchantOrderRef
def GenerateSignature(requestObj, secretKey):
    f = {
        'amount': f"{requestObj.Amount:.2f}".rstrip('0').rstrip('.'),
        'client_key': requestObj.ClientKey,
        'currency': requestObj.Currency,
        'frequency': requestObj.Frequency,
        'merchant_order_ref': requestObj.MerchantOrderRef
    }
    f = dict(sorted(f.items()))
    message1 = urllib.parse.urlencode(f)
    message = message1.encode('utf-8')
    # print(f'message: {message}\n')
    secret = secretKey.encode('utf-8')
    signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()).decode('utf-8')
    return signature
# Define constants
key = 'PORTONE_KEY'
secret = 'PORTONE_SECRET'
# The unique merchant order reference generated by the merchant
merchantOrderRef = ''
# Create an instance of RequestObj
requestObj = RequestObj(
    Amount=100.00,
    ClientKey=key,
    Currency='USD',
    Frequency=12,
    MerchantOrderRef=merchantOrderRef
)
# Call GenerateSignature
signature = GenerateSignature(requestObj, secret)
print(f'Signature: {signature}\n')
Regular Subscription#
- Golang
- PHP
- NodeJS
- C#
- Java
- Python
package main
import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
    "net/url"
    "strconv"
)
type RequestObj struct {
    ClientKey        string
    Currency         string
    FailureUrl       string
    MerchantOrderRef string
    Quantity         int64
    SuccessUrl       string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
    // Create a url.Values map and add the necessary parameters
    params := make(url.Values)
    params.Add("client_key", requestObj.ClientKey)
    params.Add("currency", requestObj.Currency)
    params.Add("failure_url", requestObj.FailureUrl)
    params.Add("merchant_order_ref", requestObj.MerchantOrderRef)
    params.Add("quantity", strconv.Itoa(int(requestObj.Quantity)))
    params.Add("success_url", requestObj.SuccessUrl)
    // Encode the parameters
    data := params.Encode()
    // fmt.Println("data is:", data)
    // Create the HMAC hash using SHA-256
    secret := []byte(portOneSecret)
    message := []byte(data)
    hash := hmac.New(sha256.New, secret)
    hash.Write(message)
    // Convert the hash to a base64 string
    hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
    return hashValue
}
func main() {
    portOneKey := "PORTONE_KEY"
    portOneSecret := "PORTONE_SECRET"
    // The unique merchant order reference generated by the merchant
    merchantOrderRef := ""
    requestObj := RequestObj{
        ClientKey:        portOneKey,
        Currency:         "USD",
        MerchantOrderRef: merchantOrderRef,
        Quantity:         1,
        SuccessUrl:       "https://checkout.portone.cloud/success.html",
        FailureUrl:       "https://checkout.portone.cloud/failure.html",
    }
    // Generate the signature
    signature := GenerateSignature(requestObj, portOneSecret)
    // Output the signature
    fmt.Println("Signature is:", signature)
}
<?php
function GenerateSignature($requestObj, $secretKey) {
  $data = array(
      'client_key' => $requestObj->ClientKey,
      'currency' => $requestObj->Currency,
      'failure_url' => $requestObj->FailureUrl,
      'merchant_order_ref' => $requestObj->MerchantOrderRef,
      'quantity' => $requestObj->Quantity,
      'success_url' => $requestObj->SuccessUrl
  );
  ksort($data);
  $data = http_build_query($data);
  $message = $data;
  return base64_encode(hash_hmac('sha256', $message, $secretKey, true));
}
function main() {
    $key = "PORTONE_KEY";
    $secret_key = "PORTONE_SECRET";
    // The unique merchant order reference generated by the merchant
    $merchant_order_ref = "";
    // Example request object
    $requestObj = (object) [
        'ClientKey' => $key,
        'Currency' => 'USD',
        'MerchantOrderRef' => $merchant_order_ref,
        'Quantity' => 1,
        'SuccessUrl' => 'https://checkout.portone.cloud/success.html',
        'FailureUrl' => 'https://checkout.portone.cloud/failure.html'
    ];
    $signature = GenerateSignature($requestObj, $secret_key);
    echo "Signature: " . $signature;
}
main();
?>
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function GenerateSignature(requestObj, secretKey) {
    const params = new URLSearchParams();
    params.append('client_key', requestObj.ClientKey);
    params.append('currency', requestObj.Currency);
    params.append('failure_url', requestObj.FailureUrl);
    params.append('merchant_order_ref', requestObj.MerchantOrderRef);
    params.append('quantity', requestObj.Quantity);
    params.append('success_url', requestObj.SuccessUrl);
    params.sort();
    const message = params.toString();
    const hashValue = crypto.createHmac('sha256', secretKey).update(message).digest('base64');
    return hashValue;
}
const clientKey = 'PORTONE_KEY';
const secretKey = 'PORTONE_SECRET';
// The unique merchant order reference generated by the merchant
const merchantOrderRef = '';
const requestObj = {
    ClientKey: clientKey,
    Currency: 'USD',
    FailureUrl: 'https://checkout.portone.cloud/failure.html',
    MerchantOrderRef: merchantOrderRef,
    Quantity: 1,
    SuccessUrl: 'https://checkout.portone.cloud/success.html'
};
const signature = GenerateSignature(requestObj, secretKey);
console.log(`Signature: ${signature}\n`);
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
    class PaymentRequest
    {
        public string ClientKey;
        public string Currency;
        public string FailureUrl;
        public string MerchantOrderRef;
        public int Quantity;
        public string SuccessUrl;
    }
    class ApiSecurityExample
    {
        public static string GenerateSignature(PaymentRequest paymentRequest, string secret)
        {
            var map = new SortedDictionary<string, string>()
            {
                { "client_key", paymentRequest.ClientKey },
                { "currency", paymentRequest.Currency },
                { "failure_url", paymentRequest.FailureUrl },
                { "merchant_order_ref", paymentRequest.MerchantOrderRef },
                { "success_url", paymentRequest.SuccessUrl },
                { "quantity", paymentRequest.Quantity.ToString() }
            };
            var stringBuilder = new StringBuilder();
            foreach (var key in map.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append("&");
                }
                var value = map[key];
                try
                {
                    stringBuilder.Append((key != null ? Uri.EscapeDataString(key) : ""));
                    stringBuilder.Append("=");
                    stringBuilder.Append(value != null ? Uri.EscapeDataString(value) : "");
                }
                catch (ArgumentNullException e)
                {
                    throw new Exception("The key or value is null.", e);
                }
                catch (UriFormatException e)
                {
                    throw new Exception("Invalid format for key or value.", e);
                }
            }
            var message = stringBuilder.ToString();
            // Console.WriteLine("message: " + message);
            var encoding = new ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string hash_value = Convert.ToBase64String(hashmessage);
            return hash_value;
        }
    }
  class Program
    {
       static void Main(string[] args)
        {
            string key = "PORTONE_KEY";
            string secret = "PORTONE_SECRET";
            // The unique merchant order reference generated by the merchant
            string merchantOrderRef = "";
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                ClientKey = key,
                Currency = "USD",
                FailureUrl = "https://checkout.portone.cloud/failure.html",
                MerchantOrderRef = merchantOrderRef,
                SuccessUrl = "https://checkout.portone.cloud/success.html",
                Quantity = 1
            };
            string signature = ApiSecurityExample.GenerateSignature(paymentRequest, secret);
            Console.WriteLine("Signature: " + signature);
        }
    }
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
public class Main {
    public static void main(String[] args) {
        String key = "PORTONE_KEY";
        String secret = "PORTONE_SECRET";
        // The unique merchant order reference generated by the merchant
        String merchantOrderRef = "";
        String currency = "USD";
        Integer quantity = 1;
        // Create an instance of RequestObj using the default constructor
        RequestObj requestObj = new RequestObj();
        // Use setter methods to set the values
        requestObj.setClientKey(key);
        requestObj.setCurrency(currency);
        requestObj.setMerchantOrderRef(merchantOrderRef);
        requestObj.setSuccessUrl("https://checkout.portone.cloud/success.html");
        requestObj.setFailureUrl("https://checkout.portone.cloud/failure.html");
        requestObj.setQuantity(quantity);
        String signature = generateSignature(requestObj, secret);
        System.out.println("Signature: " + signature);
    }
    public static String generateSignature(RequestObj requestObj, String secretKey) {
        try {
            Map<String, String> params = new TreeMap<>();
            params.put("client_key", requestObj.getClientKey());
            params.put("currency", requestObj.getCurrency());
            params.put("failure_url", requestObj.getFailureUrl());
            params.put("merchant_order_ref", requestObj.getMerchantOrderRef());
            params.put("success_url", requestObj.getSuccessUrl());
            params.put("quantity", requestObj.getQuantity().toString());
            // Encode the parameters
            String data = encodeParams(params);
            // System.out.println("data: " + data);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secretKeySpec);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate hmac-sha256", e);
        }
    }
    public static String encodeParams(Map<String, String> params) {
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (encodedParams.length() > 0) {
                encodedParams.append("&");
            }
            encodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
        }
        return encodedParams.toString();
    }
}
class RequestObj {
    private String clientKey;
    private String currency;
    private String merchantOrderRef;
    private String successUrl;
    private String failureUrl;
    private Integer quantity;
    // Default constructor
    public RequestObj() {
    }
    // Setter methods
    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public void setMerchantOrderRef(String merchantOrderRef) {
        this.merchantOrderRef = merchantOrderRef;
    }
    public void setSuccessUrl(String successUrl) {
        this.successUrl = successUrl;
    }
    public void setFailureUrl(String failureUrl) {
        this.failureUrl = failureUrl;
    }
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
    // Getter methods
    public String getClientKey() {
        return clientKey;
    }
    public String getCurrency() {
        return currency;
    }
    public String getMerchantOrderRef() {
        return merchantOrderRef;
    }
    public String getSuccessUrl() {
        return successUrl;
    }
    public String getFailureUrl() {
        return failureUrl;
    }
    public Integer getQuantity() {
        return quantity;
    }
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.parse
import hashlib
import hmac
import base64
class RequestObj:   
    def __init__(self, ClientKey, Currency, FailureUrl, MerchantOrderRef, Quantity, SuccessUrl):
        # Instance Variables    
        self.ClientKey = ClientKey
        self.Currency = Currency    
        self.FailureUrl = FailureUrl 
        self.MerchantOrderRef = MerchantOrderRef
        self.Quantity = Quantity
        self.SuccessUrl = SuccessUrl
def GenerateSignature(requestObj, secretKey):
    f = {
        'client_key': requestObj.ClientKey,
        'currency': requestObj.Currency,
        'failure_url': requestObj.FailureUrl,
        'merchant_order_ref': requestObj.MerchantOrderRef,
        'success_url': requestObj.SuccessUrl,
        'quantity': requestObj.Quantity
    }
    f = dict(sorted(f.items()))
    message1 = urllib.parse.urlencode(f)
    message = message1.encode('utf-8')
    # print(f'message: {message}\n')
    secret = secretKey.encode('utf-8')
    signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()).decode('utf-8')
    return signature
# Define constants
key = 'PORTONE_KEY'
secret = 'PORTONE_SECRET'
# The unique merchant order reference generated by the merchant
merchantOrderRef = ''
# Create an instance of RequestObj
requestObj = RequestObj(
    ClientKey=key,
    Currency='USD',
    FailureUrl='https://checkout.portone.cloud/failure.html',
    MerchantOrderRef=merchantOrderRef,
    SuccessUrl='https://checkout.portone.cloud/success.html',
    Quantity=1
)
# Call GenerateSignature
signature = GenerateSignature(requestObj, secret)
print(f'Signature: {signature}\n')
Ondemand Plan#
- Golang
- PHP
- NodeJS
- C#
- Java
- Python
package main
import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
    "net/url"
)
type RequestObj struct {
    ClientKey        string
    Currency         string
    MerchantOrderRef string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
    // Create a url.Values map and add the necessary parameters
    params := make(url.Values)
    params.Add("client_key", requestObj.ClientKey)
    params.Add("currency", requestObj.Currency)
    params.Add("merchant_order_ref", requestObj.MerchantOrderRef)
    // Encode the parameters
    data := params.Encode()
    // fmt.Println("data is:", data)
    // Create the HMAC hash using SHA-256
    secret := []byte(portOneSecret)
    message := []byte(data)
    hash := hmac.New(sha256.New, secret)
    hash.Write(message)
    // Convert the hash to a base64 string
    hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
    return hashValue
}
func main() {
    portOneKey := "PORTONE_KEY"
    portOneSecret := "PORTONE_SECRET"
    // The unique merchant order reference generated by the merchant
    merchantOrderRef := ""
    requestObj := RequestObj{
        ClientKey:        portOneKey,
        Currency:         "USD",
        MerchantOrderRef: merchantOrderRef,
    }
    // Generate the signature
    signature := GenerateSignature(requestObj, portOneSecret)
    // Output the signature
    fmt.Println("Signature is:", signature)
}
<?php
function GenerateSignature($requestObj, $secretKey) {
  $data = array(
      'client_key' => $requestObj->ClientKey,
      'currency' => $requestObj->Currency,
      'merchant_order_ref' => $requestObj->MerchantOrderRef
  );
  ksort($data);
  $data = http_build_query($data);
  $message = $data;
  return base64_encode(hash_hmac('sha256', $message, $secretKey, true));
}
function main() {
    $key = "PORTONE_KEY";
    $secret_key = "PORTONE_SECRET";
    // The unique merchant order reference generated by the merchant
    $merchant_order_ref = "";
    // Example request object
    $requestObj = (object) [
        'ClientKey' => $key,
        'Currency' => 'USD',
        'MerchantOrderRef' => $merchant_order_ref
    ];
    $signature = GenerateSignature($requestObj, $secret_key);
    echo "Signature: " . $signature;
}
main();
?>
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function GenerateSignature(requestObj, secretKey) {
    const params = new URLSearchParams();
    params.append('client_key', requestObj.ClientKey);
    params.append('currency', requestObj.Currency);
    params.append('merchant_order_ref', requestObj.MerchantOrderRef);
    params.sort();
    const message = params.toString();
    const hashValue = crypto.createHmac('sha256', secretKey).update(message).digest('base64');
    return hashValue;
}
const clientKey = 'PORTONE_KEY';
const secretKey = 'PORTONE_SECRET';
// The unique merchant order reference generated by the merchant
const merchantOrderRef = '';
const requestObj = {
    ClientKey: clientKey,
    Currency: 'USD',
    MerchantOrderRef: merchantOrderRef
};
const signature = GenerateSignature(requestObj, secretKey);
console.log(`Signature: ${signature}\n`);
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
    class PaymentRequest
    {
        public string ClientKey;
        public string Currency;
        public string MerchantOrderRef;
    }
    class ApiSecurityExample
    {
        public static string GenerateSignature(PaymentRequest paymentRequest, string secret)
        {
            var map = new SortedDictionary<string, string>()
            {
                { "client_key", paymentRequest.ClientKey },
                { "currency", paymentRequest.Currency },
                { "merchant_order_ref", paymentRequest.MerchantOrderRef }
            };
            var stringBuilder = new StringBuilder();
            foreach (var key in map.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append("&");
                }
                var value = map[key];
                try
                {
                    stringBuilder.Append((key != null ? Uri.EscapeDataString(key) : ""));
                    stringBuilder.Append("=");
                    stringBuilder.Append(value != null ? Uri.EscapeDataString(value) : "");
                }
                catch (ArgumentNullException e)
                {
                    throw new Exception("The key or value is null.", e);
                }
                catch (UriFormatException e)
                {
                    throw new Exception("Invalid format for key or value.", e);
                }
            }
            var message = stringBuilder.ToString();
            // Console.WriteLine("message: " + message);
            var encoding = new ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string hash_value = Convert.ToBase64String(hashmessage);
            return hash_value;
        }
    }
  class Program
    {
       static void Main(string[] args)
        {
            string key = "PORTONE_KEY";
            string secret = "PORTONE_SECRET";
            // The unique merchant order reference generated by the merchant
            string merchantOrderRef = "";
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                ClientKey = key,
                Currency = "USD",
                MerchantOrderRef = merchantOrderRef
            };
            string signature = ApiSecurityExample.GenerateSignature(paymentRequest, secret);
            Console.WriteLine("Signature: " + signature);
        }
    }
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
public class Main {
    public static void main(String[] args) {
        String key = "PORTONE_KEY";
        String secret = "PORTONE_SECRET";
        // The unique merchant order reference generated by the merchant
        String merchantOrderRef = "";
        String currency = "USD";
        // Create an instance of RequestObj using the default constructor
        RequestObj requestObj = new RequestObj();
        // Use setter methods to set the values
        requestObj.setClientKey(key);
        requestObj.setCurrency(currency);
        requestObj.setMerchantOrderRef(merchantOrderRef);
        String signature = generateSignature(requestObj, secret);
        System.out.println("Signature: " + signature);
    }
    public static String generateSignature(RequestObj requestObj, String secretKey) {
        try {
            Map<String, String> params = new TreeMap<>();
            params.put("client_key", requestObj.getClientKey());
            params.put("currency", requestObj.getCurrency());
            params.put("merchant_order_ref", requestObj.getMerchantOrderRef());
            // Encode the parameters
            String data = encodeParams(params);
            // System.out.println("data: " + data);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secretKeySpec);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate hmac-sha256", e);
        }
    }
    public static String encodeParams(Map<String, String> params) {
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (encodedParams.length() > 0) {
                encodedParams.append("&");
            }
            encodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
        }
        return encodedParams.toString();
    }
}
class RequestObj {
    private String clientKey;
    private String currency;
    private String merchantOrderRef;
    // Default constructor
    public RequestObj() {
    }
    // Setter methods
    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public void setMerchantOrderRef(String merchantOrderRef) {
        this.merchantOrderRef = merchantOrderRef;
    }
    // Getter methods
    public String getClientKey() {
        return clientKey;
    }
    public String getCurrency() {
        return currency;
    }
    public String getMerchantOrderRef() {
        return merchantOrderRef;
    }
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.parse
import hashlib
import hmac
import base64
class RequestObj:   
    def __init__(self, ClientKey, Currency, MerchantOrderRef):
        # Instance Variables    
        self.ClientKey = ClientKey
        self.Currency = Currency    
        self.MerchantOrderRef = MerchantOrderRef
def GenerateSignature(requestObj, secretKey):
    f = {
        'client_key': requestObj.ClientKey,
        'currency': requestObj.Currency,
        'merchant_order_ref': requestObj.MerchantOrderRef
    }
    f = dict(sorted(f.items()))
    message1 = urllib.parse.urlencode(f)
    message = message1.encode('utf-8')
    # print(f'message: {message}\n')
    secret = secretKey.encode('utf-8')
    signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()).decode('utf-8')
    return signature
# Define constants
key = 'PORTONE_KEY'
secret = 'PORTONE_SECRET'
# The unique merchant order reference generated by the merchant
merchantOrderRef = ''
# Create an instance of RequestObj
requestObj = RequestObj(
    ClientKey=key,
    Currency='USD',
    MerchantOrderRef=merchantOrderRef
)
# Call GenerateSignature
signature = GenerateSignature(requestObj, secret)
print(f'Signature: {signature}\n')
Ondemand Subscription#
- Golang
- PHP
- NodeJS
- C#
- Java
- Python
package main
import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
    "net/url"
)
type RequestObj struct {
    ClientKey        string
    Currency         string
    FailureUrl       string
    MerchantOrderRef string
    SuccessUrl       string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
    // Create a url.Values map and add the necessary parameters
    params := make(url.Values)
    params.Add("client_key", requestObj.ClientKey)
    params.Add("currency", requestObj.Currency)
    params.Add("failure_url", requestObj.FailureUrl)
    params.Add("merchant_order_ref", requestObj.MerchantOrderRef)
    params.Add("success_url", requestObj.SuccessUrl)
    // Encode the parameters
    data := params.Encode()
    // fmt.Println("data is:", data)
    // Create the HMAC hash using SHA-256
    secret := []byte(portOneSecret)
    message := []byte(data)
    hash := hmac.New(sha256.New, secret)
    hash.Write(message)
    // Convert the hash to a base64 string
    hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
    return hashValue
}
func main() {
    portOneKey := "PORTONE_KEY"
    portOneSecret := "PORTONE_SECRET"
    // The unique merchant order reference generated by the merchant
    merchantOrderRef := ""
    requestObj := RequestObj{
        ClientKey:        portOneKey,
        Currency:         "USD",
        MerchantOrderRef: merchantOrderRef,
        SuccessUrl:       "https://subscription.portone.cloud/success.html",
        FailureUrl:       "https://subscription.portone.cloud/failure.html",
    }
    // Generate the signature
    signature := GenerateSignature(requestObj, portOneSecret)
    // Output the signature
    fmt.Println("Signature is:", signature)
}
<?php
function GenerateSignature($requestObj, $secretKey) {
  $data = array(
      'client_key' => $requestObj->ClientKey,
      'currency' => $requestObj->Currency,
      'failure_url' => $requestObj->FailureUrl,
      'merchant_order_ref' => $requestObj->MerchantOrderRef,
      'success_url' => $requestObj->SuccessUrl
  );
  ksort($data);
  $data = http_build_query($data);
  $message = $data;
  return base64_encode(hash_hmac('sha256', $message, $secretKey, true));
}
function main() {
    $key = "PORTONE_KEY";
    $secret_key = "PORTONE_SECRET";
    // The unique merchant order reference generated by the merchant
    $merchant_order_ref = "";
    // Example request object
    $requestObj = (object) [
        'ClientKey' => $key,
        'Currency' => 'USD',
        'MerchantOrderRef' => $merchant_order_ref,
        'SuccessUrl' => 'https://subscription.portone.cloud/success.html',
        'FailureUrl' => 'https://subscription.portone.cloud/failure.html'
    ];
    $signature = GenerateSignature($requestObj, $secret_key);
    echo "Signature: " . $signature;
}
main();
?>
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function GenerateSignature(requestObj, secretKey) {
    const params = new URLSearchParams();
    params.append('client_key', requestObj.ClientKey);
    params.append('currency', requestObj.Currency);
    params.append('failure_url', requestObj.FailureUrl);
    params.append('merchant_order_ref', requestObj.MerchantOrderRef);
    params.append('success_url', requestObj.SuccessUrl);
    params.sort();
    const message = params.toString();
    const hashValue = crypto.createHmac('sha256', secretKey).update(message).digest('base64');
    return hashValue;
}
const clientKey = 'PORTONE_KEY';
const secretKey = 'PORTONE_SECRET';
// The unique merchant order reference generated by the merchant
const merchantOrderRef = '';
const requestObj = {
    ClientKey: clientKey,
    Currency: 'USD',
    FailureUrl: 'https://subscription.portone.cloud/failure.html',
    MerchantOrderRef: merchantOrderRef,
    SuccessUrl: 'https://subscription.portone.cloud/success.html'
};
const signature = GenerateSignature(requestObj, secretKey);
console.log(`Signature: ${signature}\n`);
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
    class PaymentRequest
    {
        public string ClientKey;
        public string Currency;
        public string FailureUrl;
        public string MerchantOrderRef;
        public string SuccessUrl;
    }
    class ApiSecurityExample
    {
        public static string GenerateSignature(PaymentRequest paymentRequest, string secret)
        {
            var map = new SortedDictionary<string, string>()
            {
                { "client_key", paymentRequest.ClientKey },
                { "currency", paymentRequest.Currency },
                { "failure_url", paymentRequest.FailureUrl },
                { "merchant_order_ref", paymentRequest.MerchantOrderRef },
                { "success_url", paymentRequest.SuccessUrl }
            };
            var stringBuilder = new StringBuilder();
            foreach (var key in map.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append("&");
                }
                var value = map[key];
                try
                {
                    stringBuilder.Append((key != null ? Uri.EscapeDataString(key) : ""));
                    stringBuilder.Append("=");
                    stringBuilder.Append(value != null ? Uri.EscapeDataString(value) : "");
                }
                catch (ArgumentNullException e)
                {
                    throw new Exception("The key or value is null.", e);
                }
                catch (UriFormatException e)
                {
                    throw new Exception("Invalid format for key or value.", e);
                }
            }
            var message = stringBuilder.ToString();
            // Console.WriteLine("message: " + message);
            var encoding = new ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string hash_value = Convert.ToBase64String(hashmessage);
            return hash_value;
        }
    }
  class Program
    {
       static void Main(string[] args)
        {
            string key = "PORTONE_KEY";
            string secret = "PORTONE_SECRET";
            // The unique merchant order reference generated by the merchant
            string merchantOrderRef = "";
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                ClientKey = key,
                Currency = "USD",
                FailureUrl = "https://subscription.portone.cloud/failure.html",
                MerchantOrderRef = merchantOrderRef,
                SuccessUrl = "https://subscription.portone.cloud/success.html"
            };
            string signature = ApiSecurityExample.GenerateSignature(paymentRequest, secret);
            Console.WriteLine("Signature: " + signature);
        }
    }
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
public class Main {
    public static void main(String[] args) {
        String key = "PORTONE_KEY";
        String secret = "PORTONE_SECRET";
        // The unique merchant order reference generated by the merchant
        String merchantOrderRef = "";
        String currency = "USD";
        // Create an instance of RequestObj using the default constructor
        RequestObj requestObj = new RequestObj();
        // Use setter methods to set the values
        requestObj.setClientKey(key);
        requestObj.setCurrency(currency);
        requestObj.setMerchantOrderRef(merchantOrderRef);
        requestObj.setSuccessUrl("https://subscription.portone.cloud/success.html");
        requestObj.setFailureUrl("https://subscription.portone.cloud/failure.html");
        String signature = generateSignature(requestObj, secret);
        System.out.println("Signature: " + signature);
    }
    public static String generateSignature(RequestObj requestObj, String secretKey) {
        try {
            Map<String, String> params = new TreeMap<>();
            params.put("client_key", requestObj.getClientKey());
            params.put("currency", requestObj.getCurrency());
            params.put("failure_url", requestObj.getFailureUrl());
            params.put("merchant_order_ref", requestObj.getMerchantOrderRef());
            params.put("success_url", requestObj.getSuccessUrl());
            // Encode the parameters
            String data = encodeParams(params);
            // System.out.println("data: " + data);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secretKeySpec);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate hmac-sha256", e);
        }
    }
    public static String encodeParams(Map<String, String> params) {
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (encodedParams.length() > 0) {
                encodedParams.append("&");
            }
            encodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
        }
        return encodedParams.toString();
    }
}
class RequestObj {
    private String clientKey;
    private String currency;
    private String merchantOrderRef;
    private String successUrl;
    private String failureUrl;
    // Default constructor
    public RequestObj() {
    }
    // Setter methods
    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public void setMerchantOrderRef(String merchantOrderRef) {
        this.merchantOrderRef = merchantOrderRef;
    }
    public void setSuccessUrl(String successUrl) {
        this.successUrl = successUrl;
    }
    public void setFailureUrl(String failureUrl) {
        this.failureUrl = failureUrl;
    }
    // Getter methods
    public String getClientKey() {
        return clientKey;
    }
    public String getCurrency() {
        return currency;
    }
    public String getMerchantOrderRef() {
        return merchantOrderRef;
    }
    public String getSuccessUrl() {
        return successUrl;
    }
    public String getFailureUrl() {
        return failureUrl;
    }
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.parse
import hashlib
import hmac
import base64
class RequestObj:   
    def __init__(self, ClientKey, Currency, FailureUrl, MerchantOrderRef, SuccessUrl):
        # Instance Variables    
        self.ClientKey = ClientKey
        self.Currency = Currency    
        self.FailureUrl = FailureUrl 
        self.MerchantOrderRef = MerchantOrderRef
        self.SuccessUrl = SuccessUrl
def GenerateSignature(requestObj, secretKey):
    f = {
        'client_key': requestObj.ClientKey,
        'currency': requestObj.Currency,
        'failure_url': requestObj.FailureUrl,
        'merchant_order_ref': requestObj.MerchantOrderRef,
        'success_url': requestObj.SuccessUrl
    }
    f = dict(sorted(f.items()))
    message1 = urllib.parse.urlencode(f)
    message = message1.encode('utf-8')
    # print(f'message: {message}\n')
    secret = secretKey.encode('utf-8')
    signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()).decode('utf-8')
    return signature
# Define constants
key = 'PORTONE_KEY'
secret = 'PORTONE_SECRET'
# The unique merchant order reference generated by the merchant
merchantOrderRef = ''
# Create an instance of RequestObj
requestObj = RequestObj(
    ClientKey=key,
    Currency='USD',
    FailureUrl='https://subscription.portone.cloud/failure.html',
    MerchantOrderRef=merchantOrderRef,
    SuccessUrl='https://subscription.portone.cloud/success.html'
)
# Call GenerateSignature
signature = GenerateSignature(requestObj, secret)
print(f'Signature: {signature}\n')
Ondemand Deduction#
- Golang
- PHP
- NodeJS
- C#
- Java
- Python
package main
import (
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "fmt"
    "net/url"
    "strconv"
)
type RequestObj struct {
    Amount               float64
    ClientKey            string
    Currency             string
    MerchantOrderRef     string
    SubscriptionOrderRef string
}
func GenerateSignature(requestObj RequestObj, portOneSecret string) string {
    // Create a url.Values map and add the necessary parameters
    params := make(url.Values)
    params.Add("amount", strconv.FormatFloat(requestObj.Amount, 'f', -1, 64))
    params.Add("client_key", requestObj.ClientKey)
    params.Add("currency", requestObj.Currency)
    params.Add("merchant_order_ref", requestObj.MerchantOrderRef)
    params.Add("subscription_order_ref", requestObj.SubscriptionOrderRef)
    // Encode the parameters
    data := params.Encode()
    // fmt.Println("data is:", data)
    // Create the HMAC hash using SHA-256
    secret := []byte(portOneSecret)
    message := []byte(data)
    hash := hmac.New(sha256.New, secret)
    hash.Write(message)
    // Convert the hash to a base64 string
    hashValue := base64.StdEncoding.EncodeToString(hash.Sum(nil))
    return hashValue
}
func main() {
    portOneKey := "PORTONE_KEY"
    portOneSecret := "PORTONE_SECRET"
    // The unique merchant order reference generated by the merchant
    merchantOrderRef := ""
    // The subscription link's order reference to which you want to make an ondemand deduction
    subscriptionOrderRef := ""
    requestObj := RequestObj{
        Amount:               100.00,
        ClientKey:            portOneKey,
        Currency:             "USD",
        MerchantOrderRef:     merchantOrderRef,
        SubscriptionOrderRef: subscriptionOrderRef,
    }
    // Generate the signature
    signature := GenerateSignature(requestObj, portOneSecret)
    // Output the signature
    fmt.Println("Signature is:", signature)
}
<?php
function GenerateSignature($requestObj, $secretKey) {
  $data = array(
      'amount' => $requestObj->Amount,
      'client_key' => $requestObj->ClientKey,
      'currency' => $requestObj->Currency,
      'merchant_order_ref' => $requestObj->MerchantOrderRef,
      'subscription_order_ref' => $requestObj->SubscriptionOrderRef
  );
  ksort($data);
  $data = http_build_query($data);
  $message = $data;
  return base64_encode(hash_hmac('sha256', $message, $secretKey, true));
}
function main() {
    $key = "PORTONE_KEY";
    $secret_key = "PORTONE_SECRET";
    // The unique merchant order reference generated by the merchant
    $merchant_order_ref = "";
    // The subscription link's order reference to which you want to make an ondemand deduction
    $subscription_order_ref = "";
    // Example request object
    $requestObj = (object) [
        'Amount' => 100.00,
        'ClientKey' => $key,
        'Currency' => 'USD',
        'MerchantOrderRef' => $merchant_order_ref,
        'SubscriptionOrderRef' => $subscription_order_ref
    ];
    $signature = GenerateSignature($requestObj, $secret_key);
    echo "Signature: " . $signature;
}
main();
?>
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function GenerateSignature(requestObj, secretKey) {
    const params = new URLSearchParams();
    params.append('amount', requestObj.Amount);
    params.append('client_key', requestObj.ClientKey);
    params.append('currency', requestObj.Currency);
    params.append('merchant_order_ref', requestObj.MerchantOrderRef);
    params.append('subscription_order_ref', requestObj.SubscriptionOrderRef);
    params.sort();
    const message = params.toString();
    const hashValue = crypto.createHmac('sha256', secretKey).update(message).digest('base64');
    return hashValue;
}
const clientKey = 'PORTONE_KEY';
const secretKey = 'PORTONE_SECRET';
// The unique merchant order reference generated by the merchant
const merchantOrderRef = '';
// The subscription link's order reference to which you want to make an ondemand deduction
const subscriptionOrderRef = '';
const requestObj = {
    Amount: 100.00,
    ClientKey: clientKey,
    Currency: 'USD',
    MerchantOrderRef: merchantOrderRef,
    SubscriptionOrderRef: subscriptionOrderRef
};
const signature = GenerateSignature(requestObj, secretKey);
console.log(`Signature: ${signature}\n`);
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApp
{
    class PaymentRequest
    {
        public double Amount;
        public string ClientKey;
        public string Currency;
        public string MerchantOrderRef;
        public string SubscriptionOrderRef;
    }
    class ApiSecurityExample
    {
        public static string GenerateSignature(PaymentRequest paymentRequest, string secret)
        {
            var map = new SortedDictionary<string, string>()
            {
                { "amount", RemoveTrailingZeros(paymentRequest.Amount) },
                { "client_key", paymentRequest.ClientKey },
                { "currency", paymentRequest.Currency },
                { "merchant_order_ref", paymentRequest.MerchantOrderRef },
                { "subscription_order_ref", paymentRequest.SubscriptionOrderRef }
            };
            var stringBuilder = new StringBuilder();
            foreach (var key in map.Keys)
            {
                if (stringBuilder.Length > 0)
                {
                    stringBuilder.Append("&");
                }
                var value = map[key];
                try
                {
                    stringBuilder.Append((key != null ? Uri.EscapeDataString(key) : ""));
                    stringBuilder.Append("=");
                    stringBuilder.Append(value != null ? Uri.EscapeDataString(value) : "");
                }
                catch (ArgumentNullException e)
                {
                    throw new Exception("The key or value is null.", e);
                }
                catch (UriFormatException e)
                {
                    throw new Exception("Invalid format for key or value.", e);
                }
            }
            var message = stringBuilder.ToString();
            // Console.WriteLine("message: " + message);
            var encoding = new ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] messageBytes = encoding.GetBytes(message);
            var hmacsha256 = new HMACSHA256(keyByte);
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            string hash_value = Convert.ToBase64String(hashmessage);
            return hash_value;
        }
        private static string RemoveTrailingZeros(double amount)
        {
            return amount.ToString("0.###################");
        }
    }
  class Program
    {
       static void Main(string[] args)
        {
            string key = "PORTONE_KEY";
            string secret = "PORTONE_SECRET";
            // The unique merchant order reference generated by the merchant
            string merchantOrderRef = "";
            // The subscription link's order reference to which you want to make an ondemand deduction
            string subscriptionOrderRef = "";
            PaymentRequest paymentRequest = new PaymentRequest()
            {
                Amount = 100.00,
                ClientKey = key,
                Currency = "USD",
                MerchantOrderRef = merchantOrderRef,
                SubscriptionOrderRef = subscriptionOrderRef
            };
            string signature = ApiSecurityExample.GenerateSignature(paymentRequest, secret);
            Console.WriteLine("Signature: " + signature);
        }
    }
}
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
public class Main {
    public static void main(String[] args) {
        String key = "PORTONE_KEY";
        String secret = "PORTONE_SECRET";
        // The unique merchant order reference generated by the merchant
        String merchantOrderRef = "";
        // The subscription link's order reference to which you want to make an ondemand deduction
        String subscriptionOrderRef = "";
        String currency = "USD";
        Double amount = 100.00;
        // Create an instance of RequestObj using the default constructor
        RequestObj requestObj = new RequestObj();
        // Use setter methods to set the values
        requestObj.setAmount(amount);
        requestObj.setClientKey(key);
        requestObj.setCurrency(currency);
        requestObj.setMerchantOrderRef(merchantOrderRef);
        requestObj.setSubscriptionOrderRef(subscriptionOrderRef);
        String signature = generateSignature(requestObj, secret);
        System.out.println("Signature: " + signature);
    }
    public static String generateSignature(RequestObj requestObj, String secretKey) {
        try {
            Map<String, String> params = new TreeMap<>();
            params.put("amount", requestObj.getAmount());
            params.put("client_key", requestObj.getClientKey());
            params.put("currency", requestObj.getCurrency());
            params.put("merchant_order_ref", requestObj.getMerchantOrderRef());
            params.put("subscription_order_ref", requestObj.getSubscriptionOrderRef());
            // Encode the parameters
            String data = encodeParams(params);
            // System.out.println("data: " + data);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secretKeySpec);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new RuntimeException("Failed to calculate hmac-sha256", e);
        }
    }
    public static String encodeParams(Map<String, String> params) {
        StringBuilder encodedParams = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (encodedParams.length() > 0) {
                encodedParams.append("&");
            }
            encodedParams.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8))
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
        }
        return encodedParams.toString();
    }
}
class RequestObj {
    private Double amount;
    private String clientKey;
    private String currency;
    private String merchantOrderRef;
    private String subscriptionOrderRef;
    // Default constructor
    public RequestObj() {
    }
    // Setter methods
    public void setClientKey(String clientKey) {
        this.clientKey = clientKey;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    public void setMerchantOrderRef(String merchantOrderRef) {
        this.merchantOrderRef = merchantOrderRef;
    }
    public void setSubscriptionOrderRef(String subscriptionOrderRef) {
        this.subscriptionOrderRef = subscriptionOrderRef;
    }
    // Getter methods
    public String getClientKey() {
        return clientKey;
    }
    public String getCurrency() {
        return currency;
    }
    public String getAmount() {
        DecimalFormat df = new DecimalFormat("0.##");
        return df.format(amount);
    }
    public String getMerchantOrderRef() {
        return merchantOrderRef;
    }
    public String getSubscriptionOrderRef() {
        return subscriptionOrderRef;
    }
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib.parse
import hashlib
import hmac
import base64
class RequestObj:   
    def __init__(self, Amount, ClientKey, Currency, MerchantOrderRef, SubscriptionOrderRef):
        # Instance Variables    
        self.Amount = Amount
        self.ClientKey = ClientKey
        self.Currency = Currency    
        self.MerchantOrderRef = MerchantOrderRef
        self.SubscriptionOrderRef = SubscriptionOrderRef
def GenerateSignature(requestObj, secretKey):
    f = {
        'amount': f"{requestObj.Amount:.2f}".rstrip('0').rstrip('.'),
        'client_key': requestObj.ClientKey,
        'currency': requestObj.Currency,
        'merchant_order_ref': requestObj.MerchantOrderRef,
        'subscription_order_ref': requestObj.SubscriptionOrderRef
    }
    f = dict(sorted(f.items()))
    message1 = urllib.parse.urlencode(f)
    message = message1.encode('utf-8')
    # print(f'message: {message}\n')
    secret = secretKey.encode('utf-8')
    signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()).decode('utf-8')
    return signature
# Define constants
key = 'PORTONE_KEY'
secret = 'PORTONE_SECRET'
# The unique merchant order reference generated by the merchant
merchantOrderRef = ''
# The unique subscription order reference generated by the merchant
subscriptionOrderRef = ''
# Create an instance of RequestObj
requestObj = RequestObj(
    Amount=100.00,
    ClientKey=key,
    Currency='USD',
    MerchantOrderRef=merchantOrderRef,
    SubscriptionOrderRef=subscriptionOrderRef
)
# Call GenerateSignature
signature = GenerateSignature(requestObj, secret)
print(f'Signature: {signature}\n')