• English
  • Signature method

    Sign

    The back-end of the merchant requests palmpay open interface needs to be signed on the parameters, the signature process is as follows:

    1. Assume that the body data is a set M. Remove leading and trailing spaces from the parameters of non-empty parameter values in M, sort them in ascending order according to the ASCII code of the parameter name (lexicographic order), and concatenate them into a string strA using the correct format of the URL key value (key1=value1&key2=value2…)
    2. MD5 operation is performed on strA, and all characters of the obtained string are converted to uppercase to obtain the md5Str string.
    3. Use the merchant's private key (Base64 encoding) to sign md5Str with SHA1WithRSA to obtain the string sign, and put the sign string into the signature field in the request header information.

    Signature parameters

    Assume that the parameters to be signed are as follows:

    requestTime: 1546857549088
    version: V2.0
    nonceStr: tXIUBaQUUip97xhymcgvndYo6oSAvQXP
    orderId: e7c3784d5e2242fc00afe89ac12399

    Step 1

    The parameters are sorted in the format of key = value and ASCII alphabetical order by parameter name as follows:

    strA = "nonceStr=tXIUBaQUUip97xhymcgvndYo6oSAvQXP&orderId=e7c3784d5e2242fc00afe89ac12399&requestTime=1546857549088&version=V2.0";

    Step 2

    MD5(strA), to uppercase:

    md5Str = Md5Util.MD5(strA) = "B43F98EC214118A136077FD49CD70804";

    Step 3

    Merchant private key to generate signature:

    RsaUtil.sign(md5Str,privateKey):
    sign = RsaUtil.sign(md5Str,privateKey) = "gqJkWR5SCvAdzu5ISXckYOXkttKb5LXarOItoSPSJizfS";

    Signing example

    Signing the request with the private key saved by the merchant To facilitate access, we provide a demo project that generates signatures. You can use the PalmPayUtil.generateSig() method in the attachment 📎PalmpaySdkDemoNew to generate the signature. This signature method has implemented the three steps of the above Signature. You can directly copy the code and integrate it into the project.

    Sample code

    public class Demo {
    
    	 @Test
        public void test() throws Exception {
            // 1.Generate key pair
            Map<String, String> keyPair = RsaUtil.generateKeyPair();
            String privateKey = keyPair.get(Constants.PRIVATE_KEY);
            String publicKey = keyPair.get(Constants.PUBLIC_KEY);
    
            System.out.println("privateKey:");
            System.out.println(privateKey);
            System.out.println("publicKey:");
            System.out.println(publicKey);
         
            HashMap<String, Object> newHashMap = new HashMap<>();
            newHashMap.put("requestTime",1662171389940L);
            newHashMap.put("version","V1.1");
            newHashMap.put("nonceStr","IBJGAeTa4ZJQv4Z2qufomVo9eI1YnJ9Y");
            newHashMap.put("amount", 200);
            newHashMap.put("currency", "NGN");
            newHashMap.put("notifyUrl", "https://xx.cn/callback/payment");
            newHashMap.put("orderId", "testc9ffae997fc4");
            newHashMap.put("title", "pay");
            newHashMap.put("transType", "24");
            newHashMap.put("userId", "110");
            newHashMap.put("country", "NG");
            newHashMap.put("orderType", "1");
    
            String sign = PalmPayUtil.generateSign(newHashMap, privateKey, SignType.RSA);
            System.out.println("sign: "+sign);
            
            boolean result = PalmPayUtil.verifySign(newHashMap, publicKey, sign, SignType.RSA);
    
            Assert.assertTrue(result);
        }
    }

    Signature Verification

    In order to ensure data security, the merchant's back-end needs to authenticate palmpay's callback requests to the merchant.

    Step 1

    The parameters are sorted in the format of key = value and ASCII alphabetical order by parameter name as follows:

    strA = "nonceStr=tXIUBaQUUip97xhymcgvndYo6oSAvQXP&orderId=e7c3784d5e2242fc00afe89ac12399&requestTime=1546857549088&version=V2.0";

    Step 2

    MD5(strA), to uppercase:

    md5Str = Md5Util.MD5(strA) = "B43F98EC214118A136077FD49CD70804";

    Signature verification example

    Use the public key provided by the PalmPay to verify PalmPay signature of the callback merchant interface. PalmPay notifies the callback request, the request parameters in @RequestBody that are non-null except sign get md5Str according to the rules in step 1 and step 2. Use the PalmPayUtil.verifySignForCallback method in the attachment 📎PalmpaySdkDemoNew to verify the signature

    Sample code:

     public void testCallbackVerify() throws Exception {
            String paramBody = "{\"orderId\":\"P23101802543812274\",\"orderNo\":\"2424231018025438544222\",\"appId\":\"L10378191362\",\"transType\":\"24\",\"orderType\":\"01\",\"amount\":999,\"couponAmount\":0,\"status\":1,\"completeTime\":1697597698516,\"orderStatus\":2,\"sign\":\"Dj9ycTD91W9ti4n%2FK6GbtVu2qNDE83tB6C7ToKrVe9tSM2aIhMrk%2Fqy9CgQ9fwXJhH6QQ1kg8kx2AV7O4tOIpSXs%2BnZ7aHXjricn8pUx06yQ%2BIqBXWSY9Hrhb6qkPXAzcskQ1MI%2B7SOieBVJTMf4vxIoxLOhMSYHeQB5jyfgZBA%3D\"}";
            //PalmPay public key in test env (not  the keys generate by merchant)
            String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVT+pLc1nkz9z803SOmF48bMFn0GYF4ng6nxj0ojUeu4KeNKkkw/nfureTtL77j9RpMjquJzzKdOZfHRvQyuAbaLoaSD1uU47npNiAL05bLYZEoZWvFOar9gNbIesea8MX0DeYncA2Tkr3kUo8K6XBrZ+TcV2Q8NEvm1T536LOGwIDAQAB";
            boolean res = PalmPayUtil.verifySignForCallback(paramBody,publicKey,SignType.RSA);
            Assert.assertTrue(res);
        }

    Notes on signature verification

    1. Callback interface signature verification a. Callback interface signature verification needs to be a full-field signature (remove all fields after the sign), the first Palmpay callback request parameters are parsed into a map for the signature verification, after the completion of the signature verification and then normal deserialization into a Java object as needed.

    Other considerations

    1. Callback interface return value a. The return result of Palmpay callback should be success (not in json format, does not contain other characters), other return results will trigger 6 retries.